Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/src/traced_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ impl<
"http.url" = ?uri,
"http.user_agent" = user_agent,
"http.status_code" = field::Empty,
"otel.status_code" = "ok"
"otel.status_code" = "ok",
"otel.kind" = "server"
);

if self.is_internal {
Expand Down
2 changes: 1 addition & 1 deletion frontend/routes/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const tracing = define.middleware(async (ctx) => {
return resp;
} finally {
const end = new Date();
ctx.state.span.record(ctx.url.pathname, start, end, attributes);
ctx.state.span.record(ctx.url.pathname, start, end, attributes, "SERVER");
}
});

Expand Down
20 changes: 18 additions & 2 deletions frontend/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,32 @@ export const handler = define.handlers({
anonymous: true,
}),
(async () => {
const span = ctx.state.span.child();
const start = new Date();
const attributes: Record<string, string | bigint | boolean> = {
"http.url": "https://deno.com/blog/json?tag=JSR",
"http.method": "GET",
};
let posts: Post[] = [];
try {
const jsrPosts = await fetch("https://deno.com/blog/json?tag=JSR", {
signal: AbortSignal.timeout(1000),
});
attributes["http.status_code"] = BigInt(jsrPosts.status);
if (jsrPosts.ok) {
posts = await jsrPosts.json() as Post[];
}
} catch (_e) {
// ignore
} catch (e) {
attributes["error"] = "true";
attributes["error.message"] = String((e as Error).message);
} finally {
span.record(
"fetch_dotcom_posts",
start,
new Date(),
attributes,
"CLIENT",
);
}
return posts;
})(),
Expand Down
1 change: 1 addition & 0 deletions frontend/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export class API {
"error.message": result.message,
}),
},
"CLIENT",
);
}
return result;
Expand Down
13 changes: 13 additions & 0 deletions frontend/utils/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const OTLP_ENDPOINT = Deno.env.get("OTLP_ENDPOINT");

const FLUSH_INTERVAL = 1000; // 5s

export type SpanKind = "SERVER" | "CLIENT" | "INTERNAL";

interface RecordedSpan {
traceId: string;
parentSpanId: string | null;
Expand All @@ -60,8 +62,15 @@ interface RecordedSpan {
endTime: Date;
displayName: string;
attributes: Record<string, string | bigint | boolean>;
spanKind: SpanKind;
}

const OTLP_SPAN_KIND: Record<SpanKind, number> = {
INTERNAL: 1,
SERVER: 2,
CLIENT: 3,
};

const BATCH_SPAN_IMMEDIATE_FLUSH_LEN = 100;
const BATCH_SPAN_OVERFLOW_LEN = 1000;

Expand Down Expand Up @@ -164,6 +173,7 @@ export class Tracer {
startTime: span.startTime,
endTime: span.endTime,
attributes: { attributeMap },
spanKind: span.spanKind,
} satisfies Span;
});
await this.#cloudTrace!.projectsTracesBatchWrite(
Expand Down Expand Up @@ -198,6 +208,7 @@ export class Tracer {
parentSpanId: span.parentSpanId,
spanId: span.spanId,
name: span.displayName,
kind: OTLP_SPAN_KIND[span.spanKind],
startTimeUnixNano: span.startTime.getTime() * 1e6,
endTimeUnixNano: span.endTime.getTime() * 1e6,
attributes,
Expand Down Expand Up @@ -325,6 +336,7 @@ export class TraceSpan {
startTime: Date,
endTime: Date,
attributes: Record<string, string | bigint | boolean>,
spanKind: SpanKind,
) {
if (!this.isSampled) return;
this.#tracer.recordSpan({
Expand All @@ -335,6 +347,7 @@ export class TraceSpan {
endTime,
displayName,
attributes,
spanKind,
});
}

Expand Down
Loading