-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect and propagate X-Datadog-Sampling-Priority. #217
Changes from 15 commits
0f737d5
f9feb99
ef890a9
42c6c31
49dd480
63e3deb
884f08f
5f5a81e
7a1716b
fdc9211
4ba9ecc
e4bc77b
12c5623
70b1dde
f60006c
2ad16a8
3cd0d5a
3e7a98a
8fb4100
0a83e0c
c5e82a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
const priority = require('./priority') | ||
const tags = require('./tags') | ||
|
||
module.exports = { | ||
priority, | ||
tags | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
USER_REJECT: -1, | ||
AUTO_REJECT: 0, | ||
AUTO_KEEP: 1, | ||
USER_KEEP: 2 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
SERVICE_NAME: 'service.name', | ||
RESOURCE_NAME: 'resource.name', | ||
SPAN_TYPE: 'span.type', | ||
SAMPLING_PRIORITY: 'sampling.priority' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,9 @@ const map = { | |
function format (span) { | ||
const formatted = formatSpan(span) | ||
|
||
extractError(formatted, span._error) | ||
extractTags(formatted, span._tags) | ||
extractMetrics(formatted, span._metrics) | ||
extractError(formatted, span) | ||
extractTags(formatted, span) | ||
extractMetrics(formatted, span) | ||
|
||
return formatted | ||
} | ||
|
@@ -37,7 +37,9 @@ function formatSpan (span) { | |
} | ||
} | ||
|
||
function extractTags (trace, tags) { | ||
function extractTags (trace, span) { | ||
const tags = span.context().tags | ||
|
||
Object.keys(tags).forEach(tag => { | ||
switch (tag) { | ||
case 'service.name': | ||
|
@@ -62,20 +64,28 @@ function extractTags (trace, tags) { | |
}) | ||
} | ||
|
||
function extractMetrics (trace, metrics) { | ||
Object.keys(metrics).forEach(metric => { | ||
if (typeof metrics[metric] === 'number') { | ||
trace.metrics[metric] = metrics[metric] | ||
} | ||
}) | ||
} | ||
function extractError (trace, span) { | ||
const error = span._error | ||
|
||
function extractError (trace, error) { | ||
if (error) { | ||
trace.meta['error.msg'] = error.message | ||
trace.meta['error.type'] = error.name | ||
trace.meta['error.stack'] = error.stack | ||
} | ||
} | ||
|
||
function extractMetrics (trace, span) { | ||
const spanContext = span.context() | ||
|
||
Object.keys(spanContext.metrics).forEach(metric => { | ||
if (typeof spanContext.metrics[metric] === 'number') { | ||
trace.metrics[metric] = spanContext.metrics[metric] | ||
} | ||
}) | ||
|
||
if (spanContext.sampling.priority !== undefined) { | ||
trace.metrics['_sampling_priority_v1'] = spanContext.sampling.priority | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use a constant here for |
||
} | ||
} | ||
|
||
module.exports = format |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,39 +6,67 @@ const DatadogSpanContext = require('../span_context') | |
|
||
const traceKey = 'x-datadog-trace-id' | ||
const spanKey = 'x-datadog-parent-id' | ||
const samplingKey = 'x-datadog-sampling-priority' | ||
const baggagePrefix = 'ot-baggage-' | ||
const baggageExpr = new RegExp(`^${baggagePrefix}(.+)$`) | ||
|
||
class TextMapPropagator { | ||
constructor (prioritySampler) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not fond of the idea of having the propagator have a sampler. It feels like this logic can be done from the tracer and makes more sense to do from the tracer. |
||
this._prioritySampler = prioritySampler | ||
} | ||
|
||
inject (spanContext, carrier) { | ||
carrier[traceKey] = new Int64BE(spanContext.traceId.toBuffer()).toString() | ||
carrier[spanKey] = new Int64BE(spanContext.spanId.toBuffer()).toString() | ||
|
||
spanContext.baggageItems && Object.keys(spanContext.baggageItems).forEach(key => { | ||
carrier[baggagePrefix + key] = String(spanContext.baggageItems[key]) | ||
}) | ||
this._injectSamplingPriority(spanContext, carrier) | ||
this._injectBaggageItems(spanContext, carrier) | ||
} | ||
|
||
extract (carrier) { | ||
if (!carrier[traceKey] || !carrier[spanKey]) { | ||
return null | ||
} | ||
|
||
const baggageItems = {} | ||
const spanContext = new DatadogSpanContext({ | ||
traceId: new Uint64BE(carrier[traceKey], 10), | ||
spanId: new Uint64BE(carrier[spanKey], 10) | ||
}) | ||
|
||
this._extractBaggageItems(carrier, spanContext) | ||
this._extractSamplingPriority(carrier, spanContext) | ||
|
||
return spanContext | ||
} | ||
|
||
_injectSamplingPriority (spanContext, carrier) { | ||
this._prioritySampler.sample(spanContext) | ||
|
||
carrier[samplingKey] = spanContext.sampling.priority.toString() | ||
} | ||
|
||
_injectBaggageItems (spanContext, carrier) { | ||
spanContext.baggageItems && Object.keys(spanContext.baggageItems).forEach(key => { | ||
carrier[baggagePrefix + key] = String(spanContext.baggageItems[key]) | ||
}) | ||
} | ||
|
||
_extractBaggageItems (carrier, spanContext) { | ||
Object.keys(carrier).forEach(key => { | ||
const match = key.match(baggageExpr) | ||
|
||
if (match) { | ||
baggageItems[match[1]] = carrier[key] | ||
spanContext.baggageItems[match[1]] = carrier[key] | ||
} | ||
}) | ||
} | ||
|
||
return new DatadogSpanContext({ | ||
traceId: new Uint64BE(carrier[traceKey], 10), | ||
spanId: new Uint64BE(carrier[spanKey], 10), | ||
baggageItems | ||
}) | ||
_extractSamplingPriority (carrier, spanContext) { | ||
const priority = parseInt(carrier[samplingKey], 10) | ||
Kyle-Verhoog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (this._prioritySampler.validate(priority)) { | ||
spanContext.sampling.priority = priority | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TextMapPropagator
doesn't accept aprioritySampler
anymore, right?