From 16e4fcb704d4ed3a8f84c590167270b46a500665 Mon Sep 17 00:00:00 2001 From: ucpr Date: Sat, 13 Jul 2024 15:59:38 +0900 Subject: [PATCH] chore: support otel logging --- go.mod | 1 + go.sum | 2 ++ pkg/log/log.go | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/go.mod b/go.mod index 789d88b..5533f89 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/google/wire v0.5.0 github.com/hamba/avro/v2 v2.18.0 github.com/prometheus/client_golang v1.17.0 + github.com/remychantenay/slog-otel v1.3.2 github.com/sethvargo/go-envconfig v1.0.1 github.com/stretchr/testify v1.9.0 go.mongodb.org/mongo-driver v1.13.0 diff --git a/go.sum b/go.sum index 2949cad..50398cc 100644 --- a/go.sum +++ b/go.sum @@ -127,6 +127,8 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI= github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= +github.com/remychantenay/slog-otel v1.3.2 h1:ZBx8qnwfLJ6e18Vba4e9Xp9B7khTmpIwFsU1sAmActw= +github.com/remychantenay/slog-otel v1.3.2/go.mod h1:gKW4tQ8cGOKoA+bi7wtYba/tcJ6Tc9XyQ/EW8gHA/2E= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= diff --git a/pkg/log/log.go b/pkg/log/log.go index 1fa94c3..464bfef 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -7,6 +7,8 @@ import ( "os" "runtime" "strings" + + slogotel "github.com/remychantenay/slog-otel" ) //nolint:gochecknoglobals @@ -30,6 +32,8 @@ const ( formatJSON format = "json" // formatText is the text format. formatText format = "text" + // formatOtel is the OpenTelemetry format. + formatOtel format = "otel" ) // logger is the global logger. @@ -78,6 +82,12 @@ func newHandler(opts newHandlerOptions) slog.Handler { Level: SeverityInfo, ReplaceAttr: attrReplacerForDefault, }) + case formatOtel: + return slogotel.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + AddSource: true, + Level: SeverityInfo, + ReplaceAttr: attrReplacerForDefault, + })) } return slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{