Skip to content

Commit b766a6e

Browse files
authored
Add tracing to Rollbar.configure and allow span options to update resource (#1327)
* add tracing to Rollbar.configure * span options can update resource
1 parent 439abb1 commit b766a6e

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/browser/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ class Rollbar {
128128
payload,
129129
);
130130

131+
this.tracing?.configure(this.options);
131132
this.recorder?.configure(this.options);
132133
this.client.configure(this.options, payloadData);
133-
this.instrumenter && this.instrumenter.configure(this.options);
134+
this.instrumenter?.configure(this.options);
134135
this.setupUnhandledCapture();
135136
return this;
136137
};

src/tracing/tracer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ export class Tracer {
2929

3030
const kind = 0;
3131
const spanContext = { traceId, spanId, traceFlags, traceState };
32+
const resource = {
33+
attributes: {
34+
...(this.tracing.resource?.attributes || {}),
35+
...(options.resource?.attributes || {}),
36+
},
37+
};
3238

3339
const span = new Span({
34-
resource: this.tracing.resource,
40+
resource: resource,
3541
scope: this.tracing.scope,
3642
session: this.tracing.session.session,
3743
context,

src/tracing/tracing.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export default class Tracing {
1818
this.createTracer();
1919
}
2020

21+
configure(options) {
22+
// Options merge happens before configure is called, so we can just replace.
23+
this.options = options;
24+
}
25+
2126
initSession() {
2227
if (this.session) {
2328
this.session.init();

test/tracing/tracing.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ describe('Tracing()', function () {
5151
done();
5252
});
5353

54+
it('should configure', function () {
55+
const options = tracingOptions();
56+
const t = new Tracing(window, options);
57+
t.initSession();
58+
59+
expect(t.options.resource).to.deep.equal(options.resource);
60+
expect(t.options.version).to.equal(options.version);
61+
62+
t.configure({
63+
resource: {
64+
'service.name': 'changed_service',
65+
},
66+
version: '0.2.0',
67+
});
68+
69+
expect(t.options.resource).to.deep.equal({
70+
'service.name': 'changed_service',
71+
});
72+
expect(t.options.version).to.equal('0.2.0');
73+
});
74+
5475
it('should create and export spans', function (done) {
5576
const options = tracingOptions();
5677
const t = new Tracing(window, options);
@@ -97,6 +118,29 @@ describe('Tracing()', function () {
97118
done();
98119
});
99120

121+
it('should create spans with span option overrides', function () {
122+
const options = tracingOptions();
123+
const t = new Tracing(window, options);
124+
t.initSession();
125+
126+
expect(t.sessionId).to.match(/^[a-f0-9]{32}$/);
127+
128+
const span = t.startSpan(
129+
'test.span',
130+
{ resource: { attributes: { 'rollbar.environment': 'new-env' }}},
131+
);
132+
133+
expect(span.span.name).to.equal('test.span');
134+
expect(span.span.spanContext.traceId).to.match(/^[a-f0-9]{32}$/);
135+
expect(span.span.spanContext.spanId).to.match(/^[a-f0-9]{16}$/);
136+
expect(span.span.resource.attributes['service.name']).to.equal(
137+
'unknown_service',
138+
);
139+
expect(span.span.resource.attributes['rollbar.environment']).to.equal(
140+
'new-env',
141+
);
142+
});
143+
100144
it('should get and set session attributes', function () {
101145
const options = tracingOptions();
102146
const t = new Tracing(window, options);

0 commit comments

Comments
 (0)