Skip to content

Commit f5d2168

Browse files
authored
add transformSpan callback (#1331)
1 parent 2d86760 commit f5d2168

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/tracing/spanProcessor.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import logger from '../logger.js';
2+
13
export class SpanProcessor {
2-
constructor(exporter) {
4+
constructor(exporter, options = {}) {
35
this.exporter = exporter;
6+
this.options = options;
47
this.pendingSpans = new Map()
58
}
69

@@ -9,6 +12,13 @@ export class SpanProcessor {
912
}
1013

1114
onEnd(span) {
15+
try {
16+
if (this.options.transformSpan) {
17+
this.options.transformSpan({span: span.span});
18+
}
19+
} catch (e) {
20+
logger.error('Error running transformSpan callback', e);
21+
}
1222
this.exporter.export([span.export()])
1323
this.pendingSpans.delete(span.span.spanContext.spanId);
1424
}

src/tracing/tracing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class Tracing {
6060
createTracer() {
6161
this.contextManager = new ContextManager();
6262
this.exporter = new SpanExporter();
63-
this.spanProcessor = new SpanProcessor(this.exporter);
63+
this.spanProcessor = new SpanProcessor(this.exporter, this.options.tracing);
6464
this.tracer = new Tracer(this, this.spanProcessor);
6565
}
6666

test/tracing/spanProcessor.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,57 @@ describe('SpanProcessor()', function () {
6767

6868
done();
6969
});
70+
71+
it('should transform spans', function (done) {
72+
const tracingOptions = {
73+
transformSpan: ({span}) => {
74+
span.attributes['test-group'] = 'blue';
75+
span.resource.attributes['rollbar.environment'] = 'prod-3';
76+
},
77+
};
78+
const exporter = new SpanExporter();
79+
const spanProcessor = new SpanProcessor(exporter, tracingOptions);
80+
81+
expect(spanProcessor.pendingSpans.size).to.equal(0);
82+
83+
const overrides = {
84+
spanProcessor: spanProcessor,
85+
};
86+
const span = new Span(spanOptions(overrides));
87+
88+
expect(spanProcessor.pendingSpans.size).to.equal(1);
89+
90+
span.end();
91+
92+
expect(span.span.attributes['test-group']).to.equal('blue');
93+
expect(span.span.resource.attributes['rollbar.environment']).to.equal('prod-3');
94+
expect(spanProcessor.pendingSpans.size).to.equal(0);
95+
96+
done();
97+
});
98+
99+
it('should catch exception in transformSpan', function (done) {
100+
const tracingOptions = {
101+
transformSpan: ({span}) => {
102+
throw new Error('test error');
103+
},
104+
};
105+
const exporter = new SpanExporter();
106+
const spanProcessor = new SpanProcessor(exporter, tracingOptions);
107+
108+
expect(spanProcessor.pendingSpans.size).to.equal(0);
109+
110+
const overrides = {
111+
spanProcessor: spanProcessor,
112+
};
113+
const span = new Span(spanOptions(overrides));
114+
115+
expect(spanProcessor.pendingSpans.size).to.equal(1);
116+
117+
span.end();
118+
119+
expect(spanProcessor.pendingSpans.size).to.equal(0);
120+
121+
done();
122+
});
70123
});

0 commit comments

Comments
 (0)