Skip to content

Commit 351be06

Browse files
authored
feat(tracing): Allow to set instrumenter on Span & Transaction (#6136)
This defaults to `sentry` if not set.
1 parent 454fd5d commit 351be06

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

packages/tracing/src/span.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-lines */
2-
import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
2+
import { Instrumenter, Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
33
import { dropUndefinedKeys, logger, timestampWithMs, uuid4 } from '@sentry/utils';
44

55
/**
@@ -102,6 +102,11 @@ export class Span implements SpanInterface {
102102
*/
103103
public transaction?: Transaction;
104104

105+
/**
106+
* The instrumenter that created this span.
107+
*/
108+
public instrumenter: Instrumenter = 'sentry';
109+
105110
/**
106111
* You should never call the constructor manually, always use `Sentry.startTransaction()`
107112
* or call `startChild()` on an existing span.
@@ -147,6 +152,9 @@ export class Span implements SpanInterface {
147152
if (spanContext.endTimestamp) {
148153
this.endTimestamp = spanContext.endTimestamp;
149154
}
155+
if (spanContext.instrumenter) {
156+
this.instrumenter = spanContext.instrumenter;
157+
}
150158
}
151159

152160
/**

packages/tracing/test/span.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ describe('Span', () => {
2424
expect((span2 as any).traceId).toBe((span as any).traceId);
2525
expect((span2 as any).sampled).toBe((span as any).sampled);
2626
});
27+
28+
test('sets instrumenter to `sentry` if not specified in constructor', () => {
29+
const span = new Span({});
30+
31+
expect(span.instrumenter).toBe('sentry');
32+
});
33+
34+
test('allows to set instrumenter in constructor', () => {
35+
const span = new Span({ instrumenter: 'otel' });
36+
37+
expect(span.instrumenter).toBe('otel');
38+
});
2739
});
2840

2941
describe('new Transaction', () => {

packages/tracing/test/transaction.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ describe('`Transaction` class', () => {
2424
expect(transaction.metadata.source).toEqual('custom');
2525
});
2626

27+
it('sets instrumenter to be `sentry` in constructor if not provided', () => {
28+
const transaction = new Transaction({ name: 'dogpark' });
29+
30+
expect(transaction.instrumenter).toEqual('sentry');
31+
});
32+
33+
it('allows to set instrumenter', () => {
34+
const transaction = new Transaction({ name: 'dogpark', instrumenter: 'otel' });
35+
36+
expect(transaction.instrumenter).toEqual('otel');
37+
});
38+
2739
it('updates transaction name changes with correct variables needed', () => {
2840
const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'url' } });
2941
expect(transaction.metadata.changes).toEqual([]);

packages/types/src/span.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Instrumenter } from './instrumenter';
12
import { Primitive } from './misc';
23
import { Transaction } from './transaction';
34

@@ -58,6 +59,11 @@ export interface SpanContext {
5859
* Timestamp in seconds (epoch time) indicating when the span ended.
5960
*/
6061
endTimestamp?: number;
62+
63+
/**
64+
* The instrumenter that created this span.
65+
*/
66+
instrumenter?: Instrumenter;
6167
}
6268

6369
/** Span holding trace_id, span_id */
@@ -92,6 +98,11 @@ export interface Span extends SpanContext {
9298
*/
9399
transaction?: Transaction;
94100

101+
/**
102+
* The instrumenter that created this span.
103+
*/
104+
instrumenter: Instrumenter;
105+
95106
/**
96107
* Sets the finish timestamp on the current span.
97108
* @param endTimestamp Takes an endTimestamp if the end should not be the time when you call this function.

packages/types/src/transaction.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DynamicSamplingContext } from './envelope';
2+
import { Instrumenter } from './instrumenter';
23
import { MeasurementUnit } from './measurement';
34
import { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
45
import { PolymorphicRequest } from './polymorphics';
@@ -71,6 +72,11 @@ export interface Transaction extends TransactionContext, Span {
7172
*/
7273
metadata: TransactionMetadata;
7374

75+
/**
76+
* The instrumenter that created this transaction.
77+
*/
78+
instrumenter: Instrumenter;
79+
7480
/**
7581
* Set the name of the transaction
7682
*/

0 commit comments

Comments
 (0)