UNPKG

@fiberplane/hono-otel

Version:

Hono middleware to forward OpenTelemetry traces to a local instance of @fiberplane/studio

27 lines (26 loc) 981 B
import { SpanKind } from "@opentelemetry/api"; import shimmer from "shimmer"; import { measure } from "../measure.js"; import { getRequestAttributes, getResponseAttributes, isWrapped, } from "../utils/index.js"; const { wrap } = shimmer; export function patchFetch() { // Check if the function is already patched // If it is, we don't want to patch it again if (isWrapped(globalThis.fetch)) { return; } wrap(globalThis, "fetch", (original) => { return measure({ name: "fetch", spanKind: SpanKind.CLIENT, onStart: (span, [input, init]) => { span.setAttributes(getRequestAttributes(input, init)); }, onSuccess: async (span, responsePromise) => { const response = await responsePromise; const attributes = await getResponseAttributes(response); span.setAttributes(attributes); }, }, original); }); }