stefanprodan / podinfo

Go microservice template for Kubernetes
Apache License 2.0
5.35k stars 1.7k forks source link

feat: add logger to context and logging erorr #342

Open toyamagu-2021 opened 6 months ago

toyamagu-2021 commented 6 months ago

Signed-off-by: toyamagu-2021 toyamagu2021@gmail.com

Motivation

Modification

Verification

Sample logs:

$ ./bin/podinfo --random-error=true --level debug
{"level":"debug","ts":"2024-02-26T02:34:06.399+0900","caller":"http/logging.go:29","msg":"request started","proto":"HTTP/1.1","uri":"/","method":"GET","remote":"127.0.0.1:54902","user-agent":"curl/7.81.0"}
{"level":"error","ts":"2024-02-26T02:34:06.399+0900","caller":"http/http.go:22","msg":"random error occurred","proto":"HTTP/1.1","uri":"/","method":"GET","remote":"127.0.0.1:54902","user-agent":"curl/7.81.0","stacktrace":"github.com/stefanprodan/podinfo/pkg/api/http.randomErrorMiddleware.func1\n\t/home/toyamagu/github.com/toyamagu-2021/podinfo/pkg/api/http/http.go:22\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2136\ngithub.com/stefanprodan/podinfo/pkg/api/http.versionMiddleware.func1\n\t/home/toyamagu/github.com/toyamagu-2021/podinfo/pkg/api/http/http.go:36\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2136\ngithub.com/stefanprodan/podinfo/pkg/api/http.(*LoggingMiddleware).Handler-fm.(*LoggingMiddleware).Handler.func1\n\t/home/toyamagu/github.com/toyamagu-2021/podinfo/pkg/api/http/logging.go:30\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2136\ngo.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux.traceware.ServeHTTP\n\t/home/toyamagu/go/pkg/mod/go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux@v0.46.1/mux.go:180\ngithub.com/stefanprodan/podinfo/pkg/api/http.(*PrometheusMiddleware).Handler-fm.(*PrometheusMiddleware).Handler.func1\n\t/home/toyamagu/github.com/toyamagu-2021/podinfo/pkg/api/http/metrics.go:62\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2136\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/toyamagu/go/pkg/mod/github.com/gorilla/mux@v1.8.1/mux.go:212\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2938\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:2009"}