From d2dad62ca98e440a82b661fd72d2a1bd652eaeef Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sun, 25 Apr 2021 16:45:19 +0200 Subject: [PATCH] chore: move span method for context in trace API #40 --- src/api/trace.ts | 14 +++ src/baggage/index.ts | 23 ++++ src/context/context.ts | 108 ------------------ src/trace/NoopTracer.ts | 2 +- src/trace/context-utils.ts | 106 +++++++++++++++++ test/noop-implementations/noop-tracer.test.ts | 4 +- .../context-utils.test.ts} | 5 +- 7 files changed, 148 insertions(+), 114 deletions(-) create mode 100644 src/trace/context-utils.ts rename test/{context/context.test.ts => trace/context-utils.test.ts} (95%) diff --git a/src/api/trace.ts b/src/api/trace.ts index c5195969..f44fe256 100644 --- a/src/api/trace.ts +++ b/src/api/trace.ts @@ -26,6 +26,12 @@ import { } from '../trace/spancontext-utils'; import { Tracer } from '../trace/tracer'; import { TracerProvider } from '../trace/tracer_provider'; +import { + getSpan, + getSpanContext, + setSpan, + setSpanContext, +} from '../trace/context-utils'; const API_NAME = 'trace'; @@ -82,4 +88,12 @@ export class TraceAPI { public wrapSpanContext = wrapSpanContext; public isSpanContextValid = isSpanContextValid; + + public getSpan = getSpan; + + public getSpanContext = getSpanContext; + + public setSpan = setSpan; + + public setSpanContext = setSpanContext; } diff --git a/src/baggage/index.ts b/src/baggage/index.ts index ba0a297c..03eda420 100644 --- a/src/baggage/index.ts +++ b/src/baggage/index.ts @@ -18,10 +18,17 @@ import { Baggage } from './Baggage'; import { BaggageEntry, BaggageEntryMetadata } from './Entry'; import { BaggageImpl } from './internal/baggage'; import { baggageEntryMetadataSymbol } from './internal/symbol'; +import { Context } from '../context/types'; +import { createContextKey } from '../context/context'; export * from './Baggage'; export * from './Entry'; +/** + * Baggage key + */ +const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key'); + /** * Create a new Baggage with optional entries * @@ -33,6 +40,22 @@ export function createBaggage( return new BaggageImpl(new Map(Object.entries(entries))); } +/** + * @param {Context} Context that manage all context values + * @returns {Baggage} Extracted baggage from the context + */ +export function getBaggage(context: Context): Baggage | undefined { + return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined; +} + +/** + * @param {Context} Context that manage all context values + * @param {Baggage} baggage that will be set in the actual context + */ +export function setBaggage(context: Context, baggage: Baggage): Context { + return context.setValue(BAGGAGE_KEY, baggage); +} + /** * Create a serializable BaggageEntryMetadata object from a string. * diff --git a/src/context/context.ts b/src/context/context.ts index 0be6163e..ab058b85 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -15,114 +15,6 @@ */ import { Context } from './types'; -import { Baggage, Span, SpanContext } from '../'; -import { NonRecordingSpan } from '../trace/NonRecordingSpan'; - -/** - * span key - */ -const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN'); - -/** - * Shared key for indicating if instrumentation should be suppressed beyond - * this current scope. - */ -const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( - 'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION' -); - -/** - * Baggage key - */ -const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key'); - -/** - * Return the span if one exists - * - * @param context context to get span from - */ -export function getSpan(context: Context): Span | undefined { - return (context.getValue(SPAN_KEY) as Span) || undefined; -} - -/** - * Set the span on a context - * - * @param context context to use as parent - * @param span span to set active - */ -export function setSpan(context: Context, span: Span): Context { - return context.setValue(SPAN_KEY, span); -} - -/** - * Wrap span context in a NonRecordingSpan and set as span in a new - * context - * - * @param context context to set active span on - * @param spanContext span context to be wrapped - */ -export function setSpanContext( - context: Context, - spanContext: SpanContext -): Context { - return setSpan(context, new NonRecordingSpan(spanContext)); -} - -/** - * Get the span context of the span if it exists. - * - * @param context context to get values from - */ -export function getSpanContext(context: Context): SpanContext | undefined { - return getSpan(context)?.spanContext(); -} - -/** - * Sets value on context to indicate that instrumentation should - * be suppressed beyond this current scope. - * - * @param context context to set the suppress instrumentation value on. - */ -export function suppressInstrumentation(context: Context): Context { - return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true); -} - -/** - * Sets value on context to indicate that instrumentation should - * no-longer be suppressed beyond this current scope. - * - * @param context context to set the suppress instrumentation value on. - */ -export function unsuppressInstrumentation(context: Context): Context { - return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, false); -} - -/** - * Return current suppress instrumentation value for the given context, - * if it exists. - * - * @param context context check for the suppress instrumentation value. - */ -export function isInstrumentationSuppressed(context: Context): boolean { - return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY)); -} - -/** - * @param {Context} Context that manage all context values - * @returns {Baggage} Extracted baggage from the context - */ -export function getBaggage(context: Context): Baggage | undefined { - return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined; -} - -/** - * @param {Context} Context that manage all context values - * @param {Baggage} baggage that will be set in the actual context - */ -export function setBaggage(context: Context, baggage: Baggage): Context { - return context.setValue(BAGGAGE_KEY, baggage); -} /** Get a key to uniquely identify a context value */ export function createContextKey(description: string) { diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index 1fbaf869..67eac54f 100644 --- a/src/trace/NoopTracer.ts +++ b/src/trace/NoopTracer.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { getSpanContext } from '../context/context'; +import { getSpanContext } from '../trace/context-utils'; import { Context } from '../context/types'; import { NonRecordingSpan } from './NonRecordingSpan'; import { Span } from './span'; diff --git a/src/trace/context-utils.ts b/src/trace/context-utils.ts new file mode 100644 index 00000000..4d8d2fe8 --- /dev/null +++ b/src/trace/context-utils.ts @@ -0,0 +1,106 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createContextKey } from '../context/context'; +import { Context } from '../context/types'; +import { Span } from './span'; +import { SpanContext } from './span_context'; +import { NonRecordingSpan } from './NonRecordingSpan'; + +/** + * span key + */ +const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN'); + +/** + * Shared key for indicating if instrumentation should be suppressed beyond + * this current scope. + */ +const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( + 'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION' +); + +/** + * Return the span if one exists + * + * @param context context to get span from + */ +export function getSpan(context: Context): Span | undefined { + return (context.getValue(SPAN_KEY) as Span) || undefined; +} + +/** + * Set the span on a context + * + * @param context context to use as parent + * @param span span to set active + */ +export function setSpan(context: Context, span: Span): Context { + return context.setValue(SPAN_KEY, span); +} + +/** + * Wrap span context in a NoopSpan and set as span in a new + * context + * + * @param context context to set active span on + * @param spanContext span context to be wrapped + */ +export function setSpanContext( + context: Context, + spanContext: SpanContext +): Context { + return setSpan(context, new NonRecordingSpan(spanContext)); +} + +/** + * Get the span context of the span if it exists. + * + * @param context context to get values from + */ +export function getSpanContext(context: Context): SpanContext | undefined { + return getSpan(context)?.spanContext(); +} + +/** + * Sets value on context to indicate that instrumentation should + * be suppressed beyond this current scope. + * + * @param context context to set the suppress instrumentation value on. + */ +export function suppressInstrumentation(context: Context): Context { + return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true); +} + +/** + * Sets value on context to indicate that instrumentation should + * no-longer be suppressed beyond this current scope. + * + * @param context context to set the suppress instrumentation value on. + */ +export function unsuppressInstrumentation(context: Context): Context { + return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, false); +} + +/** + * Return current suppress instrumentation value for the given context, + * if it exists. + * + * @param context context check for the suppress instrumentation value. + */ +export function isInstrumentationSuppressed(context: Context): boolean { + return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY)); +} diff --git a/test/noop-implementations/noop-tracer.test.ts b/test/noop-implementations/noop-tracer.test.ts index 7527ae7b..292b2fbb 100644 --- a/test/noop-implementations/noop-tracer.test.ts +++ b/test/noop-implementations/noop-tracer.test.ts @@ -21,7 +21,7 @@ import { SpanKind, TraceFlags, context, - setSpanContext, + trace, } from '../../src'; import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan'; @@ -50,7 +50,7 @@ describe('NoopTracer', () => { const span = tracer.startSpan( 'test-1', {}, - setSpanContext(context.active(), parent) + trace.setSpanContext(context.active(), parent) ); assert(span.spanContext().traceId === parent.traceId); assert(span.spanContext().spanId === parent.spanId); diff --git a/test/context/context.test.ts b/test/trace/context-utils.test.ts similarity index 95% rename from test/context/context.test.ts rename to test/trace/context-utils.test.ts index dfea20b9..1f24c34c 100644 --- a/test/context/context.test.ts +++ b/test/trace/context-utils.test.ts @@ -16,12 +16,11 @@ import * as assert from 'assert'; import { - createContextKey, isInstrumentationSuppressed, - ROOT_CONTEXT, suppressInstrumentation, unsuppressInstrumentation, -} from '../../src/context/context'; +} from '../../src/trace/context-utils'; +import { createContextKey, ROOT_CONTEXT } from '../../src/context/context'; const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( 'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'