Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

chore: move span method for context in trace API #40 #47

Merged
merged 2 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Because the npm installer and node module resolution algorithm could potentially

- `HttpBaggage` renamed to `HttpBaggagePropagator`
- [#45](https://github.com/open-telemetry/opentelemetry-js-api/pull/45) `Span#context` renamed to `Span#spanContext`
- [#47](https://github.com/open-telemetry/opentelemetry-js-api/pull/47) `getSpan`/`setSpan`/`getSpanContext`/`setSpanContext` moved to `trace` namespace

## Useful links

Expand Down
14 changes: 14 additions & 0 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
23 changes: 23 additions & 0 deletions src/baggage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.
*
Expand Down
108 changes: 0 additions & 108 deletions src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
106 changes: 106 additions & 0 deletions src/trace/context-utils.ts
Original file line number Diff line number Diff line change
@@ -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));
}
4 changes: 2 additions & 2 deletions test/noop-implementations/noop-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
SpanKind,
TraceFlags,
context,
setSpanContext,
trace,
} from '../../src';
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan';

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down