Skip to content

Commit 5f7736d

Browse files
committed
feat: SpanContext to Span
1 parent df8e5b8 commit 5f7736d

File tree

9 files changed

+38
-38
lines changed

9 files changed

+38
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- [opentracing] ref: Removed opentracing package
66
- [integrations] feat: Add tracing integration
7-
- [hub] feat: Add tracing related function to scope and hub (`Scope.startSpan`, `Scope.setSpanContext`, `Hub.traceHeaders`)
7+
- [hub] feat: Add tracing related function to scope and hub (`Scope.startSpan`, `Scope.setSpan`, `Hub.traceHeaders`)
88
- [hub] feat: Add new function to Scope `setContext`
99
- [hub] feat: Add new function to Scope `setTransaction`
1010

packages/hub/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { Carrier, Layer } from './interfaces';
22
export { addGlobalEventProcessor, Scope } from './scope';
33
export { getCurrentHub, getHubFromCarrier, getMainCarrier, Hub, makeMain, setHubOnCarrier } from './hub';
4-
export { SpanContext, TRACEPARENT_REGEX } from './spancontext';
4+
export { Span, TRACEPARENT_REGEXP } from './span';

packages/hub/src/scope.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Breadcrumb, Event, EventHint, EventProcessor, Scope as ScopeInterface, Severity, User } from '@sentry/types';
22
import { getGlobalObject, isThenable, normalize, SyncPromise } from '@sentry/utils';
33

4-
import { SpanContext } from './spancontext';
4+
import { Span } from './span';
55

66
/**
77
* Holds additional event information. {@link Scope.applyToEvent} will be
@@ -41,8 +41,8 @@ export class Scope implements ScopeInterface {
4141
/** Transaction */
4242
protected _transaction?: string;
4343

44-
/** SpanContext */
45-
protected _span?: SpanContext;
44+
/** Span */
45+
protected _span?: Span;
4646

4747
/**
4848
* Add internal on change listener. Used for sub SDKs that need to store the scope.
@@ -194,26 +194,26 @@ export class Scope implements ScopeInterface {
194194
/**
195195
* @inheritDoc
196196
*/
197-
public setSpanContext(spanContext?: SpanContext): this {
198-
this._span = spanContext;
197+
public setSpan(span?: Span): this {
198+
this._span = span;
199199
this._notifyScopeListeners();
200200
return this;
201201
}
202202

203203
/**
204204
* @inheritDoc
205205
*/
206-
public startSpan(): SpanContext {
207-
const span = new SpanContext();
208-
this.setSpanContext(span);
206+
public startSpan(): Span {
207+
const span = new Span();
208+
this.setSpan(span);
209209
return span;
210210
}
211211

212212
/**
213-
* Internal getter for SpanContext, used in Hub.
213+
* Internal getter for Span, used in Hub.
214214
* @hidden
215215
*/
216-
public getSpan(): SpanContext | undefined {
216+
public getSpan(): Span | undefined {
217217
return this._span;
218218
}
219219

packages/hub/src/spancontext.ts renamed to packages/hub/src/span.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import { SpanContext as SpanContextInterface } from '@sentry/types';
1+
import { Span as SpanInterface } from '@sentry/types';
22
import { uuid4 } from '@sentry/utils';
33

4-
export const TRACEPARENT_REGEX = /([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})/;
4+
export const TRACEPARENT_REGEXP = /([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})/;
55

66
/**
7-
* SpanContext containg all data about a span
7+
* Span containg all data about a span
88
*/
9-
export class SpanContext implements SpanContextInterface {
9+
export class Span implements SpanInterface {
1010
public constructor(
1111
private readonly _traceId: string = uuid4(),
1212
private readonly _spanId: string = uuid4().substring(16),
1313
private readonly _recorded: boolean = false,
14-
private readonly _parent?: SpanContext,
14+
private readonly _parent?: Span,
1515
) {}
1616

1717
/**
1818
* Continues a trace
1919
* @param traceparent Traceparent string
2020
*/
21-
public static fromTraceparent(traceparent: string): SpanContext | undefined {
22-
const matches = traceparent.match(TRACEPARENT_REGEX);
21+
public static fromTraceparent(traceparent: string): Span | undefined {
22+
const matches = traceparent.match(TRACEPARENT_REGEXP);
2323
if (matches) {
24-
const parent = new SpanContext(matches[2], matches[3], matches[4] === '01' ? true : false);
25-
return new SpanContext(matches[2], undefined, undefined, parent);
24+
const parent = new Span(matches[2], matches[3], matches[4] === '01' ? true : false);
25+
return new Span(matches[2], undefined, undefined, parent);
2626
}
2727
return undefined;
2828
}

packages/hub/test/spancontext.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import { SpanContext, TRACEPARENT_REGEX } from '../src';
1+
import { Span, TRACEPARENT_REGEXP } from '../src';
22

3-
describe('SpanContext', () => {
3+
describe('Span', () => {
44
test('toTraceparent', () => {
5-
expect(new SpanContext().toTraceparent()).toMatch(TRACEPARENT_REGEX);
5+
expect(new Span().toTraceparent()).toMatch(TRACEPARENT_REGEXP);
66
});
77

88
test('fromTraceparent', () => {
9-
const from = SpanContext.fromTraceparent('00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-00') as any;
9+
const from = Span.fromTraceparent('00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-00') as any;
1010
expect(from._parent._traceId).toEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
1111
expect(from._parent._spanId).toEqual('bbbbbbbbbbbbbbbb');
1212
expect(from._traceId).toEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
1313
expect(from._spanId).not.toEqual('bbbbbbbbbbbbbbbb');
1414
});
1515

1616
test('fromTraceparent - invalid', () => {
17-
expect(SpanContext.fromTraceparent('00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-x')).toBeUndefined();
17+
expect(Span.fromTraceparent('00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-x')).toBeUndefined();
1818
});
1919

2020
test('toJSON', () => {
21-
expect(JSON.stringify(new SpanContext('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbb'))).toEqual(
21+
expect(JSON.stringify(new Span('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbb'))).toEqual(
2222
`{"span_id":"bbbbbbbbbbbbbbbb","trace_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}`,
2323
);
2424
});

packages/node/src/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { captureException, getCurrentHub, withScope } from '@sentry/core';
2-
import { SpanContext } from '@sentry/hub';
2+
import { Span } from '@sentry/hub';
33
import { Event } from '@sentry/types';
44
import { forget, isString, logger, normalize } from '@sentry/utils';
55
import * as cookie from 'cookie';
@@ -295,8 +295,8 @@ export function errorHandler(): (
295295
}
296296
withScope(scope => {
297297
if (isString(_req.headers['sentry-trace'])) {
298-
const span = SpanContext.fromTraceparent(_req.headers['sentry-trace'] as string);
299-
scope.setSpanContext(span);
298+
const span = Span.fromTraceparent(_req.headers['sentry-trace'] as string);
299+
scope.setSpan(span);
300300
}
301301
const eventId = captureException(error);
302302
(_res as any).sentry = eventId;

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export { Response } from './response';
1616
export { Scope } from './scope';
1717
export { SdkInfo } from './sdkinfo';
1818
export { Severity } from './severity';
19-
export { SpanContext } from './spancontext';
19+
export { Span } from './span';
2020
export { StackFrame } from './stackframe';
2121
export { Stacktrace } from './stacktrace';
2222
export { Status } from './status';

packages/types/src/scope.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Breadcrumb } from './breadcrumb';
22
import { EventProcessor } from './eventprocessor';
33
import { Severity } from './severity';
4-
import { SpanContext } from './spancontext';
4+
import { Span } from './span';
55
import { User } from './user';
66

77
/**
@@ -71,15 +71,15 @@ export interface Scope {
7171
setContext(name: string, context: { [key: string]: any } | null): this;
7272

7373
/**
74-
* Sets the SpanContext on the scope.
75-
* @param spanContext SpanContext
74+
* Sets the Span on the scope.
75+
* @param span Span
7676
*/
77-
setSpanContext(spanContext?: SpanContext): this;
77+
setSpan(span?: Span): this;
7878

7979
/**
8080
* Starts a new Span.
8181
*/
82-
startSpan(): SpanContext;
82+
startSpan(): Span;
8383

8484
/** Clears the current scope and resets its properties. */
8585
clear(): this;

packages/types/src/spancontext.ts renamed to packages/types/src/span.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/** SpanContext holding trace_id, span_id */
2-
export interface SpanContext {
1+
/** Span holding trace_id, span_id */
2+
export interface Span {
33
/** Return a traceparent compatible header string */
44
toTraceparent(): string;
55
/** Convert the object to JSON */

0 commit comments

Comments
 (0)