github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/obs/tracing_test.go (about)

     1  package obs
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"go.opentelemetry.io/otel/trace"
    10  )
    11  
    12  func tracerTestSetEnv() {
    13  	os.Setenv("APP_SERVICE_OBS_ENABLED", "true")
    14  	os.Setenv("APP_SERVICE_NAME", "obs-testing")
    15  	os.Setenv("APP_SERVICE_ENV", "development")
    16  	os.Setenv("APP_OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:8888")
    17  }
    18  
    19  func tracerSetUnsetEnv() {
    20  	os.Setenv("APP_SERVICE_OBS_ENABLED", "")
    21  	os.Setenv("APP_SERVICE_NAME", "")
    22  	os.Setenv("APP_SERVICE_LABELS", "")
    23  	os.Setenv("APP_OTEL_EXPORTER_OTLP_ENDPOINT", "")
    24  }
    25  
    26  func TestSpannedSetStatusOkOnSuccess(t *testing.T) {
    27  	tracerTestSetEnv()
    28  	t.Cleanup(tracerSetUnsetEnv)
    29  
    30  	shutdown := InitTracing()
    31  	defer shutdown(context.Background())
    32  
    33  	var span trace.Span
    34  	err := Spanned(context.Background(), "span.test", func(ctx context.Context) error {
    35  		span = trace.SpanFromContext(ctx)
    36  		return nil
    37  	})
    38  
    39  	assert.Nil(t, err)
    40  	assert.False(t, span.IsRecording())
    41  	assert.True(t, span.SpanContext().IsValid())
    42  }