The type definition for the "dd-trace" library, in "@types/dd-trace", exports a single variable.
declare var trace: TraceProxy;
export = trace;
declare class TraceProxy extends Tracer {
/**
* Initializes the tracer. This should be called before importing other libraries.
*/
init(options?: TracerOptions): this;
// A bunch of other irrelevant code.
}
How can I import this in my code? If I incorrectly try assign ddTrace.init() to a boolean, TypeScript tells me the type is 'TraceProxy'. However I have tried every seeming variation:
import { TraceProxy } from "dd-trace"
fails with node_modules/@types/dd-trace"' has no exported member 'TraceProxy'.
import { init, trace } from "dd-trace"
const tracer: trace = init()
The import succeeds there but then the declaration fails: 3:15: cannot find name "trace"
All of these variations fail:
const tracer: trace.trace = init()
const tracer: trace.TraceProxy = init()
const tracer: trace.Tracer = init()
const tracer: TraceProxy = init()
const tracer: Tracer = init()
Importing the module fails:
import * as ddTrace from "dd-trace"
const tracer: ddTrace = ddTrace.init()
with Cannot find name 'ddTrace'.
on line 3.
These also failed:
import * as ddTrace from "dd-trace"
const tracer: ddTrace.TraceProxy = ddTrace.init()
with Cannot find namespace 'ddTrace'.
One suggested answer (since deleted) was:
import trace from "dd-trace"
const tracer: trace = trace.init()
This fails with: @types/dd-trace/index"' has no default export.
How can I declare the type definition there? I'm using the latest version of TypeScript and compiling by running ./node_modules/.bin/tsc myfile.ts
.
The export from the module is actually the tracer itself. Calling tracer.init()
simply initializes the tracer and returns itself. This means you don't need to create a new variable.
This should work:
import * as ddTrace from "dd-trace";
const tracer = ddTrace;
tracer.init();
// continue to use "tracer" for example "tracer.startSpan()" etc
One important thing to note also if you are using any of our integrations is that the initialization should happen in a separate file to avoid hoisting.
For example:
// server.ts
import tracer from "./tracer";
import * as express from "express"; // or any other modules
// tracer.ts
import * as tracer from "dd-trace";
tracer.init();
export default tracer;
This is done to make sure that the tracer is initialized before any instrumented module is imported.
If you are not actually using the tracer in your application entrypoint, simply replace import tracer from "./tracer"
with import "./tracer"
.