update trace logic
This commit is contained in:
@ -16,6 +16,7 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
@ -114,14 +115,7 @@ func (k attributeStringKey) Value(value string) attribute.KeyValue {
|
||||
}
|
||||
|
||||
func middlewareTrace() mux.MiddlewareFunc {
|
||||
methodAttr := attributeStringKey("http.request.method")
|
||||
reqidAttr := attributeStringKey("http.request_id")
|
||||
routeAttr := attributeStringKey("http.route")
|
||||
queryAttr := attributeStringKey("url.query")
|
||||
pathAttr := attributeStringKey("http.path")
|
||||
uaAttr := attributeStringKey("user_agent.original")
|
||||
statusAttr := attribute.Key("http.response.status_code")
|
||||
payloadAttr := attribute.Key("http.response_content_length")
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@ -134,12 +128,12 @@ func middlewareTrace() mux.MiddlewareFunc {
|
||||
ctx, span = webtracer.Start(
|
||||
ctx, r.Method+" "+hpath,
|
||||
trace.WithAttributes(
|
||||
methodAttr.Value(r.Method),
|
||||
reqidAttr.Value(reqid),
|
||||
routeAttr.Value(hpath),
|
||||
pathAttr.Value(r.URL.Path),
|
||||
queryAttr.Value(r.URL.RawQuery),
|
||||
uaAttr.Value(r.UserAgent()),
|
||||
semconv.HTTPRequestMethodOriginal(r.Method),
|
||||
semconv.URLFull(hpath),
|
||||
semconv.URLPath(r.URL.Path),
|
||||
semconv.URLQuery(r.URL.RawQuery),
|
||||
semconv.UserAgentOriginal(r.UserAgent()),
|
||||
),
|
||||
trace.WithSpanKind(trace.SpanKindServer),
|
||||
)
|
||||
@ -150,8 +144,8 @@ func middlewareTrace() mux.MiddlewareFunc {
|
||||
if wr, ok := w.(*customResponseWriter); ok {
|
||||
statusCode := xdefault.WithFallback(wr.statusCode, http.StatusOK)
|
||||
span.SetAttributes(
|
||||
statusAttr.Int(statusCode),
|
||||
payloadAttr.Int(wr.wroteBytes),
|
||||
semconv.HTTPResponseStatusCode(statusCode),
|
||||
semconv.HTTPResponseBodySize(wr.wroteBytes),
|
||||
)
|
||||
if statusCode > 399 {
|
||||
span.SetStatus(codes.Error, "error during request")
|
||||
|
||||
Reference in New Issue
Block a user