Skip to content

Commit

Permalink
feat(node-sdk): add resource attributes config option
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem Ullah <24660299+naseemkullah@users.noreply.github.com>
  • Loading branch information
naseemkullah committed Mar 27, 2022
1 parent 16eaeb6 commit 3f92f1f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
4 changes: 4 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ Configure a trace exporter. If an exporter OR span processor is not configured,

Configure tracing parameters. These are the same trace parameters used to [configure a tracer](../../../packages/opentelemetry-sdk-trace-base/src/types.ts#L71).

### resourceAttributes

Configure [resource attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md), key names are in CONSTANT_CASE, e.g. `SERVICE_NAME`.

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
32 changes: 28 additions & 4 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
InstrumentationOption,
registerInstrumentations,
} from '@opentelemetry/instrumentation';
import { NodeTracerConfig, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
NodeTracerConfig,
NodeTracerProvider,
} from '@opentelemetry/sdk-trace-node';
import { awsEc2Detector } from '@opentelemetry/resource-detector-aws';
import { gcpDetector } from '@opentelemetry/resource-detector-gcp';
import {
Expand All @@ -32,8 +35,12 @@ import {
Resource,
ResourceDetectionConfig,
} from '@opentelemetry/resources';
import { BatchSpanProcessor, SpanProcessor } from '@opentelemetry/sdk-trace-base';
import {
BatchSpanProcessor,
SpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeSDKConfiguration } from './types';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

/** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */
export class NodeSDK {
Expand All @@ -57,7 +64,22 @@ export class NodeSDK {
* Create a new NodeJS SDK instance
*/
public constructor(configuration: Partial<NodeSDKConfiguration> = {}) {
this._resource = configuration.resource ?? new Resource({});
const resource = configuration.resource ?? new Resource({});

const { resourceAttributes } = configuration;

this._resource = resourceAttributes === undefined
? resource
: resource.merge(new Resource(
Object.keys(resourceAttributes).reduce((attributes, key) => {
attributes[
SemanticResourceAttributes[
key as keyof typeof SemanticResourceAttributes
]
] = resourceAttributes[key];
return attributes;
}, {} as any) // TODO: fix type
));

this._autoDetectResources = configuration.autoDetectResources ?? true;

Expand Down Expand Up @@ -125,7 +147,9 @@ export class NodeSDK {
}

/** Detect resource attributes */
public async detectResources(config?: ResourceDetectionConfig): Promise<void> {
public async detectResources(
config?: ResourceDetectionConfig
): Promise<void> {
const internalConfig: ResourceDetectionConfig = {
detectors: [awsEc2Detector, gcpDetector, envDetector, processDetector],
...config,
Expand Down
9 changes: 5 additions & 4 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@
* limitations under the License.
*/

import { SpanAttributes, TextMapPropagator, Sampler } from '@opentelemetry/api';
import type { ContextManager } from '@opentelemetry/api';
import { Sampler, Attributes, TextMapPropagator } from '@opentelemetry/api';
import { InstrumentationOption } from '@opentelemetry/instrumentation';
import { Resource, ResourceAttributes } from '@opentelemetry/resources';
import { MetricExporter, Processor } from '@opentelemetry/sdk-metrics-base';
import { Resource } from '@opentelemetry/resources';
import {
SpanExporter,
SpanProcessor,
SpanLimits,
SpanProcessor,
} from '@opentelemetry/sdk-trace-base';

export interface NodeSDKConfiguration {
autoDetectResources: boolean;
contextManager: ContextManager;
defaultAttributes: SpanAttributes;
defaultAttributes: Attributes;
textMapPropagator: TextMapPropagator;
metricProcessor: Processor;
metricExporter: MetricExporter;
metricInterval: number;
instrumentations: InstrumentationOption[];
resource: Resource;
resourceAttributes?: ResourceAttributes;
sampler: Sampler;
spanProcessor: SpanProcessor;
traceExporter: SpanExporter;
Expand Down
21 changes: 21 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,25 @@ describe('Node SDK', () => {
});
});
});

describe('configureResourceAttributes', async () => {
it('configures resource attributes', async () => {
const sdk = new NodeSDK({
resourceAttributes: {
SERVICE_NAME: 'foo',
SERVICE_INSTANCE_ID: '627cc493',
},
});

await sdk.start();
const resource = sdk['_resource'];

assertServiceResource(resource, {
instanceId: '627cc493',
name: 'foo',
});
});

// TODO: more tests before merging
});
});

0 comments on commit 3f92f1f

Please sign in to comment.