Skip to content

Commit

Permalink
feat: simplify active span logic (open-telemetry#1589)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartlomiej Obecny <bobecny@gmail.com>
  • Loading branch information
2 people authored and dyladan committed Feb 18, 2021
1 parent 0cc4ae3 commit 722bd6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
30 changes: 8 additions & 22 deletions api/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Span, SpanContext } from '../';
import { NoopSpan, Span, SpanContext } from '../';
import { Context, createContextKey } from '@opentelemetry/context-base';

/**
Expand All @@ -23,9 +23,7 @@ import { Context, createContextKey } from '@opentelemetry/context-base';
const ACTIVE_SPAN_KEY = createContextKey(
'OpenTelemetry Context Key ACTIVE_SPAN'
);
const EXTRACTED_SPAN_CONTEXT_KEY = createContextKey(
'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT'
);

/**
* Shared key for indicating if instrumentation should be suppressed beyond
* this current scope.
Expand Down Expand Up @@ -54,29 +52,17 @@ export function setActiveSpan(context: Context, span: Span): Context {
}

/**
* Get the extracted span context from a context
*
* @param context context to get span context from
*/
export function getExtractedSpanContext(
context: Context
): SpanContext | undefined {
return (
(context.getValue(EXTRACTED_SPAN_CONTEXT_KEY) as SpanContext) || undefined
);
}

/**
* Set the extracted span context on a context
* Wrap extracted span context in a NoopSpan and set as active span in a new
* context
*
* @param context context to set span context on
* @param spanContext span context to set
* @param context context to set active span on
* @param spanContext span context to be wrapped
*/
export function setExtractedSpanContext(
context: Context,
spanContext: SpanContext
): Context {
return context.setValue(EXTRACTED_SPAN_CONTEXT_KEY, spanContext);
return setActiveSpan(context, new NoopSpan(spanContext));
}

/**
Expand All @@ -89,7 +75,7 @@ export function setExtractedSpanContext(
export function getParentSpanContext(
context: Context
): SpanContext | undefined {
return getActiveSpan(context)?.context() || getExtractedSpanContext(context);
return getActiveSpan(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 { getExtractedSpanContext } from '../context/context';
import { getActiveSpan } from '../context/context';

/**
* No-op implementations of {@link Tracer}.
Expand All @@ -31,7 +31,7 @@ export class NoopTracer implements Tracer {
// startSpan starts a noop span.
startSpan(name: string, options?: SpanOptions, context?: Context): Span {
const parent = options?.parent;
const parentFromContext = context && getExtractedSpanContext(context);
const parentFromContext = context && getActiveSpan(context)?.context();
if (isSpanContext(parent) && isSpanContextValid(parent)) {
return new NoopSpan(parent);
} else if (
Expand Down

0 comments on commit 722bd6c

Please sign in to comment.