mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-16 10:19:23 +02:00
a6e139e1d4
* api(trace): change trace id to byte array. * fix lint errors * add helper to create trace id from hex and improve stdout exporter. * remove comma. * fix lint * change TraceIDFromHex to be compliant with w3 trace-context * revert remove of hex16 regex because its used to parse SpanID * lint * fix typo
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package propagation
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"go.opentelemetry.io/api/core"
|
|
"go.opentelemetry.io/api/trace"
|
|
mocktrace "go.opentelemetry.io/internal/trace"
|
|
)
|
|
|
|
func BenchmarkInject(b *testing.B) {
|
|
var t HTTPTraceContextPropagator
|
|
|
|
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 = uint64(0x00f067aa0ba902b7)
|
|
traceID = core.TraceID{}
|
|
)
|
|
bt, _ := hex.DecodeString("4bf92f3577b34da6a3ce929d0e0e4736")
|
|
copy(traceID[:], bt)
|
|
|
|
mockTracer := &mocktrace.MockTracer{
|
|
Sampled: false,
|
|
StartSpanID: &id,
|
|
}
|
|
b.ReportAllocs()
|
|
sc := core.SpanContext{
|
|
TraceID: traceID,
|
|
SpanID: spanID,
|
|
TraceFlags: core.TraceFlagsSampled,
|
|
}
|
|
ctx := context.Background()
|
|
ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(sc))
|
|
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 HTTPTraceContextPropagator
|
|
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)
|
|
})
|
|
}
|