Skip to content

Commit 88a79d6

Browse files
authored
feat(node-sdk): allow setting additional resource attributes (#198)
Add an optional `initSDK` property that lets one set additional, user controlled resource attributes in the node SDK. This feature exists in the browser SDK already.
1 parent 940fdfe commit 88a79d6

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

.changeset/long-gifts-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hyperdx/node-opentelemetry': minor
3+
---
4+
5+
Allows setting user defined resource attributes when initializing the SDK.

packages/node-opentelemetry/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ import { initSDK } from '@hyperdx/node-opentelemetry';
106106
initSDK({
107107
consoleCapture: true, // optional, default: true
108108
additionalInstrumentations: [], // optional, default: []
109+
additionalResourceAttributes: { // optional, default: {}
110+
// Add custom resource attributes to all telemetry data
111+
'environment': 'production',
112+
'deployment.version': '1.0.0',
113+
'custom.attribute': 'value'
114+
},
109115
});
110116

111117
// Other instrumentation code...
@@ -164,6 +170,30 @@ app.use((req, res, next) => {
164170

165171
### (Optional) Advanced Instrumentation Configuration
166172

173+
#### Adding Custom Resource Attributes
174+
175+
Resource attributes are key-value pairs that describe the resource (service/application) producing telemetry data. These attributes are attached to all traces, metrics, and logs exported from your application. Common use cases include adding environment information, deployment versions, or custom metadata for filtering and grouping telemetry data.
176+
177+
When using manual instrumentation with `initSDK`, you can add custom resource attributes using the `additionalResourceAttributes` parameter:
178+
179+
```ts
180+
import { initSDK } from '@hyperdx/node-opentelemetry';
181+
182+
initSDK({
183+
additionalResourceAttributes: {
184+
'deployment.environment': process.env.NODE_ENV || 'development',
185+
'service.version': process.env.APP_VERSION || '0.0.0',
186+
'service.namespace': 'my-namespace',
187+
'cloud.region': process.env.AWS_REGION,
188+
// Add any custom attributes your organization needs
189+
'team.name': 'backend-team',
190+
'feature.flag': 'new-checkout-flow'
191+
},
192+
});
193+
```
194+
195+
These attributes will be included with all telemetry data sent to HyperDX, making it easier to filter and analyze your observability data. For standard attribute names, refer to the [OpenTelemetry Resource Semantic Conventions](https://opentelemetry.io/docs/specs/semconv/resource/).
196+
167197
#### Adding Additional 3rd-Party Instrumentation Packages
168198

169199
When manually instrumenting the SDK, use the `additionalInstrumentations` key to create an array of additional 3rd-party instrumentations. Check [here](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/metapackages/auto-instrumentations-node#supported-instrumentations) to see the current automatically instrumented packages.

packages/node-opentelemetry/src/__tests__/otel.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ describe('otel', () => {
1818
shutdown();
1919
});
2020

21+
it('should be able to initialize the SDK with initSDK using additional resource attributes', async () => {
22+
initSDK({
23+
apiKey: 'blabla',
24+
advancedNetworkCapture: true,
25+
consoleCapture: true,
26+
additionalResourceAttributes: {
27+
'cloud.region': 'us-east-2',
28+
'cloud.availability_zone': 'us-east-2a',
29+
},
30+
});
31+
32+
await new Promise((resolve) => setTimeout(resolve, 1000));
33+
34+
shutdown();
35+
});
36+
2137
it('should be able to initialize the SDK with init', async () => {
2238
init({
2339
apiKey: 'blabla',

packages/node-opentelemetry/src/otel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
InstrumentationModuleDefinition,
1313
} from '@opentelemetry/instrumentation';
1414
import { RuntimeNodeInstrumentation } from '@opentelemetry/instrumentation-runtime-node';
15-
import { Resource } from '@opentelemetry/resources';
15+
import { Resource, ResourceAttributes } from '@opentelemetry/resources';
1616
import { MetricReader } from '@opentelemetry/sdk-metrics';
1717
import { NodeSDK } from '@opentelemetry/sdk-node';
1818
import cliSpinners from 'cli-spinners';
@@ -58,6 +58,7 @@ const IS_LOCAL = env.NODE_ENV === 'development' || !env.NODE_ENV;
5858

5959
export type SDKConfig = {
6060
additionalInstrumentations?: InstrumentationBase[];
61+
additionalResourceAttributes?: ResourceAttributes;
6162
advancedNetworkCapture?: boolean;
6263
apiKey?: string;
6364
betaMode?: boolean;
@@ -331,6 +332,7 @@ export const initSDK = (config: SDKConfig) => {
331332

332333
sdk = new NodeSDK({
333334
resource: new Resource({
335+
...config.additionalResourceAttributes,
334336
// https://opentelemetry.io/docs/specs/semconv/resource/#telemetry-sdk-experimental
335337
'telemetry.distro.name': 'hyperdx',
336338
'telemetry.distro.version': PKG_VERSION,

0 commit comments

Comments
 (0)