Skip to content

(Java): Add OpenTelemetry existing parent span support #5061

@currantw

Description

@currantw

Describe the feature

Add support for attaching Valkey GLIDE operations to existing OpenTelemetry spans in the Java client, similar to the SpanFromContext functionality implemented for the Go client.

The Go client gained the ability to attach Valkey GLIDE operations as child spans to existing traces through #4243. This allows for proper parent-child span relationships (e.g., for an existing request trace) and improved distributed tracing visibility.

The Java client currently lacks equivalent functionality: there's no way to extract active span context from the current execution context and set existing spans as parents for Valkey operations.

Use Case

This enables end-to-end distributed tracing where:

  1. Application creates a parent span for a request
  2. Valkey operations automatically become child spans
  3. Full request flow is visible in tracing systems

Current behaviour: Valkey GLIDE operations create independent spans.
Desired behaviour: Valkey GLIDE operations appear as child spans under the application's active span.

Proposed Solution

From a client perspective, this could look something like:

import io.opentelemetry.api.trace.Span;
import glide.api.OpenTelemetry;

OpenTelemetry.OpenTelemetryConfig config = 
    OpenTelemetry.OpenTelemetryConfig.builder()
        .traces(OpenTelemetry.TracesConfig.builder()
            .endpoint("http://localhost:4318/v1/traces")
            .spanFromContext(() -> {
                Span span = Span.current();
                if (span != null && span.getSpanContext().isValid()) {
                    return span.getSpanContext();
                }
                return null;
            })
            .build())
        .build();

OpenTelemetry.init(config);

Implementation approach:

  1. Add spanFromContext callback to TracesConfig.Builder
  2. Expose create_otel_span_with_parent from FFI layer via JNI as
    OpenTelemetryResolver.createLeakedOtelSpanWithParent(String spanName, long parentSpanPtr)
  3. Extract Java OpenTelemetry SDK's active span context and convert to GLIDE span pointer format
  4. Update CommandManager.prepareCommandRequest() methods to pass parent span pointer when creating operation spans

Other Information

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Client version used

2.2

Environment details (OS name and version, etc.)

Mac

Metadata

Metadata

Assignees

No one assigned

    Labels

    Task 🔧Implementation work (not a bug)Telemetry 📺java ☕issues and fixes related to the java client

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions