2020-03-24 07:41:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-11-13 17:34:24 +02:00
|
|
|
package propagation_test
|
2019-10-14 23:36:34 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-13 17:34:24 +02:00
|
|
|
"go.opentelemetry.io/otel/propagation"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-10-14 23:36:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkInject(b *testing.B) {
|
2020-11-13 17:34:24 +02:00
|
|
|
var t propagation.TraceContext
|
2019-10-14 23:36:34 +02:00
|
|
|
|
|
|
|
injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) {
|
2021-07-21 16:25:18 +02:00
|
|
|
h := http.Header{}
|
2019-10-14 23:36:34 +02:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-07-21 16:25:18 +02:00
|
|
|
t.Inject(ctx, propagation.HeaderCarrier(h))
|
2019-10-14 23:36:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
|
|
|
|
b.Run("SampledSpanContext", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
2021-03-09 18:17:29 +02:00
|
|
|
sc := trace.NewSpanContext(trace.SpanContextConfig{
|
2019-10-14 23:36:34 +02:00
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
2020-11-07 00:13:31 +02:00
|
|
|
TraceFlags: trace.FlagsSampled,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-11-07 00:13:31 +02:00
|
|
|
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
|
2019-10-14 23:36:34 +02:00
|
|
|
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) {
|
2020-11-13 17:34:24 +02:00
|
|
|
var propagator propagation.TraceContext
|
2019-10-14 23:36:34 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-02-17 18:04:49 +02:00
|
|
|
propagator.Extract(ctx, propagation.HeaderCarrier(req.Header))
|
2019-10-14 23:36:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|