set span name and attributes after the request is handled
parent
5315ef7e0d
commit
a638a15346
|
@ -1,5 +1,5 @@
|
||||||
import type { TracerProvider } from '@opentelemetry/api'
|
import type { TracerProvider } from '@opentelemetry/api'
|
||||||
import { SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'
|
import { SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'
|
||||||
import {
|
import {
|
||||||
ATTR_HTTP_REQUEST_HEADER,
|
ATTR_HTTP_REQUEST_HEADER,
|
||||||
ATTR_HTTP_REQUEST_METHOD,
|
ATTR_HTTP_REQUEST_METHOD,
|
||||||
|
@ -10,17 +10,19 @@ import {
|
||||||
} from '@opentelemetry/semantic-conventions'
|
} from '@opentelemetry/semantic-conventions'
|
||||||
import type { Env, Input } from 'hono'
|
import type { Env, Input } from 'hono'
|
||||||
import { createMiddleware } from 'hono/factory'
|
import { createMiddleware } from 'hono/factory'
|
||||||
import metadata from '../package.json' with { type: 'json'}
|
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;
|
| {
|
||||||
tracerProvider?: TracerProvider
|
augmentSpan?: false
|
||||||
} | {
|
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 = {}>(
|
||||||
options: OtelOptions = {}
|
options: OtelOptions = {}
|
||||||
|
@ -42,7 +44,7 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
|
||||||
return createMiddleware<E, P, I>(async (c, next) => {
|
return createMiddleware<E, P, I>(async (c, next) => {
|
||||||
const routePath = c.req.routePath
|
const routePath = c.req.routePath
|
||||||
await tracer.startActiveSpan(
|
await tracer.startActiveSpan(
|
||||||
`${c.req.method} ${routePath}`,
|
`${c.req.method} ${c.req.routePath}`,
|
||||||
{
|
{
|
||||||
kind: SpanKind.SERVER,
|
kind: SpanKind.SERVER,
|
||||||
attributes: {
|
attributes: {
|
||||||
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue