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

@@ -24,11 +24,9 @@ type InMemoryClient interface {
// Spans returns a copy of the list of in-memory stored spans as a base64
// encoded otlp protobuf string.
Spans() (string, error)
// ProtoSpans returns a copy of the list of in-memory stored spans as otlp
// protobuf byte array.
ProtoSpans() ([]byte, error)
// Clear spans removes all in-memory spans
ClearSpans()
// ExportProtoSpans returns a copy of the list of in-memory stored spans as otlp
// protobuf byte array and clears the in-memory spans.
ExportProtoSpans() ([]byte, error)
}
// 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.
func (mc *InMemoryOtlpClient) Spans() (string, error) {
data, err := mc.ProtoSpans()
data, err := mc.ExportProtoSpans()
if err != nil {
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.
func (mc *InMemoryOtlpClient) ProtoSpans() ([]byte, error) {
func (mc *InMemoryOtlpClient) ExportProtoSpans() ([]byte, error) {
mc.mu.Lock()
defer mc.mu.Unlock()
if len(mc.spans) <= 0 {
@@ -77,13 +75,12 @@ func (mc *InMemoryOtlpClient) ProtoSpans() ([]byte, error) {
pbRequest := &coltracepb.ExportTraceServiceRequest{
ResourceSpans: mc.spans,
}
return proto.Marshal(pbRequest)
}
func (mc *InMemoryOtlpClient) ClearSpans() {
mc.mu.Lock()
defer mc.mu.Unlock()
serializedSpans, err := proto.Marshal(pbRequest)
if err != nil {
return nil, err
}
mc.spans = make([]*tracepb.ResourceSpans, 0)
return serializedSpans, nil
}
// 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
func (mc *NoopOtlpClient) ProtoSpans() ([]byte, error) {
func (mc *NoopOtlpClient) ExportProtoSpans() ([]byte, error) {
return nil, errNoopTracer
}