google.golang.org/grpc@v1.74.2/stats/opentelemetry/server_tracing.go (about)

     1  /*
     2   * Copyright 2024 gRPC authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package opentelemetry
    18  
    19  import (
    20  	"context"
    21  	"log"
    22  	"strings"
    23  
    24  	"go.opentelemetry.io/otel/trace"
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/grpc/stats"
    27  	otelinternaltracing "google.golang.org/grpc/stats/opentelemetry/internal/tracing"
    28  )
    29  
    30  type serverTracingHandler struct {
    31  	options Options
    32  }
    33  
    34  func (h *serverTracingHandler) initializeTraces() {
    35  	if h.options.TraceOptions.TracerProvider == nil {
    36  		log.Printf("TracerProvider is not provided in server TraceOptions")
    37  		return
    38  	}
    39  }
    40  
    41  // TagRPC implements per RPC attempt context management for traces.
    42  func (h *serverTracingHandler) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context {
    43  	ctx, ai := getOrCreateRPCAttemptInfo(ctx)
    44  	ctx, ai = h.traceTagRPC(ctx, ai)
    45  	return setRPCInfo(ctx, &rpcInfo{ai: ai})
    46  }
    47  
    48  // traceTagRPC populates context with new span data using the TextMapPropagator
    49  // supplied in trace options and internal itracing.Carrier. It creates a new
    50  // incoming carrier which extracts an existing span context (if present) by
    51  // deserializing from provided context. If valid span context is extracted, it
    52  // is set as parent of the new span otherwise new span remains the root span.
    53  // If TextMapPropagator is not provided in the trace options, it returns context
    54  // as is.
    55  func (h *serverTracingHandler) traceTagRPC(ctx context.Context, ai *attemptInfo) (context.Context, *attemptInfo) {
    56  	mn := "Recv." + strings.Replace(ai.method, "/", ".", -1)
    57  	var span trace.Span
    58  	tracer := h.options.TraceOptions.TracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(grpc.Version))
    59  	ctx = h.options.TraceOptions.TextMapPropagator.Extract(ctx, otelinternaltracing.NewIncomingCarrier(ctx))
    60  	// If the context.Context provided in `ctx` to tracer.Start(), contains a
    61  	// span then the newly-created Span will be a child of that span,
    62  	// otherwise it will be a root span.
    63  	ctx, span = tracer.Start(ctx, mn, trace.WithSpanKind(trace.SpanKindServer))
    64  	ai.traceSpan = span
    65  	return ctx, ai
    66  }
    67  
    68  // HandleRPC handles per RPC tracing implementation.
    69  func (h *serverTracingHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
    70  	ri := getRPCInfo(ctx)
    71  	if ri == nil {
    72  		logger.Error("ctx passed into server side tracing handler trace event handling has no server call data present")
    73  		return
    74  	}
    75  	populateSpan(rs, ri.ai)
    76  }
    77  
    78  // TagConn exists to satisfy stats.Handler for tracing.
    79  func (h *serverTracingHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
    80  	return ctx
    81  }
    82  
    83  // HandleConn exists to satisfy stats.Handler for tracing.
    84  func (h *serverTracingHandler) HandleConn(context.Context, stats.ConnStats) {}