Skip to content

Commit 740b506

Browse files
authored
Merge pull request #1545 from ydb-platform/traceparent
Small broken change: Fixed `traceparent` header for tracing grpc requests + * Added method `ID()` into `spans.Span` interface
2 parents 15ac7bf + e0bfb7c commit 740b506

File tree

4 files changed

+82
-34
lines changed

4 files changed

+82
-34
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: 61 additions & 34 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("-01")
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) {
@@ -149,26 +169,28 @@ func driver(adapter Adapter) trace.Driver { //nolint:gocyclo,funlen
149169
if adapter.Details()&trace.DriverConnStreamEvents == 0 {
150170
return nil
151171
}
152-
start := childSpanWithReplaceCtx(
153-
adapter,
154-
info.Context,
155-
info.Call.FunctionID(),
172+
173+
var (
174+
call = info.Call
175+
start = adapter.SpanFromContext(*info.Context)
156176
)
157177

158178
return func(info trace.DriverConnStreamCloseSendDoneInfo) {
159-
finish(start, info.Error)
179+
if info.Error != nil {
180+
start.Log(call.FunctionID(), kv.Error(info.Error))
181+
}
160182
}
161183
},
162184
OnConnStreamFinish: func(info trace.DriverConnStreamFinishInfo) {
163185
if adapter.Details()&trace.DriverConnStreamEvents == 0 {
164186
return
165187
}
166-
start := childSpanWithReplaceCtx(
167-
adapter,
168-
&info.Context,
169-
info.Call.FunctionID(),
188+
189+
var (
190+
call = info.Call
191+
start = adapter.SpanFromContext(info.Context)
192+
counters = grpcStreamMsgCountersFromContext(info.Context)
170193
)
171-
counters := grpcStreamMsgCountersFromContext(info.Context)
172194

173195
attributes := make([]kv.KeyValue, 0, 2)
174196
if counters != nil {
@@ -177,42 +199,47 @@ func driver(adapter Adapter) trace.Driver { //nolint:gocyclo,funlen
177199
kv.Int64("sent_messages", counters.sentMessages()),
178200
)
179201
}
180-
finish(start, info.Error, attributes...)
202+
203+
if info.Error != nil {
204+
attributes = append(attributes, kv.Error(info.Error))
205+
}
206+
207+
start.Log(call.FunctionID(), attributes...)
181208
},
182209
OnConnPark: func(info trace.DriverConnParkStartInfo) func(trace.DriverConnParkDoneInfo) {
183210
if adapter.Details()&trace.DriverConnEvents == 0 {
184211
return nil
185212
}
186-
start := childSpanWithReplaceCtx(
187-
adapter,
188-
info.Context,
189-
info.Call.FunctionID(),
190-
kv.String("address", safeAddress(info.Endpoint)),
213+
214+
var (
215+
call = info.Call
216+
start = adapter.SpanFromContext(*info.Context)
191217
)
192218

193219
return func(info trace.DriverConnParkDoneInfo) {
194-
finish(
195-
start,
196-
info.Error,
197-
)
220+
if info.Error != nil {
221+
start.Log(call.FunctionID(), kv.Error(info.Error))
222+
} else {
223+
start.Log(call.FunctionID())
224+
}
198225
}
199226
},
200227
OnConnClose: func(info trace.DriverConnCloseStartInfo) func(trace.DriverConnCloseDoneInfo) {
201228
if adapter.Details()&trace.DriverConnEvents == 0 {
202229
return nil
203230
}
204-
start := childSpanWithReplaceCtx(
205-
adapter,
206-
info.Context,
207-
info.Call.FunctionID(),
208-
kv.String("address", safeAddress(info.Endpoint)),
231+
232+
var (
233+
call = info.Call
234+
start = adapter.SpanFromContext(*info.Context)
209235
)
210236

211237
return func(info trace.DriverConnCloseDoneInfo) {
212-
finish(
213-
start,
214-
info.Error,
215-
)
238+
if info.Error != nil {
239+
start.Log(call.FunctionID(), kv.Error(info.Error))
240+
} else {
241+
start.Log(call.FunctionID())
242+
}
216243
}
217244
},
218245
OnConnBan: func(info trace.DriverConnBanStartInfo) func(trace.DriverConnBanDoneInfo) {

spans/driver_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package spans
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func BenchmarkTraceparent(b *testing.B) {
10+
b.ReportAllocs()
11+
for i := 0; i < b.N; i++ {
12+
require.Equal(b,
13+
"00-8e3790822789a6917883e08d0eeb783e-729d847ca290963e-00",
14+
traceparent("8e3790822789a6917883e08d0eeb783e", "729d847ca290963e"),
15+
)
16+
}
17+
}

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)