...

Source file src/go.opentelemetry.io/otel/trace/noop_test.go

Documentation: go.opentelemetry.io/otel/trace

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package trace
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  )
    21  
    22  func TestNewNoopTracerProvider(t *testing.T) {
    23  	got, want := NewNoopTracerProvider(), noopTracerProvider{}
    24  	if got != want {
    25  		t.Errorf("NewNoopTracerProvider() returned %#v, want %#v", got, want)
    26  	}
    27  }
    28  
    29  func TestNoopTracerProviderTracer(t *testing.T) {
    30  	tp := NewNoopTracerProvider()
    31  	got, want := tp.Tracer(""), noopTracer{}
    32  	if got != want {
    33  		t.Errorf("noopTracerProvider.Tracer() returned %#v, want %#v", got, want)
    34  	}
    35  }
    36  
    37  func TestNoopTracerStart(t *testing.T) {
    38  	ctx := context.Background()
    39  	tracer := NewNoopTracerProvider().Tracer("test instrumentation")
    40  
    41  	var span Span
    42  	ctx, span = tracer.Start(ctx, "span name")
    43  	got, ok := span.(noopSpan)
    44  	if !ok {
    45  		t.Fatalf("noopTracer.Start() returned a non-noopSpan: %#v", span)
    46  	}
    47  	want := noopSpan{}
    48  	if got != want {
    49  		t.Errorf("noopTracer.Start() returned %#v, want %#v", got, want)
    50  	}
    51  	got, ok = SpanFromContext(ctx).(noopSpan)
    52  	if !ok {
    53  		t.Fatal("noopTracer.Start() did not set span as current in returned context")
    54  	}
    55  	if got != want {
    56  		t.Errorf("noopTracer.Start() current span in returned context set to %#v, want %#v", got, want)
    57  	}
    58  }
    59  
    60  func TestNoopSpan(t *testing.T) {
    61  	tracer := NewNoopTracerProvider().Tracer("test instrumentation")
    62  	_, s := tracer.Start(context.Background(), "test span")
    63  	span := s.(noopSpan)
    64  
    65  	if got, want := span.SpanContext(), (SpanContext{}); !assertSpanContextEqual(got, want) {
    66  		t.Errorf("span.SpanContext() returned %#v, want %#v", got, want)
    67  	}
    68  
    69  	if got, want := span.IsRecording(), false; got != want {
    70  		t.Errorf("span.IsRecording() returned %#v, want %#v", got, want)
    71  	}
    72  }
    73  
    74  func TestNonRecordingSpanTracerStart(t *testing.T) {
    75  	tid, err := TraceIDFromHex("01000000000000000000000000000000")
    76  	if err != nil {
    77  		t.Fatalf("failure creating TraceID: %s", err.Error())
    78  	}
    79  	sid, err := SpanIDFromHex("0200000000000000")
    80  	if err != nil {
    81  		t.Fatalf("failure creating SpanID: %s", err.Error())
    82  	}
    83  	sc := NewSpanContext(SpanContextConfig{TraceID: tid, SpanID: sid})
    84  
    85  	ctx := ContextWithSpanContext(context.Background(), sc)
    86  	_, span := NewNoopTracerProvider().Tracer("test instrumentation").Start(ctx, "span1")
    87  
    88  	if got, want := span.SpanContext(), sc; !assertSpanContextEqual(got, want) {
    89  		t.Errorf("SpanContext not carried by nonRecordingSpan. got %#v, want %#v", got, want)
    90  	}
    91  }
    92  

View as plain text