Skip to content

Commit

Permalink
fix: set attributes from SpanOptions (open-telemetry#235)
Browse files Browse the repository at this point in the history
* fix: set attributes from SpanOptions

* fix: add more tests
  • Loading branch information
mayurkale22 authored Sep 5, 2019
1 parent af5d88a commit 634f577
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ export class BasicTracer implements types.Tracer {
options.startTime
);
// Set default attributes
span.setAttributes(this._defaultAttributes);
span.setAttributes(
Object.assign({}, this._defaultAttributes, options.attributes)
);
return span;
}

Expand Down
33 changes: 33 additions & 0 deletions packages/opentelemetry-basic-tracer/test/BasicTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ describe('BasicTracer', () => {
span.end();
});

it('should start a span with defaultAttributes and spanoptions->attributes', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
defaultAttributes: { foo: 'bar' },
});
const span = tracer.startSpan('my-span', {
attributes: { foo: 'foo', bar: 'bar' },
}) as Span;
assert.deepStrictEqual(span.attributes, { bar: 'bar', foo: 'foo' });
span.end();
});

it('should start a span with defaultAttributes and undefined spanoptions->attributes', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
defaultAttributes: { foo: 'bar' },
});
const span = tracer.startSpan('my-span', {}) as Span;
assert.deepStrictEqual(span.attributes, { foo: 'bar' });
span.end();
});

it('should start a span with spanoptions->attributes', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span', {
attributes: { foo: 'foo', bar: 'bar' },
}) as Span;
assert.deepStrictEqual(span.attributes, { foo: 'foo', bar: 'bar' });
span.end();
});

it('should start a span with name and parent spancontext', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-basic-tracer/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import {
} from '@opentelemetry/types';
import { BasicTracer } from '../src';
import { NoopScopeManager } from '@opentelemetry/scope-base';
import { NoopLogger } from '@opentelemetry/core';

describe('Span', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
logger: new NoopLogger(),
});
const name = 'span1';
const spanContext: SpanContext = {
Expand Down

0 comments on commit 634f577

Please sign in to comment.