Skip to content

Commit

Permalink
Fix missing requestID due to empty traceID
Browse files Browse the repository at this point in the history
  • Loading branch information
Kleonikos Kyriakis committed May 17, 2024
1 parent 8b583f5 commit 39323bf
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
8 changes: 5 additions & 3 deletions internal/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"log"
"net"

"github.com/chain4travel/camino-messenger-bot/internal/tracing"

"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc"
"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v1alpha/bookv1alphagrpc"
bookv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/book/v1alpha"
Expand Down Expand Up @@ -59,7 +61,7 @@ type server struct {
grpcServer *grpc.Server
cfg *config.RPCServerConfig
logger *zap.SugaredLogger
tracer trace.Tracer
tracer tracing.Tracer
processor messaging.Processor
serviceRegistry *messaging.ServiceRegistry
}
Expand All @@ -68,7 +70,7 @@ func (s *server) Checkpoint() string {
return "request-gateway"
}

func NewServer(cfg *config.RPCServerConfig, logger *zap.SugaredLogger, tracer trace.Tracer, processor messaging.Processor, serviceRegistry *messaging.ServiceRegistry) *server {
func NewServer(cfg *config.RPCServerConfig, logger *zap.SugaredLogger, tracer tracing.Tracer, processor messaging.Processor, serviceRegistry *messaging.ServiceRegistry) *server {
var opts []grpc.ServerOption
if cfg.Unencrypted {
logger.Warn("Running gRPC server without TLS!")
Expand Down Expand Up @@ -182,7 +184,7 @@ func (s *server) processInternalRequest(ctx context.Context, requestType messagi
func (s *server) processExternalRequest(ctx context.Context, requestType messaging.MessageType, request *messaging.RequestContent) (messaging.ResponseContent, error) {
ctx, span := s.tracer.Start(ctx, "server.processExternalRequest", trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
err, md := s.processMetadata(ctx, span.SpanContext().TraceID())
err, md := s.processMetadata(ctx, s.tracer.TraceIDForSpan(span))
if err != nil {
return messaging.ResponseContent{}, fmt.Errorf("error processing metadata: %v", err)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/tracing/nooptracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package tracing

import (
"context"
"crypto/rand"

"go.opentelemetry.io/otel/trace"
)

Expand All @@ -19,9 +21,18 @@ type noopTracer struct {
func NewNoOpTracer() (Tracer, error) {
return &noopTracer{tp: trace.NewNoopTracerProvider()}, nil
}

func (n *noopTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return n.tp.Tracer("").Start(ctx, spanName, opts...)
}

func (n *noopTracer) Shutdown() error {
return nil // nothing to do here
}

// TraceIDForSpan returns a random trace ID in tha case of noopTracer. A non-empty trace ID is required for the span to be exported.
func (n *noopTracer) TraceIDForSpan(trace.Span) trace.TraceID {
traceID := trace.TraceID{}
rand.Read(traceID[:])
return traceID
}
24 changes: 24 additions & 0 deletions internal/tracing/nooptracer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024, Chain4Travel AG. All rights reserved.
* See the file LICENSE for licensing terms.
*/

package tracing

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/trace"
)

func TestNoopTraceIDForSpan(t *testing.T) {
tracer, err := NewNoOpTracer()
require.NoError(t, err)
_, span := tracer.Start(context.Background(), "test")
got := tracer.TraceIDForSpan(span)
emptyTraceID := trace.TraceID{}
require.NotEqual(t, emptyTraceID, got)
}
8 changes: 7 additions & 1 deletion internal/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ package tracing

import (
"context"
"time"

"github.com/chain4travel/camino-messenger-bot/config"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"time"
)

const tracerProviderShutdownTimeout = exportTimeout + 5*time.Second
Expand All @@ -22,6 +23,7 @@ var _ Tracer = (*tracer)(nil)

type Tracer interface {
trace.Tracer
TraceIDForSpan(trace.Span) trace.TraceID // TraceIDForSpan returns the trace ID of the given span.
Shutdown() error
}

Expand All @@ -38,6 +40,10 @@ func (t *tracer) Shutdown() error {
return t.tp.Shutdown(ctx)
}

func (t *tracer) TraceIDForSpan(span trace.Span) trace.TraceID {
return span.SpanContext().TraceID()
}

func NewTracer(tracingConfig *config.TracingConfig, name string) (Tracer, error) {
exporter, err := newExporter(tracingConfig)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions internal/tracing/tracer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2024, Chain4Travel AG. All rights reserved.
* See the file LICENSE for licensing terms.
*/

package tracing

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestTraceIDForSpan(t *testing.T) {
tracer, err := NewNoOpTracer()
require.NoError(t, err)
_, span := tracer.Start(context.Background(), "test")
got := tracer.TraceIDForSpan(span)
require.NotEqual(t, span.SpanContext().TraceID(), got)
}

0 comments on commit 39323bf

Please sign in to comment.