Skip to content

Commit

Permalink
feat: add public API for root span id (googleapis#542)
Browse files Browse the repository at this point in the history
Make it possible for external users to get a unique identifier for the
root span. This can be used to uniquely identify the top level
incoming http request context for example.
  • Loading branch information
ofrobots authored Aug 29, 2017
1 parent a101205 commit debc493
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/trace-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var TracingPolicy = require('./tracing-policy.js');
var phantomApiImpl = {
enhancedDatabaseReportingEnabled: function() { return false; },
runInRootSpan: function(opts, fn) { return fn(null); },
getCurrentContext: function() { return null; },
createChildSpan: function(opts) { return null; },
getResponseTraceContext: function(context, traced) { return ''; },
wrap: function(fn) { return fn; },
Expand Down Expand Up @@ -176,6 +177,22 @@ TraceAgent.prototype.runInRootSpan = function(options, fn) {
});
};

/**
* Returns a unique identifier for the currently active context. This can be
* used to uniquely identify the current root span. If there is no current,
* context, or if we have lost context, this will return null. The structure and
* the length of the returned string should be treated opaquely - the only
* guarantee is that the value would unique for every root span.
* @returns {string} an id for the current context, or null if there is none
*/
TraceAgent.prototype.getCurrentContextId = function() {
const rootSpan = cls.getRootContext();
if (!rootSpan || rootSpan === nullSpan) {
return null;
}
return rootSpan.trace.traceId;
};

/**
* 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
14 changes: 14 additions & 0 deletions test/test-trace-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function assertAPISurface(traceAPI) {
assert.strictEqual(typeof root.getTraceContext(), 'string');
}
});
assert.strictEqual(typeof traceAPI.getCurrentContextId, 'function');
var child = traceAPI.createChildSpan({ name: 'child' });
// TODO: Ditto but with child spans
if (child) {
Expand Down Expand Up @@ -169,6 +170,19 @@ describe('Trace Interface', function() {
});
});

it('should return null context id when one does not exist', function() {
var traceAPI = createTraceAgent();
assert.strictEqual(traceAPI.getCurrentContextId(), null);
});

it('should return the appropriate trace id', function() {
var traceAPI = createTraceAgent();
traceAPI.runInRootSpan({name: 'root', url: 'root'}, function(rootSpan) {
var id = traceAPI.getCurrentContextId();
assert.strictEqual(id, rootSpan.trace.traceId);
});
});

it('should add labels to spans', function() {
var traceAPI = createTraceAgent();
traceAPI.runInRootSpan({name: 'root', url: 'root'}, function(root) {
Expand Down

0 comments on commit debc493

Please sign in to comment.