Skip to content

Commit

Permalink
Implement Config Consistency (#4725)
Browse files Browse the repository at this point in the history
* standardize configurations
  • Loading branch information
khanayan123 authored Oct 22, 2024
1 parent 597d7c5 commit 1522a48
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
9 changes: 9 additions & 0 deletions packages/datadog-instrumentations/src/helpers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const disabledInstrumentations = new Set(
DD_TRACE_DISABLED_INSTRUMENTATIONS ? DD_TRACE_DISABLED_INSTRUMENTATIONS.split(',') : []
)

// Check for DD_TRACE_<INTEGRATION>_ENABLED environment variables
for (const [key, value] of Object.entries(process.env)) {
const match = key.match(/^DD_TRACE_(.+)_ENABLED$/)
if (match && (value.toLowerCase() === 'false' || value === '0')) {
const integration = match[1].toLowerCase()
disabledInstrumentations.add(integration)
}
}

const loadChannel = channel('dd-trace:instrumentation:load')

// Globals
Expand Down
11 changes: 9 additions & 2 deletions packages/dd-trace/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class Config {
this._setValue(defaults, 'reportHostname', false)
this._setValue(defaults, 'runtimeMetrics', false)
this._setValue(defaults, 'sampleRate', undefined)
this._setValue(defaults, 'sampler.rateLimit', undefined)
this._setValue(defaults, 'sampler.rateLimit', 100)
this._setValue(defaults, 'sampler.rules', [])
this._setValue(defaults, 'sampler.spanSamplingRules', [])
this._setValue(defaults, 'scope', undefined)
Expand All @@ -541,6 +541,7 @@ class Config {
this._setValue(defaults, 'telemetry.heartbeatInterval', 60000)
this._setValue(defaults, 'telemetry.logCollection', false)
this._setValue(defaults, 'telemetry.metrics', true)
this._setValue(defaults, 'traceEnabled', true)
this._setValue(defaults, 'traceId128BitGenerationEnabled', true)
this._setValue(defaults, 'traceId128BitLoggingEnabled', false)
this._setValue(defaults, 'tracePropagationExtractFirst', false)
Expand Down Expand Up @@ -627,6 +628,7 @@ class Config {
DD_TRACE_AGENT_PROTOCOL_VERSION,
DD_TRACE_CLIENT_IP_ENABLED,
DD_TRACE_CLIENT_IP_HEADER,
DD_TRACE_ENABLED,
DD_TRACE_EXPERIMENTAL_EXPORTER,
DD_TRACE_EXPERIMENTAL_GET_RUM_DATA_ENABLED,
DD_TRACE_EXPERIMENTAL_RUNTIME_ID_ENABLED,
Expand Down Expand Up @@ -713,6 +715,7 @@ class Config {
this._setBoolean(env, 'dsmEnabled', DD_DATA_STREAMS_ENABLED)
this._setBoolean(env, 'dynamicInstrumentationEnabled', DD_DYNAMIC_INSTRUMENTATION_ENABLED)
this._setString(env, 'env', DD_ENV || tags.env)
this._setBoolean(env, 'traceEnabled', DD_TRACE_ENABLED)
this._setBoolean(env, 'experimental.enableGetRumData', DD_TRACE_EXPERIMENTAL_GET_RUM_DATA_ENABLED)
this._setString(env, 'experimental.exporter', DD_TRACE_EXPERIMENTAL_EXPORTER)
this._setBoolean(env, 'experimental.runtimeId', DD_TRACE_EXPERIMENTAL_RUNTIME_ID_ENABLED)
Expand Down Expand Up @@ -1155,7 +1158,11 @@ class Config {
}

if (typeof value === 'string') {
value = value.split(',')
value = value.split(',').map(item => {
// Trim each item and remove whitespace around the colon
const [key, val] = item.split(':').map(part => part.trim())
return val !== undefined ? `${key}:${val}` : key
})
}

if (Array.isArray(value)) {
Expand Down
9 changes: 8 additions & 1 deletion packages/dd-trace/src/opentracing/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ class DatadogTracer {
? getContext(options.childOf)
: getParent(options.references)

// as per spec, allow the setting of service name through options
const tags = {
'service.name': this._service
'service.name': options?.tags?.service ? String(options.tags.service) : this._service
}

// As per unified service tagging spec if a span is created with a service name different from the global
// service name it will not inherit the global version value
if (options?.tags?.service && options.tags.service !== this._service) {
options.tags.version = undefined
}

const span = new Span(this, this._processor, this._prioritySampler, {
Expand Down
10 changes: 9 additions & 1 deletion packages/dd-trace/src/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,15 @@ function updateConfig (changes, config) {
logInjection: 'DD_LOG_INJECTION',
headerTags: 'DD_TRACE_HEADER_TAGS',
tags: 'DD_TAGS',
'sampler.rules': 'DD_TRACE_SAMPLING_RULES'
'sampler.rules': 'DD_TRACE_SAMPLING_RULES',
traceEnabled: 'DD_TRACE_ENABLED',
url: 'DD_TRACE_AGENT_URL',
'sampler.rateLimit': 'DD_TRACE_RATE_LIMIT',
queryStringObfuscation: 'DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP',
version: 'DD_VERSION',
env: 'DD_ENV',
service: 'DD_SERVICE',
clientIpHeader: 'DD_TRACE_CLIENT_IP_HEADER'
}

const namesNeedFormatting = new Set(['DD_TAGS', 'peerServiceMapping', 'serviceMapping'])
Expand Down
8 changes: 6 additions & 2 deletions packages/dd-trace/test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ describe('Config', () => {
expect(config).to.have.property('runtimeMetrics', false)
expect(config.tags).to.have.property('service', 'node')
expect(config).to.have.property('plugins', true)
expect(config).to.have.property('traceEnabled', true)
expect(config).to.have.property('env', undefined)
expect(config).to.have.property('reportHostname', false)
expect(config).to.have.property('scope', undefined)
Expand Down Expand Up @@ -350,7 +351,8 @@ describe('Config', () => {
{ name: 'reportHostname', value: false, origin: 'default' },
{ name: 'runtimeMetrics', value: false, origin: 'default' },
{ name: 'sampleRate', value: undefined, origin: 'default' },
{ name: 'sampler.rateLimit', value: undefined, origin: 'default' },
{ name: 'sampler.rateLimit', value: 100, origin: 'default' },
{ name: 'traceEnabled', value: true, origin: 'default' },
{ name: 'sampler.rules', value: [], origin: 'default' },
{ name: 'scope', value: undefined, origin: 'default' },
{ name: 'service', value: 'node', origin: 'default' },
Expand Down Expand Up @@ -495,6 +497,7 @@ describe('Config', () => {
process.env.DD_INSTRUMENTATION_INSTALL_TYPE = 'k8s_single_step'
process.env.DD_INSTRUMENTATION_INSTALL_TIME = '1703188212'
process.env.DD_INSTRUMENTATION_CONFIG_ID = 'abcdef123'
process.env.DD_TRACE_ENABLED = 'true'

// required if we want to check updates to config.debug and config.logLevel which is fetched from logger
reloadLoggerAndConfig()
Expand All @@ -518,6 +521,7 @@ describe('Config', () => {
expect(config).to.have.property('dynamicInstrumentationEnabled', true)
expect(config).to.have.property('env', 'test')
expect(config).to.have.property('sampleRate', 0.5)
expect(config).to.have.property('traceEnabled', true)
expect(config).to.have.property('traceId128BitGenerationEnabled', true)
expect(config).to.have.property('traceId128BitLoggingEnabled', true)
expect(config).to.have.property('spanAttributeSchema', 'v1')
Expand Down Expand Up @@ -1633,7 +1637,7 @@ describe('Config', () => {
}, true)
expect(config).to.have.deep.nested.property('sampler', {
spanSamplingRules: [],
rateLimit: undefined,
rateLimit: 100,
rules: [
{
resource: '*',
Expand Down
17 changes: 15 additions & 2 deletions packages/dd-trace/test/config/disabled_instrumentations.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
'use strict'

process.env.DD_TRACE_DISABLED_INSTRUMENTATIONS = 'express'

require('../setup/tap')

describe('config/disabled_instrumentations', () => {
it('should disable loading instrumentations completely', () => {
process.env.DD_TRACE_DISABLED_INSTRUMENTATIONS = 'express'
const handleBefore = require('express').application.handle
const tracer = require('../../../..')
const handleAfterImport = require('express').application.handle
tracer.init()
const handleAfterInit = require('express').application.handle

expect(handleBefore).to.equal(handleAfterImport)
expect(handleBefore).to.equal(handleAfterInit)
delete process.env.DD_TRACE_DISABLED_INSTRUMENTATIONS
})

it('should disable loading instrumentations using DD_TRACE_<INTEGRATION>_ENABLED', () => {
process.env.DD_TRACE_EXPRESS_ENABLED = 'false'
const handleBefore = require('express').application.handle
const tracer = require('../../../..')
const handleAfterImport = require('express').application.handle
Expand All @@ -14,5 +26,6 @@ describe('config/disabled_instrumentations', () => {

expect(handleBefore).to.equal(handleAfterImport)
expect(handleBefore).to.equal(handleAfterInit)
delete process.env.DD_TRACE_EXPRESS_ENABLED
})
})
34 changes: 34 additions & 0 deletions packages/dd-trace/test/opentracing/tracer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,40 @@ describe('Tracer', () => {
expect(span.addTags).to.have.been.calledWith(fields.tags)
})

it('If span is granted a service name that differs from the global service name' +
'ensure spans `version` tag is undefined.', () => {
config.tags = {
foo: 'tracer',
bar: 'tracer'
}

fields.tags = {
bar: 'span',
baz: 'span',
service: 'new-service'

}

tracer = new Tracer(config)
const testSpan = tracer.startSpan('name', fields)

expect(span.addTags).to.have.been.calledWith(config.tags)
expect(span.addTags).to.have.been.calledWith({ ...fields.tags, version: undefined })
expect(Span).to.have.been.calledWith(tracer, processor, prioritySampler, {
operationName: 'name',
parent: null,
tags: {
'service.name': 'new-service'
},
startTime: fields.startTime,
hostname: undefined,
traceId128BitGenerationEnabled: undefined,
integrationName: undefined,
links: undefined
})
expect(testSpan).to.equal(span)
})

it('should start a span with the trace ID generation configuration', () => {
config.traceId128BitGenerationEnabled = true
tracer = new Tracer(config)
Expand Down

0 comments on commit 1522a48

Please sign in to comment.