|
| 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 | + * NOTICE from the Sentry authors: |
| 17 | + * - Minimal vendored implementation of the tracing-suppression helpers from |
| 18 | + * `@opentelemetry/core` to avoid pulling in that dependency. |
| 19 | + * - The context key string must stay byte-identical to OTel's, since other |
| 20 | + * OpenTelemetry instrumentation (e.g. `@opentelemetry/instrumentation-http`) |
| 21 | + * writes to the same key and our tracer reads it back to suppress spans. |
| 22 | + * - `unsuppressTracing` is dropped, as the SDK never removes the flag. |
| 23 | + */ |
| 24 | +import type { Context } from '@opentelemetry/api'; |
| 25 | +import { createContextKey } from '@opentelemetry/api'; |
| 26 | + |
| 27 | +const SUPPRESS_TRACING_KEY = createContextKey('OpenTelemetry SDK Context Key SUPPRESS_TRACING'); |
| 28 | + |
| 29 | +/** Returns a new context with tracing suppressed, so no spans are created within it. */ |
| 30 | +export function suppressTracing(context: Context): Context { |
| 31 | + return context.setValue(SUPPRESS_TRACING_KEY, true); |
| 32 | +} |
| 33 | + |
| 34 | +/** Whether tracing is suppressed in the given context. */ |
| 35 | +export function isTracingSuppressed(context: Context): boolean { |
| 36 | + return context.getValue(SUPPRESS_TRACING_KEY) === true; |
| 37 | +} |
0 commit comments