Documentation ¶
Overview ¶
Package ddzerolog provides a log/span correlation func for the github.com/rs/zerolog.
Example ¶
package main import ( "context" "os" "github.com/johejo/ddzerolog" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" ) func main() { l := zerolog.New(os.Stdout) ctx := context.Background() span, ctx := tracer.StartSpanFromContext(ctx, "testSpan") defer span.Finish() l.UpdateContext(ddzerolog.UpdateContext(ctx)) ctx = l.WithContext(ctx) log.Ctx(ctx).Info().Msg("test") }
Output: {"level":"info","dd.trace_id":0,"dd.span_id":0,"message":"test"}
Example (Nethttp) ¶
package main import ( "net/http" "net/http/httptest" "os" "github.com/johejo/ddzerolog" "github.com/rs/zerolog" "github.com/rs/zerolog/log" httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http" ) func main() { mux := http.NewServeMux() log.Logger = zerolog.New(os.Stdout) mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() log.Ctx(ctx).Info().Msg("hello") w.Write([]byte("hello\n")) }) middleware := func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() l := log.Logger l.UpdateContext(ddzerolog.UpdateContext(ctx)) ctx = l.WithContext(ctx) next.ServeHTTP(w, r.WithContext(ctx)) }) } handler := httptrace.WrapHandler(middleware(mux), "myService", "myResource") s := httptest.NewServer(handler) defer s.Close() _, err := http.Get(s.URL + "/hello") if err != nil { panic(err) } }
Output: {"level":"info","dd.trace_id":0,"dd.span_id":0,"message":"hello"}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.