Skip to content

Commit be71b6c

Browse files
committed
fix: when auto-detecting service name, use version as service.version
(instead of concatenating it into service.name)
1 parent 11457e8 commit be71b6c

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/detectors/node/opentelemetry-resource-detector-service-name-fallback/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

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

77
import { readPackageJson } from './packageJsonUtil';
88

@@ -15,7 +15,7 @@ export default class ServiceNameFallbackDetector implements DetectorSync {
1515
if (
1616
hasOptedOutOfServiceNameFallbackDetection() ||
1717
hasOTelServiceNameSet() ||
18-
hasServiceNameSetViaResourceAttributesThing()
18+
hasServiceNameSetViaOTelResourceAttributesEnvVar()
1919
) {
2020
return {};
2121
}
@@ -24,7 +24,10 @@ export default class ServiceNameFallbackDetector implements DetectorSync {
2424
if (!packageJson) {
2525
return {};
2626
}
27-
return { [SEMRESATTRS_SERVICE_NAME]: `${packageJson.name}@${packageJson.version}` };
27+
return {
28+
[SEMRESATTRS_SERVICE_NAME]: packageJson.name,
29+
[SEMRESATTRS_SERVICE_VERSION]: packageJson.version,
30+
};
2831
}
2932
}
3033

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

41-
function hasServiceNameSetViaResourceAttributesThing() {
44+
function hasServiceNameSetViaOTelResourceAttributesEnvVar() {
4245
const otelResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES;
4346
if (otelResourceAttributes) {
4447
const rawAttributes: string[] = otelResourceAttributes.split(',');

src/detectors/node/opentelemetry-resource-detector-service-name-fallback/index_test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { Resource } from '@opentelemetry/resources';
5-
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
5+
import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
66
import { expect } from 'chai';
77
import Sinon from 'sinon';
88
import sinon from 'sinon';
@@ -57,11 +57,12 @@ describe('service name fallback', () => {
5757
});
5858
});
5959

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

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

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

107110
it('does not set a service name if no package.json can be found', async () => {

test/integration/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ describe('attach', () => {
177177
expectMatchingSpan(
178178
traces,
179179
[
180-
resource =>
181-
expectResourceAttribute(resource, 'service.name', 'dash0-app-under-test-express-typescript@1.0.0'),
180+
resource => expectResourceAttribute(resource, 'service.name', 'dash0-app-under-test-express-typescript'),
181+
resource => expectResourceAttribute(resource, 'service.version', '1.0.0'),
182182
],
183183
[
184184
span => expect(span.kind).to.equal(SpanKind.SERVER, 'span kind should be server'),

0 commit comments

Comments
 (0)