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

v5.41.1 proposal #5367

Merged
merged 5 commits into from
Mar 7, 2025
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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dd-trace",
"version": "5.41.0",
"version": "5.41.1",
"description": "Datadog APM tracing client for JavaScript",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (globalThis.fetch) {

const ch = tracingChannel('apm:fetch:request')
const wrapFetch = createWrapFetch(globalThis.Request, ch, () => {
channel('dd-trace:instrumentation:load').publish({ name: 'fetch' })
channel('dd-trace:instrumentation:load').publish({ name: 'global:fetch' })
})

fetch = wrapFetch(globalFetch)
Expand Down
59 changes: 59 additions & 0 deletions packages/datadog-plugin-fetch/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,5 +609,64 @@ describe('Plugin', function () {
})
})
})

describe('in serverless', () => {
beforeEach(() => {
process.env.DD_TRACE_EXPERIMENTAL_EXPORTER = 'agent'
process.env.AWS_LAMBDA_FUNCTION_NAME = 'test'
})

beforeEach(() => {
return agent.load('fetch')
.then(() => {
express = require('express')
fetch = globalThis.fetch
})
})

beforeEach(() => {
delete process.env.DD_TRACE_EXPERIMENTAL_EXPORTER
delete process.env.AWS_LAMBDA_FUNCTION_NAME
})

withNamingSchema(
() => {
const app = express()
app.get('/user', (req, res) => {
res.status(200).send()
})

appListener = server(app, port => {
fetch(`http://localhost:${port}/user`)
})
},
rawExpectedSchema.client
)

it('should do automatic instrumentation', done => {
const app = express()
app.get('/user', (req, res) => {
res.status(200).send()
})
appListener = server(app, port => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', SERVICE_NAME)
expect(traces[0][0]).to.have.property('type', 'http')
expect(traces[0][0]).to.have.property('resource', 'GET')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
expect(traces[0][0].meta).to.have.property('http.url', `http://localhost:${port}/user`)
expect(traces[0][0].meta).to.have.property('http.method', 'GET')
expect(traces[0][0].meta).to.have.property('http.status_code', '200')
expect(traces[0][0].meta).to.have.property('component', 'fetch')
expect(traces[0][0].meta).to.have.property('out.host', 'localhost')
})
.then(done)
.catch(done)

fetch(`http://localhost:${port}/user`)
})
})
})
})
})
165 changes: 137 additions & 28 deletions packages/dd-trace/src/dogstatsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dgram = require('dgram')
const isIP = require('net').isIP
const log = require('./log')
const { URL, format } = require('url')
const Histogram = require('./histogram')

const MAX_BUFFER_SIZE = 1024 // limit from the agent

Expand Down Expand Up @@ -193,6 +194,117 @@ class DogStatsDClient {
}
}

// TODO: Handle arrays of tags and tags translation.
class MetricsAggregationClient {
constructor (client) {
this._client = client

this.reset()
}

flush () {
this._captureCounters()
this._captureGauges()
this._captureHistograms()

this._client.flush()
}

reset () {
this._counters = {}
this._gauges = {}
this._histograms = {}
}

distribution (name, value, tag) {
this._client.distribution(name, value, tag && [tag])
}

boolean (name, value, tag) {
this.gauge(name, value ? 1 : 0, tag)
}

histogram (name, value, tag) {
this._histograms[name] = this._histograms[name] || new Map()

if (!this._histograms[name].has(tag)) {
this._histograms[name].set(tag, new Histogram())
}

this._histograms[name].get(tag).record(value)
}

count (name, count, tag, monotonic = false) {
if (typeof tag === 'boolean') {
monotonic = tag
tag = undefined
}

const map = monotonic ? this._counters : this._gauges

map[name] = map[name] || new Map()

const value = map[name].get(tag) || 0

map[name].set(tag, value + count)
}

gauge (name, value, tag) {
this._gauges[name] = this._gauges[name] || new Map()
this._gauges[name].set(tag, value)
}

increment (name, count = 1, tag, monotonic) {
this.count(name, count, tag, monotonic)
}

decrement (name, count = 1, tag) {
this.count(name, -count, tag)
}

_captureGauges () {
Object.keys(this._gauges).forEach(name => {
this._gauges[name].forEach((value, tag) => {
this._client.gauge(name, value, tag && [tag])
})
})
}

_captureCounters () {
Object.keys(this._counters).forEach(name => {
this._counters[name].forEach((value, tag) => {
this._client.increment(name, value, tag && [tag])
})
})

this._counters = {}
}

_captureHistograms () {
Object.keys(this._histograms).forEach(name => {
this._histograms[name].forEach((stats, tag) => {
const tags = tag && [tag]

// Stats can contain garbage data when a value was never recorded.
if (stats.count === 0) {
stats = { max: 0, min: 0, sum: 0, avg: 0, median: 0, p95: 0, count: 0, reset: stats.reset }
}

this._client.gauge(`${name}.min`, stats.min, tags)
this._client.gauge(`${name}.max`, stats.max, tags)
this._client.increment(`${name}.sum`, stats.sum, tags)
this._client.increment(`${name}.total`, stats.sum, tags)
this._client.gauge(`${name}.avg`, stats.avg, tags)
this._client.increment(`${name}.count`, stats.count, tags)
this._client.gauge(`${name}.median`, stats.median, tags)
this._client.gauge(`${name}.95percentile`, stats.p95, tags)

stats.reset()
})
})
}
}

/**
* This is a simplified user-facing proxy to the underlying DogStatsDClient instance
*
Expand All @@ -201,7 +313,7 @@ class DogStatsDClient {
class CustomMetrics {
constructor (config) {
const clientConfig = DogStatsDClient.generateClientConfig(config)
this.dogstatsd = new DogStatsDClient(clientConfig)
this._client = new MetricsAggregationClient(new DogStatsDClient(clientConfig))

const flush = this.flush.bind(this)

Expand All @@ -212,47 +324,43 @@ class CustomMetrics {
}

increment (stat, value = 1, tags) {
return this.dogstatsd.increment(
stat,
value,
CustomMetrics.tagTranslator(tags)
)
for (const tag of this._normalizeTags(tags)) {
this._client.increment(stat, value, tag)
}
}

decrement (stat, value = 1, tags) {
return this.dogstatsd.decrement(
stat,
value,
CustomMetrics.tagTranslator(tags)
)
for (const tag of this._normalizeTags(tags)) {
this._client.decrement(stat, value, tag)
}
}

gauge (stat, value, tags) {
return this.dogstatsd.gauge(
stat,
value,
CustomMetrics.tagTranslator(tags)
)
for (const tag of this._normalizeTags(tags)) {
this._client.gauge(stat, value, tag)
}
}

distribution (stat, value, tags) {
return this.dogstatsd.distribution(
stat,
value,
CustomMetrics.tagTranslator(tags)
)
for (const tag of this._normalizeTags(tags)) {
this._client.distribution(stat, value, tag)
}
}

histogram (stat, value, tags) {
return this.dogstatsd.histogram(
stat,
value,
CustomMetrics.tagTranslator(tags)
)
for (const tag of this._normalizeTags(tags)) {
this._client.histogram(stat, value, tag)
}
}

flush () {
return this.dogstatsd.flush()
return this._client.flush()
}

_normalizeTags (tags) {
tags = CustomMetrics.tagTranslator(tags)

return tags.length === 0 ? [undefined] : tags
}

/**
Expand All @@ -274,5 +382,6 @@ class CustomMetrics {

module.exports = {
DogStatsDClient,
CustomMetrics
CustomMetrics,
MetricsAggregationClient
}
4 changes: 0 additions & 4 deletions packages/dd-trace/src/plugin_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ module.exports = class PluginManager {
this._tracerConfig = config
this._tracer._nomenclature.configure(config)

if (!config._isInServerlessEnvironment?.()) {
maybeEnable(require('../../datadog-plugin-fetch/src'))
}

for (const name in pluginClasses) {
this.loadPlugin(name)
}
Expand Down
1 change: 1 addition & 0 deletions packages/dd-trace/src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
get express () { return require('../../../datadog-plugin-express/src') },
get fastify () { return require('../../../datadog-plugin-fastify/src') },
get 'find-my-way' () { return require('../../../datadog-plugin-find-my-way/src') },
get 'global:fetch' () { return require('../../../datadog-plugin-fetch/src') },
get graphql () { return require('../../../datadog-plugin-graphql/src') },
get grpc () { return require('../../../datadog-plugin-grpc/src') },
get hapi () { return require('../../../datadog-plugin-hapi/src') },
Expand Down
Loading
Loading