Skip to content
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
4 changes: 2 additions & 2 deletions integration-tests/aiguard/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ describe('AIGuard SDK integration tests', () => {
env: {
DD_SERVICE: 'ai_guard_integration_test',
DD_ENV: 'test',
DD_TRACING_ENABLED: 'true',
DD_TRACE_AGENT_PORT: agent.port,
DD_TRACE_ENABLED: 'true',
DD_TRACE_AGENT_PORT: String(agent.port),
DD_AI_GUARD_ENABLED: 'true',
DD_AI_GUARD_BLOCK: 'true',
DD_AI_GUARD_ENDPOINT: `http://localhost:${api.address().port}`,
Expand Down
39 changes: 39 additions & 0 deletions integration-tests/debugger/tracing-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@ const assert = require('assert')
const { setup, testBasicInput, testBasicInputWithoutDD } = require('./utils')

describe('Dynamic Instrumentation', function () {
describe('DD_TRACE_ENABLED=true, DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=true', function () {
const t = setup({
testApp: 'target-app/basic.js',
env: { DD_TRACE_ENABLED: 'true', DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED: true },
dependencies: ['fastify'],
})

describe('input messages', function () {
it('should capture and send expected payload when a log line probe is triggered', testBasicInput.bind(null, t))
})
})

describe('DD_TRACE_ENABLED=true, DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=false', function () {
const t = setup({
testApp: 'target-app/basic.js',
env: { DD_TRACE_ENABLED: 'true', DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED: false },
dependencies: ['fastify'],
})

describe('input messages', function () {
it('should capture and send expected payload when a log line probe is triggered', testBasicInput.bind(null, t))
})
})

describe('DD_TRACE_ENABLED=false', function () {
const t = setup({
testApp: 'target-app/basic.js',
env: { DD_TRACE_ENABLED: 'false' },
dependencies: ['fastify'],
})

describe('input messages', function () {
it(
'should capture and send expected payload when a log line probe is triggered',
testBasicInputWithoutDD.bind(null, t)
)
})
})

describe('DD_TRACING_ENABLED=true, DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=true', function () {
const t = setup({
testApp: 'target-app/basic.js',
Expand Down
8 changes: 5 additions & 3 deletions integration-tests/debugger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,15 @@ function setupAssertionListeners (t, done, probe) {
let traceId, spanId, dd

const messageListener = ({ payload }) => {
const span = payload.find((arr) => arr[0].name === 'fastify.request')?.[0]
const span = payload
.flat()
.find((span) => span.name === 'fastify.request' && (!dd || span.span_id.toString() === dd.span_id))

if (!span) return

traceId = span.trace_id.toString()
spanId = span.span_id.toString()

t.agent.removeListener('message', messageListener)

assertDD()
}

Expand All @@ -336,6 +337,7 @@ function setupAssertionListeners (t, done, probe) {
if (!traceId || !spanId || !dd) return
assert.strictEqual(dd.trace_id, traceId)
assert.strictEqual(dd.span_id, spanId)
t.agent.removeListener('message', messageListener)
done()
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/datadog-plugin-openai/src/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports.init = function (tracerConfig) {
`env:${tracerConfig.tags.env}`,
`version:${tracerConfig.tags.version}`,
],
lookup: tracerConfig.lookup,
})
: new NoopDogStatsDClient()

Expand Down
51 changes: 48 additions & 3 deletions packages/datadog-plugin-openai/test/services.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
'use strict'

const sinon = require('sinon')
const proxyquire = require('proxyquire')

const services = require('../src/services')
const { getConfigFresh } = require('../../dd-trace/test/helpers/config')

describe('Plugin', () => {
describe('openai services', () => {
describe('when unconfigured', () => {
afterEach(() => {
services.shutdown()
afterEach(() => {
services.shutdown()
})

it('should initialize DogStatsDClient with explicit config values', () => {
const flush = sinon.stub()
const DogStatsDClient = sinon.stub().returns({
flush,
})
const ExternalLogger = sinon.stub().returns({
log: sinon.stub(),
})
const NoopDogStatsDClient = sinon.stub()
const NoopExternalLogger = sinon.stub()
const proxiedServices = proxyquire('../src/services', {
'../../dd-trace/src/dogstatsd': { DogStatsDClient },
'../../dd-trace/src/noop/dogstatsd': NoopDogStatsDClient,
'../../dd-trace/src/external-logger/src': {
ExternalLogger,
NoopExternalLogger,
},
})
const config = getConfigFresh({
env: 'prod',
hostname: 'foo',
service: 'bar',
version: '1.2.3',
})

proxiedServices.init(config)

sinon.assert.calledOnceWithExactly(DogStatsDClient, {
host: config.dogstatsd.hostname,
lookup: config.lookup,
port: config.dogstatsd.port,
tags: [
'service:bar',
'env:prod',
'version:1.2.3',
],
})
sinon.assert.notCalled(NoopDogStatsDClient)

proxiedServices.shutdown()
})

describe('when unconfigured', () => {
it('dogstatsd does not throw when missing .dogstatsd', () => {
const service = services.init(getConfigFresh({
hostname: 'foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const probeIdToResolveBreakpointSet = new Map()
const probeIdToResolveBreakpointRemove = new Map()

class TestVisDynamicInstrumentation {
/**
* @param {import('../../config/config-base')} config - Tracer configuration
*/
constructor (config) {
this._config = config
this.worker = null
Expand Down Expand Up @@ -83,7 +86,6 @@ class TestVisDynamicInstrumentation {
DD_TRACE_ENABLED: 'false',
DD_TEST_FAILED_TEST_REPLAY_ENABLED: 'false',
DD_CIVISIBILITY_MANUAL_API_ENABLED: 'false',
DD_TRACING_ENABLED: 'false',
DD_INSTRUMENTATION_TELEMETRY_ENABLED: 'false',
},
workerData: {
Expand Down Expand Up @@ -150,6 +152,9 @@ class TestVisDynamicInstrumentation {

let dynamicInstrumentation

/**
* @param {import('../../config/config-base')} config - Tracer configuration
*/
module.exports = function createAndGetTestVisDynamicInstrumentation (config) {
if (dynamicInstrumentation) {
return dynamicInstrumentation
Expand Down
21 changes: 12 additions & 9 deletions packages/dd-trace/src/dogstatsd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

const lookup = require('dns').lookup // cache to avoid instrumentation
const dgram = require('dgram')
const isIP = require('net').isIP

const request = require('./exporters/common/request')
const log = require('./log')
const Histogram = require('./histogram')
const { defaults } = require('./config/defaults')
const { getAgentUrl } = require('./agent/url')
const { entityId } = require('./exporters/common/docker')

Expand All @@ -23,7 +21,9 @@ const TYPE_HISTOGRAM = 'h'
* @implements {DogStatsD}
*/
class DogStatsDClient {
constructor (options = {}) {
#lookup
constructor (options) {
this.#lookup = options.lookup
if (options.metricsProxyUrl) {
this._httpOptions = {
method: 'POST',
Expand All @@ -32,11 +32,10 @@ class DogStatsDClient {
}
}

this._host = options.host || defaults['dogstatsd.hostname']
this._host = options.host
this._family = isIP(this._host)
this._port = options.port || defaults['dogstatsd.port']
this._prefix = options.prefix || ''
this._tags = options.tags || []
this._port = options.port
this._tags = options.tags
this._queue = []
this._buffer = ''
this._offset = 0
Expand Down Expand Up @@ -99,7 +98,7 @@ class DogStatsDClient {

_sendUdp (queue) {
if (this._family === 0) {
lookup(this._host, (err, address, family) => {
this.#lookup(this._host, (err, address, family) => {
if (err) return log.error('DogStatsDClient: Host not found', err)
this._sendUdpFromQueue(queue, address, family)
})
Expand All @@ -118,7 +117,7 @@ class DogStatsDClient {
}

_add (stat, value, type, tags) {
let message = `${this._prefix + stat}:${value}|${type}`
let message = `${stat}:${value}|${type}`

// Don't manipulate this._tags as it is still used
tags = tags ? [...this._tags, ...tags] : this._tags
Expand Down Expand Up @@ -164,6 +163,9 @@ class DogStatsDClient {
return socket
}

/**
* @param {import('./config/config-base')} config - Tracer configuration
*/
static generateClientConfig (config) {
const tags = []

Expand All @@ -183,6 +185,7 @@ class DogStatsDClient {
host: config.dogstatsd.hostname,
port: config.dogstatsd.port,
tags,
lookup: config.lookup,
}

if (config.url || config.port) {
Expand Down
Loading
Loading