Skip to content

Commit

Permalink
Patch Client.prototype.request
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Jan 31, 2022
1 parent 3d57adf commit f63858c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 5 additions & 9 deletions packages/datadog-plugin-undici/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const url = require('url')
const opentracing = require('opentracing')
const log = require('../../dd-trace/src/log')
const tags = require('../../../ext/tags')
const kinds = require('../../../ext/kinds')
Expand All @@ -10,15 +9,12 @@ const urlFilter = require('../../dd-trace/src/plugins/util/urlfilter')
const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
const { AsyncResource, AsyncLocalStorage } = require('async_hooks')

const Reference = opentracing.Reference

const HTTP_HEADERS = formats.HTTP_HEADERS
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
const HTTP_REQUEST_HEADERS = tags.HTTP_REQUEST_HEADERS
const HTTP_RESPONSE_HEADERS = tags.HTTP_RESPONSE_HEADERS
const SPAN_KIND = tags.SPAN_KIND
const CLIENT = kinds.CLIENT
const REFERENCE_CHILD_OF = opentracing.REFERENCE_CHILD_OF

const asyncLocalStorage = new AsyncLocalStorage()

Expand Down Expand Up @@ -99,10 +95,9 @@ function diagnostics (tracer, config) {
'http.url': uri,
'service.name': getServiceName(tracer, config, request)
})
requestSpansMap.set(request, span)
}

requestSpansMap.set(request, span)

if (!(hasAmazonSignature(request) || !config.propagationFilter(uri))) {
const injectedHeaders = {}
tracer.inject(span, HTTP_HEADERS, injectedHeaders)
Expand All @@ -115,7 +110,7 @@ function diagnostics (tracer, config) {
}

function handleRequestError ({ request, error }) {
const span = asyncLocalStorage.getStore()
const span = requestSpansMap.get(request)
addError(span, error)
finish(request, null, span, config)
}
Expand Down Expand Up @@ -314,7 +309,7 @@ function patch (undici, methodName, tracer, config) {
const scope = tracer.scope()
const childOf = scope.active()
const span = tracer.startSpan('http.request', {
references: [new Reference(REFERENCE_CHILD_OF, childOf)],
childOf,
tags: {
[SPAN_KIND]: CLIENT
}
Expand All @@ -336,8 +331,9 @@ module.exports = [
patch.call(this, undici, 'upgrade', tracer, config)
patch.call(this, undici, 'connect', tracer, config)
patch.call(this, undici, 'fetch', tracer, config)
patch.call(this, undici.Client.prototype, 'request', tracer, config)

// Stream take 3 arguments
// Stream take different args arguments
// patch.call(this, undici, 'stream', tracer, config)
this.unpatch = diagnostics.call(this, tracer, config)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/datadog-plugin-undici/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,9 @@ describe('undici', () => {
})
})

it('should skip requests to the agent', (done) => {
/// Undici is not the client making requests to the agent
// this seems irrelevant at that time
xit('should skip requests to the agent', (done) => {
const app = express()

app.get('/user', (req, res) => {
Expand All @@ -560,7 +562,7 @@ describe('undici', () => {
getPort().then((port) => {
const timer = setTimeout(done, 100)

agent.use(() => {
agent.use((traces) => {
done(new Error('Request to the agent was traced.'))
clearTimeout(timer)
})
Expand Down

0 comments on commit f63858c

Please sign in to comment.