Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hotrod/observer_test] Switch to OpenTelemetry #4635

Merged
merged 9 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions examples/hotrod/pkg/tracing/rpcmetrics/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@
package rpcmetrics

import (
"context"
"fmt"
"testing"
"time"

opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
otbridge "go.opentelemetry.io/otel/bridge/opentracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
"github.com/opentracing/opentracing-go/ext"
"go.opentelemetry.io/otel/trace"

u "github.com/jaegertracing/jaeger/internal/metricstest"
)

type testTracer struct {
metrics *u.Factory
tracer opentracing.Tracer
tracer trace.Tracer
}

func withTestTracer(runTest func(tt *testTracer)) {
Expand All @@ -47,46 +48,39 @@ func withTestTracer(runTest func(tt *testTracer)) {
semconv.ServiceNameKey.String("test"),
)),
)
tracer, _ := otbridge.NewTracerPair(tp.Tracer(""))
runTest(&testTracer{
metrics: metrics,
tracer: tracer,
tracer: tp.Tracer("test"),
})
}

func TestObserver(t *testing.T) {
withTestTracer(func(testTracer *testTracer) {
ts := time.Now()
finishOptions := opentracing.FinishOptions{
FinishTime: ts.Add(50 * time.Millisecond),
}
finishOptions := trace.WithTimestamp(ts.Add((50 * time.Millisecond)))

testCases := []struct {
name string
tag opentracing.Tag
spanKind trace.SpanKind
opNameOverride string
err bool
}{
{name: "local-span", tag: opentracing.Tag{Key: "x", Value: "y"}},
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
{name: "get-user", tag: ext.SpanKindRPCServer},
{name: "get-user", tag: ext.SpanKindRPCServer, opNameOverride: "get-user-override"},
{name: "get-user", tag: ext.SpanKindRPCServer, err: true},
{name: "get-user-client", tag: ext.SpanKindRPCClient},
{name: "local-span", spanKind: trace.SpanKindInternal},
{name: "get-user", spanKind: trace.SpanKindServer},
{name: "get-user", spanKind: trace.SpanKindServer, opNameOverride: "get-user-override"},
{name: "get-user", spanKind: trace.SpanKindServer, err: true},
{name: "get-user-client", spanKind: trace.SpanKindClient},
}

for _, testCase := range testCases {
span := testTracer.tracer.StartSpan(
testCase.name,
testCase.tag,
opentracing.StartTime(ts),
)
_, span := testTracer.tracer.Start(context.Background(), testCase.name, trace.WithSpanKind(testCase.spanKind), trace.WithTimestamp(ts))
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
if testCase.opNameOverride != "" {
span.SetOperationName(testCase.opNameOverride)
span.SetName(testCase.opNameOverride)
}
if testCase.err {
ext.Error.Set(span, true)
span.SetStatus(codes.Error, "An error occured")
}
span.FinishWithOptions(finishOptions)
span.End(finishOptions)
}

testTracer.metrics.AssertCounterMetrics(t,
Expand Down Expand Up @@ -119,10 +113,6 @@ func TestTags(t *testing.T) {
{key: "error", value: true, metrics: []u.ExpectedMetric{
{Name: "requests", Value: 1, Tags: tags("error", "true")},
}},
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
// OTEL bridge does not interpret string "true" as error status
// {key: "error", value: "true", variant: "string", metrics: []u.ExpectedMetric{
// {Name: "requests", Value: 1, Tags: tags("error", "true")},
// }},
}

for i := 200; i <= 500; i += 100 {
Expand Down Expand Up @@ -152,9 +142,20 @@ func TestTags(t *testing.T) {
}
t.Run(fmt.Sprintf("%s-%v-%s", testCase.key, testCase.value, testCase.variant), func(t *testing.T) {
withTestTracer(func(testTracer *testTracer) {
span := testTracer.tracer.StartSpan("span", ext.SpanKindRPCServer)
span.SetTag(testCase.key, testCase.value)
span.Finish()
_, span := testTracer.tracer.Start(context.Background(), "span", trace.WithSpanKind(trace.SpanKindServer))
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
switch v := testCase.value.(type) {
afzal442 marked this conversation as resolved.
Show resolved Hide resolved
case int:
span.SetAttributes(attribute.Key(testCase.key).String(fmt.Sprint(v)))
case bool:
span.SetAttributes(attribute.Key(testCase.key).String(fmt.Sprint(v)))
case int64:
span.SetAttributes(attribute.Key(testCase.key).String(fmt.Sprint(v)))
case uint16:
span.SetAttributes(attribute.Key(testCase.key).String(fmt.Sprint(v)))
default:
span.SetAttributes(attribute.Key(testCase.key).String(v.(string)))
}
span.End()
testTracer.metrics.AssertCounterMetrics(t, testCase.metrics...)
})
})
Expand Down
2 changes: 1 addition & 1 deletion model/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Span) GetSpanKind() (spanKind trace.SpanKind, found bool) {

// GetSamplerType returns the sampler type for span
func (s *Span) GetSamplerType() string {
// There's no corresponding opentracing-go tag label corresponding to sampler.type
// There's no corresponding opentelemetry tag label corresponding to sampler.type
if tag, ok := KeyValues(s.Tags).FindByKey(samplerType); ok {
if tag.VStr == "" {
return samplerTypeUnknown
Expand Down
Loading