Skip to content

Commit bc61e61

Browse files
committed
Fixed traceparent header for tracing grpc requests + * Added method ID() into spans.Span interface
1 parent 15ac7bf commit bc61e61

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Small broken change: added method `ID()` into `spans.Span` interface (need to implement in adapter)
2+
* Fixed traceparent header for tracing grpc requests
3+
14
## v3.90.0
25
* Fixed closing of child driver with shared balancer
36

spans/driver.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@ import (
88
"sync/atomic"
99

1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/kv"
11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
1112
"github.com/ydb-platform/ydb-go-sdk/v3/meta"
1213
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
1314
)
1415

16+
func traceparent(traceID string, spanID string) string {
17+
b := xstring.Buffer()
18+
defer b.Free()
19+
20+
b.WriteString("00-")
21+
b.WriteString(traceID)
22+
b.WriteByte('-')
23+
b.WriteString(spanID)
24+
b.WriteString("-0")
25+
26+
return b.String()
27+
}
28+
1529
// driver makes driver with publishing traces
1630
func driver(adapter Adapter) trace.Driver { //nolint:gocyclo,funlen
1731
return trace.Driver{
@@ -56,9 +70,7 @@ func driver(adapter Adapter) trace.Driver { //nolint:gocyclo,funlen
5670
if adapter.Details()&trace.DriverConnEvents == 0 {
5771
return nil
5872
}
59-
if traceID, valid := adapter.SpanFromContext(*info.Context).TraceID(); valid {
60-
*info.Context = meta.WithTraceParent(*info.Context, traceID)
61-
}
73+
6274
start := childSpanWithReplaceCtx(
6375
adapter,
6476
info.Context,
@@ -67,6 +79,12 @@ func driver(adapter Adapter) trace.Driver { //nolint:gocyclo,funlen
6779
kv.String("method", string(info.Method)),
6880
)
6981

82+
if id, valid := start.ID(); valid {
83+
if traceID, valid := start.TraceID(); valid {
84+
*info.Context = meta.WithTraceParent(*info.Context, traceparent(traceID, id))
85+
}
86+
}
87+
7088
return func(info trace.DriverConnInvokeDoneInfo) {
7189
fields := []KeyValue{
7290
kv.String("opID", info.OpID),
@@ -101,8 +119,10 @@ func driver(adapter Adapter) trace.Driver { //nolint:gocyclo,funlen
101119
kv.String("method", string(info.Method)),
102120
)
103121

104-
if traceID, valid := start.TraceID(); valid {
105-
*info.Context = meta.WithTraceParent(*info.Context, traceID)
122+
if id, valid := start.ID(); valid {
123+
if traceID, valid := start.TraceID(); valid {
124+
*info.Context = meta.WithTraceParent(*info.Context, traceparent(traceID, id))
125+
}
106126
}
107127

108128
return func(info trace.DriverConnNewStreamDoneInfo) {

spans/driver_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package spans
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
11+
)
12+
13+
func traceparentFmt(traceID string, spanID string) string {
14+
return fmt.Sprintf("00-%s-%s-0", traceID, spanID)
15+
}
16+
17+
func BenchmarkTraceparentFmt(b *testing.B) {
18+
b.ReportAllocs()
19+
for i := 0; i < b.N; i++ {
20+
traceparent := traceparentFmt("8e3790822789a6917883e08d0eeb783e", "729d847ca290963e")
21+
require.Equal(b, "00-8e3790822789a6917883e08d0eeb783e-729d847ca290963e-0", traceparent)
22+
}
23+
}
24+
25+
func traceparentBuilder(traceID string, spanID string) string {
26+
var b strings.Builder
27+
b.WriteString("00-")
28+
b.WriteString(traceID)
29+
b.WriteByte('-')
30+
b.WriteString(spanID)
31+
b.WriteString("-0")
32+
33+
return b.String()
34+
}
35+
36+
func BenchmarkTraceparentBuilder(b *testing.B) {
37+
b.ReportAllocs()
38+
for i := 0; i < b.N; i++ {
39+
traceparent := traceparentBuilder("8e3790822789a6917883e08d0eeb783e", "729d847ca290963e")
40+
require.Equal(b, "00-8e3790822789a6917883e08d0eeb783e-729d847ca290963e-0", traceparent)
41+
}
42+
}
43+
44+
func traceparentBuffer(traceID string, spanID string) string {
45+
b := xstring.Buffer()
46+
defer b.Free()
47+
48+
b.WriteString("00-")
49+
b.WriteString(traceID)
50+
b.WriteByte('-')
51+
b.WriteString(spanID)
52+
b.WriteString("-0")
53+
54+
return b.String()
55+
}
56+
57+
func BenchmarkTraceparentBuffer(b *testing.B) {
58+
b.ReportAllocs()
59+
for i := 0; i < b.N; i++ {
60+
traceparent := traceparentBuffer("8e3790822789a6917883e08d0eeb783e", "729d847ca290963e")
61+
require.Equal(b, "00-8e3790822789a6917883e08d0eeb783e-729d847ca290963e-0", traceparent)
62+
}
63+
}
64+
65+
func BenchmarkTraceparent(b *testing.B) {
66+
b.ReportAllocs()
67+
for i := 0; i < b.N; i++ {
68+
traceparent := traceparent("8e3790822789a6917883e08d0eeb783e", "729d847ca290963e")
69+
require.Equal(b, "00-8e3790822789a6917883e08d0eeb783e-729d847ca290963e-0", traceparent)
70+
}
71+
}

spans/spans.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
//
1616
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
1717
Span interface {
18+
ID() (_ string, valid bool)
1819
TraceID() (_ string, valid bool)
1920

2021
Link(link Span, attributes ...KeyValue)

0 commit comments

Comments
 (0)