diff --git a/types/dd-trace/dd-trace-tests.ts b/types/dd-trace/dd-trace-tests.ts index 18f5daae10..bfde3536f4 100644 --- a/types/dd-trace/dd-trace-tests.ts +++ b/types/dd-trace/dd-trace-tests.ts @@ -1,7 +1,7 @@ -import * as ddTrace from 'dd-trace'; -import SpanContext = require('dd-trace/src/opentracing/span_context'); +import * as tracer from 'dd-trace'; +import { SpanContext } from 'opentracing'; -const tracer = ddTrace.init({ +tracer.init({ service: 'MyLovelyService', hostname: 'localhost', port: 8126, @@ -14,7 +14,7 @@ const tracer = ddTrace.init({ tracer .trace('web.request', { service: 'my_service', - childOf: new SpanContext({ traceId: 1337, spanId: 42 }), + childOf: new SpanContext(), tags: { env: 'dev' } @@ -23,3 +23,13 @@ tracer span.setTag('my_tag', 'my_value'); span.finish(); }); + +const parentScope = tracer.scopeManager().active(); +const span = tracer.startSpan('memcached', { + childOf: parentScope && parentScope.span(), + tags: { + '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 11bcdb42f2..d7130f3f5b 100644 --- a/types/dd-trace/index.d.ts +++ b/types/dd-trace/index.d.ts @@ -1,11 +1,10 @@ -// Type definitions for dd-trace-js 0.2 +// Type definitions for dd-trace-js 0.5 // Project: https://github.com/DataDog/dd-trace-js // Definitions by: Colin Bradley +// Eloy DurĂ¡n // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -import Tracer = require('./src/opentracing/tracer'); -import Span = require('./src/opentracing/span'); -import SpanContext = require('./src/opentracing/span_context'); +import { Tracer, Span, SpanContext } from "opentracing"; declare var trace: TraceProxy; export = trace; @@ -42,6 +41,11 @@ declare class TraceProxy extends Tracer { * @returns The current span or null if outside a trace context. */ currentSpan(): Span | null; + + /** + * Get the scope manager to manager context propagation for the tracer. + */ + scopeManager(): ScopeManager; } interface TracerOptions { @@ -147,3 +151,34 @@ interface TraceOptions { */ tags?: { [key: string]: any } | string; } + +declare class ScopeManager { + /** + * Get the current active scope or null if there is none. + * + * @todo The dd-trace source returns null, but opentracing's childOf span + * option is typed as taking undefined or a scope, so using undefined + * here instead. + */ + active(): Scope | undefined; + + /** + * Activate a new scope wrapping the provided span. + * + * @param span The span for which to activate the new scope. + * @param finishSpanOnClose Whether to automatically finish the span when the scope is closed. + */ + activate(span: Span, finishSpanOnClose?: boolean): Scope; +} + +declare class Scope { + /** + * Get the span wrapped by this scope. + */ + span(): Span; + + /** + * Close the scope, and finish the span if the scope was created with `finishSpanOnClose` set to true. + */ + close(): void; +} diff --git a/types/dd-trace/src/opentracing/span.d.ts b/types/dd-trace/src/opentracing/span.d.ts deleted file mode 100644 index cf9286210b..0000000000 --- a/types/dd-trace/src/opentracing/span.d.ts +++ /dev/null @@ -1,121 +0,0 @@ -import Tracer = require('./tracer'); -import SpanContext = require('./span_context'); -import { Span } from 'opentracing'; - -declare class DatadogSpan extends Span { - /** - * Returns the SpanContext object associated with this Span. - */ - context(): SpanContext; - - /** - * Returns the Tracer object used to create this Span. - */ - tracer(): Tracer; - - /** - * Sets the string name for the logical operation this span represents. - */ - setOperationName(name: string): this; - - /** - * Sets a key:value pair on this Span that also propagates to future - * children of the associated Span. - * - * setBaggageItem() enables powerful functionality given a full-stack - * opentracing integration (e.g., arbitrary application data from a web - * client can make it, transparently, all the way into the depths of a - * storage system), and with it some powerful costs: use this feature with - * care. - * - * IMPORTANT NOTE #1: setBaggageItem() will only propagate baggage items to - * *future* causal descendants of the associated Span. - * - * IMPORTANT NOTE #2: Use this thoughtfully and with care. Every key and - * value is copied into every local *and remote* child of the associated - * Span, and that can add up to a lot of network and cpu overhead. - */ - setBaggageItem(key: string, value: string): this; - - /** - * Returns the value for a baggage item given its key. - * - * @param key - * The key for the given trace attribute. - * @return String value for the given key, or undefined if the key does not - * correspond to a set trace attribute. - */ - getBaggageItem(key: string): string | undefined; - - /** - * Adds a single tag to the span. See `addTags()` for details. - */ - setTag(key: string, value: any): this; - - /** - * Adds the given key value pairs to the set of span tags. - * - * Multiple calls to addTags() results in the tags being the superset of - * all calls. - * - * The behavior of setting the same key multiple times on the same span - * is undefined. - * - * The supported type of the values is implementation-dependent. - * Implementations are expected to safely handle all types of values but - * may choose to ignore unrecognized / unhandle-able values (e.g. objects - * with cyclic references, function objects). - */ - addTags(keyValueMap: { - [key: string]: any; - }): this; - - /** - * Add a log record to this Span, optionally at a user-provided timestamp. - * - * For example: - * - * span.log({ - * size: rpc.size(), // numeric value - * URI: rpc.URI(), // string value - * payload: rpc.payload(), // Object value - * "keys can be arbitrary strings": rpc.foo(), - * }); - * - * span.log({ - * "error.description": someError.description(), - * }, someError.timestampMillis()); - * - * @param keyValuePairs - * An object mapping string keys to arbitrary value types. All - * Tracer implementations should support bool, string, and numeric - * value types, and some may also support Object values. - * @param timestamp - * An optional parameter specifying the timestamp in milliseconds - * since the Unix epoch. Fractional values are allowed so that - * timestamps with sub-millisecond accuracy can be represented. If - * not specified, the implementation is expected to use its notion - * of the current time of the call. - */ - - log(keyValuePairs: { - [key: string]: any; - }, timestamp?: number): this; - - /** - * Sets the end timestamp and finalizes Span state. - * - * With the exception of calls to Span.context() (which are always allowed), - * finish() must be the last call made to any span instance, and to do - * otherwise leads to undefined behavior. - * - * @param finishTime - * Optional finish time in milliseconds as a Unix timestamp. Decimal - * values are supported for timestamps with sub-millisecond accuracy. - * If not specified, the current time (as defined by the - * implementation) will be used. - */ - finish(finishTime?: number): void; -} - -export = DatadogSpan; diff --git a/types/dd-trace/src/opentracing/span_context.d.ts b/types/dd-trace/src/opentracing/span_context.d.ts deleted file mode 100644 index cf9565d5a5..0000000000 --- a/types/dd-trace/src/opentracing/span_context.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { SpanContext } from 'opentracing'; - -/** - * SpanContext represents Span state that must propagate to descendant Spans - * and across process boundaries. - * - * SpanContext is logically divided into two pieces: the user-level "Baggage" - * (see setBaggageItem and getBaggageItem) that propagates across Span - * boundaries and any Tracer-implementation-specific fields that are needed to - * identify or otherwise contextualize the associated Span instance (e.g., a - * tuple). - */ -declare class DatadogSpanContext extends SpanContext { - /** - * Use to create references to parent spans. - */ - constructor(props: SpanContextLike); - - public traceId: number; - - public spanId: number; - - public parentId?: number | null; - - public sampled?: boolean; - - public baggageItems?: { [key: string]: string }; - - public trace?: { - started: number[], - finished: number[] - } -} - -interface SpanContextLike { - traceId: number; - spanId: number; -} - -export = DatadogSpanContext; diff --git a/types/dd-trace/src/opentracing/tracer.d.ts b/types/dd-trace/src/opentracing/tracer.d.ts deleted file mode 100644 index 252a0d558c..0000000000 --- a/types/dd-trace/src/opentracing/tracer.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Tracer } from 'opentracing'; - -/** - * Tracer is the entry-point between the instrumentation API and the tracing - * implementation. - */ -declare class DatadogTracer extends Tracer { - -} - -export = DatadogTracer;