feat(unstable): Instrument fetch (#27057)

Add basic tracing to `fetch`. Also fix span kinds so that we can
differentiate fetch and serve.
This commit is contained in:
snek 2024-11-25 16:38:07 +01:00 committed by GitHub
parent 08a56763d4
commit d59bd5e8c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 223 additions and 90 deletions

View file

@ -41,6 +41,8 @@ const { AsyncVariable, setAsyncContext } = core;
let TRACING_ENABLED = false;
let DETERMINISTIC = false;
// Note: These start at 0 in the JS library,
// but start at 1 when serialized with JSON.
enum SpanKind {
INTERNAL = 0,
SERVER = 1,
@ -91,6 +93,11 @@ interface Attributes {
type SpanAttributes = Attributes;
interface SpanOptions {
attributes?: Attributes;
kind?: SpanKind;
}
interface Link {
context: SpanContext;
attributes?: SpanAttributes;
@ -354,7 +361,7 @@ export class Span {
#recording = TRACING_ENABLED;
#kind: number = 0;
#kind: number = SpanKind.INTERNAL;
#name: string;
#startTime: number;
#status: { code: number; message?: string } | null = null;
@ -429,7 +436,7 @@ export class Span {
constructor(
name: string,
attributes?: Attributes,
options?: SpanOptions,
) {
if (!this.isRecording) {
this.#name = "";
@ -442,7 +449,8 @@ export class Span {
this.#name = name;
this.#startTime = now();
this.#attributes = attributes ?? { __proto__: null } as never;
this.#attributes = options?.attributes ?? { __proto__: null } as never;
this.#kind = options?.kind ?? SpanKind.INTERNAL;
const currentSpan: Span | {
spanContext(): { traceId: string; spanId: string };