From d0532ee8e7bddda7910a1ff465a682d4d83b3f53 Mon Sep 17 00:00:00 2001 From: Hans Kristian Flaatten Date: Wed, 14 Aug 2024 12:23:09 +0200 Subject: [PATCH] Fix tracing for flaky-service --- flaky-service/go.mod | 2 ++ flaky-service/go.sum | 4 +++ flaky-service/main.go | 67 +++++++++++++++++++++++-------------------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/flaky-service/go.mod b/flaky-service/go.mod index a91abe6..d410b2e 100644 --- a/flaky-service/go.mod +++ b/flaky-service/go.mod @@ -14,6 +14,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect @@ -58,6 +59,7 @@ require ( github.com/stretchr/testify v1.9.0 // indirect github.com/twmb/murmur3 v1.1.8 // indirect go.opentelemetry.io/contrib/exporters/autoexport v0.53.0 + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 go.opentelemetry.io/contrib/propagators/b3 v1.28.0 golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 ) diff --git a/flaky-service/go.sum b/flaky-service/go.sum index 83cac9f..6393777 100644 --- a/flaky-service/go.sum +++ b/flaky-service/go.sum @@ -10,6 +10,8 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -56,6 +58,8 @@ go.opentelemetry.io/contrib/bridges/prometheus v0.53.0 h1:BdkKDtcrHThgjcEia1737O go.opentelemetry.io/contrib/bridges/prometheus v0.53.0/go.mod h1:ZkhVxcJgeXlL/lVyT/vxNHVFiSG5qOaDwYaSgD8IfZo= go.opentelemetry.io/contrib/exporters/autoexport v0.53.0 h1:13K+tY7E8GJInkrvRiPAhC0gi/7vKjzDNhtmCf+QXG8= go.opentelemetry.io/contrib/exporters/autoexport v0.53.0/go.mod h1:lyQF6xQ4iDnMg4sccNdFs1zf62xd79YI8vZqKjOTwMs= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIXefpVJtvA/8srF4V4y0akAoPHkIslgAkjixJA= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg= go.opentelemetry.io/contrib/propagators/b3 v1.28.0 h1:XR6CFQrQ/ttAYmTBX2loUEFGdk1h17pxYI8828dk/1Y= go.opentelemetry.io/contrib/propagators/b3 v1.28.0/go.mod h1:DWRkzJONLquRz7OJPh2rRbZ7MugQj62rk7g6HRnEqh0= go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= diff --git a/flaky-service/main.go b/flaky-service/main.go index 914b87b..38caf97 100644 --- a/flaky-service/main.go +++ b/flaky-service/main.go @@ -9,9 +9,11 @@ import ( "github.com/joho/godotenv" "github.com/sethvargo/go-envconfig" + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/trace" "golang.org/x/exp/rand" ) @@ -36,9 +38,6 @@ func init() { func main() { slog.Info("Starting server", "host", c.Server.Host, "port", c.Server.Port) - tp := otel.GetTracerProvider() - tracer := tp.Tracer("flaky-service") - mp := otel.GetMeterProvider() meter := mp.Meter("flaky-service") requestsCounter, err := meter.Int64Counter("http_requests", metric.WithDescription("Number of HTTP requests.")) @@ -46,44 +45,50 @@ func main() { panic("Counter setup failed") } - // Start a simple HTTP server - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - ctx, span := tracer.Start(r.Context(), "hello-span") - defer span.End() + var mux http.ServeMux + mux.Handle("/", + otelhttp.WithRouteTag("/", http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + ctx, span := trace.SpanFromContext(r.Context()).TracerProvider().Tracer("exampleTracer").Start(r.Context(), "doWork") + defer span.End() - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json") - level := c.FlakinessLevel() - slog.Info("Flakiness level", "level", level) + level := c.FlakinessLevel() + slog.Info("Flakiness level", "level", level) - w.Header().Set("X-Flakiness-Level", fmt.Sprintf("%d", level)) + w.Header().Set("X-Flakiness-Level", fmt.Sprintf("%d", level)) - if rand.Intn(100) < level { - span.RecordError(fmt.Errorf("you found me")) - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(`{"error": "Internal Server Error"}`)) + if rand.Intn(100) < level { + span.RecordError(fmt.Errorf("you found me")) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(`{"error": "Internal Server Error"}`)) - requestsCounter.Add(ctx, 1, metric.WithAttributes( - attribute.Key("path").String("/"), - attribute.Key("status_code").Int(500), - )) - return - } + requestsCounter.Add(ctx, 1, metric.WithAttributes( + attribute.Key("path").String("/"), + attribute.Key("status_code").Int(500), + )) + return + } - requestsCounter.Add(ctx, 1, metric.WithAttributes( - attribute.Key("path").String("/"), - attribute.Key("status_code").Int(200), - )) + requestsCounter.Add(ctx, 1, metric.WithAttributes( + attribute.Key("path").String("/"), + attribute.Key("status_code").Int(200), + )) - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"message": "Hello, World!"}`)) - }) + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"message": "Hello, World!"}`)) + }), + ), + ) - http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { + mux.Handle("/healthz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write([]byte(`{"status": "ok"}`)) - }) + })) - log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", c.Server.Host, c.Server.Port), nil)) + log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", c.Server.Host, c.Server.Port), otelhttp.NewHandler(&mux, "server", + otelhttp.WithMessageEvents(otelhttp.ReadEvents, otelhttp.WriteEvents), + ))) }