-
-
Notifications
You must be signed in to change notification settings - Fork 584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Req: Otel middleware #1176
Labels
Comments
any updates on this @yusukebe ? |
resemble? |
Hi @DawnImpulse There is no update. We (at least I) don't plan to create it right now, so I hope someone works on it. |
Here is a simple solution. It needs a test as well. Do you have any suggestion about coding? @kevbook @DawnImpulse @yusukebe import otelapi, { type Tracer } from "@opentelemetry/api";
import {
context,
propagation,
SpanKind,
SpanStatusCode,
trace,
} from "@opentelemetry/api";
import type { MiddlewareHandler } from "hono";
import type { Env } from "../../env.ts";
import type { PortLogger } from "../logger/logger.ts";
let otel: typeof otelapi | undefined;
let rawTracer: Tracer | undefined;
export const opentelemetryMiddleware =
(logger: PortLogger): MiddlewareHandler<Env> =>
async (ctx, next) => {
if (!otel) {
try {
await next();
if (ctx.error) {
logger.error({ error: ctx.error.message });
}
} catch (error) {
logger.error({
error: error instanceof Error ? error.message : "unknown error",
});
throw error;
}
return;
}
if (!rawTracer) {
rawTracer = otel.trace.getTracer("hono-poc", "0.0.0");
}
const span = rawTracer.startSpan(
"opentelemetry.infrastructure.middleware",
{
attributes: {
"http.method": ctx.req.method,
"http.url": ctx.req.url,
},
kind: SpanKind.SERVER,
},
propagation.extract(context.active(), ctx.req.raw.headers),
);
try {
await context.with(trace.setSpan(context.active(), span), async () => {
await next();
});
if (ctx.error) {
logger.error({ error: ctx.error.message });
span.recordException(ctx.error);
span.setStatus({
code: SpanStatusCode.ERROR,
message: ctx.error.message,
});
} else {
span.setStatus({ code: SpanStatusCode.OK });
}
} catch (error) {
logger.error({
error: error instanceof Error ? error.message : "unknown error",
});
span.recordException(error as Error);
span.setStatus({
code: SpanStatusCode.ERROR,
message: error instanceof Error ? error.message : "unknown error",
});
throw error;
}
span.end();
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since Otel Telemetry is becoming the standard, it would be good to have a middleware that also plays well with Hono logger
The text was updated successfully, but these errors were encountered: