use routePath

Replace indexing into the matchedRoutes with a direct reference to routePath
pull/1113/head
Milo Hansen 2025-04-08 11:56:27 -07:00 committed by GitHub
parent 542391f8db
commit 2bcb3ead24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 6 deletions

View File

@ -30,9 +30,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 +40,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} ${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) => {