|
| 1 | +/* |
| 2 | + * Copyright The 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 { createContextKey } from '../context/context'; |
| 18 | +import { Context } from '../context/types'; |
| 19 | +import { Span } from './span'; |
| 20 | +import { SpanContext } from './span_context'; |
| 21 | +import { NonRecordingSpan } from './NonRecordingSpan'; |
| 22 | + |
| 23 | +/** |
| 24 | + * span key |
| 25 | + */ |
| 26 | +const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN'); |
| 27 | + |
| 28 | +/** |
| 29 | + * Shared key for indicating if instrumentation should be suppressed beyond |
| 30 | + * this current scope. |
| 31 | + */ |
| 32 | +const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( |
| 33 | + 'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION' |
| 34 | +); |
| 35 | + |
| 36 | +/** |
| 37 | + * Return the span if one exists |
| 38 | + * |
| 39 | + * @param context context to get span from |
| 40 | + */ |
| 41 | +export function getSpan(context: Context): Span | undefined { |
| 42 | + return (context.getValue(SPAN_KEY) as Span) || undefined; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Set the span on a context |
| 47 | + * |
| 48 | + * @param context context to use as parent |
| 49 | + * @param span span to set active |
| 50 | + */ |
| 51 | +export function setSpan(context: Context, span: Span): Context { |
| 52 | + return context.setValue(SPAN_KEY, span); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Wrap span context in a NoopSpan and set as span in a new |
| 57 | + * context |
| 58 | + * |
| 59 | + * @param context context to set active span on |
| 60 | + * @param spanContext span context to be wrapped |
| 61 | + */ |
| 62 | +export function setSpanContext( |
| 63 | + context: Context, |
| 64 | + spanContext: SpanContext |
| 65 | +): Context { |
| 66 | + return setSpan(context, new NonRecordingSpan(spanContext)); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Get the span context of the span if it exists. |
| 71 | + * |
| 72 | + * @param context context to get values from |
| 73 | + */ |
| 74 | +export function getSpanContext(context: Context): SpanContext | undefined { |
| 75 | + return getSpan(context)?.spanContext(); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Sets value on context to indicate that instrumentation should |
| 80 | + * be suppressed beyond this current scope. |
| 81 | + * |
| 82 | + * @param context context to set the suppress instrumentation value on. |
| 83 | + */ |
| 84 | +export function suppressInstrumentation(context: Context): Context { |
| 85 | + return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Sets value on context to indicate that instrumentation should |
| 90 | + * no-longer be suppressed beyond this current scope. |
| 91 | + * |
| 92 | + * @param context context to set the suppress instrumentation value on. |
| 93 | + */ |
| 94 | +export function unsuppressInstrumentation(context: Context): Context { |
| 95 | + return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, false); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Return current suppress instrumentation value for the given context, |
| 100 | + * if it exists. |
| 101 | + * |
| 102 | + * @param context context check for the suppress instrumentation value. |
| 103 | + */ |
| 104 | +export function isInstrumentationSuppressed(context: Context): boolean { |
| 105 | + return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY)); |
| 106 | +} |
0 commit comments