Skip to content

Commit

Permalink
Add get writer project id
Browse files Browse the repository at this point in the history
  • Loading branch information
draffensperger committed Aug 30, 2017
1 parent 0e15b6c commit 6446e09
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/trace-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var SpanData = require('./span-data.js');
var uuid = require('uuid');
var TracingPolicy = require('./tracing-policy.js');
var semver = require('semver');
var TraceWriter = require('./trace-writer.js');

var ROOT_SPAN_STACK_OFFSET = semver.satisfies(process.version, '>=8') ? 0 : 2;

Expand All @@ -36,9 +37,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 +198,23 @@ 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
* will return null until the projectId auto-discovery completes.
*/
TraceAgent.prototype.getWriterProjectId = function() {
var traceWriter;
try {
traceWriter = TraceWriter.get();
} catch(err) {
// The TraceWriter.get() call could fail if the TraceWriter has not been
// initialized yet.
return null;
}
return traceWriter.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
8 changes: 7 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,12 @@ 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 the writer', function() {
var traceApi = createTraceAgent();
assert.equal(traceApi.getWriterProjectId(), '0');
});

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

0 comments on commit 6446e09

Please sign in to comment.