Skip to content

Commit

Permalink
chore: improve naming of span related context APIs (open-telemetry#1749)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flarna authored and dyladan committed Feb 18, 2021
1 parent d6f983a commit 0ae3e85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
34 changes: 14 additions & 20 deletions api/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import { Context, createContextKey } from '@opentelemetry/context-base';
import { Baggage, NoopSpan, Span, SpanContext } from '../';

/**
* Active span key
* span key
*/
const ACTIVE_SPAN_KEY = createContextKey(
'OpenTelemetry Context Key ACTIVE_SPAN'
);
const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN');

/**
* Shared key for indicating if instrumentation should be suppressed beyond
Expand All @@ -38,49 +36,45 @@ const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');

/**
* Return the active span if one exists
* Return the span if one exists
*
* @param context context to get span from
*/
export function getActiveSpan(context: Context): Span | undefined {
return (context.getValue(ACTIVE_SPAN_KEY) as Span) || undefined;
export function getSpan(context: Context): Span | undefined {
return (context.getValue(SPAN_KEY) as Span) || undefined;
}

/**
* Set the active span on a context
* Set the span on a context
*
* @param context context to use as parent
* @param span span to set active
*/
export function setActiveSpan(context: Context, span: Span): Context {
return context.setValue(ACTIVE_SPAN_KEY, span);
export function setSpan(context: Context, span: Span): Context {
return context.setValue(SPAN_KEY, span);
}

/**
* Wrap extracted span context in a NoopSpan and set as active span in a new
* 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 setExtractedSpanContext(
export function setSpanContext(
context: Context,
spanContext: SpanContext
): Context {
return setActiveSpan(context, new NoopSpan(spanContext));
return setSpan(context, new NoopSpan(spanContext));
}

/**
* Get the span context of the parent span if it exists,
* or the extracted span context if there is no active
* span.
* Get the span context of the span if it exists.
*
* @param context context to get values from
*/
export function getParentSpanContext(
context: Context
): SpanContext | undefined {
return getActiveSpan(context)?.context();
export function getSpanContext(context: Context): SpanContext | undefined {
return getSpan(context)?.context();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions api/src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Span, SpanOptions, Tracer, SpanContext } from '..';
import { Context } from '@opentelemetry/context-base';
import { NoopSpan, NOOP_SPAN } from './NoopSpan';
import { isSpanContextValid } from './spancontext-utils';
import { getParentSpanContext } from '../context/context';
import { getSpanContext } from '../context/context';

/**
* No-op implementations of {@link Tracer}.
Expand All @@ -35,7 +35,7 @@ export class NoopTracer implements Tracer {
return NOOP_SPAN;
}

const parentFromContext = context && getParentSpanContext(context);
const parentFromContext = context && getSpanContext(context);

if (
isSpanContext(parentFromContext) &&
Expand Down
4 changes: 2 additions & 2 deletions api/test/noop-implementations/noop-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SpanKind,
TraceFlags,
context,
setExtractedSpanContext,
setSpanContext,
} from '../../src';

describe('NoopTracer', () => {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('NoopTracer', () => {
const span = tracer.startSpan(
'test-1',
{},
setExtractedSpanContext(context.active(), parent)
setSpanContext(context.active(), parent)
);
assert(span.context().traceId === parent.traceId);
assert(span.context().spanId === parent.spanId);
Expand Down

0 comments on commit 0ae3e85

Please sign in to comment.