dex icon indicating copy to clipboard operation
dex copied to clipboard

feat(otel_traces): Integrate OpenTelemetry tracing and metrics support.

Open jensholdgaard opened this issue 1 year ago • 6 comments

Overview

This PR adds comprehensive OpenTelemetry tracing infrastructure to Dex, enabling distributed tracing capabilities for monitoring and debugging authentication flows, token operations, and connector interactions.

What this PR does / why we need it

What it does:

  • Adds OpenTelemetry traces attributes and events specifically designed for Dex operations

  • Implements instrumentation scope utilities for consistent tracing across the codebase

  • Integrates OTEL configuration and initialization into the main Dex server () cmd/dex

  • Provides comprehensive test coverage for all OTEL components

  • Instruments key handlers including:

    • Public keys handlers () publickeyshandlers.go
    • Password login handlers () passwordloginhandlers.go
    • Introspection handlers () introspectionhandlers.go
    • Core server handlers () handlers.go
  • Adds tracing to connectors (mock connector as example)

  • Includes tracing utilities in the package pkg/otel/traces/utils

Why we need it:

  • Observability: Provides detailed insights into authentication flows and token operations
  • Debugging: Enables tracing of complex authentication scenarios across distributed systems
  • Performance Monitoring: Allows identification of bottlenecks in authentication processes
  • Compliance: Helps with audit trails and security monitoring
  • Operational Excellence: Improves troubleshooting capabilities for production deployments

This foundational tracing infrastructure will significantly improve the observability of Dex deployments, making it easier to:

  • Debug authentication issues
  • Monitor performance across connectors
  • Track token lifecycle operations
  • Identify slow or failing authentication flows

Special notes for your reviewer

  • The implementation uses OpenTelemetry's standard Go SDK
  • Tracing is implemented non-intrusively using context propagation
  • All instrumentation follows OpenTelemetry semantic conventions
  • The InstrumentHandler utility automatically extracts context from HTTP requests
  • Test coverage includes both unit tests and integration scenarios
  • The implementation is backward compatible - tracing is optional and doesn't affect existing functionality
  • Configuration for OTEL can be provided through standard OpenTelemetry environment variables

Testing:

  • All new tracing utilities have comprehensive test coverage
  • Mock connectors demonstrate proper tracing integration patterns
  • Handler instrumentation has been tested with existing test suites

Performance:

  • Minimal overhead when tracing is disabled
  • Efficient span creation and context propagation
  • No impact on existing authentication flows

This forms the foundation for future observability enhancements and can be extended to include metrics and logging correlation.

jensholdgaard avatar Jul 03 '25 10:07 jensholdgaard

This pr is awaiting Ent to use the github.com/olekukonko/tablewriter v1.0.8 in pr

jensholdgaard avatar Jul 22 '25 13:07 jensholdgaard

Any that has time to take a look at this PR or is it simply too big?

jensholdgaard avatar Jul 31 '25 11:07 jensholdgaard

Hey @jensholdgaard !

I really appreciate the effort you've put into this!

You are right, the PR is indeed huge and it's not easy to review.

There are also conflicts now due to merging a couple other PRs.

I wonder if there is a way to add changes incrementally, so we can review them in chunks.

Also, it seems to me there are significant refactors in the PR, unrelated to the actual feature (like moving some code around).

I'd suggest moving those changes to separate PRs. We can get those reviewed with a shorter turnaround, given those changes tend to conflict with other PRs.

I'd love to OTEL added to Dex, so I'd be happy to work with you to get this merged...in smaller pieces.

WDYT?

sagikazarmark avatar Aug 18 '25 13:08 sagikazarmark

@sagikazarmark i totally agree, this was also more or less to see what a complete trace instrumentation could look like. Indeed i think we could start off with a simple trace instrumentation of a single endpoint as a start. Like you also mentioned there is some refactoring going on too, which was more of a way for me to get a more holistic view of the system. Not strictly needed. Thank you for the kind words aswell, this was also a really great chance for me to deep dive into manual instrumentation.

jensholdgaard avatar Aug 19 '25 06:08 jensholdgaard

@sagikazarmark @jensholdgaard any plans to revive this? We have a working version in our fork focused on using otelhttp, sql for pgx (we'll drop it from this diff) and supporting manual instrumentation interfaces with a much smaller diff:

 cmd/dex/config.go              |   3 +++
 cmd/dex/serve.go               |  25 +++++++++++++++++++++
 go.mod                         |  26 ++++++++++++++--------
 go.sum                         |  35 +++++++++++++++++++++++++++++
 pkg/tracing/attributes.go      |  34 ++++++++++++++++++++++++++++
 pkg/tracing/config.go          |  48 ++++++++++++++++++++++++++++++++++++++++
 pkg/tracing/noop.go            |  49 +++++++++++++++++++++++++++++++++++++++++
 pkg/tracing/otel.go            | 240 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pkg/tracing/tracing.go         | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 server/deviceflowhandlers.go   |   6 ++++-
 server/handlers.go             |  75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 server/introspectionhandler.go |   5 ++++-
 server/refreshhandlers.go      |   7 ++++++
 server/server.go               |  51 +++++++++++++++++++++++++++++++++++++++++-
 storage/sql/config.go          |  20 +++++++++++++----
 15 files changed, 733 insertions(+), 23 deletions(-)
``

marwanad avatar Jan 05 '26 15:01 marwanad

@sagikazarmark @jensholdgaard any plans to revive this? We have a working version in our fork focused on using otelhttp, sql for pgx (we'll drop it from this diff) and supporting manual instrumentation interfaces with a much smaller diff:

 cmd/dex/config.go              |   3 +++
 cmd/dex/serve.go               |  25 +++++++++++++++++++++
 go.mod                         |  26 ++++++++++++++--------
 go.sum                         |  35 +++++++++++++++++++++++++++++
 pkg/tracing/attributes.go      |  34 ++++++++++++++++++++++++++++
 pkg/tracing/config.go          |  48 ++++++++++++++++++++++++++++++++++++++++
 pkg/tracing/noop.go            |  49 +++++++++++++++++++++++++++++++++++++++++
 pkg/tracing/otel.go            | 240 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pkg/tracing/tracing.go         | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 server/deviceflowhandlers.go   |   6 ++++-
 server/handlers.go             |  75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 server/introspectionhandler.go |   5 ++++-
 server/refreshhandlers.go      |   7 ++++++
 server/server.go               |  51 +++++++++++++++++++++++++++++++++++++++++-
 storage/sql/config.go          |  20 +++++++++++++----
 15 files changed, 733 insertions(+), 23 deletions(-)
``

Sounds like a good idea, i am happy to review some i just got totally caught up in work so my open source commitment dropped to a zero

jensholdgaard avatar Jan 12 '26 06:01 jensholdgaard