Skip to content

Commit

Permalink
use resource detector for service instance id
Browse files Browse the repository at this point in the history
use a resource detector for service instance id
  • Loading branch information
maryliag committed Apr 10, 2024
1 parent 971a85d commit 19c7189
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 27 deletions.
13 changes: 8 additions & 5 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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 @@ -121,11 +122,13 @@ export class NodeSDK {
this._configuration = configuration;

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

if (env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID) {
defaultDetectors.push(serviceInstanceIDDetector);
}
this._resourceDetectors =
configuration.resourceDetectors ?? defaultDetectors;

this._serviceName = configuration.serviceName;

Expand Down
11 changes: 8 additions & 3 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,15 @@ describe('Node SDK', () => {
const resource = sdk['_resource'];
await resource.waitForAsyncAttributes?.();

const UUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
assert.equal(
resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID]?.toString().length,
36
UUID_REGEX.test(
resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID]?.toString() || ''
),
true
);

delete process.env.OTEL_RESOURCE_ATTRIBUTES;
delete process.env.OTEL_NODE_EXPERIMENTAL_DEFAULT_SERVICE_INSTANCE_ID;
await sdk.shutdown();
Expand Down Expand Up @@ -746,7 +751,7 @@ describe('Node SDK', () => {
name: 'my-service',
});
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
sdk.shutdown();
await sdk.shutdown();
});
});

Expand Down
8 changes: 2 additions & 6 deletions packages/opentelemetry-resources/src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@opentelemetry/semantic-conventions';
import { SDK_INFO } from '@opentelemetry/core';
import { ResourceAttributes } from './types';
import { defaultServiceName, addExperimentalDefault } from './platform';
import { defaultServiceName } from './platform';
import { IResource } from './IResource';

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ export class Resource implements IResource {
* Returns a Resource that identifies the SDK in use.
*/
static default(): IResource {
const defaultResource = new Resource({
return new Resource({
[SEMRESATTRS_SERVICE_NAME]: defaultServiceName(),
[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE]:
SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE],
Expand All @@ -64,10 +64,6 @@ export class Resource implements IResource {
[SEMRESATTRS_TELEMETRY_SDK_VERSION]:
SDK_INFO[SEMRESATTRS_TELEMETRY_SDK_VERSION],
});

addExperimentalDefault(defaultResource);

return defaultResource;
}

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* limitations under the License.
*/

import { IResource } from '../../IResource';
import { noopDetector } from '../../detectors/NoopDetector';

export function addExperimentalDefault(_: IResource): void {}
export const serviceInstanceIDDetector = noopDetector;
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
* 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';
import { noopDetectorSync } from '../../detectors/NoopDetectorSync';

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

export * from './default-service-name';
export * from './default-experimental-values';
export * from './HostDetector';
export * from './OSDetector';
export * from './HostDetectorSync';
export * from './OSDetectorSync';
export * from './ProcessDetector';
export * from './ProcessDetectorSync';
export * from './ServiceInstanceIDDetector';
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { Detector } from '../../types';
import { ResourceDetectionConfig } from '../../config';
import { IResource } from '../../IResource';
import { serviceInstanceIDDetectorSync } from '../browser/ServiceInstanceIDDetectorSync';

/**
* ServiceInstanceIDDetector detects the resources related to the service instance ID.
*/
class ServiceInstanceIDDetector implements Detector {
detect(_config?: ResourceDetectionConfig): Promise<IResource> {
return Promise.resolve(serviceInstanceIDDetectorSync.detect());
}
}

export const serviceInstanceIDDetector = new ServiceInstanceIDDetector();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { SEMRESATTRS_SERVICE_INSTANCE_ID } from '@opentelemetry/semantic-conventions';
import { Resource } from '../../Resource';
import { DetectorSync, ResourceAttributes } from '../../types';
import { ResourceDetectionConfig } from '../../config';
import { randomUUID } from 'crypto';

/**
* ServiceInstanceIDDetectorSync detects the resources related to the service instance ID.
*/
class ServiceInstanceIDDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): Resource {
const attributes: ResourceAttributes = {
[SEMRESATTRS_SERVICE_INSTANCE_ID]: randomUUID(),
};

return new Resource(attributes);
}
}

export const serviceInstanceIDDetectorSync =
new ServiceInstanceIDDetectorSync();
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

export * from './default-service-name';
export * from './default-experimental-values';
export * from './HostDetector';
export * from './OSDetector';
export * from './HostDetectorSync';
export * from './OSDetectorSync';
export * from './ProcessDetector';
export * from './ProcessDetectorSync';
export * from './ServiceInstanceIDDetector';

0 comments on commit 19c7189

Please sign in to comment.