diff --git a/types/dd-trace/dd-trace-tests.ts b/types/dd-trace/dd-trace-tests.ts index 007ccc30a4..8169358155 100644 --- a/types/dd-trace/dd-trace-tests.ts +++ b/types/dd-trace/dd-trace-tests.ts @@ -1,35 +1,54 @@ -import * as tracer from 'dd-trace'; -import SpanContext = require('dd-trace/src/opentracing/span_context'); +import * as tracer from "dd-trace"; +import SpanContext = require("dd-trace/src/opentracing/span_context"); tracer.init({ - service: 'MyLovelyService', - hostname: 'localhost', + service: "MyLovelyService", + hostname: "localhost", port: 8126, logger: { - debug: msg => { }, - error: err => { } - } + debug: msg => {}, + error: err => {}, + }, +}); + +function useWebFrameworkPlugin(plugin: "express" | "hapi" | "koa" | "restify") { + tracer.use(plugin, { + service: "incoming-request", + headers: ["User-Agent"], + validateStatus: code => code !== 418, + }); +} + +tracer.use("graphql", { + depth: 1, + // Can’t use spread operator here due to https://github.com/Microsoft/TypeScript/issues/10727 + // tslint:disable-next-line:prefer-object-spread + variables: variables => Object.assign({}, variables, { password: "REDACTED" }), +}); + +tracer.use("http", { + splitByDomain: true, }); tracer - .trace('web.request', { - service: 'my_service', - childOf: new SpanContext({ traceId: 1337, spanId: 42 }), // Childof must be an instance of this type. See: https://github.com/DataDog/dd-trace-js/blob/master/src/opentracing/tracer.js#L99 + .trace("web.request", { + service: "my_service", + childOf: new SpanContext({ traceId: 1337, spanId: 42 }), // childOf must be an instance of this type. See: https://github.com/DataDog/dd-trace-js/blob/master/src/opentracing/tracer.js#L99 tags: { - env: 'dev' - } + env: "dev", + }, }) .then(span => { - span.setTag('my_tag', 'my_value'); + span.setTag("my_tag", "my_value"); span.finish(); }); const parentScope = tracer.scopeManager().active(); -const span = tracer.startSpan('memcached', { +const span = tracer.startSpan("memcached", { childOf: parentScope && parentScope.span(), tags: { - 'service.name': 'my-memcached', - 'resource.name': 'get', - 'span.type': 'memcached', + "service.name": "my-memcached", + "resource.name": "get", + "span.type": "memcached", }, }); diff --git a/types/dd-trace/index.d.ts b/types/dd-trace/index.d.ts index a6bbe7dde9..90aa9afc66 100644 --- a/types/dd-trace/index.d.ts +++ b/types/dd-trace/index.d.ts @@ -1,11 +1,15 @@ -// Type definitions for dd-trace-js 0.5 +// Type definitions for dd-trace-js 0.6 // Project: https://github.com/DataDog/dd-trace-js // Definitions by: Colin Bradley // Eloy Durán // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +// Prettified with: +// $ prettier --parser typescript --tab-width 4 --semi --trailing-comma es5 --write --print-width 120 types/dd-trace/{,*}/*.ts* import { Tracer, Span, SpanContext } from "opentracing"; -import DatadogSpanContext = require('./src/opentracing/span_context'); +import DatadogSpanContext = require("./src/opentracing/span_context"); declare var trace: TraceProxy; export = trace; @@ -21,7 +25,7 @@ declare class TraceProxy extends Tracer { * @param plugin The name of a built-in plugin. * @param config Configuration options. */ - use(plugin: string, config: PluginOptions): this; + use

(plugin: P, config: PluginConfiguration[P]): this; /** * Initiate a trace and creates a new span. @@ -103,25 +107,12 @@ interface TracerOptions { * see https://datadog.github.io/dd-trace-js/#custom-logging__anchor */ logger?: { - debug: (message: string) => void - error: (err: Error) => void + debug: (message: string) => void; + error: (err: Error) => void; }; } -interface ExperimentalOptions { - /** - * Whether to use Node's experimental async hooks. - * @default false - */ - asyncHooks?: boolean; -} - -interface PluginOptions { - /** - * The service name to be used for this plugin. - */ - service: string; -} +interface ExperimentalOptions {} interface TraceOptions { /** @@ -185,3 +176,81 @@ declare class Scope { */ close(): void; } + +type Plugin = + | "amqp10" + | "amqplib" + | "elasticsearch" + | "express" + | "graphql" + | "hapi" + | "http" + | "ioredis" + | "koa" + | "memcached" + | "mongodb-core" + | "mysql" + | "mysql2" + | "pg" + | "redis" + | "restify"; + +interface BasePluginOptions { + /** + * The service name to be used for this plugin. + */ + service?: string; +} + +interface BaseWebFrameworkPluginOptions extends BasePluginOptions { + /** + * An array of headers to include in the span metadata. + */ + headers?: string[]; + + /** + * Callback function to determine if there was an error. It should take a + * status code as its only parameter and return `true` for success or `false` + * for errors. + */ + validateStatus?: (code: number) => boolean; +} + +interface ExpressPluginOptions extends BaseWebFrameworkPluginOptions {} + +interface HapiPluginOptions extends BaseWebFrameworkPluginOptions {} + +interface KoaPluginOptions extends BaseWebFrameworkPluginOptions {} + +interface RestifyPluginOptions extends BaseWebFrameworkPluginOptions {} + +interface GraphQLPluginOptions extends BasePluginOptions { + /** + * The maximum depth of fields/resolvers to instrument. Set to `0` to only + * instrument the operation or to -1 to instrument all fields/resolvers. + */ + depth?: number; + + /** + * A callback to enable recording of variables. By default, no variables are + * recorded. For example, using `variables => variables` would record all + * variables. + */ + variables?: (variables: T) => Partial; +} + +interface HTTPPluginOptions extends BasePluginOptions { + /** + * Use the remote endpoint host as the service name instead of the default. + */ + splitByDomain?: boolean; +} + +type PluginConfiguration = { [K in Plugin]: BasePluginOptions } & { + express: ExpressPluginOptions; + graphql: GraphQLPluginOptions; + hapi: HapiPluginOptions; + http: HTTPPluginOptions; + koa: KoaPluginOptions; + restify: RestifyPluginOptions; +}; diff --git a/types/dd-trace/tslint.json b/types/dd-trace/tslint.json index 3db14f85ea..4f44991c3c 100644 --- a/types/dd-trace/tslint.json +++ b/types/dd-trace/tslint.json @@ -1 +1,6 @@ -{ "extends": "dtslint/dt.json" } +{ + "extends": "dtslint/dt.json", + "rules": { + "no-empty-interface": false + } +}