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

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package tracing
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/efficientgo/core/testutil"
    10  
    11  	opentracing "github.com/opentracing/opentracing-go"
    12  	"go.opentelemetry.io/otel/sdk/trace/tracetest"
    13  )
    14  
    15  func CountSpans_ClientEnablesTracing(t *testing.T, exp *tracetest.InMemoryExporter, clientRoot, srvRoot, srvChild opentracing.Span) {
    16  	testutil.Equals(t, 0, len(exp.GetSpans()))
    17  
    18  	srvChild.Finish()
    19  	testutil.Equals(t, 1, len(exp.GetSpans()))
    20  	testutil.Equals(t, 1, CountSampledSpans(exp.GetSpans()))
    21  
    22  	srvRoot.Finish()
    23  	testutil.Equals(t, 2, len(exp.GetSpans()))
    24  	testutil.Equals(t, 2, CountSampledSpans(exp.GetSpans()))
    25  
    26  	clientRoot.Finish()
    27  	testutil.Equals(t, 3, len(exp.GetSpans()))
    28  	testutil.Equals(t, 3, CountSampledSpans(exp.GetSpans()))
    29  }
    30  
    31  func ContextTracing_ClientDisablesTracing(t *testing.T, exp *tracetest.InMemoryExporter, clientRoot, srvRoot, srvChild opentracing.Span) {
    32  	testutil.Equals(t, 0, len(exp.GetSpans()))
    33  
    34  	// Since we are not recording neither sampling, no spans should show up.
    35  	srvChild.Finish()
    36  	testutil.Equals(t, 0, len(exp.GetSpans()))
    37  
    38  	srvRoot.Finish()
    39  	testutil.Equals(t, 0, len(exp.GetSpans()))
    40  
    41  	clientRoot.Finish()
    42  	testutil.Equals(t, 0, len(exp.GetSpans()))
    43  }
    44  
    45  func ContextTracing_ForceTracing(t *testing.T, exp *tracetest.InMemoryExporter, clientRoot, srvRoot, srvChild opentracing.Span) {
    46  	testutil.Equals(t, 0, len(exp.GetSpans()))
    47  
    48  	srvChild.Finish()
    49  	testutil.Equals(t, 1, len(exp.GetSpans()))
    50  	testutil.Equals(t, 1, CountSampledSpans(exp.GetSpans()))
    51  
    52  	srvRoot.Finish()
    53  	testutil.Equals(t, 2, len(exp.GetSpans()))
    54  	testutil.Equals(t, 2, CountSampledSpans(exp.GetSpans()))
    55  
    56  	clientRoot.Finish()
    57  	testutil.Equals(t, 3, len(exp.GetSpans()))
    58  	testutil.Equals(t, 3, CountSampledSpans(exp.GetSpans()))
    59  }
    60  
    61  // Utility function for use with tests in pkg/tracing.
    62  func CountSampledSpans(ss tracetest.SpanStubs) int {
    63  	var count int
    64  	for _, s := range ss {
    65  		if s.SpanContext.IsSampled() {
    66  			count++
    67  		}
    68  	}
    69  
    70  	return count
    71  }