Skip to content

Commit

Permalink
feat(ConsoleSpanExporter): export span links (#2917)
Browse files Browse the repository at this point in the history
* feat(ConsoleSpanExporter): export span links

* test: update test case for change to console.dir

* chore: changelog entry
  • Loading branch information
trentm authored Apr 26, 2022
1 parent a5135e2 commit 6710cdd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

### :rocket: (Enhancement)

* feat(ConsoleSpanExporter): export span links [#2917](https://github.com/open-telemetry/opentelemetry-js/pull/2917) @trentm

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class ConsoleSpanExporter implements SpanExporter {
attributes: span.attributes,
status: span.status,
events: span.events,
links: span.links
};
}

Expand All @@ -78,7 +79,7 @@ export class ConsoleSpanExporter implements SpanExporter {
done?: (result: ExportResult) => void
): void {
for (const span of spans) {
console.log(this._exportInfo(span));
console.dir(this._exportInfo(span), { depth: 3 });
}
if (done) {
return done({ code: ExportResultCode.SUCCESS });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

import {
SpanContext,
TraceFlags,
} from '@opentelemetry/api';
import { AlwaysOnSampler } from '@opentelemetry/core';
import * as assert from 'assert';
import * as sinon from 'sinon';
Expand All @@ -26,16 +30,16 @@ import {
/* eslint-disable no-console */
describe('ConsoleSpanExporter', () => {
let consoleExporter: ConsoleSpanExporter;
let previousConsoleLog: any;
let previousConsoleDir: any;

beforeEach(() => {
previousConsoleLog = console.log;
console.log = () => {};
previousConsoleDir = console.dir;
console.dir = () => {};
consoleExporter = new ConsoleSpanExporter();
});

afterEach(() => {
console.log = previousConsoleLog;
console.dir = previousConsoleDir;
});

describe('.export()', () => {
Expand All @@ -46,14 +50,22 @@ describe('ConsoleSpanExporter', () => {
});
consoleExporter = new ConsoleSpanExporter();

const spyConsole = sinon.spy(console, 'log');
const spyConsole = sinon.spy(console, 'dir');
const spyExport = sinon.spy(consoleExporter, 'export');

basicTracerProvider.addSpanProcessor(
new SimpleSpanProcessor(consoleExporter)
);

const span = basicTracerProvider.getTracer('default').startSpan('foo');
const tracer = basicTracerProvider.getTracer('default');
const context: SpanContext = {
traceId: 'a3cda95b652f4a1592b449d5929fda1b',
spanId: '5e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};
const span = tracer.startSpan('foo', {
links: [ { context, attributes: { anAttr: 'aValue' } } ]
});
span.addEvent('foobar');
span.end();

Expand All @@ -70,6 +82,7 @@ describe('ConsoleSpanExporter', () => {
'events',
'id',
'kind',
'links',
'name',
'parentId',
'status',
Expand All @@ -80,7 +93,7 @@ describe('ConsoleSpanExporter', () => {
assert.ok(firstSpan.name === 'foo');
assert.ok(firstEvent.name === 'foobar');
assert.ok(consoleSpan.id === firstSpan.spanContext().spanId);
assert.ok(keys === expectedKeys);
assert.ok(keys === expectedKeys, 'expectedKeys');

assert.ok(spyExport.calledOnce);
});
Expand Down

0 comments on commit 6710cdd

Please sign in to comment.