-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathresource.ts
More file actions
99 lines (90 loc) · 3.23 KB
/
resource.ts
File metadata and controls
99 lines (90 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { Attributes, AttributeValue } from '@opentelemetry/api';
import { SDK_INFO } from '@opentelemetry/core';
import {
ATTR_SERVICE_NAME,
ATTR_SERVICE_VERSION,
ATTR_TELEMETRY_SDK_LANGUAGE,
ATTR_TELEMETRY_SDK_NAME,
ATTR_TELEMETRY_SDK_VERSION,
SEMRESATTRS_SERVICE_NAMESPACE,
} from '@opentelemetry/semantic-conventions';
import { SDK_VERSION } from '@sentry/core';
type RawResourceAttribute = [string, AttributeValue | undefined];
/**
* Minimal Resource implementation that satisfies the OpenTelemetry Resource interface
* used by BasicTracerProvider, without depending on `@opentelemetry/resources`.
*/
class SentryResource {
private _attributes: Attributes;
public constructor(attributes: Attributes) {
this._attributes = attributes;
}
public get attributes(): Attributes {
return this._attributes;
}
public merge(other: SentryResource | null): SentryResource {
if (!other) {
return this;
}
return new SentryResource({ ...this._attributes, ...other.attributes });
}
public getRawAttributes(): RawResourceAttribute[] {
return Object.entries(this._attributes);
}
}
/**
* Parses `OTEL_RESOURCE_ATTRIBUTES` env var (comma-separated `key=value` pairs).
* Values are URL-decoded per the OTel spec.
*/
function parseOtelResourceAttributes(raw: string | undefined): Attributes {
if (!raw) {
return {};
}
const result: Attributes = {};
for (const pair of raw.split(',')) {
const eq = pair.indexOf('=');
if (eq === -1) {
continue;
}
const key = pair.substring(0, eq).trim();
const value = pair.substring(eq + 1).trim();
if (key) {
try {
result[key] = decodeURIComponent(value);
} catch {
result[key] = value;
}
}
}
return result;
}
/**
* Returns a Resource for use in Sentry's OpenTelemetry TracerProvider setup.
*
* Combines the default OTel SDK telemetry attributes with Sentry-specific
* service attributes, equivalent to what was previously done via:
* `defaultResource().merge(resourceFromAttributes({ ... }))`
*
* Respects OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES environment variables
* per the OpenTelemetry specification.
*/
export function getSentryResource(serviceNameFallback: string): SentryResource {
const env = typeof process !== 'undefined' ? process.env : {};
const otelServiceName = env.OTEL_SERVICE_NAME;
const otelResourceAttrs = parseOtelResourceAttributes(env.OTEL_RESOURCE_ATTRIBUTES);
return new SentryResource({
// Lowest priority: Sentry defaults
// eslint-disable-next-line deprecation/deprecation
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
[ATTR_SERVICE_NAME]: serviceNameFallback,
// OTEL_RESOURCE_ATTRIBUTES overrides defaults (including service.name and service.namespace)
...otelResourceAttrs,
// OTEL_SERVICE_NAME explicitly overrides service.name
...(otelServiceName ? { [ATTR_SERVICE_NAME]: otelServiceName } : {}),
// Highest priority: Sentry SDK telemetry attrs (cannot be overridden by env vars)
[ATTR_SERVICE_VERSION]: SDK_VERSION,
[ATTR_TELEMETRY_SDK_LANGUAGE]: SDK_INFO[ATTR_TELEMETRY_SDK_LANGUAGE],
[ATTR_TELEMETRY_SDK_NAME]: SDK_INFO[ATTR_TELEMETRY_SDK_NAME],
[ATTR_TELEMETRY_SDK_VERSION]: SDK_INFO[ATTR_TELEMETRY_SDK_VERSION],
});
}