TUN-6867: Clear spans right after they are serialized to avoid returning duplicate spans

This commit is contained in:
cthuang 2022-10-18 12:15:51 +01:00 committed by Chung-Ting
parent b1de2a74fa
commit c3c050aa79
3 changed files with 12 additions and 21 deletions

View File

@ -176,8 +176,6 @@ func (pr *packetResponder) exportSpan() {
} }
spans := pr.tracedCtx.GetProtoSpans() spans := pr.tracedCtx.GetProtoSpans()
if len(spans) > 0 { if len(spans) > 0 {
// Make sure spans are cleared after they are sent
defer pr.tracedCtx.ClearSpans()
pr.datagramMuxer.SendPacket(&quicpogs.TracingSpanPacket{ pr.datagramMuxer.SendPacket(&quicpogs.TracingSpanPacket{
Spans: spans, Spans: spans,
TracingIdentity: pr.serializedIdentity, TracingIdentity: pr.serializedIdentity,

View File

@ -24,11 +24,9 @@ type InMemoryClient interface {
// Spans returns a copy of the list of in-memory stored spans as a base64 // Spans returns a copy of the list of in-memory stored spans as a base64
// encoded otlp protobuf string. // encoded otlp protobuf string.
Spans() (string, error) Spans() (string, error)
// ProtoSpans returns a copy of the list of in-memory stored spans as otlp // ExportProtoSpans returns a copy of the list of in-memory stored spans as otlp
// protobuf byte array. // protobuf byte array and clears the in-memory spans.
ProtoSpans() ([]byte, error) ExportProtoSpans() ([]byte, error)
// Clear spans removes all in-memory spans
ClearSpans()
} }
// InMemoryOtlpClient is a client implementation for otlptrace.Client // InMemoryOtlpClient is a client implementation for otlptrace.Client
@ -60,7 +58,7 @@ func (mc *InMemoryOtlpClient) UploadTraces(_ context.Context, protoSpans []*trac
// Spans returns the list of in-memory stored spans as a base64 encoded otlp protobuf string. // Spans returns the list of in-memory stored spans as a base64 encoded otlp protobuf string.
func (mc *InMemoryOtlpClient) Spans() (string, error) { func (mc *InMemoryOtlpClient) Spans() (string, error) {
data, err := mc.ProtoSpans() data, err := mc.ExportProtoSpans()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -68,7 +66,7 @@ func (mc *InMemoryOtlpClient) Spans() (string, error) {
} }
// ProtoSpans returns the list of in-memory stored spans as the protobuf byte array. // ProtoSpans returns the list of in-memory stored spans as the protobuf byte array.
func (mc *InMemoryOtlpClient) ProtoSpans() ([]byte, error) { func (mc *InMemoryOtlpClient) ExportProtoSpans() ([]byte, error) {
mc.mu.Lock() mc.mu.Lock()
defer mc.mu.Unlock() defer mc.mu.Unlock()
if len(mc.spans) <= 0 { if len(mc.spans) <= 0 {
@ -77,13 +75,12 @@ func (mc *InMemoryOtlpClient) ProtoSpans() ([]byte, error) {
pbRequest := &coltracepb.ExportTraceServiceRequest{ pbRequest := &coltracepb.ExportTraceServiceRequest{
ResourceSpans: mc.spans, ResourceSpans: mc.spans,
} }
return proto.Marshal(pbRequest) serializedSpans, err := proto.Marshal(pbRequest)
if err != nil {
return nil, err
} }
func (mc *InMemoryOtlpClient) ClearSpans() {
mc.mu.Lock()
defer mc.mu.Unlock()
mc.spans = make([]*tracepb.ResourceSpans, 0) mc.spans = make([]*tracepb.ResourceSpans, 0)
return serializedSpans, nil
} }
// NoopOtlpClient is a client implementation for otlptrace.Client that does nothing // NoopOtlpClient is a client implementation for otlptrace.Client that does nothing
@ -107,7 +104,7 @@ func (mc *NoopOtlpClient) Spans() (string, error) {
} }
// Spans always returns no traces error // Spans always returns no traces error
func (mc *NoopOtlpClient) ProtoSpans() ([]byte, error) { func (mc *NoopOtlpClient) ExportProtoSpans() ([]byte, error) {
return nil, errNoopTracer return nil, errNoopTracer
} }

View File

@ -157,7 +157,7 @@ func (cft *cfdTracer) GetSpans() (enc string) {
// GetProtoSpans returns the spans as the otlp traces in protobuf byte array. // GetProtoSpans returns the spans as the otlp traces in protobuf byte array.
func (cft *cfdTracer) GetProtoSpans() (proto []byte) { func (cft *cfdTracer) GetProtoSpans() (proto []byte) {
proto, err := cft.exporter.ProtoSpans() proto, err := cft.exporter.ExportProtoSpans()
switch err { switch err {
case nil: case nil:
break break
@ -189,10 +189,6 @@ func (cft *cfdTracer) AddSpans(headers http.Header) {
headers[CanonicalCloudflaredTracingHeader] = []string{enc} headers[CanonicalCloudflaredTracingHeader] = []string{enc}
} }
func (cft *cfdTracer) ClearSpans() {
cft.exporter.ClearSpans()
}
// End will set the OK status for the span and then end it. // End will set the OK status for the span and then end it.
func End(span trace.Span) { func End(span trace.Span) {
endSpan(span, -1, codes.Ok, nil) endSpan(span, -1, codes.Ok, nil)