Skip to content

Commit

Permalink
create separate add experimental for browser and node
Browse files Browse the repository at this point in the history
create a function to add experimental default values (currently only service instance id, but others can be added in the future),
with the value being set on the node case, but not on the browser case.

Signed-off-by: maryliag <[email protected]>
  • Loading branch information
maryliag committed Apr 10, 2024
1 parent 1bafff9 commit 6eb66ea
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID` is set to true.
* 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`
Expand Down
19 changes: 8 additions & 11 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,9 @@ describe('Node SDK', () => {
sdk.shutdown();
});

it('should configure service instance id with random UUID with OTEL_NODE_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID env var', async () => {
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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID =
'true';
process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID = 'true';
const sdk = new NodeSDK();

sdk.start();
Expand All @@ -711,15 +710,14 @@ describe('Node SDK', () => {
36
);
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
delete process.env.OTEL_NODE_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID;
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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID env var', async () => {
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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID =
'true';
process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID = 'true';
const sdk = new NodeSDK();

sdk.start();
Expand All @@ -731,14 +729,13 @@ describe('Node SDK', () => {
instanceId: '627cc493',
});
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
delete process.env.OTEL_NODE_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID;
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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID env var as false', async () => {
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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID =
'false';
process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID = 'false';
const sdk = new NodeSDK();

sdk.start();
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const DEFAULT_LIST_SEPARATOR = ',';

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

type ENVIRONMENT_BOOLEANS = {
Expand Down Expand Up @@ -239,7 +239,7 @@ 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_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID: false,
OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID: false,
};

/**
Expand Down
10 changes: 2 additions & 8 deletions packages/opentelemetry-resources/src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@

import { diag } from '@opentelemetry/api';
import {
SEMRESATTRS_SERVICE_INSTANCE_ID,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
} from '@opentelemetry/semantic-conventions';
import { SDK_INFO } from '@opentelemetry/core';
import { ResourceAttributes } from './types';
import { defaultServiceName } from './platform';
import { defaultServiceName, addExperimentalDefault } from './platform';
import { IResource } from './IResource';
import { getEnv } from '@opentelemetry/core';
import { randomUUID } from 'crypto';

/**
* A Resource describes the entity for which a signals (metrics or trace) are
Expand Down Expand Up @@ -68,10 +65,7 @@ export class Resource implements IResource {
SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION],
});

if (getEnv().OTEL_NODE_JS_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID) {
defaultResource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID] =
randomUUID();
}
addExperimentalDefault(defaultResource);

return defaultResource;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IResource } from '../../IResource';

export function addExperimentalDefault(_: IResource): void {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

export * from './default-service-name';
export * from './default-experimental-values';
export * from './HostDetector';
export * from './OSDetector';
export * from './HostDetectorSync';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IResource } from '../../IResource';
import { getEnv } from '@opentelemetry/core';
import { randomUUID } from 'crypto';
import { SEMRESATTRS_SERVICE_INSTANCE_ID } from '@opentelemetry/semantic-conventions';

export function addExperimentalDefault(resource: IResource): void {
if (getEnv().OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID) {
resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID] = randomUUID();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

export * from './default-service-name';
export * from './default-experimental-values';
export * from './HostDetector';
export * from './OSDetector';
export * from './HostDetectorSync';
Expand Down

0 comments on commit 6eb66ea

Please sign in to comment.