1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

api(trace): change SpanID to byte array (#241)

* api(trace): change SpanID to byte array

* fix doc and create const errors
This commit is contained in:
Gustavo Silva Paiva
2019-10-28 14:05:06 -03:00
committed by rghetia
parent d9c4aa5cee
commit 4e545e2ab8
26 changed files with 194 additions and 212 deletions

View File

@@ -15,8 +15,6 @@
package propagation
import (
"encoding/binary"
"go.opentelemetry.io/api/core"
apipropagation "go.opentelemetry.io/api/propagation"
)
@@ -40,7 +38,7 @@ func (bp binaryPropagator) ToBytes(sc core.SpanContext) []byte {
var b [29]byte
copy(b[2:18], sc.TraceID[:])
b[18] = 1
binary.BigEndian.PutUint64(b[19:27], sc.SpanID)
copy(b[19:27], sc.SpanID[:])
b[27] = 2
b[28] = sc.TraceFlags
return b[:]
@@ -60,7 +58,7 @@ func (bp binaryPropagator) FromBytes(b []byte) (sc core.SpanContext) {
return core.EmptySpanContext()
}
if len(b) >= 9 && b[0] == 1 {
sc.SpanID = binary.BigEndian.Uint64(b[1:9])
copy(sc.SpanID[:], b[1:9])
b = b[9:]
}
if len(b) >= 2 && b[0] == 2 {

View File

@@ -15,7 +15,6 @@
package propagation_test
import (
"encoding/hex"
"testing"
"github.com/google/go-cmp/cmp"
@@ -25,11 +24,9 @@ import (
)
func TestExtractSpanContextFromBytes(t *testing.T) {
bTraceID, _ := hex.DecodeString("4bf92f3577b34da6a3ce929d0e0e4736")
traceID := core.TraceID{}
copy(traceID[:], bTraceID)
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
spanID := uint64(0x00f067aa0ba902b7)
propagator := propagation.BinaryPropagator()
tests := []struct {
name string
@@ -123,11 +120,9 @@ func TestExtractSpanContextFromBytes(t *testing.T) {
}
func TestConvertSpanContextToBytes(t *testing.T) {
bTraceID, _ := hex.DecodeString("4bf92f3577b34da6a3ce929d0e0e4736")
traceID := core.TraceID{}
copy(traceID[:], bTraceID)
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
spanID := uint64(0x00f067aa0ba902b7)
propagator := propagation.BinaryPropagator()
tests := []struct {
name string

View File

@@ -17,8 +17,6 @@ package propagation
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"go.opentelemetry.io/api/trace"
@@ -57,8 +55,6 @@ type HTTPB3Propagator struct {
var _ apipropagation.TextFormatPropagator = HTTPB3Propagator{}
var hexStr16ByteRegex = regexp.MustCompile("^[a-f0-9]{16}$")
func (b3 HTTPB3Propagator) Inject(ctx context.Context, supplier apipropagation.Supplier) {
sc := trace.CurrentSpan(ctx).SpanContext()
if sc.IsValid() {
@@ -98,12 +94,12 @@ func (b3 HTTPB3Propagator) GetAllKeys() []string {
}
func (b3 HTTPB3Propagator) extract(supplier apipropagation.Supplier) core.SpanContext {
tid, ok := b3.extractTraceID(supplier.Get(B3TraceIDHeader))
if !ok {
tid, err := core.TraceIDFromHex(supplier.Get(B3TraceIDHeader))
if err != nil {
return core.EmptySpanContext()
}
sid, ok := b3.extractSpanID(supplier.Get(B3SpanIDHeader))
if !ok {
sid, err := core.SpanIDFromHex(supplier.Get(B3SpanIDHeader))
if err != nil {
return core.EmptySpanContext()
}
sampled, ok := b3.extractSampledState(supplier.Get(B3SampledHeader))
@@ -148,26 +144,27 @@ func (b3 HTTPB3Propagator) extractSingleHeader(supplier apipropagation.Supplier)
return core.EmptySpanContext()
}
var ok bool
sc.TraceID, ok = b3.extractTraceID(parts[0])
if !ok {
var err error
sc.TraceID, err = core.TraceIDFromHex(parts[0])
if err != nil {
return core.EmptySpanContext()
}
sc.SpanID, ok = b3.extractSpanID(parts[1])
if !ok {
sc.SpanID, err = core.SpanIDFromHex(parts[1])
if err != nil {
return core.EmptySpanContext()
}
if l > 2 {
var ok bool
sc.TraceFlags, ok = b3.extractSampledState(parts[2])
if !ok {
return core.EmptySpanContext()
}
}
if l == 4 {
_, ok = b3.extractSpanID(parts[3])
if !ok {
_, err = core.SpanIDFromHex(parts[3])
if err != nil {
return core.EmptySpanContext()
}
}
@@ -179,21 +176,6 @@ func (b3 HTTPB3Propagator) extractSingleHeader(supplier apipropagation.Supplier)
return sc
}
// extractTraceID parses the value of the X-B3-TraceId b3Header.
func (b3 HTTPB3Propagator) extractTraceID(tid string) (traceID core.TraceID, ok bool) {
traceID, err := core.TraceIDFromHex(tid)
return traceID, err == nil
}
// extractSpanID parses the value of the X-B3-SpanId or X-B3-ParentSpanId headers.
func (b3 HTTPB3Propagator) extractSpanID(sid string) (spanID uint64, ok bool) {
if hexStr16ByteRegex.MatchString(sid) {
spanID, _ = strconv.ParseUint(sid, 16, 64)
ok = true
}
return spanID, ok
}
// extractSampledState parses the value of the X-B3-Sampled b3Header.
func (b3 HTTPB3Propagator) extractSampledState(sampled string) (flag byte, ok bool) {
switch sampled {

View File

@@ -20,7 +20,6 @@ import (
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"go.opentelemetry.io/api/core"
@@ -119,7 +118,7 @@ func (hp HTTPTraceContextPropagator) extractSpanContext(
var sc core.SpanContext
_, err = hex.Decode(sc.TraceID[0:16], []byte(sections[1][0:32]))
sc.TraceID, err = core.TraceIDFromHex(sections[1][:32])
if err != nil {
return core.EmptySpanContext()
}
@@ -127,11 +126,10 @@ func (hp HTTPTraceContextPropagator) extractSpanContext(
if len(sections[2]) != 16 {
return core.EmptySpanContext()
}
result, err := strconv.ParseUint(sections[2][0:], 16, 64)
sc.SpanID, err = core.SpanIDFromHex(sections[2][:])
if err != nil {
return core.EmptySpanContext()
}
sc.SpanID = result
if len(sections[3]) != 2 {
return core.EmptySpanContext()

View File

@@ -2,7 +2,6 @@ package propagation
import (
"context"
"encoding/hex"
"net/http"
"testing"
@@ -25,13 +24,9 @@ func BenchmarkInject(b *testing.B) {
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)
var id uint64
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
mockTracer := &mocktrace.MockTracer{
Sampled: false,

View File

@@ -32,7 +32,7 @@ import (
var (
traceID = mustTraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID = uint64(0x00f067aa0ba902b7)
spanID = mustSpanIDFromHex("00f067aa0ba902b7")
)
func mustTraceIDFromHex(s string) (t core.TraceID) {
@@ -40,6 +40,11 @@ func mustTraceIDFromHex(s string) (t core.TraceID) {
return
}
func mustSpanIDFromHex(s string) (t core.SpanID) {
t, _ = core.SpanIDFromHex(s)
return
}
func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
var propagator propagation.HTTPTraceContextPropagator
tests := []struct {