Skip to content

Commit

Permalink
feat(node-sdk): add serviceName config option
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah committed May 28, 2022
1 parent e44f6d1 commit 15cf5b6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 13 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ All notable changes to experimental packages in this project will be documented
* fix(sdk-metrics-base): misbehaving aggregation temporality selector tolerance #2958 @legendecas
* feat(trace-otlp-grpc): configure security with env vars #2827 @svetlanabrennan
* feat(sdk-metrics-base): async instruments callback timeout #2742 @legendecas
* feat(node-sdk): add serviceName config option #2867 @naseemkullah

### :bug: (Bug Fix)

Expand Down
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).

### serviceName

Configure the [service name](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md#service).

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
34 changes: 26 additions & 8 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@
* limitations under the License.
*/

import { TextMapPropagator } from '@opentelemetry/api';
import { ContextManager, TextMapPropagator } from '@opentelemetry/api';
import { metrics } from '@opentelemetry/api-metrics';
import { ContextManager } from '@opentelemetry/api';
import { MeterProvider, MetricReader } from '@opentelemetry/sdk-metrics-base';
import {
InstrumentationOption,
registerInstrumentations,
registerInstrumentations
} from '@opentelemetry/instrumentation';
import { NodeTracerConfig, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { awsEc2Detector } from '@opentelemetry/resource-detector-aws';
import { gcpDetector } from '@opentelemetry/resource-detector-gcp';
import {
detectResources,
envDetector,
processDetector,
Resource,
ResourceDetectionConfig,
ResourceDetectionConfig
} from '@opentelemetry/resources';
import { BatchSpanProcessor, SpanProcessor } from '@opentelemetry/sdk-trace-base';
import { MeterProvider, MetricReader } from '@opentelemetry/sdk-metrics-base';
import {
BatchSpanProcessor,
SpanProcessor
} from '@opentelemetry/sdk-trace-base';
import {
NodeTracerConfig,
NodeTracerProvider
} from '@opentelemetry/sdk-trace-node';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { NodeSDKConfiguration } from './types';

/** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */
Expand All @@ -52,13 +58,17 @@ export class NodeSDK {

private _tracerProvider?: NodeTracerProvider;
private _meterProvider?: MeterProvider;
private _serviceName?: string;

/**
* Create a new NodeJS SDK instance
*/
public constructor(configuration: Partial<NodeSDKConfiguration> = {}) {
this._resource = configuration.resource ?? new Resource({});


this._serviceName = configuration.serviceName;

this._autoDetectResources = configuration.autoDetectResources ?? true;

if (configuration.spanProcessor || configuration.traceExporter) {
Expand Down Expand Up @@ -115,7 +125,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 All @@ -137,6 +149,12 @@ export class NodeSDK {
await this.detectResources();
}

this._resource = this._serviceName === undefined
? this._resource
: this._resource.merge(new Resource(
{[SemanticResourceAttributes.SERVICE_NAME]: this._serviceName}
));

if (this._tracerProviderConfig) {
const tracerProvider = new NodeTracerProvider({
...this._tracerProviderConfig.tracerConfig,
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,26 +14,27 @@
* limitations under the License.
*/

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

export interface NodeSDKConfiguration {
autoDetectResources: boolean;
contextManager: ContextManager;
defaultAttributes: SpanAttributes;
defaultAttributes: Attributes;
textMapPropagator: TextMapPropagator;
metricReader: MetricReader;
instrumentations: InstrumentationOption[];
resource: Resource;
sampler: Sampler;
serviceName?: string;
spanProcessor: SpanProcessor;
traceExporter: SpanExporter;
spanLimits: SpanLimits;
Expand Down
72 changes: 72 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,76 @@ describe('Node SDK', () => {
});
});
});

describe('configureServiceName', async () => {
it('should configure service name via config', async () => {
const sdk = new NodeSDK({
serviceName: 'config-set-name',
});

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

assertServiceResource(resource, {
name: 'config-set-name',
});
});

it('should configure service name via OTEL_SERVICE_NAME env var', async () => {
process.env.OTEL_SERVICE_NAME='env-set-name';
const sdk = new NodeSDK();

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

assertServiceResource(resource, {
name: 'env-set-name',
});
delete process.env.OTEL_SERVICE_NAME;
});

it('should favor config set service name over OTEL_SERVICE_NAME env set service name', async () => {
process.env.OTEL_SERVICE_NAME='env-set-name';
const sdk = new NodeSDK({
serviceName: 'config-set-name',
});

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

assertServiceResource(resource, {
name: 'config-set-name',
});
delete process.env.OTEL_SERVICE_NAME;
});


it('should configure service name via OTEL_RESOURCE_ATTRIBUTES env var', async () => {
process.env.OTEL_RESOURCE_ATTRIBUTES = 'service.name=resource-env-set-name';
const sdk = new NodeSDK();

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

assertServiceResource(resource, {
name: 'resource-env-set-name',
});
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
});

it('should favor config set service name over OTEL_RESOURCE_ATTRIBUTES env set service name', async () => {
process.env.OTEL_RESOURCE_ATTRIBUTES = 'service.name=resource-env-set-name';
const sdk = new NodeSDK({
serviceName: 'config-set-name',
});

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

assertServiceResource(resource, {
name: 'config-set-name',
});
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const assertServiceResource = (
resource: Resource,
validations: {
name: string;
instanceId: string;
instanceId?: string;
namespace?: string;
version?: string;
}
Expand Down

0 comments on commit 15cf5b6

Please sign in to comment.