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

add synthetics integration #461

Merged
merged 1 commit into from
Feb 25, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module.exports = {
SAMPLE_RATE_METRIC_KEY: '_sample_rate',
SAMPLING_PRIORITY_KEY: '_sampling_priority_v1'
SAMPLING_PRIORITY_KEY: '_sampling_priority_v1',
ORIGIN_KEY: '_dd.origin'
}
6 changes: 6 additions & 0 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const constants = require('./constants')
const log = require('./log')

const SAMPLING_PRIORITY_KEY = constants.SAMPLING_PRIORITY_KEY
const ORIGIN_KEY = constants.ORIGIN_KEY

const map = {
'service.name': 'service',
Expand Down Expand Up @@ -40,6 +41,7 @@ function formatSpan (span) {
}

function extractTags (trace, span) {
const origin = span.context()._trace.origin
const tags = span.context()._tags

Object.keys(tags).forEach(tag => {
Expand All @@ -62,6 +64,10 @@ function extractTags (trace, span) {
addTag(trace.meta, tag, tags[tag])
}
})

if (origin) {
addTag(trace.meta, ORIGIN_KEY, origin)
}
}

function extractError (trace, span) {
Expand Down
21 changes: 20 additions & 1 deletion src/opentracing/propagation/text_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ const log = require('../../log')

const traceKey = 'x-datadog-trace-id'
const spanKey = 'x-datadog-parent-id'
const originKey = 'x-datadog-origin'
const samplingKey = 'x-datadog-sampling-priority'
const baggagePrefix = 'ot-baggage-'
const baggageExpr = new RegExp(`^${baggagePrefix}(.+)$`)
const logKeys = [traceKey, spanKey, samplingKey]
const logKeys = [traceKey, spanKey, samplingKey, originKey]

class TextMapPropagator {
inject (spanContext, carrier) {
carrier[traceKey] = spanContext.toTraceId()
carrier[spanKey] = spanContext.toSpanId()

this._injectOrigin(spanContext, carrier)
this._injectSamplingPriority(spanContext, carrier)
this._injectBaggageItems(spanContext, carrier)

Expand All @@ -33,6 +35,7 @@ class TextMapPropagator {
spanId: new platform.Uint64BE(carrier[spanKey], 10)
})

this._extractOrigin(carrier, spanContext)
this._extractBaggageItems(carrier, spanContext)
this._extractSamplingPriority(carrier, spanContext)

Expand All @@ -41,6 +44,14 @@ class TextMapPropagator {
return spanContext
}

_injectOrigin (spanContext, carrier) {
const origin = spanContext._trace.origin

if (origin) {
carrier[originKey] = origin
}
}

_injectSamplingPriority (spanContext, carrier) {
const priority = spanContext._sampling.priority

Expand All @@ -55,6 +66,14 @@ class TextMapPropagator {
})
}

_extractOrigin (carrier, spanContext) {
const origin = carrier[originKey]

if (typeof carrier[originKey] === 'string') {
spanContext._trace.origin = origin
}
}

_extractBaggageItems (carrier, spanContext) {
Object.keys(carrier).forEach(key => {
const match = key.match(baggageExpr)
Expand Down
9 changes: 8 additions & 1 deletion src/opentracing/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,21 @@ class DatadogSpan extends Span {
let spanContext

if (parent) {
const trace = parent._trace
const finished = trace.started.length === trace.finished.length

spanContext = new SpanContext({
traceId: parent._traceId,
spanId: platform.id(),
parentId: parent._spanId,
sampled: parent._sampled,
sampling: parent._sampling,
baggageItems: parent._baggageItems,
trace: parent._trace.started.length !== parent._trace.finished.length ? parent._trace : null
trace: {
started: finished ? [] : trace.started,
finished: finished ? [] : trace.finished,
origin: trace.origin
}
})
} else {
const spanId = platform.id()
Expand Down
10 changes: 10 additions & 0 deletions test/format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Int64BE = require('int64-buffer').Int64BE
const constants = require('../src/constants')

const SAMPLING_PRIORITY_KEY = constants.SAMPLING_PRIORITY_KEY
const ORIGIN_KEY = constants.ORIGIN_KEY

const id = new Int64BE(0x02345678, 0x12345678)

Expand All @@ -21,6 +22,7 @@ describe('format', () => {
_tags: {},
_metrics: {},
_sampling: {},
_trace: {},
_name: 'operation'
}

Expand Down Expand Up @@ -101,6 +103,14 @@ describe('format', () => {
expect(trace.meta['error.stack']).to.equal(span._error.stack)
})

it('should extract the origin', () => {
spanContext._trace.origin = 'synthetics'

trace = format(span)

expect(trace.meta[ORIGIN_KEY]).to.equal('synthetics')
})

describe('when there is an `error` tag ', () => {
it('should set the error flag when error tag is true', () => {
spanContext._tags['error'] = true
Expand Down
23 changes: 23 additions & 0 deletions test/opentracing/propagation/text_map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ describe('TextMapPropagator', () => {

expect(carrier).to.have.property('x-datadog-sampling-priority', '0')
})

it('should inject the origin', () => {
const carrier = {}
const spanContext = new SpanContext({
traceId: new platform.Uint64BE(0, 123),
spanId: new platform.Uint64BE(-456),
trace: {
origin: 'synthetics'
}
})

propagator.inject(spanContext, carrier)

expect(carrier).to.have.property('x-datadog-origin', 'synthetics')
})
})

describe('extract', () => {
Expand Down Expand Up @@ -109,5 +124,13 @@ describe('TextMapPropagator', () => {
baggageItems
}))
})

it('should extract the origin', () => {
textMap['x-datadog-origin'] = 'synthetics'
const carrier = textMap
const spanContext = propagator.extract(carrier)

expect(spanContext._trace).to.have.property('origin', 'synthetics')
})
})
})
8 changes: 6 additions & 2 deletions test/opentracing/span.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ describe('Span', () => {
_baggageItems: { foo: 'bar' },
_trace: {
started: ['span'],
finished: []
finished: [],
origin: 'synthetics'
}
}

Expand All @@ -70,6 +71,7 @@ describe('Span', () => {
expect(span.context()._parentId).to.deep.equal(new Uint64BE(456, 456))
expect(span.context()._baggageItems).to.deep.equal({ foo: 'bar' })
expect(span.context()._trace.started).to.deep.equal(['span', span])
expect(span.context()._trace.origin).to.equal('synthetics')
})

it('should start a new trace if the parent trace is finished', () => {
Expand All @@ -80,13 +82,15 @@ describe('Span', () => {
_baggageItems: { foo: 'bar' },
_trace: {
started: ['span'],
finished: ['span']
finished: ['span'],
origin: 'synthetics'
}
}

span = new Span(tracer, recorder, sampler, prioritySampler, { operationName: 'operation', parent })

expect(span.context()._trace.started).to.deep.equal([span])
expect(span.context()._trace.origin).to.equal('synthetics')
})

it('should set the sample rate metric from the sampler', () => {
Expand Down