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 all 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
94 changes: 40 additions & 54 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/opentracing/opentracing-go/ext"
"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.20.0"
"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,43 @@ 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),
)
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 All @@ -106,55 +104,43 @@ func TestObserver(t *testing.T) {

func TestTags(t *testing.T) {
type tagTestCase struct {
key string
variant string
value interface{}
attr attribute.KeyValue
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
err bool
metrics []u.ExpectedMetric
}

testCases := []tagTestCase{
{key: "something", value: 42, metrics: []u.ExpectedMetric{
{err: false, metrics: []u.ExpectedMetric{
{Name: "requests", Value: 1, Tags: tags("error", "false")},
}},
{key: "error", value: true, metrics: []u.ExpectedMetric{
{err: 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 {
status_codes := []struct {
value interface{}
variant string
}{
{value: i},
{value: uint16(i), variant: "uint16"},
{value: fmt.Sprintf("%d", i), variant: "string"},
}
for _, v := range status_codes {
testCases = append(testCases, tagTestCase{
key: "http.status_code",
value: v.value,
variant: v.variant,
metrics: []u.ExpectedMetric{
{Name: "http_requests", Value: 1, Tags: tags("status_code", fmt.Sprintf("%dxx", i/100))},
},
})
}
testCases = append(testCases, tagTestCase{
attr: semconv.HTTPStatusCode(i),
metrics: []u.ExpectedMetric{
{Name: "http_requests", Value: 1, Tags: tags("status_code", fmt.Sprintf("%dxx", i/100))},
},
})
}

for _, testCase := range testCases {
for i := range testCase.metrics {
testCase.metrics[i].Tags["endpoint"] = "span"
}
t.Run(fmt.Sprintf("%s-%v-%s", testCase.key, testCase.value, testCase.variant), func(t *testing.T) {
t.Run(fmt.Sprintf("%s-%v", testCase.attr.Key, testCase.attr.Value), 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),
)
span.SetAttributes(testCase.attr)
if testCase.err {
span.SetStatus(codes.Error, "An error occured")
}
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