1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/propagators/trace_context_benchmark_test.go
Tyler Yahn f1dad21e47
Move propagators out of API and into their own package (#1118)
* Add new propagators package

* Move B3 propagator to propagators

Update all `api/trace` dependencies in the propagator.

Export the isDeferred and isDebug methods of the SpanContext. These are
needed by the B3 propagator to track trace state.

* Move W3C trace context propagator to propagators

* Update package docs with supported encodings

* Move unified propagators code to own file

* Update b3 exported documentation

* Update trace_context exported documentation

* Add code examples for B3 propagator

* Add TraceContext example code

* Remove internal package

Move common testing declarations to the propagators_test.go file.

* Add changes to Changelog

* Add test to check default propagators
2020-09-08 19:07:59 -04:00

101 lines
2.8 KiB
Go

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package propagators_test
import (
"context"
"net/http"
"testing"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/api/trace/tracetest"
"go.opentelemetry.io/otel/propagators"
)
func BenchmarkInject(b *testing.B) {
var t propagators.TraceContext
injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Inject(ctx, req.Header)
}
})
}
func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
b.Run("SampledSpanContext", func(b *testing.B) {
var id uint64
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
mockTracer := &tracetest.MockTracer{
Sampled: false,
StartSpanID: &id,
}
b.ReportAllocs()
sc := trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
}
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
ctx, _ = mockTracer.Start(ctx, "inject")
fn(ctx, b)
})
b.Run("WithoutSpanContext", func(b *testing.B) {
b.ReportAllocs()
ctx := context.Background()
fn(ctx, b)
})
}
func BenchmarkExtract(b *testing.B) {
extractSubBenchmarks(b, func(b *testing.B, req *http.Request) {
var propagator propagators.TraceContext
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
propagator.Extract(ctx, req.Header)
}
})
}
func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) {
b.Run("Sampled", func(b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
b.ReportAllocs()
fn(b, req)
})
b.Run("BogusVersion", func(b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01")
b.ReportAllocs()
fn(b, req)
})
b.Run("FutureAdditionalData", func(b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09")
b.ReportAllocs()
fn(b, req)
})
}