Skip to content

Commit b111406

Browse files
committed
feat: Add traceId + references
1 parent a8ca14a commit b111406

File tree

7 files changed

+87
-61
lines changed

7 files changed

+87
-61
lines changed

packages/opentracing/src/index.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
export { Span } from './span';
22
export { Tracer } from './tracer';
3-
export { SessionTracking } from './sessiontracking';
4-
export { initGlobalTracer } from 'opentracing/lib/global_tracer';
5-
6-
import { globalTracer as otGlobalTracer } from 'opentracing/lib/global_tracer';
7-
import { Tracer } from './tracer';
8-
9-
/**
10-
* Return the global set Tracer
11-
*/
12-
export function globalTracer(): Tracer {
13-
return (otGlobalTracer() as unknown) as Tracer;
14-
}
3+
export { OpenTracingIntegration as Integration } from './opentracingintegration';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Integration } from '@sentry/types';
2+
import { Tracer } from './tracer';
3+
4+
/**
5+
* Session Tracking Integration
6+
*/
7+
export class OpenTracingIntegration implements Integration {
8+
/**
9+
* @inheritDoc
10+
*/
11+
public name: string = OpenTracingIntegration.id;
12+
13+
/**
14+
* @inheritDoc
15+
*/
16+
public static id: string = 'OpenTracingIntegration';
17+
18+
/**
19+
* Constructor for OpenTracingIntegration
20+
*
21+
* @param traceId Optional TraceId that should be set into the integration.
22+
* @param tracer Optional custom tracer that should be used.
23+
*/
24+
public constructor(traceId?: string, private readonly tracer: Tracer = new Tracer()) {
25+
tracer.setTraceId(traceId);
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public setupOnce(): void {
32+
const span = this.tracer.startSpan('sdk.init');
33+
span.finish();
34+
setTimeout(() => {
35+
this.tracer.flush();
36+
});
37+
}
38+
39+
/**
40+
* Returns the Tracer which can be used as the parent.
41+
*/
42+
public getTracer(): Tracer {
43+
return this.tracer;
44+
}
45+
}

packages/opentracing/src/sessiontracking.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/opentracing/src/span.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Span as SpanInterface } from '@sentry/types';
2-
import { Span as otSpan } from 'opentracing/lib/span';
2+
import * as opentracing from 'opentracing';
33
import { SpanContext } from './spancontext';
44
import { Tracer } from './tracer';
55

@@ -10,7 +10,7 @@ interface Log {
1010
}
1111

1212
/** JSDoc */
13-
export class Span extends otSpan implements SpanInterface {
13+
export class Span extends opentracing.Span implements SpanInterface {
1414
private finishTime: number = 0;
1515

1616
private readonly logs: Log[] = [];
@@ -27,6 +27,7 @@ export class Span extends otSpan implements SpanInterface {
2727
private readonly usedTracer: Tracer,
2828
private operation: string,
2929
private readonly spanContext: SpanContext,
30+
private readonly references?: opentracing.Reference[],
3031
private readonly startTime: number = Date.now(),
3132
) {
3233
super();
@@ -107,9 +108,15 @@ export class Span extends otSpan implements SpanInterface {
107108
/**
108109
* @inheritdoc
109110
*/
110-
public toJSON(): any {
111-
// TODO
112-
console.log('ajajajajaj');
113-
return 'ababab';
111+
public toJSON(): object {
112+
return {
113+
finishTime: this.finishTime || undefined,
114+
logs: this.logs,
115+
references: this.references,
116+
span_id: this.spanContext.spanId,
117+
startTime: this.startTime,
118+
tags: this.tags,
119+
trace_id: this.spanContext.traceId,
120+
};
114121
}
115122
}

packages/opentracing/src/spancontext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { uuid4 } from '@sentry/utils/misc';
44
*/
55
export class SpanContext {
66
public constructor(
7-
private readonly traceId: string,
8-
private readonly spanId: string = uuid4(), // private readonly parentId: string,
7+
public readonly traceId?: string,
8+
public readonly spanId: string = uuid4(), // private readonly parentId: string,
99
) {}
1010

1111
/**

packages/opentracing/src/tracer.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
1-
import { SpanOptions, Tracer as otTracer } from 'opentracing/lib/tracer';
1+
import { getCurrentHub } from '@sentry/hub';
2+
import * as opentracing from 'opentracing';
23
import { Span } from './span';
34
import { SpanContext } from './spancontext';
45

56
/**
67
* JSDoc
78
*/
8-
export class Tracer extends otTracer {
9-
private readonly spans: Span[] = [];
9+
export class Tracer extends opentracing.Tracer {
10+
private traceId?: string = undefined;
11+
private spans: Span[] = [];
1012

1113
/**
1214
* Called by public method startSpan
1315
* @param name Name of the operation
1416
* @param fields Options for the span {@link opentracing.SpanOptions}
1517
*/
16-
protected _startSpan(name: string, fields: SpanOptions): Span {
17-
// TODO: Traceid
18-
const span = new Span(this, name, new SpanContext('1'), fields.startTime);
18+
protected _startSpan(name: string, fields: opentracing.SpanOptions): Span {
19+
const span = new Span(this, name, new SpanContext(this.traceId), fields.references, fields.startTime);
1920
this.spans.push(span);
20-
// TODO: Implement childof
2121
return span;
2222
}
2323

2424
/**
2525
* @inheritdoc
2626
*/
27-
public startSpan(name: string, options: SpanOptions = {}): Span {
27+
public startSpan(name: string, options: opentracing.SpanOptions = {}): Span {
2828
return (super.startSpan(name, options) as unknown) as Span;
2929
}
30+
31+
/**
32+
* Sets the current traceId, all new created spans will receive the traceId.
33+
*
34+
* @param traceId A string representing the traceId
35+
*/
36+
public setTraceId(traceId?: string): void {
37+
this.traceId = traceId;
38+
}
39+
40+
/**
41+
* Flushes all spans and sends an event
42+
*/
43+
public flush(): void {
44+
getCurrentHub().captureEvent({ spans: [...this.spans] });
45+
this.spans = [];
46+
}
3047
}

packages/types/src/span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** JSDoc */
22
export interface Span {
33
/** JSDoc */
4-
toJSON(): string;
4+
toJSON(): object;
55
}

0 commit comments

Comments
 (0)