Skip to content

Commit

Permalink
fix: when auto-detecting service name, use version as service.version
Browse files Browse the repository at this point in the history
(instead of concatenating it into service.name)
  • Loading branch information
basti1302 committed Sep 17, 2024
1 parent 11457e8 commit be71b6c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { DetectorSync, Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';

import { readPackageJson } from './packageJsonUtil';

Expand All @@ -15,7 +15,7 @@ export default class ServiceNameFallbackDetector implements DetectorSync {
if (
hasOptedOutOfServiceNameFallbackDetection() ||
hasOTelServiceNameSet() ||
hasServiceNameSetViaResourceAttributesThing()
hasServiceNameSetViaOTelResourceAttributesEnvVar()
) {
return {};
}
Expand All @@ -24,7 +24,10 @@ export default class ServiceNameFallbackDetector implements DetectorSync {
if (!packageJson) {
return {};
}
return { [SEMRESATTRS_SERVICE_NAME]: `${packageJson.name}@${packageJson.version}` };
return {
[SEMRESATTRS_SERVICE_NAME]: packageJson.name,
[SEMRESATTRS_SERVICE_VERSION]: packageJson.version,
};
}
}

Expand All @@ -38,7 +41,7 @@ function hasOTelServiceNameSet() {
return otelServiceName && otelServiceName.trim() !== '';
}

function hasServiceNameSetViaResourceAttributesThing() {
function hasServiceNameSetViaOTelResourceAttributesEnvVar() {
const otelResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES;
if (otelResourceAttributes) {
const rawAttributes: string[] = otelResourceAttributes.split(',');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
import { expect } from 'chai';
import Sinon from 'sinon';
import sinon from 'sinon';
Expand Down Expand Up @@ -57,11 +57,12 @@ describe('service name fallback', () => {
});
});

it('sets a service name based on package.json attributes', async () => {
it('sets a service name and version based on package.json attributes', async () => {
givenAValidPackageJsonFile();
const result = serviceNameFallback.detect();
const attributes = await waitForAsyncDetection(result);
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_NAME, '@example/[email protected]');
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_NAME, '@example/app-under-test');
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_VERSION, '2.13.47');
});

it('does not set a service name if DASH0_AUTOMATIC_SERVICE_NAME is false', async () => {
Expand All @@ -85,7 +86,8 @@ describe('service name fallback', () => {
process.env.OTEL_SERVICE_NAME = ' ';
const result = serviceNameFallback.detect();
const attributes = await waitForAsyncDetection(result);
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_NAME, '@example/[email protected]');
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_NAME, '@example/app-under-test');
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_VERSION, '2.13.47');
});

it('does not set a service name if OTEL_RESOURCE_ATTRIBUTES has the service.name key', async () => {
Expand All @@ -101,7 +103,8 @@ describe('service name fallback', () => {
process.env.OTEL_RESOURCE_ATTRIBUTES = 'key1=value,key2=value';
const result = serviceNameFallback.detect();
const attributes = await waitForAsyncDetection(result);
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_NAME, '@example/[email protected]');
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_NAME, '@example/app-under-test');
expect(attributes).to.have.property(SEMRESATTRS_SERVICE_VERSION, '2.13.47');
});

it('does not set a service name if no package.json can be found', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ describe('attach', () => {
expectMatchingSpan(
traces,
[
resource =>
expectResourceAttribute(resource, 'service.name', 'dash0-app-under-test-express-typescript@1.0.0'),
resource => expectResourceAttribute(resource, 'service.name', 'dash0-app-under-test-express-typescript'),
resource => expectResourceAttribute(resource, 'service.version', '1.0.0'),
],
[
span => expect(span.kind).to.equal(SpanKind.SERVER, 'span kind should be server'),
Expand Down

0 comments on commit be71b6c

Please sign in to comment.