Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when auto-detecting service name, use version as service.version #68

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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