Skip to content

Commit

Permalink
fix: service name precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Jun 7, 2021
1 parent 52edeaa commit db529b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
32 changes: 13 additions & 19 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,25 @@ export function _setDefaultOptions(options: Partial<Options> = {}): Options {
options.logInjectionEnabled = getEnvBoolean('SPLUNK_LOGS_INJECTION', false);
}

options.serviceName =
options.serviceName || env.SPLUNK_SERVICE_NAME || defaultServiceName;
options.endpoint =
options.endpoint || env.OTEL_EXPORTER_JAEGER_ENDPOINT || defaultEndpoint;

const extraTracerConfig = options.tracerConfig || {};

const resource = new EnvResourceDetector().detect();

options.serviceName =
options.serviceName ||
env.SPLUNK_SERVICE_NAME ||
resource.attributes[ResourceAttributes.SERVICE_NAME]?.toString() ||
defaultServiceName;

const tracerConfig = {
resource: configureResource({
serviceName: options.serviceName,
}),
resource: resource.merge(
new Resource({
[ResourceAttributes.SERVICE_NAME]: options.serviceName,
})
),
...extraTracerConfig,
};

Expand Down Expand Up @@ -168,17 +176,3 @@ function getEnvBoolean(key: string, defaultValue = true) {

return true;
}

function configureResource(options: { serviceName: string }): Resource {
const resource = new EnvResourceDetector().detect();

if (resource.attributes[ResourceAttributes.SERVICE_NAME] !== undefined) {
return resource;
}

return resource.merge(
new Resource({
[ResourceAttributes.SERVICE_NAME]: options.serviceName,
})
);
}
2 changes: 1 addition & 1 deletion test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('options', () => {
});
});

it('prefers resource information loaded from the environment', () => {
it('prefers service name from env resource info over the default service name', () => {
process.env.OTEL_RESOURCE_ATTRIBUTES = 'service.name=foobar';
const options = _setDefaultOptions();
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
Expand Down

0 comments on commit db529b7

Please sign in to comment.