|
| 1 | +/*! |
| 2 | + * Copyright 2020, OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Span, SpanContext } from '@opentelemetry/api'; |
| 18 | +import { Context } from '@opentelemetry/scope-base'; |
| 19 | + |
| 20 | +const ACTIVE_SPAN_KEY = Context.createKey( |
| 21 | + 'OpenTelemetry Context Key ACTIVE_SPAN' |
| 22 | +); |
| 23 | +const EXTRACTED_SPAN_CONTEXT_KEY = Context.createKey( |
| 24 | + 'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT' |
| 25 | +); |
| 26 | + |
| 27 | +/** |
| 28 | + * Return the active span if one exists |
| 29 | + * |
| 30 | + * @param context context to get span from |
| 31 | + */ |
| 32 | +export function getActiveSpan(context: Context): Span | undefined { |
| 33 | + return (context.getValue(ACTIVE_SPAN_KEY) as Span) || undefined; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Set the active span on a context |
| 38 | + * |
| 39 | + * @param context context to use as parent |
| 40 | + * @param span span to set active |
| 41 | + */ |
| 42 | +export function setActiveSpan(context: Context, span: Span): Context { |
| 43 | + return context.setValue(ACTIVE_SPAN_KEY, span); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Get the extracted span context from a context |
| 48 | + * |
| 49 | + * @param context context to get span context from |
| 50 | + */ |
| 51 | +export function getExtractedSpanContext( |
| 52 | + context: Context |
| 53 | +): SpanContext | undefined { |
| 54 | + return ( |
| 55 | + (context.getValue(EXTRACTED_SPAN_CONTEXT_KEY) as SpanContext) || undefined |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Set the extracted span context on a context |
| 61 | + * |
| 62 | + * @param context context to set span context on |
| 63 | + * @param spanContext span context to set |
| 64 | + */ |
| 65 | +export function setExtractedSpanContext( |
| 66 | + context: Context, |
| 67 | + spanContext: SpanContext |
| 68 | +): Context { |
| 69 | + return context.setValue(EXTRACTED_SPAN_CONTEXT_KEY, spanContext); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Get the span context of the parent span if it exists, |
| 74 | + * or the extracted span context if there is no active |
| 75 | + * span. |
| 76 | + * |
| 77 | + * @param context context to get values from |
| 78 | + */ |
| 79 | +export function getParentSpanContext( |
| 80 | + context: Context |
| 81 | +): SpanContext | undefined { |
| 82 | + return getActiveSpan(context)?.context() || getExtractedSpanContext(context); |
| 83 | +} |
0 commit comments