Skip to content

Commit

Permalink
remove references to env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
maryliag committed Apr 10, 2024
1 parent 19c7189 commit 605cd62
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 59 deletions.
6 changes: 1 addition & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
### :rocket: (Enhancement)

* feat(sdk-trace-base): log resource attributes in ConsoleSpanExporter [#4605](https://github.com/open-telemetry/opentelemetry-js/pull/4605) @pichlermarc
* feat(resources): set value for `service.instance.id` as random UUID by default if environment variable `OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID` is set to true.
* The value can be overwritten by
* merging a resource containing the `service.instance.id` attribute
* setting `service.instance.id` via the `OTEL_RESOURCE_ATTRIBUTES` environment variable when using `envDetector`
* using another resource detector which writes `service.instance.id`
* feat(resources): new detector ServiceInstanceIDDetector that sets the value for `service.instance.id` as random UUID.

### :bug: (Bug Fix)

Expand Down
12 changes: 5 additions & 7 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
processDetector,
Resource,
ResourceDetectionConfig,
serviceInstanceIDDetector,
} from '@opentelemetry/resources';
import { LogRecordProcessor, LoggerProvider } from '@opentelemetry/sdk-logs';
import { MeterProvider, MetricReader, View } from '@opentelemetry/sdk-metrics';
Expand Down Expand Up @@ -122,13 +121,12 @@ export class NodeSDK {
this._configuration = configuration;

this._resource = configuration.resource ?? new Resource({});
const defaultDetectors = [envDetector, processDetector, hostDetector];

if (env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID) {
defaultDetectors.push(serviceInstanceIDDetector);
}
this._resourceDetectors =
configuration.resourceDetectors ?? defaultDetectors;
this._resourceDetectors = configuration.resourceDetectors ?? [
envDetector,
processDetector,
hostDetector,
];

this._serviceName = configuration.serviceName;

Expand Down
53 changes: 11 additions & 42 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
processDetector,
hostDetector,
Resource,
serviceInstanceIDDetector,
} from '@opentelemetry/resources';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { logs } from '@opentelemetry/api-logs';
Expand Down Expand Up @@ -696,10 +697,16 @@ describe('Node SDK', () => {
sdk.shutdown();
});

it('should configure service instance id with random UUID with OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID env var', async () => {
process.env.OTEL_RESOURCE_ATTRIBUTES = 'service.name=my-service';
process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID = 'true';
const sdk = new NodeSDK();
it('should configure service instance id with random UUID', async () => {
const sdk = new NodeSDK({
autoDetectResources: true,
resourceDetectors: [
processDetector,
envDetector,
hostDetector,
serviceInstanceIDDetector,
],
});

sdk.start();
const resource = sdk['_resource'];
Expand All @@ -713,44 +720,6 @@ describe('Node SDK', () => {
),
true
);

delete process.env.OTEL_RESOURCE_ATTRIBUTES;
delete process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID;
await sdk.shutdown();
});

it('should configure service instance id via OTEL_RESOURCE_ATTRIBUTES env var even with OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID env var', async () => {
process.env.OTEL_RESOURCE_ATTRIBUTES =
'service.instance.id=627cc493,service.name=my-service';
process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID = 'true';
const sdk = new NodeSDK();

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

assertServiceResource(resource, {
name: 'my-service',
instanceId: '627cc493',
});
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
delete process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID;
sdk.shutdown();
});

it('should not configure service instance id with no value for it on OTEL_RESOURCE_ATTRIBUTES env var and OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID env var as false', async () => {
process.env.OTEL_RESOURCE_ATTRIBUTES = 'service.name=my-service';
process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID = 'false';
const sdk = new NodeSDK();

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

assertServiceResource(resource, {
name: 'my-service',
});
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
await sdk.shutdown();
});
});
Expand Down
6 changes: 1 addition & 5 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ const DEFAULT_LIST_SEPARATOR = ',';
* Environment interface to define all names
*/

const ENVIRONMENT_BOOLEAN_KEYS = [
'OTEL_SDK_DISABLED',
'OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID',
] as const;
const ENVIRONMENT_BOOLEAN_KEYS = ['OTEL_SDK_DISABLED'] as const;

type ENVIRONMENT_BOOLEANS = {
[K in (typeof ENVIRONMENT_BOOLEAN_KEYS)[number]]?: boolean;
Expand Down Expand Up @@ -239,7 +236,6 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL: 'http/protobuf',
OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: 'http/protobuf',
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE: 'cumulative',
OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID: false,
};

/**
Expand Down

0 comments on commit 605cd62

Please sign in to comment.