UNPKG

@fiberplane/hono-otel

Version:

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

49 lines (48 loc) 2.18 kB
import { getFpResolvedConfig, getShouldTraceEverything, } from "../../config/index.js"; import { FPX_REQUEST_ENV } from "../../constants.js"; import { getLogger } from "../../logger.js"; import { getPlatformSafeEnv } from "../env.js"; import { safelySerializeJSON } from "../json.js"; import { formatRootRequestBody } from "./body.js"; import { getSafeHeaderValue, headersToObject } from "./headers.js"; /** * Helper to get the request attributes for the root request. * * When tracing e.v.e.r.y.t.h.i.n.g, this requires that we have a cloned request, * so we can get the body and headers without consuming the original request. */ export async function getIncomingRequestAttributes(request, honoEnv, config) { const resolvedConfig = config ?? getFpResolvedConfig(); const shouldTraceEverything = getShouldTraceEverything(resolvedConfig); const logger = getLogger(resolvedConfig?.logLevel ?? "debug"); if (!resolvedConfig) { logger.debug("[getIncomingRequestAttributes] No config found in otel context, using default values"); } let attributes = {}; // NOTE - In practice, we only send env vars when running in "local" mode if (shouldTraceEverything) { // We need to account for the fact that the Hono `env` is different across runtimes // If process.env is available, we use that, otherwise we use the `env` object from the Hono runtime const env = getPlatformSafeEnv(honoEnv); if (env) { attributes[FPX_REQUEST_ENV] = safelySerializeJSON(env); } } if (shouldTraceEverything && request.body) { const bodyAttr = await formatRootRequestBody(request); if (bodyAttr) { attributes = { ...attributes, ...bodyAttr, }; } } if (request.headers) { const headers = headersToObject(new Headers(request.headers)); for (const [key, value] of Object.entries(headers)) { // Redact sensitive headers when running in production attributes[`http.request.header.${key}`] = getSafeHeaderValue(key, value, resolvedConfig); } } return attributes; }