Milo Hansen 2025-04-09 20:25:42 +10:00 committed by GitHub
commit ddb4300df7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 14 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/otel': patch
---
Use `req.routePath` to augment spans with the path that handled the request.

View File

@ -15,11 +15,13 @@ import metadata from '../package.json' with { type: 'json'}
const PACKAGE_NAME = metadata.name const PACKAGE_NAME = metadata.name
const PACKAGE_VERSION = metadata.version const PACKAGE_VERSION = metadata.version
export type OtelOptions = { export type OtelOptions =
augmentSpan?: false; | {
augmentSpan?: false
tracerProvider?: TracerProvider tracerProvider?: TracerProvider
} | { }
augmentSpan: true; | {
augmentSpan: true
} }
export const otel = <E extends Env = any, P extends string = any, I extends Input = {}>( export const otel = <E extends Env = any, P extends string = any, I extends Input = {}>(
@ -30,9 +32,9 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
const result = await next() const result = await next()
const span = trace.getActiveSpan() const span = trace.getActiveSpan()
if (span != null) { if (span != null) {
const route = c.req.matchedRoutes[c.req.matchedRoutes.length - 1] const routePath = c.req.routePath
span.setAttribute(ATTR_HTTP_ROUTE, route.path) span.setAttribute(ATTR_HTTP_ROUTE, routePath)
span.updateName(`${c.req.method} ${route.path}`) span.updateName(`${c.req.method} ${routePath}`)
} }
return result return result
}) })
@ -40,15 +42,15 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider() const tracerProvider = options.tracerProvider ?? trace.getTracerProvider()
const tracer = tracerProvider.getTracer(PACKAGE_NAME, PACKAGE_VERSION) const tracer = tracerProvider.getTracer(PACKAGE_NAME, PACKAGE_VERSION)
return createMiddleware<E, P, I>(async (c, next) => { return createMiddleware<E, P, I>(async (c, next) => {
const route = c.req.matchedRoutes[c.req.matchedRoutes.length - 1] const routePath = c.req.routePath
await tracer.startActiveSpan( await tracer.startActiveSpan(
`${c.req.method} ${route.path}`, `${c.req.method} ${c.req.routePath}`,
{ {
kind: SpanKind.SERVER, kind: SpanKind.SERVER,
attributes: { attributes: {
[ATTR_HTTP_REQUEST_METHOD]: c.req.method, [ATTR_HTTP_REQUEST_METHOD]: c.req.method,
[ATTR_URL_FULL]: c.req.url, [ATTR_URL_FULL]: c.req.url,
[ATTR_HTTP_ROUTE]: route.path, [ATTR_HTTP_ROUTE]: routePath,
}, },
}, },
async (span) => { async (span) => {
@ -57,6 +59,10 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
} }
try { try {
await next() await next()
// Update the span name and route path now that we have the response
// because the route path may have changed
span.updateName(`${c.req.method} ${c.req.routePath}`)
span.setAttribute(ATTR_HTTP_ROUTE, c.req.routePath)
span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, c.res.status) span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, c.res.status)
for (const [name, value] of c.res.headers.entries()) { for (const [name, value] of c.res.headers.entries()) {
span.setAttribute(ATTR_HTTP_RESPONSE_HEADER(name), value) span.setAttribute(ATTR_HTTP_RESPONSE_HEADER(name), value)