From 4892335cca72e213fc19d7c189685635c7e76723 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Mon, 30 Oct 2023 10:39:51 +0100 Subject: [PATCH] feat: adding otel --- README.md | 15 +++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ middleware.go | 17 +++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 91628bd..8175034 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,21 @@ e.Logger.Fatal(e.Start(":4242")) // time=2023-04-10T14:00:00Z level=INFO msg="Success" status=200 method=GET path=/ route=/ ip=::1 latency=25.958µs user-agent=curl/7.77.0 time=2023-04-10T14:00:00Z request-id=229c7fc8-64f5-4467-bc4a-940700503b0d ``` +### OTEL + +```go +logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + +config := sloggin.Config{ + WithSpanID: true, + WithTraceID: true, +} + +e := echo.New() +e.Use(slogecho.NewWithConfig(logger, config)) +e.Use(middleware.Recover()) +``` + ### Verbose ```go diff --git a/go.mod b/go.mod index ba68c0b..f96e899 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,8 @@ require ( github.com/mattn/go-isatty v0.0.19 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect + go.opentelemetry.io/otel v1.19.0 // indirect + go.opentelemetry.io/otel/trace v1.19.0 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect golang.org/x/net v0.17.0 // indirect diff --git a/go.sum b/go.sum index ebfb2c5..f1667a2 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,10 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs= +go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY= +go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg= +go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= diff --git a/middleware.go b/middleware.go index 106f10a..cd317b2 100644 --- a/middleware.go +++ b/middleware.go @@ -12,6 +12,7 @@ import ( "github.com/labstack/echo/v4" "github.com/samber/lo" + "go.opentelemetry.io/otel/trace" ) const ( @@ -45,6 +46,8 @@ type Config struct { WithRequestHeader bool WithResponseBody bool WithResponseHeader bool + WithSpanID bool + WithTraceID bool Filters []Filter } @@ -64,6 +67,8 @@ func New(logger *slog.Logger) echo.MiddlewareFunc { WithRequestHeader: false, WithResponseBody: false, WithResponseHeader: false, + WithSpanID: false, + WithTraceID: false, Filters: []Filter{}, }) @@ -84,6 +89,8 @@ func NewWithFilters(logger *slog.Logger, filters ...Filter) echo.MiddlewareFunc WithRequestHeader: false, WithResponseBody: false, WithResponseHeader: false, + WithSpanID: false, + WithTraceID: false, Filters: filters, }) @@ -169,6 +176,16 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc { } } + // otel + if config.WithTraceID { + traceID := trace.SpanFromContext(c.Request().Context()).SpanContext().TraceID().String() + attributes = append(attributes, slog.String("trace-id", traceID)) + } + if config.WithSpanID { + spanID := trace.SpanFromContext(c.Request().Context()).SpanContext().SpanID().String() + attributes = append(attributes, slog.String("span-id", spanID)) + } + // request if config.WithRequestBody { attributes = append(attributes, slog.Group("request", slog.String("body", string(reqBody))))