Skip to content

Commit 0326010

Browse files
committed
remove root tag logic from format.js
1 parent 2d08176 commit 0326010

File tree

4 files changed

+33
-48
lines changed

4 files changed

+33
-48
lines changed

packages/dd-trace/src/format.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ const { isError } = require('./util')
77
const { registerExtraService } = require('./service-naming/extra-services')
88

99
const SAMPLING_PRIORITY_KEY = constants.SAMPLING_PRIORITY_KEY
10-
const SAMPLING_RULE_DECISION = constants.SAMPLING_RULE_DECISION
11-
const SAMPLING_LIMIT_DECISION = constants.SAMPLING_LIMIT_DECISION
12-
const SAMPLING_AGENT_DECISION = constants.SAMPLING_AGENT_DECISION
1310
const SPAN_SAMPLING_MECHANISM = constants.SPAN_SAMPLING_MECHANISM
1411
const SPAN_SAMPLING_RULE_RATE = constants.SPAN_SAMPLING_RULE_RATE
1512
const SPAN_SAMPLING_MAX_PER_SECOND = constants.SPAN_SAMPLING_MAX_PER_SECOND
1613
const SAMPLING_MECHANISM_SPAN = constants.SAMPLING_MECHANISM_SPAN
1714
const { MEASURED, BASE_SERVICE, ANALYTICS } = tags
1815
const ORIGIN_KEY = constants.ORIGIN_KEY
1916
const HOSTNAME_KEY = constants.HOSTNAME_KEY
20-
const TOP_LEVEL_KEY = constants.TOP_LEVEL_KEY
2117
const PROCESS_ID = constants.PROCESS_ID
2218
const ERROR_MESSAGE = constants.ERROR_MESSAGE
2319
const ERROR_STACK = constants.ERROR_STACK
@@ -37,7 +33,6 @@ function format (span) {
3733

3834
extractSpanLinks(formatted, span)
3935
extractSpanEvents(formatted, span)
40-
extractRootTags(formatted, span)
4136
extractTags(formatted, span)
4237

4338
return formatted
@@ -178,19 +173,6 @@ function extractTags (formattedSpan, span) {
178173
addTag(formattedSpan.meta, formattedSpan.metrics, HOSTNAME_KEY, hostname)
179174
}
180175

181-
function extractRootTags (formattedSpan, span) {
182-
const context = span.context()
183-
const isLocalRoot = span === context._trace.started[0]
184-
const parentId = context._parentId
185-
186-
if (!isLocalRoot || (parentId && parentId.toString(10) !== '0')) return
187-
188-
addTag({}, formattedSpan.metrics, SAMPLING_RULE_DECISION, context._trace[SAMPLING_RULE_DECISION])
189-
addTag({}, formattedSpan.metrics, SAMPLING_LIMIT_DECISION, context._trace[SAMPLING_LIMIT_DECISION])
190-
addTag({}, formattedSpan.metrics, SAMPLING_AGENT_DECISION, context._trace[SAMPLING_AGENT_DECISION])
191-
addTag({}, formattedSpan.metrics, TOP_LEVEL_KEY, 1)
192-
}
193-
194176
function extractError (formattedSpan, error) {
195177
if (!error) return
196178

packages/dd-trace/src/opentracing/span_context.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
const util = require('util')
44
const { AUTO_KEEP } = require('../../../../ext/priority')
55

6+
const {
7+
SAMPLING_RULE_DECISION,
8+
SAMPLING_LIMIT_DECISION,
9+
SAMPLING_AGENT_DECISION,
10+
TOP_LEVEL_KEY
11+
} = require('../constants');
12+
613
// the lowercase, hex encoded upper 64 bits of a 128-bit trace id, if present
714
const TRACE_ID_128 = '_dd.p.tid'
815

16+
function getChunkRoot (self) {
17+
return self._trace.started[0]?.context() || self
18+
}
19+
20+
function assignRootTag (self, prop, val) {
21+
const chunkRoot = getChunkRoot(self)
22+
if (!chunkRoot._parentId || chunkRoot._parentId.toString(10) === '0') {
23+
chunkRoot._tags[SAMPLING_RULE_DECISION] = val
24+
chunkRoot._tags[TOP_LEVEL_KEY] = 1
25+
}
26+
}
27+
928
class DatadogSpanContext {
1029
constructor (props) {
1130
props = props || {}
@@ -27,7 +46,16 @@ class DatadogSpanContext {
2746
const self = this
2847
this._trace = props.trace || {
2948
started: [],
30-
finished: []
49+
finished: [],
50+
set [SAMPLING_RULE_DECISION] (val) {
51+
assignRootTag(self, SAMPLING_RULE_DECISION, val)
52+
},
53+
set [SAMPLING_LIMIT_DECISION] (val) {
54+
assignRootTag(self, SAMPLING_LIMIT_DECISION, val)
55+
},
56+
set [SAMPLING_AGENT_DECISION] (val) {
57+
assignRootTag(self, SAMPLING_AGENT_DECISION, val)
58+
}
3159
}
3260
if (!props.trace) {
3361
Object.defineProperty(this._trace, 'tags', {

packages/dd-trace/test/format.spec.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -180,35 +180,6 @@ describe('format', () => {
180180
expect(trace.resource).to.equal('resource')
181181
})
182182

183-
it('should extract Datadog specific root tags', () => {
184-
spanContext._parentId = null
185-
spanContext._trace[SAMPLING_AGENT_DECISION] = 0.8
186-
spanContext._trace[SAMPLING_LIMIT_DECISION] = 0.2
187-
spanContext._trace[SAMPLING_RULE_DECISION] = 0.5
188-
189-
trace = format(span)
190-
191-
expect(trace.metrics).to.include({
192-
[SAMPLING_AGENT_DECISION]: 0.8,
193-
[SAMPLING_LIMIT_DECISION]: 0.2,
194-
[SAMPLING_RULE_DECISION]: 0.5
195-
})
196-
})
197-
198-
it('should not extract Datadog specific root tags from non-root spans', () => {
199-
spanContext._trace[SAMPLING_AGENT_DECISION] = 0.8
200-
spanContext._trace[SAMPLING_LIMIT_DECISION] = 0.2
201-
spanContext._trace[SAMPLING_RULE_DECISION] = 0.5
202-
203-
trace = format(span)
204-
205-
expect(trace.metrics).to.not.have.keys(
206-
SAMPLING_AGENT_DECISION,
207-
SAMPLING_LIMIT_DECISION,
208-
SAMPLING_RULE_DECISION
209-
)
210-
})
211-
212183
it('should always add single span ingestion tags from options if present', () => {
213184
spanContext._spanSampling = {
214185
maxPerSecond: 5,

packages/dd-trace/test/opentracing/span_context.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ describe('SpanContext', () => {
7070
spanId: '456'
7171
})
7272

73+
const tags = {}
7374
expect(spanContext).to.deep.equal({
7475
_traceId: '123',
7576
_spanId: '456',
@@ -86,6 +87,9 @@ describe('SpanContext', () => {
8687
_trace: {
8788
started: [],
8889
finished: [],
90+
'_dd.rule_psr': undefined,
91+
'_dd.limit_psr': undefined,
92+
'_dd.agent_psr': undefined,
8993
tags: {}
9094
},
9195
_traceparent: undefined,

0 commit comments

Comments
 (0)