update trace logic

This commit is contained in:
Aleksandr Trushkin
2024-09-24 21:59:33 +03:00
parent c0f45d98c2
commit c7fada2c54
7 changed files with 345 additions and 599 deletions

View File

@ -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")

View File

@ -93,6 +93,10 @@ func newCommonTraceProvider(ctx context.Context, params TraceProviderParams) (tp
return "bigstats:kuriweb", nil
}),
),
resource.WithAttributes(
semconv.ServiceName("bigstats:kuriweb"),
semconv.DeploymentEnvironment("production"),
),
)
if err != nil {
return nil, fmt.Errorf("making new resource: %w", err)
@ -124,14 +128,19 @@ func newCommonTraceProvider(ctx context.Context, params TraceProviderParams) (tp
spanExporter, err = otlptracegrpc.New(
ctx,
otlptracegrpc.WithEndpointURL(params.Endpoint),
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithHeaders(headers),
otlptracegrpc.WithCompressor("gzip"),
)
case config.TraceClientTypeHTTP:
httpClient := otlptracehttp.NewClient(
otlptracehttp.WithEndpointURL(params.Endpoint),
otlptracehttp.WithHeaders(headers),
otlptracehttp.WithCompression(otlptracehttp.GzipCompression),
)
spanExporter, err = otlptrace.New(
ctx, httpClient,
)
spanExporter, err = otlptrace.New(ctx, httpClient)
case config.TraceClientTypeStdout:
spanExporter, err = stdouttrace.New(stdouttrace.WithPrettyPrint())
default: