From 7e870313999a93c24f67d429627a27ed77d1a7b7 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Mon, 13 May 2024 10:58:03 -0700 Subject: [PATCH 1/3] Add logs to dice example Use slog to log a message on the server handler. --- example/dice/go.mod | 10 ++++++++++ example/dice/go.sum | 2 ++ example/dice/main.go | 3 ++- example/dice/otel.go | 24 ++++++++++++++++++++++++ example/dice/rolldice.go | 25 +++++++++++++++++++++---- 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/example/dice/go.mod b/example/dice/go.mod index a99576f30e0..1a3ec7ed47f 100644 --- a/example/dice/go.mod +++ b/example/dice/go.mod @@ -3,12 +3,16 @@ module go.opentelemetry.io/otel/example/dice go 1.21 require ( + go.opentelemetry.io/contrib/bridges/otelslog v0.1.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 go.opentelemetry.io/otel v1.26.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.2.0-alpha go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.26.0 go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.26.0 + go.opentelemetry.io/otel/log v0.2.0-alpha go.opentelemetry.io/otel/metric v1.26.0 go.opentelemetry.io/otel/sdk v1.26.0 + go.opentelemetry.io/otel/sdk/log v0.2.0-alpha go.opentelemetry.io/otel/sdk/metric v1.26.0 ) @@ -33,3 +37,9 @@ replace go.opentelemetry.io/otel/metric => ../../metric replace go.opentelemetry.io/otel/sdk/metric => ../../sdk/metric replace go.opentelemetry.io/otel/sdk => ../../sdk + +replace go.opentelemetry.io/otel/exporters/stdout/stdoutlog => ../../exporters/stdout/stdoutlog + +replace go.opentelemetry.io/otel/log => ../../log + +replace go.opentelemetry.io/otel/sdk/log => ../../sdk/log diff --git a/example/dice/go.sum b/example/dice/go.sum index bf1d6c31f24..33e07d56dc0 100644 --- a/example/dice/go.sum +++ b/example/dice/go.sum @@ -13,6 +13,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/contrib/bridges/otelslog v0.1.0 h1:XQka5HbIS6JOeZvWz3oKyvzGmMaw7bsHH6r26Kmbrc4= +go.opentelemetry.io/contrib/bridges/otelslog v0.1.0/go.mod h1:TTRD9H+Ddlxv9NaX+2kQXUGiYAOpcOS0kkTlC1EI4Wg= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 h1:Xs2Ncz0gNihqu9iosIZ5SkBbWo5T8JhhLJFMQL1qmLI= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0/go.mod h1:vy+2G/6NvVMpwGX/NyLqcC41fxepnuKHk16E6IZUcJc= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= diff --git a/example/dice/main.go b/example/dice/main.go index f0327b360ea..2a4c2453ec0 100644 --- a/example/dice/main.go +++ b/example/dice/main.go @@ -78,7 +78,8 @@ func newHTTPHandler() http.Handler { } // Register handlers. - handleFunc("/rolldice", rolldice) + handleFunc("/rolldice/", rolldice) + handleFunc("/rolldice/{player}", rolldice) // Add HTTP instrumentation for the whole server. handler := otelhttp.NewHandler(mux, "/") diff --git a/example/dice/otel.go b/example/dice/otel.go index 15540cf873c..47eb17bc950 100644 --- a/example/dice/otel.go +++ b/example/dice/otel.go @@ -9,9 +9,12 @@ import ( "time" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + "go.opentelemetry.io/otel/log/global" "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/sdk/log" "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/trace" ) @@ -60,6 +63,15 @@ func setupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, er shutdownFuncs = append(shutdownFuncs, meterProvider.Shutdown) otel.SetMeterProvider(meterProvider) + // Set up logger provider. + loggerProvider, err := newLoggerProvider() + if err != nil { + handleErr(err) + return + } + shutdownFuncs = append(shutdownFuncs, loggerProvider.Shutdown) + global.SetLoggerProvider(loggerProvider) + return } @@ -98,3 +110,15 @@ func newMeterProvider() (*metric.MeterProvider, error) { ) return meterProvider, nil } + +func newLoggerProvider() (*log.LoggerProvider, error) { + logExporter, err := stdoutlog.New() + if err != nil { + return nil, err + } + + meterProvider := log.NewLoggerProvider( + log.WithProcessor(log.NewBatchProcessor(logExporter)), + ) + return meterProvider, nil +} diff --git a/example/dice/rolldice.go b/example/dice/rolldice.go index 24ebd1c424b..bdb0ae288e0 100644 --- a/example/dice/rolldice.go +++ b/example/dice/rolldice.go @@ -4,20 +4,29 @@ package main import ( + "fmt" "io" - "log" "math/rand" "net/http" "strconv" + "go.opentelemetry.io/contrib/bridges/otelslog" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/sdk/instrumentation" ) +const name = "rolldice" + var ( - tracer = otel.Tracer("rolldice") - meter = otel.Meter("rolldice") + tracer = otel.Tracer(name) + meter = otel.Meter(name) + logger = otelslog.NewLogger( + otelslog.WithInstrumentationScope(instrumentation.Scope{ + Name: name, + }), + ) rollCnt metric.Int64Counter ) @@ -37,12 +46,20 @@ func rolldice(w http.ResponseWriter, r *http.Request) { roll := 1 + rand.Intn(6) + var msg string + if player := r.PathValue("player"); player != "" { + msg = fmt.Sprintf("%s is rolling the dice", player) + } else { + msg = "Anonymous player is rolling the dice" + } + logger.InfoContext(ctx, msg, "result", roll) + rollValueAttr := attribute.Int("roll.value", roll) span.SetAttributes(rollValueAttr) rollCnt.Add(ctx, 1, metric.WithAttributes(rollValueAttr)) resp := strconv.Itoa(roll) + "\n" if _, err := io.WriteString(w, resp); err != nil { - log.Printf("Write failed: %v\n", err) + logger.ErrorContext(ctx, "Write failed", "error", err) } } From cd9240b256669398d0714bcb9866f12491e88d1d Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Mon, 13 May 2024 11:08:30 -0700 Subject: [PATCH 2/3] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d83413dd081..61db0af8b1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add `RecordFactory` in `go.opentelemetry.io/otel/sdk/log/logtest` to facilitate testing the exporter and processor implementations. (#5258) - Add example for `go.opentelemetry.io/otel/exporters/stdout/stdoutlog`. (#5242) - The count of dropped records from the `BatchProcessor` in `go.opentelemetry.io/otel/sdk/log` is logged. (#5276) +- Add logs to `go.opentelemetry.io/otel/example/dice`. (#5349) ### Changed From 79ebf119a6d41625130d71d412cbfa67335ef324 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Mon, 13 May 2024 11:49:54 -0700 Subject: [PATCH 3/3] Split impl for 1.22 and 1.21 --- example/dice/rolldice.go | 31 ------------------------ example/dice/rolldice_go1.21.go | 35 +++++++++++++++++++++++++++ example/dice/rolldice_go1.22.go | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 example/dice/rolldice_go1.21.go create mode 100644 example/dice/rolldice_go1.22.go diff --git a/example/dice/rolldice.go b/example/dice/rolldice.go index bdb0ae288e0..6206db9bee0 100644 --- a/example/dice/rolldice.go +++ b/example/dice/rolldice.go @@ -4,15 +4,8 @@ package main import ( - "fmt" - "io" - "math/rand" - "net/http" - "strconv" - "go.opentelemetry.io/contrib/bridges/otelslog" "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/sdk/instrumentation" ) @@ -39,27 +32,3 @@ func init() { panic(err) } } - -func rolldice(w http.ResponseWriter, r *http.Request) { - ctx, span := tracer.Start(r.Context(), "roll") - defer span.End() - - roll := 1 + rand.Intn(6) - - var msg string - if player := r.PathValue("player"); player != "" { - msg = fmt.Sprintf("%s is rolling the dice", player) - } else { - msg = "Anonymous player is rolling the dice" - } - logger.InfoContext(ctx, msg, "result", roll) - - rollValueAttr := attribute.Int("roll.value", roll) - span.SetAttributes(rollValueAttr) - rollCnt.Add(ctx, 1, metric.WithAttributes(rollValueAttr)) - - resp := strconv.Itoa(roll) + "\n" - if _, err := io.WriteString(w, resp); err != nil { - logger.ErrorContext(ctx, "Write failed", "error", err) - } -} diff --git a/example/dice/rolldice_go1.21.go b/example/dice/rolldice_go1.21.go new file mode 100644 index 00000000000..5f3b83f35b4 --- /dev/null +++ b/example/dice/rolldice_go1.21.go @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +//go:build !go1.22 +// +build !go1.22 + +package main + +import ( + "io" + "math/rand" + "net/http" + "strconv" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" +) + +func rolldice(w http.ResponseWriter, r *http.Request) { + ctx, span := tracer.Start(r.Context(), "roll") + defer span.End() + + roll := 1 + rand.Intn(6) + + logger.InfoContext(ctx, "Anonymous player is rolling the dice", "result", roll) + + rollValueAttr := attribute.Int("roll.value", roll) + span.SetAttributes(rollValueAttr) + rollCnt.Add(ctx, 1, metric.WithAttributes(rollValueAttr)) + + resp := strconv.Itoa(roll) + "\n" + if _, err := io.WriteString(w, resp); err != nil { + logger.ErrorContext(ctx, "Write failed", "error", err) + } +} diff --git a/example/dice/rolldice_go1.22.go b/example/dice/rolldice_go1.22.go new file mode 100644 index 00000000000..fe964397bc0 --- /dev/null +++ b/example/dice/rolldice_go1.22.go @@ -0,0 +1,42 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +//go:build go1.22 +// +build go1.22 + +package main + +import ( + "fmt" + "io" + "math/rand" + "net/http" + "strconv" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" +) + +func rolldice(w http.ResponseWriter, r *http.Request) { + ctx, span := tracer.Start(r.Context(), "roll") + defer span.End() + + roll := 1 + rand.Intn(6) + + var msg string + if player := r.PathValue("player"); player != "" { + msg = fmt.Sprintf("%s is rolling the dice", player) + } else { + msg = "Anonymous player is rolling the dice" + } + logger.InfoContext(ctx, msg, "result", roll) + + rollValueAttr := attribute.Int("roll.value", roll) + span.SetAttributes(rollValueAttr) + rollCnt.Add(ctx, 1, metric.WithAttributes(rollValueAttr)) + + resp := strconv.Itoa(roll) + "\n" + if _, err := io.WriteString(w, resp); err != nil { + logger.ErrorContext(ctx, "Write failed", "error", err) + } +}