Skip to content

Commit 735a3a6

Browse files
committed
feat(node-sdk): allow setting additional resource attributes
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 735a3a6

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ 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+
2133
it('should be able to initialize the SDK with init', async () => {
2234
init({
2335
apiKey: 'blabla',

packages/node-opentelemetry/src/otel.ts

Lines changed: 5 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';
@@ -56,8 +56,11 @@ const env = process.env;
5656

5757
const IS_LOCAL = env.NODE_ENV === 'development' || !env.NODE_ENV;
5858

59+
60+
5961
export type SDKConfig = {
6062
additionalInstrumentations?: InstrumentationBase[];
63+
additionalResourceAttributes?: ResourceAttributes;
6164
advancedNetworkCapture?: boolean;
6265
apiKey?: string;
6366
betaMode?: boolean;
@@ -331,6 +334,7 @@ export const initSDK = (config: SDKConfig) => {
331334

332335
sdk = new NodeSDK({
333336
resource: new Resource({
337+
...config.additionalResourceAttributes,
334338
// https://opentelemetry.io/docs/specs/semconv/resource/#telemetry-sdk-experimental
335339
'telemetry.distro.name': 'hyperdx',
336340
'telemetry.distro.version': PKG_VERSION,

0 commit comments

Comments
 (0)