Skip to content

Commit

Permalink
Merge branch 'main' into check-semconv-version
Browse files Browse the repository at this point in the history
  • Loading branch information
perhapsmaple authored Aug 13, 2023
2 parents 35b30cf + 941c115 commit efe85f3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 73 deletions.
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"}},
{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
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")},
}},
// 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
29 changes: 11 additions & 18 deletions examples/memstore-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
package main

import (
"context"
"flag"
"fmt"
"strings"

"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/hashicorp/go-plugin"
"github.com/opentracing/opentracing-go"
"github.com/spf13/viper"
jaegerClientConfig "github.com/uber/jaeger-client-go/config"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
googleGRPC "google.golang.org/grpc"

"github.com/jaegertracing/jaeger/pkg/jtracer"
"github.com/jaegertracing/jaeger/plugin/storage/grpc"
grpcMemory "github.com/jaegertracing/jaeger/plugin/storage/grpc/memory"
"github.com/jaegertracing/jaeger/plugin/storage/grpc/shared"
Expand Down Expand Up @@ -55,21 +57,12 @@ func main() {
opts := memory.Options{}
opts.InitFromViper(v)

traceCfg := &jaegerClientConfig.Configuration{
ServiceName: serviceName,
Sampler: &jaegerClientConfig.SamplerConfig{
Type: "const",
Param: 1.0,
},
RPCMetrics: true,
}

tracer, closer, err := traceCfg.NewTracer()
tracer, err := jtracer.New(serviceName)
if err != nil {
panic("Failed to initialize tracer")
panic(fmt.Errorf("failed to initialize tracer: %w", err))
}
defer closer.Close()
opentracing.SetGlobalTracer(tracer)
defer tracer.Close(context.Background())
otel.SetTracerProvider(tracer.OTEL)

memStorePlugin := grpcMemory.NewStoragePlugin(memory.NewStore(), memory.NewStore())
service := &shared.PluginServices{
Expand All @@ -81,8 +74,8 @@ func main() {
}
grpc.ServeWithGRPCServer(service, func(options []googleGRPC.ServerOption) *googleGRPC.Server {
return plugin.DefaultGRPCServer([]googleGRPC.ServerOption{
googleGRPC.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
googleGRPC.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
googleGRPC.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(tracer.OTEL))),
googleGRPC.StreamInterceptor(otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(tracer.OTEL))),
})
})
}
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

0 comments on commit efe85f3

Please sign in to comment.