Skip to content

Commit d726266

Browse files
feat: add otelResourceAttributes option for OpenTelemetry resource attributes (#190)
1 parent abcf28c commit d726266

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

.changeset/afraid-grapes-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hyperdx/browser': minor
3+
---
4+
5+
Added an optional otelResourceAttributes array to the BrowserSDKConfig type.

packages/browser/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ HyperDX.init({
1717
tracePropagationTargets: [/api.myapp.domain/i], // Set to link traces from frontend to backend requests
1818
consoleCapture: true, // Capture console logs (default false)
1919
advancedNetworkCapture: true, // Capture full HTTP request/response headers and bodies (default false)
20+
otelResourceAttributes: {
21+
'service.version': '1.0.0',
22+
'deployment.environment': 'production',
23+
},
2024
});
2125
```
2226

@@ -38,6 +42,14 @@ HyperDX.init({
3842
- `maskAllText` - (Optional) Whether to mask all text in session replay (default
3943
`false`).
4044
- `disableIntercom` - (Optional) Whether to disable Intercom integration (default `false`)
45+
- `otelResourceAttributes` - (Optional) Object containing OpenTelemetry resource attributes to be added to all spans. These are set at the resource level and merged with default SDK attributes. Example:
46+
```js
47+
otelResourceAttributes: {
48+
'service.version': '1.0.0',
49+
'deployment.environment': 'production',
50+
'custom.attribute': 'value',
51+
}
52+
```
4153
- `disableReplay` - (Optional) Whether to disable session replay (default `false`)
4254
- `recordCanvas` - (Optional) Whether to record canvas elements (default `false`)
4355
- `sampling` - (Optional) The sampling [config](https://github.com/rrweb-io/rrweb/blob/5fbb904edb653f3da17e6775ee438d81ef0bba83/docs/recipes/optimize-storage.md?plain=1#L22) in the session recording

packages/browser/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import SessionRecorder, {
44
RumRecorderConfig,
55
} from '@hyperdx/otel-web-session-recorder';
66
import opentelemetry, { Attributes } from '@opentelemetry/api';
7+
import { ResourceAttributes } from '@opentelemetry/resources';
78

89
import { resolveAsyncGlobal } from './utils';
910

@@ -32,6 +33,7 @@ type BrowserSDKConfig = {
3233
service: string;
3334
tracePropagationTargets?: (string | RegExp)[];
3435
url?: string;
36+
otelResourceAttributes?: ResourceAttributes;
3537
};
3638

3739
const URL_BASE = 'https://in-otel.hyperdx.io';
@@ -64,6 +66,7 @@ class Browser {
6466
service,
6567
tracePropagationTargets,
6668
url,
69+
otelResourceAttributes,
6770
}: BrowserSDKConfig) {
6871
if (!hasWindow()) {
6972
return;
@@ -92,6 +95,7 @@ class Browser {
9295
apiKey,
9396
applicationName: service,
9497
ignoreUrls,
98+
resourceAttributes: otelResourceAttributes,
9599
instrumentations: {
96100
visibility: true,
97101
console: captureConsole ?? consoleCapture ?? false,

packages/otel-web/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ export interface RumOtelWebConfig {
189189
* Config options passed to web tracer
190190
*/
191191
tracer?: WebTracerConfig;
192+
193+
/**
194+
* Additional resource attributes to be added to all spans.
195+
* These will be merged with default SDK resource attributes.
196+
*/
197+
resourceAttributes?: ResourceAttributes;
192198
}
193199

194200
interface RumOtelWebConfigInternal extends RumOtelWebConfig {
@@ -530,20 +536,23 @@ export const Rum: RumOtelWebType = {
530536
processedOptions.cookieDomain,
531537
).deinit;
532538

533-
const { ignoreUrls, applicationName, deploymentEnvironment, version } =
539+
const { ignoreUrls, applicationName, deploymentEnvironment, version, resourceAttributes } =
534540
processedOptions;
535541
// enabled: false prevents registerInstrumentations from enabling instrumentations in constructor
536542
// they will be enabled in registerInstrumentations
537543
const pluginDefaults = { ignoreUrls, enabled: false };
538544

539545
const resourceAttrs: ResourceAttributes = {
546+
// User-provided resource attributes
547+
...(resourceAttributes || {}),
540548
...SDK_INFO,
541549
[SemanticResourceAttributes.TELEMETRY_SDK_NAME]: '@hyperdx/otel-web',
542550
[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]: VERSION,
543551
[SemanticResourceAttributes.SERVICE_NAME]: applicationName,
544552
// Splunk specific attributes
545553
'rum.version': VERSION,
546554
'rum.scriptInstance': instanceId,
555+
547556
};
548557

549558
const syntheticsRunId = getSyntheticsRunId();

0 commit comments

Comments
 (0)