remove code that propagates errors to parent spans. improve comments

This commit is contained in:
jonny rhea 2026-01-06 12:08:39 -06:00
parent c50d3e6cea
commit 57a536ea0e
2 changed files with 7 additions and 12 deletions

View file

@ -32,7 +32,6 @@ import (
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0" semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
@ -598,7 +597,6 @@ func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMes
// end the span. The function will record errors and set span status based on // end the span. The function will record errors and set span status based on
// the error value. // the error value.
func (h *handler) startSpan(ctx context.Context, msg *jsonrpcMessage, spanName string) (context.Context, func(*error)) { func (h *handler) startSpan(ctx context.Context, msg *jsonrpcMessage, spanName string) (context.Context, func(*error)) {
parentSpan := trace.SpanFromContext(ctx)
ctx, span := h.tracer().Start(ctx, spanName) ctx, span := h.tracer().Start(ctx, spanName)
// Fast path: noop provider or span not sampled // Fast path: noop provider or span not sampled
@ -615,19 +613,10 @@ func (h *handler) startSpan(ctx context.Context, msg *jsonrpcMessage, spanName s
// Define the function to end the span and handle error recording // Define the function to end the span and handle error recording
spanEnd := func(err *error) { spanEnd := func(err *error) {
ro, _ := span.(sdktrace.ReadOnlySpan)
if *err != nil { if *err != nil {
// Error occurred, record it and set status on span and parent // Error occurred, record it and set status on span and parent
span.RecordError(*err) span.RecordError(*err)
span.SetStatus(codes.Error, (*err).Error()) span.SetStatus(codes.Error, (*err).Error())
parentSpan.SetStatus(codes.Error, (*err).Error())
} else if ro.Status().Code == codes.Error {
// Span's child had an error, propagate it to parent
// Note: Span's status was already set in the child
parentSpan.SetStatus(codes.Error, ro.Status().Description)
} else if ro.Status().Code == codes.Unset {
// No error and no status set, mark as success
span.SetStatus(codes.Ok, "")
} }
span.End() span.End()
} }
@ -654,7 +643,9 @@ func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *cal
// If parent span is recording, start a span for the response. // If parent span is recording, start a span for the response.
// Note: This prevents msg.response spans from being created when // Note: This prevents msg.response spans from being created when
// the parent span is not recording (e.g. subscription tracing disabled). // the parent span is not recording. This is needed bc we aren't
// currently recording spans for subscriptions, but we do record spans
// for other RPC methods and this is called for both.
parentSpan := trace.SpanFromContext(ctx) parentSpan := trace.SpanFromContext(ctx)
if parentSpan.IsRecording() { if parentSpan.IsRecording() {
_, spanEnd := h.startSpan(ctx, msg, "rpc.msg.response") _, spanEnd := h.startSpan(ctx, msg, "rpc.msg.response")

View file

@ -102,6 +102,10 @@ func (s *Server) SetWebsocketReadLimit(limit int64) {
} }
// SetTracerProvider configures the OpenTelemetry TracerProvider for RPC call tracing. // SetTracerProvider configures the OpenTelemetry TracerProvider for RPC call tracing.
// Note: This method (and the TracerProvider field in the Server/Handler struct) is
// primarily intended for testing. In particular, it allows tests to configure an
// isolated TracerProvider without changing the global provider, avoiding
// interference between tests running in parallel.
func (s *Server) SetTracerProvider(tp trace.TracerProvider) { func (s *Server) SetTracerProvider(tp trace.TracerProvider) {
s.tracerProvider = tp s.tracerProvider = tp
} }