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

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

Closed
lafer-fz opened this issue Aug 21, 2023 · 5 comments
Closed

Comments

@lafer-fz
Copy link
Contributor

lafer-fz commented Aug 21, 2023

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.

@rogerogers
Copy link
Collaborator

rogerogers commented Aug 21, 2023

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())
	}
}

@lafer-fz
Copy link
Contributor Author

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.

@rogerogers
Copy link
Collaborator

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

@lafer-fz
Copy link
Contributor Author

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

@rogerogers
Copy link
Collaborator

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

Of course, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants