|
1 |
| -import { OtlpTransformer } from './transformers/otlp'; |
2 |
| -import { ATTRIBUTE_NAME } from './utils/constants'; |
| 1 | +import { OtlpTransformer } from './exporters/otlp'; |
| 2 | +import { ATTRIBUTE_NAME, SPAN_NAME } from './utils/constants'; |
3 | 3 | import { generateSpanId, generateTraceId } from './utils/rand';
|
4 | 4 | import { traceFn } from './trace';
|
5 | 5 | import { SpanBuilder } from './builder';
|
@@ -33,11 +33,19 @@ export function getDefaultAttributes(opts: TracerOptions): Attributes {
|
33 | 33 | }
|
34 | 34 |
|
35 | 35 | export class Span {
|
36 |
| - |
| 36 | + |
| 37 | + #trace: Trace; |
37 | 38 | #span: SpanData;
|
38 | 39 | #childSpans: Span[];
|
39 | 40 |
|
40 | 41 | constructor(traceId: string, name: string, spanOptions?: SpanCreationOptions) {
|
| 42 | + // TODO: Figure out how I want to do this |
| 43 | + const trace = this instanceof Trace ? this : spanOptions?.trace; |
| 44 | + if (!trace) { |
| 45 | + throw new Error('No Trace provided for Span'); |
| 46 | + } |
| 47 | + |
| 48 | + this.#trace = trace; |
41 | 49 | this.#span = {
|
42 | 50 | traceId: traceId,
|
43 | 51 | name,
|
@@ -75,16 +83,37 @@ export class Span {
|
75 | 83 |
|
76 | 84 | startSpan(name: string, spanOptions?: SpanCreationOptions): Span {
|
77 | 85 | const span = new Span(this.#span.traceId, name, spanOptions);
|
| 86 | + span.#trace = this.#trace; // TODO: I hate this, fix. |
78 | 87 | span.#span.parentId = this.getSpanId();
|
79 | 88 | this.#childSpans.push(span);
|
80 | 89 |
|
81 | 90 | return span;
|
82 | 91 | }
|
83 | 92 |
|
| 93 | + injectPropagation(req: Request) { |
| 94 | + // TODO: Figure out a better way to get the exporter in these situations |
| 95 | + const exporter = this.#trace.getTracerOptions().collector.exporter ?? new OtlpTransformer(); |
| 96 | + |
| 97 | + for (const [name, value] of Object.entries(exporter.injectContextHeaders(this))) { |
| 98 | + req.headers.append(name, value); |
| 99 | + } |
| 100 | + } |
| 101 | + |
84 | 102 | trace<T>(name: string, fn: TracedFn<T>, opts?: SpanCreationOptions): T {
|
85 | 103 | return traceFn(this, name, fn, opts);
|
86 | 104 | }
|
87 | 105 |
|
| 106 | + tracedFetch( |
| 107 | + request: string | Request, |
| 108 | + requestInit?: RequestInit | Request, |
| 109 | + spanOpts?: SpanCreationOptions, |
| 110 | + ): Promise<Response> { |
| 111 | + const tracedRequest = new Request(request, requestInit); |
| 112 | + this.injectPropagation(tracedRequest); |
| 113 | + |
| 114 | + return traceFn(this, SPAN_NAME.FETCH, () => fetch(tracedRequest), spanOpts); |
| 115 | + } |
| 116 | + |
88 | 117 | buildSpan(name: string) {
|
89 | 118 | return new SpanBuilder(this, name);
|
90 | 119 | }
|
@@ -114,7 +143,7 @@ export class Trace extends Span {
|
114 | 143 | ) {
|
115 | 144 | super(
|
116 | 145 | tracerOptions.traceContext?.traceId ?? generateTraceId(),
|
117 |
| - 'Request (fetch event)', |
| 146 | + 'Request', |
118 | 147 | {
|
119 | 148 | parentId: tracerOptions.traceContext?.spanId,
|
120 | 149 | ...spanOptions,
|
@@ -146,10 +175,6 @@ export class Trace extends Span {
|
146 | 175 | return this.#tracerOptions;
|
147 | 176 | }
|
148 | 177 |
|
149 |
| - injectPropagation(req: Request) { |
150 |
| - |
151 |
| - } |
152 |
| - |
153 | 178 | async send() {
|
154 | 179 | // We need to end the trace here
|
155 | 180 | this.end();
|
|
0 commit comments