Skip to content

Commit 109656a

Browse files
authored
add rollbar.occurrence.uuids attribute via a transform fn (#1375)
1 parent 1f78c3c commit 109656a

File tree

8 files changed

+47
-8
lines changed

8 files changed

+47
-8
lines changed

src/browser/replay/replay.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as _ from '../../utility.js';
2+
import hrtime from '../../tracing/hrtime.js';
23
import id from '../../tracing/id.js';
34
import logger from '../../logger.js';
45
import Recorder from './recorder.js';
@@ -62,6 +63,7 @@ export default class Replay {
6263
shouldSend: this._shouldSendScheduled.bind(this),
6364
onComplete: this._onScheduledComplete.bind(this),
6465
});
66+
this._tracing.addSpanTransform(this.uuidsTransform.bind(this));
6567
}
6668

6769
/**
@@ -357,6 +359,15 @@ export default class Replay {
357359
return true;
358360
}
359361

362+
uuidsTransform(span) {
363+
const startTime = hrtime.toMillis(span.startTime);
364+
const uuidEvents = this._telemeter.queue.filter((e) => {
365+
return e.timestamp_ms >= startTime && e.uuid;
366+
});
367+
const uuids = uuidEvents.map((e) => e.uuid);
368+
span.attributes['rollbar.occurrence.uuids'] = JSON.stringify(uuids);
369+
}
370+
360371
/**
361372
* Gets spans for the given replay ID
362373
*

src/tracing/spanProcessor.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@ export class SpanProcessor {
55
this.exporter = exporter;
66
this.options = options;
77
this.pendingSpans = new Map();
8+
this.transforms = [this.userTransform.bind(this)];
9+
}
10+
11+
addTransform(transformFn) {
12+
this.transforms.unshift(transformFn);
13+
}
14+
15+
userTransform(span) {
16+
if (this.options.transformSpan) {
17+
this.options.transformSpan({ span: span });
18+
}
19+
}
20+
21+
applyTransforms(span) {
22+
for (const transform of this.transforms) {
23+
try {
24+
transform(span);
25+
} catch (e) {
26+
logger.error('Error running span transform callback', e);
27+
}
28+
}
829
}
930

1031
onStart(span, _parentContext) {
1132
this.pendingSpans.set(span.span.spanContext.spanId, span);
1233
}
1334

1435
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-
}
36+
this.applyTransforms(span.span);
2237
this.exporter.export([span.export()]);
2338
this.pendingSpans.delete(span.span.spanContext.spanId);
2439
}

src/tracing/tracing.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export default class Tracing {
6969
return this.tracer;
7070
}
7171

72+
addSpanTransform(transformFn) {
73+
this.spanProcessor.addTransform(transformFn);
74+
}
75+
7276
getSpan(context = this.contextManager.active()) {
7377
return context.getValue(SPAN_KEY);
7478
}

test/replay/integration/e2e.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe('Session Replay E2E', function () {
178178
expect(span_r).to.have.property('events');
179179
expect(span_r.events).to.be.an('array');
180180
expect(span_r).to.have.property('attributes').that.is.an('array');
181-
expect(span_r.attributes).to.have.lengthOf(15);
181+
expect(span_r.attributes).to.have.lengthOf(16);
182182

183183
expect(span_r.attributes).to.deep.include({
184184
key: 'rollbar.replay.id',

test/replay/integration/replay.bufferIndex.checkoutResilience.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Replay - Buffer Index Checkout Resilience', function () {
3131
post: sinon.stub(),
3232
},
3333
session: { attributes: {} },
34+
addSpanTransform() {},
3435
};
3536

3637
telemeter = { exportTelemetrySpan: sinon.stub() };

test/replay/integration/replay.bufferIndex.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('Replay buffer-index integration', function () {
4848
session: {
4949
attributes: {},
5050
},
51+
addSpanTransform() {},
5152
};
5253

5354
api = {

test/replay/unit/replay.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class MockTracing {
2929
post: sinon.stub().resolves({ success: true }),
3030
};
3131
}
32+
33+
addSpanTransform() {}
3234
}
3335

3436
class MockTelemeter {

test/tracing/spanProcessor.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ describe('SpanProcessor()', function () {
7575
span.resource.attributes['rollbar.environment'] = 'prod-3';
7676
},
7777
};
78+
const otherTransform = (span) => {
79+
span.attributes['test-id'] = '1234';
80+
};
7881
const exporter = new SpanExporter();
7982
const spanProcessor = new SpanProcessor(exporter, tracingOptions);
83+
spanProcessor.addTransform(otherTransform);
8084

8185
expect(spanProcessor.pendingSpans.size).to.equal(0);
8286

@@ -93,6 +97,7 @@ describe('SpanProcessor()', function () {
9397
expect(span.span.resource.attributes['rollbar.environment']).to.equal(
9498
'prod-3',
9599
);
100+
expect(span.span.attributes['test-id']).to.equal('1234');
96101
expect(spanProcessor.pendingSpans.size).to.equal(0);
97102

98103
done();

0 commit comments

Comments
 (0)