feat: Add W3C Trace Context support for distributed tracing#874
feat: Add W3C Trace Context support for distributed tracing#874jadewang-db wants to merge 2 commits into
Conversation
## Summary This PR adds W3C Trace Context (traceparent/tracestate) header support to the Databricks JDBC driver, enabling distributed tracing across service boundaries. The implementation follows the [W3C Trace Context specification](https://www.w3.org/TR/trace-context/) and allows trace context propagation for better observability. ## Changes ### Core Implementation - **TracingUtil**: Added utility class for W3C trace context operations - Validates and parses traceparent headers - Generates trace IDs and span IDs - Builds W3C-compliant traceparent headers - **Connection Context**: Enhanced to store trace context at connection scope - Added `TraceParent` and `TraceState` connection parameters - Stores extracted/generated trace ID and flags per connection - Initializes trace context when connection is created - **HTTP Headers**: Added traceparent/tracestate headers to all HTTP requests - DatabricksHttpClient (volume operations, telemetry) - Thrift transport (Thrift protocol requests) - SDK client (SQL execution API requests) ### Configuration New connection parameters: - `EnableRequestTracing=1` - Enable trace context headers (existing, now enhanced) - `TraceParent=<header>` - Provide external W3C traceparent header - `TraceState=<header>` - Provide vendor-specific trace state ### Design Decisions - **Connection-scoped**: Trace context is scoped to connection lifetime, not thread-local - Avoids issues with connection pooling and async operations - Context follows the connection across thread boundaries - **Stateless utility**: TracingUtil is a pure utility class with no state - **Backward compatible**: Existing `EnableRequestTracing` behavior preserved ## Usage Examples ```java // Auto-generate trace context jdbc:databricks://host:port/db;EnableRequestTracing=1 // Propagate existing trace context jdbc:databricks://host:port/db;EnableRequestTracing=1;TraceParent=00-4bf92f3577b34da6a3ce929d 0e0e4736-00f067aa0ba902b7-01 // With trace state jdbc:databricks://host:port/db;EnableRequestTracing=1;TraceParent=00-4bf92f3577b34da6a3ce929d 0e0e4736-00f067aa0ba902b7-01;TraceState=vendor1=value1 Testing - Added comprehensive unit tests for TracingUtil - Tests cover W3C format validation, ID generation, and header building - All existing tests pass Impact - Enables distributed tracing for Databricks JDBC applications - Helps debug and monitor cross-service request flows - No impact when tracing is disabled (default)
|
Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes. |
| // Generate new trace context | ||
| traceId = TracingUtil.generateTraceId(); | ||
| traceFlags = "01"; // sampled by default | ||
| if (!isNullOrEmpty(traceParent)) { |
There was a problem hiding this comment.
This is nullOrEmpty else-case, why are we checking again?
|
|
||
| // Add tracestate if present | ||
| String traceState = connectionContext.getTraceState(); | ||
| if (traceState != null && !traceState.isEmpty()) { |
There was a problem hiding this comment.
nit: can use Strings.isNullOrEmpty
| if (connectionContext.isRequestTracingEnabled()) { | ||
| String traceHeader = TracingUtil.getTraceHeader(); | ||
| if (connectionContext.isRequestTracingEnabled() && connectionContext.getTraceId() != null) { | ||
| String traceHeader = |
There was a problem hiding this comment.
shall we check for empty also?
|
|
||
| // Add tracestate if present | ||
| String traceState = connectionContext.getTraceState(); | ||
| if (traceState != null && !traceState.isEmpty()) { |
There was a problem hiding this comment.
can use !Strings.isNullOrEmpty
| } | ||
|
|
||
| // Add W3C Trace Context headers if request tracing is enabled | ||
| if (connectionContext.isRequestTracingEnabled() && connectionContext.getTraceId() != null) { |
|
This PR has been marked as Stale because it has been open for 30 days with no activity. If you would like the PR to remain open, please remove the stale label or comment on the PR. |
|
This PR was closed because it has been inactive for 7 days since being marked as stale. |
Summary
This PR adds W3C Trace Context (traceparent/tracestate) header support to the Databricks JDBC
driver, enabling distributed tracing across service boundaries. The implementation follows
the W3C Trace Context specification and allows trace
context propagation for better observability.
Changes
Core Implementation
TracingUtil: Added utility class for W3C trace context operations
Connection Context: Enhanced to store trace context at connection scope
TraceParentandTraceStateconnection parametersHTTP Headers: Added traceparent/tracestate headers to all HTTP requests
Configuration
New connection parameters:
EnableRequestTracing=1- Enable trace context headers (existing, now enhanced)TraceParent=<header>- Provide external W3C traceparent headerTraceState=<header>- Provide vendor-specific trace stateDesign Decisions
EnableRequestTracingbehavior preservedUsage Examples