Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Fix B3 propagation headers injection #149

Merged
merged 1 commit into from
Jun 15, 2021

Conversation

pellared
Copy link
Contributor

@pellared pellared commented Jun 15, 2021

What

Add leading zeros to the B3 propagation x-b3-traceid and x-b3-spanid headers so that they are always 16 characters long as required by the specification.

Benchmarks

func BenchmarkToHex(b *testing.B) {
	for i := 0; i < b.N; i++ {
		toHex(rand.Uint64())
	}
}

Current implementation

func toHex(value uint64) string {
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, value)
	return hex.EncodeToString(b)
}
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^(BenchmarkToHex)$ github.com/signalfx/signalfx-go-tracing/ddtrace/tracer

goos: linux
goarch: amd64
pkg: github.com/signalfx/signalfx-go-tracing/ddtrace/tracer
cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
BenchmarkToHex-16    	21657834	        55.59 ns/op	      32 B/op	       2 allocs/op
PASS
ok  	github.com/signalfx/signalfx-go-tracing/ddtrace/tracer	1.268s

Alternative implementation

func toHex(value uint64) string {
	return fmt.Sprintf("%016x", value)
}
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^(BenchmarkToHex2)$ github.com/signalfx/signalfx-go-tracing/ddtrace/tracer

goos: linux
goarch: amd64
pkg: github.com/signalfx/signalfx-go-tracing/ddtrace/tracer
cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
BenchmarkToHex2-16    	 8361780	       142.4 ns/op	      24 B/op	       2 allocs/op
PASS
ok  	github.com/signalfx/signalfx-go-tracing/ddtrace/tracer	1.343s

@pellared pellared requested review from MrAlias and owais June 15, 2021 08:14
@pellared pellared force-pushed the fix-b3-id-headers branch from 99b00ed to 92e53d9 Compare June 15, 2021 08:16
@pellared pellared merged commit c16a22a into signalfx:main Jun 15, 2021
@pellared pellared deleted the fix-b3-id-headers branch June 15, 2021 09:42
@pellared pellared mentioned this pull request Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants