github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/tracer/tracer_test.go (about)

     1  package tracer_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"go.opentelemetry.io/otel"
     9  	"go.opentelemetry.io/otel/trace"
    10  
    11  	"github.com/machinefi/w3bstream/pkg/depends/base/types"
    12  	"github.com/machinefi/w3bstream/pkg/depends/conf/tracer"
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    14  )
    15  
    16  func do1(ctx context.Context, tr trace.Tracer) {
    17  	_, span := tr.Start(ctx, "do1", trace.WithTimestamp(time.Now()))
    18  	defer span.End()
    19  
    20  	time.Sleep(100 * time.Millisecond)
    21  }
    22  
    23  func do2(ctx context.Context, tr trace.Tracer) {
    24  	_, span := tr.Start(ctx, "do2", trace.WithTimestamp(time.Now()))
    25  	defer span.End()
    26  
    27  	time.Sleep(100 * time.Millisecond)
    28  }
    29  
    30  func TestConfig(t *testing.T) {
    31  	c := tracer.Config{
    32  		GrpcEndpoint: types.Endpoint{
    33  			Scheme:   "http",
    34  			Hostname: "localhost",
    35  			Port:     4317,
    36  		},
    37  		HttpEndpoint: types.Endpoint{
    38  			Scheme:   "http",
    39  			Hostname: "localhost",
    40  			Port:     4318,
    41  		},
    42  		ServiceVersion: "1.0.0",
    43  		InstanceID:     "unique",
    44  		DebugMode:      datatypes.TRUE,
    45  	}
    46  	// use grpc endpoint
    47  	cGRPC := c
    48  	cGRPC.HttpEndpoint = types.Endpoint{}
    49  	cGRPC.ServiceName = "test_config_grpc"
    50  
    51  	// use http endpoint
    52  	cHTTP := c
    53  	cHTTP.GrpcEndpoint = types.Endpoint{}
    54  	cHTTP.ServiceName = "test_config_http"
    55  
    56  	for _, c := range []*tracer.Config{&cGRPC, &cHTTP} {
    57  		c.SetDefault()
    58  		err := c.Init()
    59  		if err != nil {
    60  			t.Log(err)
    61  			return
    62  		}
    63  
    64  		tr := otel.Tracer(c.ServiceName)
    65  		ctx, span := tr.Start(context.Background(), "TestConfig", trace.WithTimestamp(time.Now()))
    66  
    67  		do1(ctx, tr)
    68  		do2(ctx, tr)
    69  
    70  		span.End()
    71  
    72  		_ = c.Shutdown(context.Background())
    73  	}
    74  }