-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: derive a fallback for service.name if not set
by finding the application's main package.json file and using its name and version attribute.
- Loading branch information
Showing
13 changed files
with
809 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/detectors/node/opentelemetry-resource-detector-service-name-fallback/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { DetectorSync, Resource } from '@opentelemetry/resources'; | ||
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; | ||
|
||
import { readPackageJson } from './packageJsonUtil'; | ||
|
||
export default class ServiceNameFallbackDetector implements DetectorSync { | ||
detect(): Resource { | ||
return new Resource({}, this.detectServiceNameFallback()); | ||
} | ||
|
||
private async detectServiceNameFallback(): Promise<any> { | ||
const automaticServiceName = process.env.DASH0_AUTOMATIC_SERVICE_NAME; | ||
if (automaticServiceName && automaticServiceName.trim().toLowerCase() === 'false') { | ||
return {}; | ||
} | ||
const otelServiceName = process.env.OTEL_SERVICE_NAME; | ||
if (otelServiceName && otelServiceName.trim() !== '') { | ||
return {}; | ||
} | ||
const otelResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES; | ||
if (otelResourceAttributes) { | ||
const rawAttributes: string[] = otelResourceAttributes.split(','); | ||
for (const rawAttribute of rawAttributes) { | ||
const keyValuePair: string[] = rawAttribute.split('='); | ||
if (keyValuePair.length !== 2) { | ||
continue; | ||
} | ||
const [key, value] = keyValuePair; | ||
if (key.trim() === SEMRESATTRS_SERVICE_NAME) { | ||
if (value != null && value.trim().split(/^"|"$/).join('').trim().length > 0) { | ||
return {}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
const packageJson = await readPackageJson(); | ||
if (!packageJson) { | ||
return {}; | ||
} | ||
return { [SEMRESATTRS_SERVICE_NAME]: `${packageJson.name}@${packageJson.version}` }; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
src/detectors/node/opentelemetry-resource-detector-service-name-fallback/index_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Resource } from '@opentelemetry/resources'; | ||
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; | ||
import { expect } from 'chai'; | ||
import Sinon from 'sinon'; | ||
import sinon from 'sinon'; | ||
|
||
import ServiceNameFallbackDetector from './index'; | ||
import * as packageJsonUtil from './packageJsonUtil'; | ||
|
||
const packageJson = { | ||
name: '@example/app-under-test', | ||
version: '2.13.47', | ||
description: 'a Node.js application', | ||
main: 'src/index.js', | ||
}; | ||
|
||
const envVarNames = [ | ||
// | ||
'DASH0_AUTOMATIC_SERVICE_NAME', | ||
'OTEL_SERVICE_NAME', | ||
'OTEL_RESOURCE_ATTRIBUTES', | ||
]; | ||
|
||
interface Dict<T> { | ||
[key: string]: T | undefined; | ||
} | ||
|
||
describe('service name fallback', () => { | ||
const sandbox = sinon.createSandbox(); | ||
let readPackageJsonStub: Sinon.SinonStub; | ||
let serviceNameFallback: ServiceNameFallbackDetector; | ||
|
||
const originalEnvVarValues: Dict<string> = {}; | ||
|
||
before(() => { | ||
envVarNames.forEach(envVarName => { | ||
originalEnvVarValues[envVarName] = process.env[envVarName]; | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
readPackageJsonStub = sandbox.stub(packageJsonUtil, 'readPackageJson'); | ||
serviceNameFallback = new ServiceNameFallbackDetector(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
envVarNames.forEach(envVarName => { | ||
if (originalEnvVarValues[envVarName] === undefined) { | ||
delete process.env[envVarName]; | ||
} else { | ||
process.env[envVarName] = originalEnvVarValues[envVarName]; | ||
} | ||
}); | ||
}); | ||
|
||
it('sets a service name 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]'); | ||
}); | ||
|
||
it('does not set a service name if DASH0_AUTOMATIC_SERVICE_NAME is false', async () => { | ||
givenAValidPackageJsonFile(); | ||
process.env.DASH0_AUTOMATIC_SERVICE_NAME = 'false'; | ||
const result = serviceNameFallback.detect(); | ||
const attributes = await waitForAsyncDetection(result); | ||
expect(attributes).to.be.empty; | ||
}); | ||
|
||
it('does not set a service name if OTEL_SERVICE_NAME is set', async () => { | ||
givenAValidPackageJsonFile(); | ||
process.env.OTEL_SERVICE_NAME = 'already-set'; | ||
const result = serviceNameFallback.detect(); | ||
const attributes = await waitForAsyncDetection(result); | ||
expect(attributes).to.be.empty; | ||
}); | ||
|
||
it('sets a service name if OTEL_SERVICE_NAME is set to an empty string', async () => { | ||
givenAValidPackageJsonFile(); | ||
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]'); | ||
}); | ||
|
||
it('does not set a service name if OTEL_RESOURCE_ATTRIBUTES has the service.name key', async () => { | ||
givenAValidPackageJsonFile(); | ||
process.env.OTEL_RESOURCE_ATTRIBUTES = 'key1=value,service.name=already-set,key2=valu,key2=valuee'; | ||
const result = serviceNameFallback.detect(); | ||
const attributes = await waitForAsyncDetection(result); | ||
expect(attributes).to.be.empty; | ||
}); | ||
|
||
it('sets a service name if OTEL_RESOURCE_ATTRIBUTES is set but does not have the service.name key', async () => { | ||
givenAValidPackageJsonFile(); | ||
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]'); | ||
}); | ||
|
||
it('does not set a service name if no package.json can be found', async () => { | ||
givenThereIsNoPackageJsonFile(); | ||
const result = serviceNameFallback.detect(); | ||
const attributes = await waitForAsyncDetection(result); | ||
expect(attributes).to.be.empty; | ||
}); | ||
|
||
function givenThereIsNoPackageJsonFile() { | ||
readPackageJsonStub.returns(null); | ||
} | ||
|
||
function givenAValidPackageJsonFile() { | ||
readPackageJsonStub.returns(null).returns(packageJson); | ||
} | ||
|
||
async function waitForAsyncDetection(result: Resource) { | ||
expect(result).to.exist; | ||
expect(result.asyncAttributesPending).to.be.true; | ||
// @ts-expect-error required | ||
await result.waitForAsyncAttributes(); | ||
return result.attributes; | ||
} | ||
}); |
Oops, something went wrong.