Skip to content

Commit c3c050a

Browse files
committed
TUN-6867: Clear spans right after they are serialized to avoid returning duplicate spans
1 parent b1de2a7 commit c3c050a

File tree

3 files changed

+12
-21
lines changed

3 files changed

+12
-21
lines changed

ingress/packet_router.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ func (pr *packetResponder) exportSpan() {
176176
}
177177
spans := pr.tracedCtx.GetProtoSpans()
178178
if len(spans) > 0 {
179-
// Make sure spans are cleared after they are sent
180-
defer pr.tracedCtx.ClearSpans()
181179
pr.datagramMuxer.SendPacket(&quicpogs.TracingSpanPacket{
182180
Spans: spans,
183181
TracingIdentity: pr.serializedIdentity,

tracing/client.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ type InMemoryClient interface {
2424
// Spans returns a copy of the list of in-memory stored spans as a base64
2525
// encoded otlp protobuf string.
2626
Spans() (string, error)
27-
// ProtoSpans returns a copy of the list of in-memory stored spans as otlp
28-
// protobuf byte array.
29-
ProtoSpans() ([]byte, error)
30-
// Clear spans removes all in-memory spans
31-
ClearSpans()
27+
// ExportProtoSpans returns a copy of the list of in-memory stored spans as otlp
28+
// protobuf byte array and clears the in-memory spans.
29+
ExportProtoSpans() ([]byte, error)
3230
}
3331

3432
// InMemoryOtlpClient is a client implementation for otlptrace.Client
@@ -60,15 +58,15 @@ func (mc *InMemoryOtlpClient) UploadTraces(_ context.Context, protoSpans []*trac
6058

6159
// Spans returns the list of in-memory stored spans as a base64 encoded otlp protobuf string.
6260
func (mc *InMemoryOtlpClient) Spans() (string, error) {
63-
data, err := mc.ProtoSpans()
61+
data, err := mc.ExportProtoSpans()
6462
if err != nil {
6563
return "", err
6664
}
6765
return base64.StdEncoding.EncodeToString(data), nil
6866
}
6967

7068
// ProtoSpans returns the list of in-memory stored spans as the protobuf byte array.
71-
func (mc *InMemoryOtlpClient) ProtoSpans() ([]byte, error) {
69+
func (mc *InMemoryOtlpClient) ExportProtoSpans() ([]byte, error) {
7270
mc.mu.Lock()
7371
defer mc.mu.Unlock()
7472
if len(mc.spans) <= 0 {
@@ -77,13 +75,12 @@ func (mc *InMemoryOtlpClient) ProtoSpans() ([]byte, error) {
7775
pbRequest := &coltracepb.ExportTraceServiceRequest{
7876
ResourceSpans: mc.spans,
7977
}
80-
return proto.Marshal(pbRequest)
81-
}
82-
83-
func (mc *InMemoryOtlpClient) ClearSpans() {
84-
mc.mu.Lock()
85-
defer mc.mu.Unlock()
78+
serializedSpans, err := proto.Marshal(pbRequest)
79+
if err != nil {
80+
return nil, err
81+
}
8682
mc.spans = make([]*tracepb.ResourceSpans, 0)
83+
return serializedSpans, nil
8784
}
8885

8986
// NoopOtlpClient is a client implementation for otlptrace.Client that does nothing
@@ -107,7 +104,7 @@ func (mc *NoopOtlpClient) Spans() (string, error) {
107104
}
108105

109106
// Spans always returns no traces error
110-
func (mc *NoopOtlpClient) ProtoSpans() ([]byte, error) {
107+
func (mc *NoopOtlpClient) ExportProtoSpans() ([]byte, error) {
111108
return nil, errNoopTracer
112109
}
113110

tracing/tracing.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (cft *cfdTracer) GetSpans() (enc string) {
157157

158158
// GetProtoSpans returns the spans as the otlp traces in protobuf byte array.
159159
func (cft *cfdTracer) GetProtoSpans() (proto []byte) {
160-
proto, err := cft.exporter.ProtoSpans()
160+
proto, err := cft.exporter.ExportProtoSpans()
161161
switch err {
162162
case nil:
163163
break
@@ -189,10 +189,6 @@ func (cft *cfdTracer) AddSpans(headers http.Header) {
189189
headers[CanonicalCloudflaredTracingHeader] = []string{enc}
190190
}
191191

192-
func (cft *cfdTracer) ClearSpans() {
193-
cft.exporter.ClearSpans()
194-
}
195-
196192
// End will set the OK status for the span and then end it.
197193
func End(span trace.Span) {
198194
endSpan(span, -1, codes.Ok, nil)

0 commit comments

Comments
 (0)