Skip to content

Commit

Permalink
feat(tracing): Allow to set instrumenter on Span & Transaction (#6136)
Browse files Browse the repository at this point in the history
This defaults to `sentry` if not set.
  • Loading branch information
mydea committed Nov 8, 2022
1 parent 454fd5d commit 351be06
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/tracing/src/span.ts
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
import { Instrumenter, Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
import { dropUndefinedKeys, logger, timestampWithMs, uuid4 } from '@sentry/utils';

/**
Expand Down Expand Up @@ -102,6 +102,11 @@ export class Span implements SpanInterface {
*/
public transaction?: Transaction;

/**
* The instrumenter that created this span.
*/
public instrumenter: Instrumenter = 'sentry';

/**
* You should never call the constructor manually, always use `Sentry.startTransaction()`
* or call `startChild()` on an existing span.
Expand Down Expand Up @@ -147,6 +152,9 @@ export class Span implements SpanInterface {
if (spanContext.endTimestamp) {
this.endTimestamp = spanContext.endTimestamp;
}
if (spanContext.instrumenter) {
this.instrumenter = spanContext.instrumenter;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/tracing/test/span.test.ts
Expand Up @@ -24,6 +24,18 @@ describe('Span', () => {
expect((span2 as any).traceId).toBe((span as any).traceId);
expect((span2 as any).sampled).toBe((span as any).sampled);
});

test('sets instrumenter to `sentry` if not specified in constructor', () => {
const span = new Span({});

expect(span.instrumenter).toBe('sentry');
});

test('allows to set instrumenter in constructor', () => {
const span = new Span({ instrumenter: 'otel' });

expect(span.instrumenter).toBe('otel');
});
});

describe('new Transaction', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/tracing/test/transaction.test.ts
Expand Up @@ -24,6 +24,18 @@ describe('`Transaction` class', () => {
expect(transaction.metadata.source).toEqual('custom');
});

it('sets instrumenter to be `sentry` in constructor if not provided', () => {
const transaction = new Transaction({ name: 'dogpark' });

expect(transaction.instrumenter).toEqual('sentry');
});

it('allows to set instrumenter', () => {
const transaction = new Transaction({ name: 'dogpark', instrumenter: 'otel' });

expect(transaction.instrumenter).toEqual('otel');
});

it('updates transaction name changes with correct variables needed', () => {
const transaction = new Transaction({ name: 'dogpark', metadata: { source: 'url' } });
expect(transaction.metadata.changes).toEqual([]);
Expand Down
11 changes: 11 additions & 0 deletions packages/types/src/span.ts
@@ -1,3 +1,4 @@
import { Instrumenter } from './instrumenter';
import { Primitive } from './misc';
import { Transaction } from './transaction';

Expand Down Expand Up @@ -58,6 +59,11 @@ export interface SpanContext {
* Timestamp in seconds (epoch time) indicating when the span ended.
*/
endTimestamp?: number;

/**
* The instrumenter that created this span.
*/
instrumenter?: Instrumenter;
}

/** Span holding trace_id, span_id */
Expand Down Expand Up @@ -92,6 +98,11 @@ export interface Span extends SpanContext {
*/
transaction?: Transaction;

/**
* The instrumenter that created this span.
*/
instrumenter: Instrumenter;

/**
* Sets the finish timestamp on the current span.
* @param endTimestamp Takes an endTimestamp if the end should not be the time when you call this function.
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/transaction.ts
@@ -1,4 +1,5 @@
import { DynamicSamplingContext } from './envelope';
import { Instrumenter } from './instrumenter';
import { MeasurementUnit } from './measurement';
import { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
import { PolymorphicRequest } from './polymorphics';
Expand Down Expand Up @@ -71,6 +72,11 @@ export interface Transaction extends TransactionContext, Span {
*/
metadata: TransactionMetadata;

/**
* The instrumenter that created this transaction.
*/
instrumenter: Instrumenter;

/**
* Set the name of the transaction
*/
Expand Down

0 comments on commit 351be06

Please sign in to comment.