github.com/thanos-io/thanos@v0.32.5/pkg/tracing/google_cloud/google_cloud_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  // This file includes unit tests that test only tiny logic in this package, but are here mainly as a showcase on how tracing can
     5  // be configured.
     6  
     7  package google_cloud
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"github.com/go-kit/log"
    14  	"github.com/opentracing/opentracing-go"
    15  	"github.com/thanos-io/thanos/pkg/testutil/custom"
    16  	"github.com/thanos-io/thanos/pkg/tracing"
    17  	"github.com/thanos-io/thanos/pkg/tracing/migration"
    18  	tracesdk "go.opentelemetry.io/otel/sdk/trace"
    19  	"go.opentelemetry.io/otel/sdk/trace/tracetest"
    20  )
    21  
    22  func TestMain(m *testing.M) {
    23  	custom.TolerantVerifyLeakMain(m)
    24  }
    25  
    26  // This test shows that if sample factor will enable tracing on client process, even when it would be disabled on server
    27  // it will be still enabled for all spans within this span.
    28  func TestContextTracing_ClientEnablesTracing(t *testing.T) {
    29  	exp := tracetest.NewInMemoryExporter()
    30  	tracerOtel := newTracerProvider(
    31  		context.Background(),
    32  		log.NewNopLogger(),
    33  		tracesdk.NewSimpleSpanProcessor(exp),
    34  		1, // always sample
    35  		"gcloud-test-client",
    36  	)
    37  	tracer, _ := migration.Bridge(tracerOtel, log.NewNopLogger())
    38  
    39  	clientRoot, clientCtx := tracing.StartSpan(tracing.ContextWithTracer(context.Background(), tracer), "a")
    40  
    41  	// Simulate Server process with different tracer, but with client span in context.
    42  	srvTracerOtel := newTracerProvider(
    43  		context.Background(),
    44  		log.NewNopLogger(),
    45  		tracesdk.NewSimpleSpanProcessor(exp),
    46  		0, // never sample
    47  		"gcloud-test-server",
    48  	)
    49  	srvTracer, _ := migration.Bridge(srvTracerOtel, log.NewNopLogger())
    50  
    51  	srvRoot, srvCtx := tracing.StartSpan(tracing.ContextWithTracer(clientCtx, srvTracer), "b")
    52  	srvChild, _ := tracing.StartSpan(srvCtx, "bb")
    53  
    54  	tracing.CountSpans_ClientEnablesTracing(t, exp, clientRoot, srvRoot, srvChild)
    55  }
    56  
    57  // This test shows that if sample factor will disable tracing on client process,  when it would be enabled on server
    58  // it will be still disabled for all spans within this span.
    59  func TestContextTracing_ClientDisablesTracing(t *testing.T) {
    60  	exp := tracetest.NewInMemoryExporter()
    61  	tracerOtel := newTracerProvider(
    62  		context.Background(),
    63  		log.NewNopLogger(),
    64  		tracesdk.NewSimpleSpanProcessor(exp),
    65  		0, // never sample
    66  		"gcloud-test-client",
    67  	)
    68  	tracer, _ := migration.Bridge(tracerOtel, log.NewNopLogger())
    69  
    70  	clientRoot, clientCtx := tracing.StartSpan(tracing.ContextWithTracer(context.Background(), tracer), "a")
    71  
    72  	// Simulate Server process with different tracer, but with client span in context.
    73  	srvTracerOtel := newTracerProvider(
    74  		context.Background(),
    75  		log.NewNopLogger(),
    76  		tracesdk.NewSimpleSpanProcessor(exp),
    77  		0, // never sample
    78  		"gcloud-test-server",
    79  	)
    80  	srvTracer, _ := migration.Bridge(srvTracerOtel, log.NewNopLogger())
    81  
    82  	srvRoot, srvCtx := tracing.StartSpan(tracing.ContextWithTracer(clientCtx, srvTracer), "b")
    83  	srvChild, _ := tracing.StartSpan(srvCtx, "bb")
    84  
    85  	tracing.ContextTracing_ClientDisablesTracing(t, exp, clientRoot, srvRoot, srvChild)
    86  }
    87  
    88  // This test shows that if span will contain special baggage (for example from special HTTP header), even when sample
    89  // factor will disable client & server tracing, it will be still enabled for all spans within this span.
    90  func TestContextTracing_ForceTracing(t *testing.T) {
    91  	exp := tracetest.NewInMemoryExporter()
    92  	tracerOtel := newTracerProvider(
    93  		context.Background(),
    94  		log.NewNopLogger(),
    95  		tracesdk.NewSimpleSpanProcessor(exp),
    96  		0, // never sample
    97  		"gcloud-test-client",
    98  	)
    99  	tracer, _ := migration.Bridge(tracerOtel, log.NewNopLogger())
   100  
   101  	// Start the root span with the tag to force tracing.
   102  	clientRoot, clientCtx := tracing.StartSpan(
   103  		tracing.ContextWithTracer(context.Background(), tracer),
   104  		"a",
   105  		opentracing.Tag{Key: migration.ForceTracingAttributeKey, Value: "true"},
   106  	)
   107  
   108  	// Simulate Server process with different tracer, but with client span in context.
   109  	srvTracerOtel := newTracerProvider(
   110  		context.Background(),
   111  		log.NewNopLogger(),
   112  		tracesdk.NewSimpleSpanProcessor(exp),
   113  		0, // never sample
   114  		"gcloud-test-server",
   115  	)
   116  	srvTracer, _ := migration.Bridge(srvTracerOtel, log.NewNopLogger())
   117  
   118  	srvRoot, srvCtx := tracing.StartSpan(tracing.ContextWithTracer(clientCtx, srvTracer), "b")
   119  	srvChild, _ := tracing.StartSpan(srvCtx, "bb")
   120  
   121  	tracing.ContextTracing_ForceTracing(t, exp, clientRoot, srvRoot, srvChild)
   122  }