Skip to content

Commit

Permalink
Set outgoing trace enabled header always
Browse files Browse the repository at this point in the history
We currently only emit a trace header on the requests that we sample.
This change causes all such outgoing traced requests to set the trace
enabled bit.
  • Loading branch information
Matt Loring committed Jun 14, 2016
1 parent 069e76c commit afc2ed1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/hooks/core/hook-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function requestWrap(request) {
var span = agent.startSpan(uri, labels);
// Adding context to the headers lets us trace the request
// as it makes it through other layers of the Google infrastructure
agent.addContextToHeaders(span, options.headers);
agent.addContextToHeaders(span, options.headers, true);

var returned = request.call(this, options, function(res) {
namespace.bindEmitter(res);
Expand Down
10 changes: 6 additions & 4 deletions lib/trace-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,17 @@ TraceAgent.prototype.parseContextFromHeader = function(str) {
* @param {SpanData} spanData The span to be added to headers.
* @param {Object} headers The http headers associated with the
* current request.
* @param {boolean} traced Whether this request was traced by the agent.
*/
TraceAgent.prototype.addContextToHeaders = function(spanData, headers) {
TraceAgent.prototype.addContextToHeaders = function(spanData, headers, traced) {
if (spanData === SpanData.nullSpan) {
return;
}
var header = spanData.trace.traceId + '/' + spanData.span.spanId;
if (spanData.options) {
header += (';o=' + spanData.options);
}
var options = traced ?
spanData.options | constants.TRACE_OPTIONS_TRACE_ENABLED :
spanData.options;
header += (';o=' + options);
headers[constants.TRACE_CONTEXT_HEADER_NAME] = header;
};

Expand Down
3 changes: 3 additions & 0 deletions test/standalone/test-trace-header-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ describe('test-trace-header-context', function() {
assert.equal(
req.headers[constants.TRACE_CONTEXT_HEADER_NAME].slice(0, 6),
context.slice(0, 6));
assert.equal(
req.headers[constants.TRACE_CONTEXT_HEADER_NAME].slice(8),
';o=1');
res.send(common.serverRes);
var traces = common.getTraces();
assert.equal(traces.length, 2);
Expand Down
34 changes: 32 additions & 2 deletions test/test-trace-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Trace Agent', function() {
var options = {
headers: {}
};
agent.addContextToHeaders(spanData, options.headers);
agent.addContextToHeaders(spanData, options.headers, true);
var parsed = agent.parseContextFromHeader(
options.headers[constants.TRACE_CONTEXT_HEADER_NAME]);
assert.equal(parsed.traceId, 1);
Expand All @@ -70,13 +70,43 @@ describe('Trace Agent', function() {
});
});

it('sets trace enabled bit when traced', function() {
cls.getNamespace().run(function() {
var spanData = agent.createRootSpanData('name', 1, 2);
spanData.options = 2;
var options = {
headers: {}
};
agent.addContextToHeaders(spanData, options.headers, true);
var parsed = agent.parseContextFromHeader(
options.headers[constants.TRACE_CONTEXT_HEADER_NAME]);
assert.equal(parsed.options, 3);
});
});

it('leaves options alone when untraced', function() {
cls.getNamespace().run(function() {
var spanData = agent.createRootSpanData('name', 1, 2);
spanData.options = 2;
var options = {
headers: {}
};
agent.addContextToHeaders(spanData, options.headers, false);
var parsed = agent.parseContextFromHeader(
options.headers[constants.TRACE_CONTEXT_HEADER_NAME]);
assert.equal(parsed.options, 2);
});
});

it('noop on nullSpan', function() {
cls.getNamespace().run(function() {
var options = {
headers: {}
};
agent.addContextToHeaders(SpanData.nullSpan, options.headers);
assert.equal(options.headers[constants.TRACE_CONTEXT_HEADER_NAME], undefined);
assert.equal(
options.headers[constants.TRACE_CONTEXT_HEADER_NAME],
undefined, true);
});
});
});
Expand Down

0 comments on commit afc2ed1

Please sign in to comment.