From 21e830e275e7f3f9731057425f493609510601c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Wed, 16 Jul 2025 13:29:59 +0200 Subject: [PATCH] feat: exclude OTEL instrumentation via build tag (#770) * feat: exclude OTEL instrumentation via build tag * add readme --- README.md | 9 +++++++++ internal/otel/otel.go | 12 ++++++++++++ internal/otel/shim.go | 22 ++++++++++++++++++++++ pkg/client/client.go | 2 +- pkg/op/op.go | 4 ++-- 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 internal/otel/otel.go create mode 100644 internal/otel/shim.go diff --git a/README.md b/README.md index bc346f5..b7ac81c 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,15 @@ CLIENT_ID=web CLIENT_SECRET=secret ISSUER=http://oidc.local:9998/ SCOPES="openid > Note: Usernames are suffixed with the hostname (`test-user@localhost` or `test-user@oidc.local`) + +### Build Tags + +The library uses build tags to enable or disable features. The following build tags are available: + +| Build Tag | Description | +|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `no_otel` | Disables the OTel instrumentation, which is enabled by default. This is useful if you do not want to use OTel or if you want to use a different instrumentation library. | + ### Server configuration Example server allows extra configuration using environment variables and could be used for end to diff --git a/internal/otel/otel.go b/internal/otel/otel.go new file mode 100644 index 0000000..21f734a --- /dev/null +++ b/internal/otel/otel.go @@ -0,0 +1,12 @@ +//go:build !no_otel + +package otel + +import ( + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" +) + +func Tracer(name string) trace.Tracer { + return otel.Tracer(name) +} diff --git a/internal/otel/shim.go b/internal/otel/shim.go new file mode 100644 index 0000000..62d3b0c --- /dev/null +++ b/internal/otel/shim.go @@ -0,0 +1,22 @@ +//go:build no_otel + +package otel + +import ( + "context" +) + +type FakeTracer struct{} +type FakeSpan struct{} + +func Tracer(name string) FakeTracer { + return FakeTracer{} +} + +func (t FakeTracer) Start(ctx context.Context, _ string) (context.Context, FakeSpan) { + return ctx, FakeSpan{} +} + +func (s FakeSpan) End() { + +} diff --git a/pkg/client/client.go b/pkg/client/client.go index 56417b5..c9707f3 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -12,7 +12,7 @@ import ( "github.com/go-jose/go-jose/v4" "github.com/zitadel/logging" - "go.opentelemetry.io/otel" + "github.com/zitadel/oidc/v3/internal/otel" "golang.org/x/oauth2" "github.com/zitadel/oidc/v3/pkg/crypto" diff --git a/pkg/op/op.go b/pkg/op/op.go index 58ae838..b761151 100644 --- a/pkg/op/op.go +++ b/pkg/op/op.go @@ -8,10 +8,10 @@ import ( "time" "github.com/go-chi/chi/v5" - jose "github.com/go-jose/go-jose/v4" + "github.com/go-jose/go-jose/v4" "github.com/rs/cors" + "github.com/zitadel/oidc/v3/internal/otel" "github.com/zitadel/schema" - "go.opentelemetry.io/otel" "golang.org/x/text/language" httphelper "github.com/zitadel/oidc/v3/pkg/http"