Skip to content
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

Open
kevbook opened this issue Jun 13, 2023 · 4 comments
Open

Feature Req: Otel middleware #1176

kevbook opened this issue Jun 13, 2023 · 4 comments

Comments

@kevbook
Copy link

kevbook commented Jun 13, 2023

Since Otel Telemetry is becoming the standard, it would be good to have a middleware that also plays well with Hono logger

@DawnImpulse
Copy link

any updates on this @yusukebe ?

@EdamAme-x
Copy link
Contributor

EdamAme-x commented Jul 11, 2024

resemble?
#3025

@yusukebe
Copy link
Member

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.

@smhmayboudi
Copy link

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
Projects
None yet
Development

No branches or pull requests

5 participants