Skip to content

Commit

Permalink
Fix tracing for flaky-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Starefossen committed Aug 14, 2024
1 parent 41c5f5c commit d0532ee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
2 changes: 2 additions & 0 deletions flaky-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions flaky-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
67 changes: 36 additions & 31 deletions flaky-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -36,54 +38,57 @@ 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."))
if err != nil {
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),
)))
}

0 comments on commit d0532ee

Please sign in to comment.