Skip to content
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

Merged
merged 21 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/platform/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ suite
protocol: 'http:',
hostname: 'test',
port: '8080',
path: '/v0.3/traces',
path: '/v0.4/traces',
method: 'PUT',
headers: {
'Content-Type': 'application/msgpack'
Expand Down
20 changes: 11 additions & 9 deletions benchmark/stubs/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ const span = {
trace: {
started: [span, span],
finished: [span, span]
}
},
tags: {
resource: '/resource',
type: 'web',
error: true
},
metrics: {
[SAMPLE_RATE_METRIC_KEY]: 1
},
sampled: true,
sampling: {}
}),
_operationName: 'operation',
_tags: {
resource: '/resource',
type: 'web',
error: true
},
_metrics: {
[SAMPLE_RATE_METRIC_KEY]: 1
},
_startTime: 1500000000000.123456,
_duration: 100
}
Expand Down
9 changes: 9 additions & 0 deletions ext/index.js
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
}
8 changes: 8 additions & 0 deletions ext/priority.js
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
}
8 changes: 8 additions & 0 deletions ext/tags.js
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'
}
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

module.exports = {
SAMPLE_RATE_METRIC_KEY: '_sample_rate'
SAMPLE_RATE_METRIC_KEY: '_sample_rate',
SAMPLING_PRIORITY_KEY: '_sampling_priority_v1'
}
37 changes: 25 additions & 12 deletions src/format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'

const Int64BE = require('int64-buffer').Int64BE
const constants = require('./constants')

const SAMPLING_PRIORITY_KEY = constants.SAMPLING_PRIORITY_KEY

const map = {
'service.name': 'service',
Expand All @@ -11,9 +14,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
}
Expand All @@ -37,7 +40,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':
Expand All @@ -62,20 +67,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_KEY] = spanContext.sampling.priority
}
}

module.exports = format
46 changes: 36 additions & 10 deletions src/opentracing/propagation/text_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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}(.+)$`)

Expand All @@ -14,31 +15,56 @@ class TextMapPropagator {
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) {
const priority = spanContext.sampling.priority

if (Number.isInteger(priority)) {
carrier[samplingKey] = 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 (Number.isInteger(priority)) {
spanContext.sampling.priority = parseInt(carrier[samplingKey], 10)
}
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/opentracing/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ const constants = require('../constants')
const SAMPLE_RATE_METRIC_KEY = constants.SAMPLE_RATE_METRIC_KEY

class DatadogSpan extends Span {
constructor (tracer, sampler, fields) {
constructor (tracer, recorder, sampler, prioritySampler, fields) {
super()

const startTime = fields.startTime || platform.now()
const operationName = fields.operationName
const parent = fields.parent || null
const tags = fields.tags || {}
const tags = Object.assign({}, fields.tags)
const metrics = {
[SAMPLE_RATE_METRIC_KEY]: sampler.rate()
}

this._parentTracer = tracer
this._sampler = sampler
this._recorder = recorder
this._prioritySampler = prioritySampler
this._operationName = operationName
this._tags = Object.assign({}, tags)
this._metrics = metrics
this._startTime = startTime

this._spanContext = this._createContext(parent)
this._spanContext.tags = tags
this._spanContext.metrics = metrics
}

_createContext (parent) {
Expand All @@ -40,6 +42,7 @@ class DatadogSpan extends Span {
spanId: platform.id(),
parentId: parent.spanId,
sampled: parent.sampled,
sampling: parent.sampling,
baggageItems: Object.assign({}, parent.baggageItems),
trace: parent.trace.started.length !== parent.trace.finished.length ? parent.trace : null
})
Expand Down Expand Up @@ -80,7 +83,7 @@ class DatadogSpan extends Span {
_addTags (keyValuePairs) {
try {
Object.keys(keyValuePairs).forEach(key => {
this._tags[key] = String(keyValuePairs[key])
this._spanContext.tags[key] = String(keyValuePairs[key])
})
} catch (e) {
log.error(e)
Expand All @@ -96,9 +99,10 @@ class DatadogSpan extends Span {

this._duration = finishTime - this._startTime
this._spanContext.trace.finished.push(this)
this._prioritySampler.sample(this)

if (this._spanContext.sampled) {
this._parentTracer._record(this)
this._recorder.record(this)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/opentracing/span_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class DatadogSpanContext extends SpanContext {
this.traceId = props.traceId
this.spanId = props.spanId
this.parentId = props.parentId || null
this.tags = props.tags || {}
this.metrics = props.metrics || {}
this.sampled = props.sampled === undefined || props.sampled
this.sampling = props.sampling || {}
this.baggageItems = props.baggageItems || {}
this.trace = props.trace || {
started: [],
Expand Down
17 changes: 8 additions & 9 deletions src/opentracing/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const Tracer = opentracing.Tracer
const Reference = opentracing.Reference
const Span = require('./span')
const SpanContext = require('./span_context')
const Writer = require('../writer')
const Recorder = require('../recorder')
const Sampler = require('../sampler')
const PrioritySampler = require('../priority_sampler')
const TextMapPropagator = require('./propagation/text_map')
const HttpPropagator = require('./propagation/http')
const BinaryPropagator = require('./propagation/binary')
Expand All @@ -23,7 +25,9 @@ class DatadogTracer extends Tracer {
this._url = config.url
this._env = config.env
this._tags = config.tags
this._recorder = new Recorder(config.url, config.flushInterval, config.bufferSize)
this._prioritySampler = new PrioritySampler(config.env)
this._writer = new Writer(this._prioritySampler, config.url, config.bufferSize)
this._recorder = new Recorder(this._writer, config.flushInterval)
this._recorder.init()
this._sampler = new Sampler(config.sampleRate)
this._propagators = {
Expand All @@ -38,28 +42,23 @@ class DatadogTracer extends Tracer {
'resource.name': name
}

if (this._service) {
tags['service.name'] = this._service
}
tags['service.name'] = this._service

if (this._env) {
tags.env = this._env
}

return new Span(this, this._sampler, {
return new Span(this, this._recorder, this._sampler, this._prioritySampler, {
operationName: fields.operationName || name,
parent: getParent(fields.references),
tags: Object.assign(tags, this._tags, fields.tags),
startTime: fields.startTime
})
}

_record (span) {
this._recorder.record(span)
}

_inject (spanContext, format, carrier) {
try {
this._prioritySampler.sample(spanContext)
this._propagators[format].inject(spanContext, carrier)
} catch (e) {
log.error(e)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function patch (http, tracer, config) {
const uri = extractUrl(options)
const method = (options.method || 'GET').toUpperCase()

if (uri === `${tracer._url.href}/v0.3/traces`) {
if (uri === `${tracer._url.href}/v0.4/traces`) {
return request.apply(this, [options, callback])
}

Expand Down
Loading