|
| 1 | +import type { Attributes, AttributeValue } from '@opentelemetry/api'; |
| 2 | +import { SDK_INFO } from '@opentelemetry/core'; |
| 3 | +import { |
| 4 | + ATTR_SERVICE_NAME, |
| 5 | + ATTR_SERVICE_VERSION, |
| 6 | + ATTR_TELEMETRY_SDK_LANGUAGE, |
| 7 | + ATTR_TELEMETRY_SDK_NAME, |
| 8 | + ATTR_TELEMETRY_SDK_VERSION, |
| 9 | + SEMRESATTRS_SERVICE_NAMESPACE, |
| 10 | +} from '@opentelemetry/semantic-conventions'; |
| 11 | +import { SDK_VERSION } from '@sentry/core'; |
| 12 | + |
| 13 | +type RawResourceAttribute = [string, AttributeValue | undefined]; |
| 14 | + |
| 15 | +/** |
| 16 | + * Minimal Resource implementation that satisfies the OpenTelemetry Resource interface |
| 17 | + * used by BasicTracerProvider, without depending on `@opentelemetry/resources`. |
| 18 | + */ |
| 19 | +class SentryResource { |
| 20 | + private _attributes: Attributes; |
| 21 | + |
| 22 | + public constructor(attributes: Attributes) { |
| 23 | + this._attributes = attributes; |
| 24 | + } |
| 25 | + |
| 26 | + public get attributes(): Attributes { |
| 27 | + return this._attributes; |
| 28 | + } |
| 29 | + |
| 30 | + public merge(other: SentryResource | null): SentryResource { |
| 31 | + if (!other) { |
| 32 | + return this; |
| 33 | + } |
| 34 | + return new SentryResource({ ...this._attributes, ...other.attributes }); |
| 35 | + } |
| 36 | + |
| 37 | + public getRawAttributes(): RawResourceAttribute[] { |
| 38 | + return Object.entries(this._attributes); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Returns a Resource for use in Sentry's OpenTelemetry TracerProvider setup. |
| 44 | + * |
| 45 | + * Combines the default OTel SDK telemetry attributes with Sentry-specific |
| 46 | + * service attributes, equivalent to what was previously done via: |
| 47 | + * `defaultResource().merge(resourceFromAttributes({ ... }))` |
| 48 | + */ |
| 49 | +export function getSentryResource(serviceName: string): SentryResource { |
| 50 | + return new SentryResource({ |
| 51 | + [ATTR_SERVICE_NAME]: serviceName, |
| 52 | + // eslint-disable-next-line deprecation/deprecation |
| 53 | + [SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry', |
| 54 | + [ATTR_SERVICE_VERSION]: SDK_VERSION, |
| 55 | + [ATTR_TELEMETRY_SDK_LANGUAGE]: SDK_INFO[ATTR_TELEMETRY_SDK_LANGUAGE], |
| 56 | + [ATTR_TELEMETRY_SDK_NAME]: SDK_INFO[ATTR_TELEMETRY_SDK_NAME], |
| 57 | + [ATTR_TELEMETRY_SDK_VERSION]: SDK_INFO[ATTR_TELEMETRY_SDK_VERSION], |
| 58 | + }); |
| 59 | +} |
0 commit comments