Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🚨 slog: Datadog handler

License

NotificationsYou must be signed in to change notification settings

samber/slog-datadog

Repository files navigation

tagGo VersionGoDocBuild StatusGo reportCoverageContributorsLicense

ADatadog Handler forslog Go library.

See also:

HTTP middlewares:

Loggers:

Log sinks:

🚀 Install

go get github.com/samber/slog-datadog/v2

Compatibility: go >= 1.21

No breaking changes will be made to exported APIs before v3.0.0.

💡 Usage

GoDoc:https://pkg.go.dev/github.com/samber/slog-datadog/v2

Handler options

typeOptionstruct {// log level (default: debug)Level slog.Leveler// datadog endpointClient*datadog.APIClientContext context.ContextTimeout time.Duration// default: 10s// source parametersServicestringHostnamestringGlobalTagsmap[string]string// optional: customize Datadog message builderConverterConverter// optional: custom marshalerMarshalerfunc(vany) ([]byte,error)// optional: fetch attributes from contextAttrFromContext []func(ctx context.Context) []slog.Attr// optional: see slog.HandlerOptionsAddSourceboolReplaceAttrfunc(groups []string,a slog.Attr) slog.Attr}

Attributes will be injected in log payload.

Other global parameters:

slogdatadog.SourceKey="source"slogdatadog.ErrorKeys= []string{"error","err"}

Example

import ("github.com/DataDog/datadog-api-client-go/v2/api/datadog"slogdatadog"github.com/samber/slog-datadog/v2""log/slog")funcnewDatadogClient(endpointstring,apiKeystring) (*datadog.APIClient, context.Context) {ctx:=datadog.NewDefaultContext(context.Background())ctx=context.WithValue(ctx,datadog.ContextAPIKeys,map[string]datadog.APIKey{"apiKeyAuth": {Key:apiKey}},)ctx=context.WithValue(ctx,datadog.ContextServerVariables,map[string]string{"site":endpoint},)configuration:=datadog.NewConfiguration()apiClient:=datadog.NewAPIClient(configuration)returnapiClient,ctx}funcmain() {host:="1.2.3.4"service:="api"endpoint:=slogdatadog.DatadogHostEUapiKey:="xxx"apiClient,ctx:=newDatadogClient(endpoint,apiKey)logger:=slog.New(slogdatadog.Option{Level:slog.LevelDebug,Client:apiClient,Context:ctx,Timeout:5*time.Second,Hostname:host,Service:service}.NewDatadogHandler())logger=logger.With("environment","dev").With("release","v1.0.0")// log errorlogger.With("category","sql").With("query.statement","SELECT COUNT(*) FROM users;").With("query.duration",1*time.Second).With("error",fmt.Errorf("could not count users")).Error("caramba!")// log user signuplogger.With(slog.Group("user",slog.String("id","user-123"),slog.Time("created_at",time.Now()),),).Info("user registration")}

Tracing

Import the samber/slog-otel library.

import (slogdatadog"github.com/samber/slog-datadog"slogotel"github.com/samber/slog-otel""go.opentelemetry.io/otel/sdk/trace")funcmain() {tp:=trace.NewTracerProvider(trace.WithSampler(trace.AlwaysSample()),)tracer:=tp.Tracer("hello/world")ctx,span:=tracer.Start(context.Background(),"foo")deferspan.End()span.AddEvent("bar")logger:=slog.New(slogdatadog.Option{// ...AttrFromContext: []func(ctx context.Context) []slog.Attr{slogotel.ExtractOtelAttrFromContext([]string{"tracing"},"trace_id","span_id"),},}.NewDatadogHandler(),)logger.ErrorContext(ctx,"a message")}

🤝 Contributing

Don't hesitate ;)

# Install some dev dependenciesmake tools# Run testsmaketest# ormake watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2023Samuel Berthe.

This project isMIT licensed.


[8]ページ先頭

©2009-2025 Movatter.jp