Skip to content

Commit

Permalink
maint: remove resource merging in honeycomb-resource
Browse files Browse the repository at this point in the history
With `merge-resources`, we don't need to worry about consolidating
additional resources w/in individual resource builders.

Removes the `options` argument from the configuration builder

Adds `getResourceAttributes` public method to `WebSDK`: I added this to
for testing, but think i could be useful more generally to have a way of
seeing the current resource attributes.

When testing, realized if `resources` or `traceExporter` were set in the
config options, they would overwrite our custom config. Swapped  so the
`resource` & `traceExporter`s bootstrapped in the `HoneycombWebSDK`
constructor take precedence
  • Loading branch information
ahrbnsn committed Jan 23, 2024
1 parent e715584 commit 4c35d48
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/base-otel-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ export class WebSDK {
tracerProvider.addSpanProcessor(new SessionIdSpanProcessor());
}

public getResourceAttributes() {
return this._resource.attributes;
}

public shutdown(): Promise<void> {
const promises: Promise<unknown>[] = [];
if (this._tracerProvider) {
Expand Down
3 changes: 1 addition & 2 deletions src/honeycomb-otel-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import { mergeResources } from './merge-resources';
export class HoneycombWebSDK extends WebSDK {
constructor(options?: HoneycombOptions) {
super({
...options,
resource: mergeResources([
configureHoneycombResource(),
options?.resource,
]),
traceExporter: configureHoneycombHttpJsonTraceExporter(options),

...options,
});
}
}
7 changes: 2 additions & 5 deletions src/honeycomb-resource.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Resource } from '@opentelemetry/resources';
import { HoneycombOptions } from './types';
import { VERSION } from './version';

export function configureHoneycombResource(
options?: HoneycombOptions,
): Resource {
export function configureHoneycombResource(): Resource {
return new Resource({
'honeycomb.distro.version': VERSION,
'honeycomb.distro.runtime_version': 'browser',
}).merge(options?.resource ? options.resource : null);
});
}
17 changes: 17 additions & 0 deletions test/honeycomb-otel-sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { HoneycombWebSDK } from '../src/honeycomb-otel-sdk';
import { Resource } from '@opentelemetry/resources';
import { WebSDK } from '../src/base-otel-sdk';
import { VERSION } from '../src/version';

test('it should extend the OTel WebSDK', () => {
const honeycomb = new HoneycombWebSDK();
expect(honeycomb).toBeInstanceOf(WebSDK);
});

test('it should merge resources from the configuration', () => {
const config = {
resource: new Resource({
myTestAttr: 'my-test-attr',
}),
};

const honeycomb = new HoneycombWebSDK(config);

const attributes = honeycomb.getResourceAttributes();
expect(attributes['honeycomb.distro.version']).toEqual(VERSION);
expect(attributes['honeycomb.distro.runtime_version']).toEqual('browser');
expect(attributes.myTestAttr).toEqual('my-test-attr');
});
14 changes: 0 additions & 14 deletions test/honeycomb-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,3 @@ test('it should return a Resource', () => {
'browser',
);
});

test('it should merge resource attributes provided from another resource', () => {
const resource = configureHoneycombResource({
resource: new Resource({
myTestAttr: 'my-test-attr',
}),
});
expect(resource).toBeInstanceOf(Resource);
expect(resource.attributes['honeycomb.distro.version']).toEqual(VERSION);
expect(resource.attributes['honeycomb.distro.runtime_version']).toEqual(
'browser',
);
expect(resource.attributes.myTestAttr).toEqual('my-test-attr');
});

0 comments on commit 4c35d48

Please sign in to comment.