Skip to content

Commit

Permalink
Trace API function for trace writer project ID (#548)
Browse files Browse the repository at this point in the history
PR-URL: #548
  • Loading branch information
draffensperger authored and matthewloring committed Sep 5, 2017
1 parent 843e728 commit 68ec8d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/trace-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ var ROOT_SPAN_STACK_OFFSET = semver.satisfies(process.version, '>=8') ? 0 : 2;
var phantomApiImpl = {
enhancedDatabaseReportingEnabled: function() { return false; },
runInRootSpan: function(opts, fn) { return fn(null); },
getCurrentContext: function() { return null; },
getCurrentContextId: function() { return null; },
createChildSpan: function(opts) { return null; },
getResponseTraceContext: function(context, traced) { return ''; },
getWriterProjectId : function() { return null; },
wrap: function(fn) { return fn; },
wrapEmitter: function(ee) {},
};
Expand Down Expand Up @@ -196,6 +197,15 @@ TraceAgent.prototype.getCurrentContextId = function() {
return rootSpan.trace.traceId;
};

/**
* Returns the projectId that was either configured or auto-discovered by the
* TraceWriter. Note that the auto-discovery is done asynchronously, so this
* may return falsey until the projectId auto-discovery completes.
*/
TraceAgent.prototype.getWriterProjectId = function() {
return this.config_.projectId;
};

/**
* Creates and returns a new ChildSpan object nested within the root span. If
* there is no current RootSpan object, this function returns null.
Expand Down
18 changes: 18 additions & 0 deletions test/test-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require('./plugins/common.js');
var assert = require('assert');
var nock = require('nock');
var nocks = require('./nocks.js');
var trace = require('..');
var TraceAgent = require('../src/trace-api.js');

Expand Down Expand Up @@ -78,4 +79,21 @@ describe('index.js', function() {
done();
}, 500);
});

it('should allow project ID to be read after discovery', function(done) {
var envProjectId = process.env.GCLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;

nocks.projectId(function() { return 'project1'; });
nocks.hostname(function() { return 'host1'; });
nocks.instanceId(function() { return 'instance1'; });

var agent = trace.start({logLevel: 0, forceNewAgent_: true});

setTimeout(function() {
assert.strictEqual(agent.getWriterProjectId(), 'project1');
process.env.GCLOUD_PROJECT = envProjectId;
done();
}, 500);
});
});
9 changes: 8 additions & 1 deletion test/test-trace-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function assertAPISurface(traceAPI) {
}
});
assert.strictEqual(typeof traceAPI.getCurrentContextId, 'function');
assert.strictEqual(typeof traceAPI.getWriterProjectId, 'function');
var child = traceAPI.createChildSpan({ name: 'child' });
// TODO: Ditto but with child spans
if (child) {
Expand Down Expand Up @@ -180,7 +181,13 @@ describe('Trace Interface', function() {
traceAPI.runInRootSpan({name: 'root', url: 'root'}, function(rootSpan) {
var id = traceAPI.getCurrentContextId();
assert.strictEqual(id, rootSpan.trace.traceId);
});
});
});

it('should return get the project ID if set in config', function() {
var config = {projectId: 'project-1'};
var traceApi = createTraceAgent(null /* policy */, config);
assert.equal(traceApi.getWriterProjectId(), 'project-1');
});

it('should add labels to spans', function() {
Expand Down

0 comments on commit 68ec8d5

Please sign in to comment.