This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStartAgent(t *testing.T) {
|
||||
logger.Disable()
|
||||
|
||||
const (
|
||||
endpoint1 = "localhost:1234"
|
||||
endpoint2 = "remotehost:1234"
|
||||
endpoint3 = "localhost:1235"
|
||||
endpoint4 = "localhost:1236"
|
||||
endpoint5 = "udp://localhost:6831"
|
||||
endpoint6 = "localhost:1237"
|
||||
endpoint71 = "/tmp/trace.log"
|
||||
endpoint72 = "/not-exist-fs/trace.log"
|
||||
)
|
||||
c1 := Config{
|
||||
Name: "foo",
|
||||
}
|
||||
c2 := Config{
|
||||
Name: "bar",
|
||||
Endpoint: endpoint1,
|
||||
Batcher: kindJaeger,
|
||||
}
|
||||
c3 := Config{
|
||||
Name: "any",
|
||||
Endpoint: endpoint2,
|
||||
Batcher: kindZipkin,
|
||||
}
|
||||
c4 := Config{
|
||||
Name: "bla",
|
||||
Endpoint: endpoint3,
|
||||
Batcher: "otlp",
|
||||
}
|
||||
c5 := Config{
|
||||
Name: "otlpgrpc",
|
||||
Endpoint: endpoint3,
|
||||
Batcher: kindOtlpGrpc,
|
||||
OtlpHeaders: map[string]string{
|
||||
"uptrace-dsn": "http://project2_secret_token@localhost:14317/2",
|
||||
},
|
||||
}
|
||||
c6 := Config{
|
||||
Name: "otlphttp",
|
||||
Endpoint: endpoint4,
|
||||
Batcher: kindOtlpHttp,
|
||||
OtlpHeaders: map[string]string{
|
||||
"uptrace-dsn": "http://project2_secret_token@localhost:14318/2",
|
||||
},
|
||||
OtlpHttpPath: "/v1/traces",
|
||||
}
|
||||
c7 := Config{
|
||||
Name: "UDP",
|
||||
Endpoint: endpoint5,
|
||||
Batcher: kindJaeger,
|
||||
}
|
||||
c8 := Config{
|
||||
Disabled: true,
|
||||
Endpoint: endpoint6,
|
||||
Batcher: kindJaeger,
|
||||
}
|
||||
c9 := Config{
|
||||
Name: "file",
|
||||
Endpoint: endpoint71,
|
||||
Batcher: kindFile,
|
||||
}
|
||||
c10 := Config{
|
||||
Name: "file",
|
||||
Endpoint: endpoint72,
|
||||
Batcher: kindFile,
|
||||
}
|
||||
|
||||
StartAgent(c1)
|
||||
StartAgent(c1)
|
||||
StartAgent(c2)
|
||||
StartAgent(c3)
|
||||
StartAgent(c4)
|
||||
StartAgent(c5)
|
||||
StartAgent(c6)
|
||||
StartAgent(c7)
|
||||
StartAgent(c8)
|
||||
StartAgent(c9)
|
||||
StartAgent(c10)
|
||||
defer StopAgent()
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
// because remotehost cannot be resolved
|
||||
assert.Equal(t, 6, len(agents))
|
||||
_, ok := agents[""]
|
||||
assert.True(t, ok)
|
||||
_, ok = agents[endpoint1]
|
||||
assert.True(t, ok)
|
||||
_, ok = agents[endpoint2]
|
||||
assert.False(t, ok)
|
||||
_, ok = agents[endpoint5]
|
||||
assert.True(t, ok)
|
||||
_, ok = agents[endpoint6]
|
||||
assert.False(t, ok)
|
||||
_, ok = agents[endpoint71]
|
||||
assert.True(t, ok)
|
||||
_, ok = agents[endpoint72]
|
||||
assert.False(t, ok)
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
gcodes "google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
func TestStatusCodeAttr(t *testing.T) {
|
||||
assert.Equal(t, GRPCStatusCodeKey.Int(int(gcodes.DataLoss)), StatusCodeAttr(gcodes.DataLoss))
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/types/dynamicpb"
|
||||
)
|
||||
|
||||
func TestMessageType_Event(t *testing.T) {
|
||||
ctx, s := otel.Tracer(TraceName).Start(context.Background(), "test")
|
||||
span := mockSpan{Span: s}
|
||||
ctx = trace.ContextWithSpan(ctx, &span)
|
||||
MessageReceived.Event(ctx, 1, "foo")
|
||||
assert.Equal(t, messageEvent, span.name)
|
||||
assert.NotEmpty(t, span.options)
|
||||
}
|
||||
|
||||
func TestMessageType_EventProtoMessage(t *testing.T) {
|
||||
var span mockSpan
|
||||
var message mockMessage
|
||||
ctx := trace.ContextWithSpan(context.Background(), &span)
|
||||
MessageReceived.Event(ctx, 1, message)
|
||||
assert.Equal(t, messageEvent, span.name)
|
||||
assert.NotEmpty(t, span.options)
|
||||
}
|
||||
|
||||
type mockSpan struct {
|
||||
trace.Span
|
||||
name string
|
||||
options []trace.EventOption
|
||||
}
|
||||
|
||||
func (m *mockSpan) End(_ ...trace.SpanEndOption) {
|
||||
}
|
||||
|
||||
func (m *mockSpan) AddEvent(name string, options ...trace.EventOption) {
|
||||
m.name = name
|
||||
m.options = options
|
||||
}
|
||||
|
||||
func (m *mockSpan) IsRecording() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *mockSpan) RecordError(_ error, _ ...trace.EventOption) {
|
||||
}
|
||||
|
||||
func (m *mockSpan) SpanContext() trace.SpanContext {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (m *mockSpan) SetStatus(_ codes.Code, _ string) {
|
||||
}
|
||||
|
||||
func (m *mockSpan) SetName(_ string) {
|
||||
}
|
||||
|
||||
func (m *mockSpan) SetAttributes(_ ...attribute.KeyValue) {
|
||||
}
|
||||
|
||||
func (m *mockSpan) TracerProvider() trace.TracerProvider {
|
||||
return nil
|
||||
}
|
||||
|
||||
type mockMessage struct{}
|
||||
|
||||
func (m mockMessage) ProtoReflect() protoreflect.Message {
|
||||
return new(dynamicpb.Message)
|
||||
}
|
||||
@@ -1,356 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
traceIDStr = "4bf92f3577b34da6a3ce929d0e0e4736"
|
||||
spanIDStr = "00f067aa0ba902b7"
|
||||
)
|
||||
|
||||
var (
|
||||
traceID = mustTraceIDFromHex(traceIDStr)
|
||||
spanID = mustSpanIDFromHex(spanIDStr)
|
||||
)
|
||||
|
||||
func mustTraceIDFromHex(s string) (t trace.TraceID) {
|
||||
var err error
|
||||
t, err = trace.TraceIDFromHex(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func mustSpanIDFromHex(s string) (t trace.SpanID) {
|
||||
var err error
|
||||
t, err = trace.SpanIDFromHex(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func TestExtractValidTraceContext(t *testing.T) {
|
||||
stateStr := "key1=value1,key2=value2"
|
||||
state, err := trace.ParseTraceState(stateStr)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
traceparent string
|
||||
tracestate string
|
||||
sc trace.SpanContext
|
||||
}{
|
||||
{
|
||||
name: "not sampled",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "sampled",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: trace.FlagsSampled,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "valid tracestate",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
tracestate: stateStr,
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceState: state,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "invalid tracestate perserves traceparent",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
tracestate: "invalid$@#=invalid",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "future version not sampled",
|
||||
traceparent: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "future version sampled",
|
||||
traceparent: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: trace.FlagsSampled,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "future version sample bit set",
|
||||
traceparent: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: trace.FlagsSampled,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "future version sample bit not set",
|
||||
traceparent: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-08",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "future version additional data",
|
||||
traceparent: "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00-XYZxsf09",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "B3 format ending in dash",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00-",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "future version B3 format ending in dash",
|
||||
traceparent: "03-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00-",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
}
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, propagation.Baggage{}))
|
||||
propagator := otel.GetTextMapPropagator()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
md := metadata.MD{}
|
||||
md.Set("traceparent", tt.traceparent)
|
||||
md.Set("tracestate", tt.tracestate)
|
||||
_, spanCtx := Extract(ctx, propagator, &md)
|
||||
assert.Equal(t, tt.sc, spanCtx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractInvalidTraceContext(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
}{
|
||||
{
|
||||
name: "wrong version length",
|
||||
header: "0000-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "wrong trace ID length",
|
||||
header: "00-ab00000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "wrong span ID length",
|
||||
header: "00-ab000000000000000000000000000000-cd0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "wrong trace flag length",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-0100",
|
||||
},
|
||||
{
|
||||
name: "bogus version",
|
||||
header: "qw-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "bogus trace ID",
|
||||
header: "00-qw000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "bogus span ID",
|
||||
header: "00-ab000000000000000000000000000000-qw00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "bogus trace flag",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-qw",
|
||||
},
|
||||
{
|
||||
name: "upper case version",
|
||||
header: "A0-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "upper case trace ID",
|
||||
header: "00-AB000000000000000000000000000000-cd00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "upper case span ID",
|
||||
header: "00-ab000000000000000000000000000000-CD00000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "upper case trace flag",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-A1",
|
||||
},
|
||||
{
|
||||
name: "zero trace ID and span ID",
|
||||
header: "00-00000000000000000000000000000000-0000000000000000-01",
|
||||
},
|
||||
{
|
||||
name: "trace-flag unused bits set",
|
||||
header: "00-ab000000000000000000000000000000-cd00000000000000-09",
|
||||
},
|
||||
{
|
||||
name: "missing options",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7",
|
||||
},
|
||||
{
|
||||
name: "empty options",
|
||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-",
|
||||
},
|
||||
}
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, propagation.Baggage{}))
|
||||
propagator := otel.GetTextMapPropagator()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
md := metadata.MD{}
|
||||
md.Set("traceparent", tt.header)
|
||||
_, spanCtx := Extract(ctx, propagator, &md)
|
||||
assert.Equal(t, trace.SpanContext{}, spanCtx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectValidTraceContext(t *testing.T) {
|
||||
stateStr := "key1=value1,key2=value2"
|
||||
state, err := trace.ParseTraceState(stateStr)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
traceparent string
|
||||
tracestate string
|
||||
sc trace.SpanContext
|
||||
}{
|
||||
{
|
||||
name: "not sampled",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "sampled",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: trace.FlagsSampled,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "unsupported trace flag bits dropped",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: 0xff,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "with tracestate",
|
||||
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
tracestate: stateStr,
|
||||
sc: trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceState: state,
|
||||
Remote: true,
|
||||
}),
|
||||
},
|
||||
}
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, propagation.Baggage{}))
|
||||
propagator := otel.GetTextMapPropagator()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx = trace.ContextWithRemoteSpanContext(ctx, tt.sc)
|
||||
|
||||
want := metadata.MD{}
|
||||
want.Set("traceparent", tt.traceparent)
|
||||
if len(tt.tracestate) > 0 {
|
||||
want.Set("tracestate", tt.tracestate)
|
||||
}
|
||||
|
||||
md := metadata.MD{}
|
||||
Inject(ctx, propagator, &md)
|
||||
assert.Equal(t, want, md)
|
||||
|
||||
mm := &metadataSupplier{
|
||||
metadata: &md,
|
||||
}
|
||||
assert.NotEmpty(t, mm.Keys())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidSpanContextDropped(t *testing.T) {
|
||||
invalidSC := trace.SpanContext{}
|
||||
require.False(t, invalidSC.IsValid())
|
||||
ctx := trace.ContextWithRemoteSpanContext(context.Background(), invalidSC)
|
||||
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, propagation.Baggage{}))
|
||||
propagator := otel.GetTextMapPropagator()
|
||||
|
||||
md := metadata.MD{}
|
||||
Inject(ctx, propagator, &md)
|
||||
mm := &metadataSupplier{
|
||||
metadata: &md,
|
||||
}
|
||||
assert.Empty(t, mm.Keys())
|
||||
assert.Equal(t, "", mm.Get("traceparent"), "injected invalid SpanContext")
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/grpc/peer"
|
||||
)
|
||||
|
||||
func TestPeerFromContext(t *testing.T) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
assert.Nil(t, err)
|
||||
assert.NotEmpty(t, addrs)
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
empty bool
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
ctx: context.Background(),
|
||||
empty: true,
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
ctx: peer.NewContext(context.Background(), nil),
|
||||
empty: true,
|
||||
},
|
||||
{
|
||||
name: "with value",
|
||||
ctx: peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: addrs[0],
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
addr := PeerFromCtx(test.ctx)
|
||||
assert.Equal(t, test.empty, len(addr) == 0)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFullMethod(t *testing.T) {
|
||||
tests := []struct {
|
||||
fullMethod string
|
||||
name string
|
||||
attr []attribute.KeyValue
|
||||
}{
|
||||
{
|
||||
fullMethod: "/grpc.test.EchoService/Echo",
|
||||
name: "grpc.test.EchoService/Echo",
|
||||
attr: []attribute.KeyValue{
|
||||
semconv.RPCServiceKey.String("grpc.test.EchoService"),
|
||||
semconv.RPCMethodKey.String("Echo"),
|
||||
},
|
||||
}, {
|
||||
fullMethod: "/com.example.ExampleRmiService/exampleMethod",
|
||||
name: "com.example.ExampleRmiService/exampleMethod",
|
||||
attr: []attribute.KeyValue{
|
||||
semconv.RPCServiceKey.String("com.example.ExampleRmiService"),
|
||||
semconv.RPCMethodKey.String("exampleMethod"),
|
||||
},
|
||||
}, {
|
||||
fullMethod: "/MyCalcService.Calculator/Add",
|
||||
name: "MyCalcService.Calculator/Add",
|
||||
attr: []attribute.KeyValue{
|
||||
semconv.RPCServiceKey.String("MyCalcService.Calculator"),
|
||||
semconv.RPCMethodKey.String("Add"),
|
||||
},
|
||||
}, {
|
||||
fullMethod: "/MyServiceReference.ICalculator/Add",
|
||||
name: "MyServiceReference.ICalculator/Add",
|
||||
attr: []attribute.KeyValue{
|
||||
semconv.RPCServiceKey.String("MyServiceReference.ICalculator"),
|
||||
semconv.RPCMethodKey.String("Add"),
|
||||
},
|
||||
}, {
|
||||
fullMethod: "/MyServiceWithNoPackage/theMethod",
|
||||
name: "MyServiceWithNoPackage/theMethod",
|
||||
attr: []attribute.KeyValue{
|
||||
semconv.RPCServiceKey.String("MyServiceWithNoPackage"),
|
||||
semconv.RPCMethodKey.String("theMethod"),
|
||||
},
|
||||
}, {
|
||||
fullMethod: "/pkg.svr",
|
||||
name: "pkg.svr",
|
||||
attr: []attribute.KeyValue(nil),
|
||||
}, {
|
||||
fullMethod: "/pkg.svr/",
|
||||
name: "pkg.svr/",
|
||||
attr: []attribute.KeyValue{
|
||||
semconv.RPCServiceKey.String("pkg.svr"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
n, a := ParseFullMethod(test.fullMethod)
|
||||
assert.Equal(t, test.name, n)
|
||||
assert.Equal(t, test.attr, a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpanInfo(t *testing.T) {
|
||||
val, kvs := SpanInfo("/fullMethod", "remote")
|
||||
assert.Equal(t, "fullMethod", val)
|
||||
assert.NotEmpty(t, kvs)
|
||||
}
|
||||
|
||||
func TestPeerAttr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr string
|
||||
expect []attribute.KeyValue
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
},
|
||||
{
|
||||
name: "port only",
|
||||
addr: ":8080",
|
||||
expect: []attribute.KeyValue{
|
||||
semconv.NetPeerIPKey.String(localhost),
|
||||
semconv.NetPeerPortKey.String("8080"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "port only",
|
||||
addr: "192.168.0.2:8080",
|
||||
expect: []attribute.KeyValue{
|
||||
semconv.NetPeerIPKey.String("192.168.0.2"),
|
||||
semconv.NetPeerPortKey.String("8080"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
kvs := PeerAttr(test.addr)
|
||||
assert.EqualValues(t, test.expect, kvs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTracerFromContext(t *testing.T) {
|
||||
traceFn := func(ctx context.Context, hasTraceId bool) {
|
||||
spanContext := trace.SpanContextFromContext(ctx)
|
||||
assert.Equal(t, spanContext.IsValid(), hasTraceId)
|
||||
parentTraceId := spanContext.TraceID().String()
|
||||
|
||||
tracer := TracerFromContext(ctx)
|
||||
_, span := tracer.Start(ctx, "b")
|
||||
defer span.End()
|
||||
|
||||
spanContext = span.SpanContext()
|
||||
assert.True(t, spanContext.IsValid())
|
||||
if hasTraceId {
|
||||
assert.Equal(t, parentTraceId, spanContext.TraceID().String())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
t.Run("context", func(t *testing.T) {
|
||||
opts := []sdktrace.TracerProviderOption{
|
||||
// Set the sampling rate based on the parent span to 100%
|
||||
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(1))),
|
||||
// Record information about this application in a Resource.
|
||||
sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String("test"))),
|
||||
}
|
||||
tp = sdktrace.NewTracerProvider(opts...)
|
||||
otel.SetTracerProvider(tp)
|
||||
ctx, span := tp.Tracer(TraceName).Start(context.Background(), "a")
|
||||
|
||||
defer span.End()
|
||||
traceFn(ctx, true)
|
||||
})
|
||||
|
||||
t.Run("global", func(t *testing.T) {
|
||||
opts := []sdktrace.TracerProviderOption{
|
||||
// Set the sampling rate based on the parent span to 100%
|
||||
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(1))),
|
||||
// Record information about this application in a Resource.
|
||||
sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String("test"))),
|
||||
}
|
||||
tp = sdktrace.NewTracerProvider(opts...)
|
||||
otel.SetTracerProvider(tp)
|
||||
|
||||
traceFn(context.Background(), false)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user