...

Source file src/go.opentelemetry.io/otel/sdk/trace/tracetest/exporter_test.go

Documentation: go.opentelemetry.io/otel/sdk/trace/tracetest

     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 tracetest
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  // TestNoop tests only that the no-op does not crash in different scenarios.
    27  func TestNoop(t *testing.T) {
    28  	nsb := NewNoopExporter()
    29  
    30  	require.NoError(t, nsb.ExportSpans(context.Background(), nil))
    31  	require.NoError(t, nsb.ExportSpans(context.Background(), make(SpanStubs, 10).Snapshots()))
    32  	require.NoError(t, nsb.ExportSpans(context.Background(), make(SpanStubs, 0, 10).Snapshots()))
    33  }
    34  
    35  func TestNewInMemoryExporter(t *testing.T) {
    36  	imsb := NewInMemoryExporter()
    37  
    38  	require.NoError(t, imsb.ExportSpans(context.Background(), nil))
    39  	assert.Len(t, imsb.GetSpans(), 0)
    40  
    41  	input := make(SpanStubs, 10)
    42  	for i := 0; i < 10; i++ {
    43  		input[i] = SpanStub{Name: fmt.Sprintf("span %d", i)}
    44  	}
    45  	require.NoError(t, imsb.ExportSpans(context.Background(), input.Snapshots()))
    46  	sds := imsb.GetSpans()
    47  	assert.Len(t, sds, 10)
    48  	for i, sd := range sds {
    49  		assert.Equal(t, input[i], sd)
    50  	}
    51  	imsb.Reset()
    52  	// Ensure that operations on the internal storage does not change the previously returned value.
    53  	assert.Len(t, sds, 10)
    54  	assert.Len(t, imsb.GetSpans(), 0)
    55  
    56  	require.NoError(t, imsb.ExportSpans(context.Background(), input.Snapshots()[0:1]))
    57  	sds = imsb.GetSpans()
    58  	assert.Len(t, sds, 1)
    59  	assert.Equal(t, input[0], sds[0])
    60  }
    61  

View as plain text