Skip to content

Commit

Permalink
fix: prefer resource information loaded by env detector
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Jun 7, 2021
1 parent 2540402 commit 52edeaa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ export function _setDefaultOptions(options: Partial<Options> = {}): Options {
options.endpoint || env.OTEL_EXPORTER_JAEGER_ENDPOINT || defaultEndpoint;

const extraTracerConfig = options.tracerConfig || {};

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

Expand Down Expand Up @@ -169,3 +168,17 @@ 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,
})
);
}
12 changes: 12 additions & 0 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ describe('options', () => {
propagatorFactory: testPropagatorFactory,
});
});

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

assert.deepStrictEqual(options.tracerConfig, {
resource: new Resource({
[ResourceAttributes.SERVICE_NAME]: 'foobar',
}),
});
});
});

class TestInstrumentation extends InstrumentationBase {
Expand Down

0 comments on commit 52edeaa

Please sign in to comment.