dd-trace-js icon indicating copy to clipboard operation
dd-trace-js copied to clipboard

dd-trace-js doesn't appear to instrument app code when initialized and imported from another package

Open leoyu037 opened this issue 8 years ago • 5 comments

Issue

I have the following in a uc/koa-datadog package:

import env from 'env-var';
import dd_trace from 'dd-trace';

const dd_agent_host = env.get('DATADOG_AGENT_HOST', 'localhost').asString();
const tags = [
  `kube_namespace:${env.get('KUBE_NAMESPACE', 'local').asString()}`,
  `kube_deployment:${env.get('KUBE_DEPLOYMENT', 'local').asString()}`,
  `pod_name:${env.get('KUBE_POD_NAME', 'local').asString()}`,
  `docker_image:${env.get('DOCKER_IMAGE', 'local').asString()}`,
];

export function tracer(options = {}) {
  return dd_trace.init(Object.assign({
    hostname: dd_agent_host,
    env: `${process.env.KUBE_NAMESPACE || 'local'}`,
    service: `${process.env.KUBE_DEPLOYMENT || 'local'}`,
    tags: tags,
  }, options));
};

// ...

And then in my app, I’ve specified koa-datadog as a dependency and I have these lines in my index.js:

const {tracer} = require('@uc/koa-datadog');
t = tracer();

However, there seems to be a problem with the auto-instrumentation of Koa code when the tracer is initialized this way. After speaking with @rochdev, we confirmed that manual traces work:

router.get('/goodbye', async (ctx) => {
  await sleep.msleep(Math.random() * 1000 | 0)
  const span = dd_tracer.startSpan('test.request')
  ctx.body = {
    status: 'success',
    message: 'goodbye!'
  };
  span.finish()
})

Docker trace agent log:

Encoding trace: [{"trace_id":5408573738803529000,"span_id":5408573738803529000,"parent_id":null,"name":"test.request","resource":"test.request","service":"local","error":0,"meta":{"0":"kube_namespace:local","1":"kube_deployment:local","2":"pod_name:local","3":"docker_image:local","env":"local"},"metrics":{"_sample_rate":1,"_sampling_priority_v1":1},"start":1538686542217239600,"duration":2232422}]
Adding encoded trace to buffer: 91 8b a8 74 72 61 63 65 5f 69 64 cf 4b 0f 1d 28 da e4 05 a1 a7 73 70 61 6e 5f 69 64 cf 4b 0f 1d 28 da e4 05 a1 a9 70 61 72 65 6e 74 5f 69 64 c0 a4 6e 61 6d 65 ac 74 65 73 74 2e 72 65 71 75 65 73 74 a8 72 65 73 6f 75 72 63 65 ac 74 65 73 74 2e 72 65 71 75 65 73 74 a7 73 65 72 76 69 63 65 a5 6c 6f 63 61 6c a5 65 72 72 6f 72 00 a4 6d 65 74 61 85 a1 30 b4 6b 75 62 65 5f 6e 61 6d 65 73 70 61 63 65 3a 6c 6f 63 61 6c a1 31 b5 6b 75 62 65 5f 64 65 70 6c 6f 79 6d 65 6e 74 3a 6c 6f 63 61 6c a1 32 ae 70 6f 64 5f 6e 61 6d 65 3a 6c 6f 63 61 6c a1 33 b2 64 6f 63 6b 65 72 5f 69 6d 61 67 65 3a 6c 6f 63 61 6c a3 65 6e 76 a5 6c 6f 63 61 6c a7 6d 65 74 72 69 63 73 82 ac 5f 73 61 6d 70 6c 65 5f 72 61 74 65 01 b5 5f 73 61 6d 70 6c 69 6e 67 5f 70 72 69 6f 72 69 74 79 5f 76 31 01 a5 73 74 61 72 74 d3 15 5a 83 42 01 88 5c 00 a8 64 75 72 61 74 69 6f 6e d3 00 00 00 00 00 22 10 66
Request to the agent: {"protocol":"http:","hostname":"localhost","port":"8126","path":"/v0.4/traces","method":"PUT","headers":{"Content-Type":"application/msgpack","Datadog-Meta-Lang":"nodejs","Datadog-Meta-Lang-Version":"v10.10.0","Datadog-Meta-Lang-Interpreter":"v8","Datadog-Meta-Tracer-Version":"0.6.0","X-Datadog-Trace-Count":"1"}}
Response from the agent: {"rate_by_service":{"service:,env:":1,"service:local,env:local":1}}

Motivation

The reason that I want to have a pre-configured version of the tracer to import is because we have a lot of product teams and I want to provide a consistent monitoring experience across their applications. Since the applications are all deployed to Kubernetes, I want the tracer to be initialized with certain tags across all of our applications.

leoyu037 avatar Oct 05 '18 18:10 leoyu037

can you set tags: tags, ? I don't see this as a field in the ts TracerOptions interface

willyyang avatar Oct 15 '18 19:10 willyyang

can you set tags: tags, ? I don't see this as a field in the ts TracerOptions interface

@willyyang https://datadog.github.io/dd-trace-js/#tracer-settings

leoyu037 avatar Oct 15 '18 20:10 leoyu037

Update: this seems to be an issue with using the esm module in our packaged, preconfigured tracer. We have found that using babel to transpile our package code from ESM to CJS style imports allows us to use to package in the following 3 cases:

  1. Node app that doesn't use esm, using CJS style imports
  2. Node app that uses esm, using CJS style imports
  3. Node app that uses esm, using ESM style imports

@rochdev not sure whether or not to close this issue, since we've technically found a solution for our use case.

leoyu037 avatar Nov 05 '18 15:11 leoyu037

Opened https://github.com/DataDog/dd-trace-js/issues/349 to track ES modules specifically.

rochdev avatar Nov 05 '18 15:11 rochdev

Okay, closing issue

leoyu037 avatar Nov 05 '18 16:11 leoyu037