hertz-contrib/obs-opentelemetry

Have you considered writing the trace ID back to the response header?

Closed this issue · 5 comments

During the debugging process, it may be necessary to use the trace ID to troubleshoot issues. As a server-side developer, I would appreciate it if others could provide me with the trace ID of the problematic requests.

Adding a middleware should meet your needs.

package middleware

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	oteltrace "go.opentelemetry.io/otel/trace"
)

func TraceMiddleware() app.HandlerFunc {
	return func(ctx context.Context, c *app.RequestContext) {
		c.Next(ctx)
		c.Header("trace-id", oteltrace.SpanFromContext(ctx).SpanContext().TraceID().String())
	}
}

Adding a middleware should meet your needs.

package middleware

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	oteltrace "go.opentelemetry.io/otel/trace"
)

func TraceMiddleware() app.HandlerFunc {
	return func(ctx context.Context, c *app.RequestContext) {
		c.Next(ctx)
		c.Header("trace-id", oteltrace.SpanFromContext(ctx).SpanContext().TraceID().String())
	}
}

Yes, it's possible to add a middleware to fulfill this requirement. Raising this issue is to inquire whether there has been consideration to add this functionality to the project.

I will take a look later, and if you can submit a PR, will greatly appreciate it.

Sure, I will submit a pull request. When the time comes, could you please review it?

Sure, I will submit a pull request. When the time comes, could you please review it?

Of course, thank you very much.