Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tracing): Allow to set instrumenter on Span & Transaction #6136

Merged
merged 1 commit into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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