From db529b7eeeef506ac6c4f0209e0ddf8f59b2743f Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 7 Jun 2021 15:49:30 +0300 Subject: [PATCH] fix: service name precedence --- src/options.ts | 32 +++++++++++++------------------- test/options.test.ts | 2 +- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/options.ts b/src/options.ts index 90113b08..d658baa1 100644 --- a/src/options.ts +++ b/src/options.ts @@ -82,17 +82,25 @@ export function _setDefaultOptions(options: Partial = {}): 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, }; @@ -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, - }) - ); -} diff --git a/test/options.test.ts b/test/options.test.ts index 42b8a324..b2758b84 100644 --- a/test/options.test.ts +++ b/test/options.test.ts @@ -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;