2022-04-06 23:20:29 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
|
|
|
|
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-11 23:02:13 +00:00
|
|
|
MaxTraceAmount = 20
|
2022-04-06 23:20:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-04-11 19:57:50 +00:00
|
|
|
errNoTraces = errors.New("no traces recorded to be exported")
|
|
|
|
errNoopTracer = errors.New("noop tracer has no traces")
|
2022-04-06 23:20:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2022-10-18 11:15:51 +00:00
|
|
|
// 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)
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InMemoryOtlpClient is a client implementation for otlptrace.Client
|
|
|
|
type InMemoryOtlpClient struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
spans []*tracepb.ResourceSpans
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *InMemoryOtlpClient) Start(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *InMemoryOtlpClient) Stop(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadTraces adds the provided list of spans to the in-memory list.
|
|
|
|
func (mc *InMemoryOtlpClient) UploadTraces(_ context.Context, protoSpans []*tracepb.ResourceSpans) error {
|
|
|
|
mc.mu.Lock()
|
|
|
|
defer mc.mu.Unlock()
|
|
|
|
// Catch to make sure too many traces aren't being added to response header.
|
|
|
|
// Returning nil makes sure we don't fail to send the traces we already recorded.
|
2022-04-11 23:02:13 +00:00
|
|
|
if len(mc.spans)+len(protoSpans) > MaxTraceAmount {
|
2022-04-06 23:20:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
mc.spans = append(mc.spans, protoSpans...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spans returns the list of in-memory stored spans as a base64 encoded otlp protobuf string.
|
|
|
|
func (mc *InMemoryOtlpClient) Spans() (string, error) {
|
2022-10-18 11:15:51 +00:00
|
|
|
data, err := mc.ExportProtoSpans()
|
2022-09-09 04:42:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base64.StdEncoding.EncodeToString(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProtoSpans returns the list of in-memory stored spans as the protobuf byte array.
|
2022-10-18 11:15:51 +00:00
|
|
|
func (mc *InMemoryOtlpClient) ExportProtoSpans() ([]byte, error) {
|
2022-04-06 23:20:29 +00:00
|
|
|
mc.mu.Lock()
|
|
|
|
defer mc.mu.Unlock()
|
|
|
|
if len(mc.spans) <= 0 {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, errNoTraces
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
pbRequest := &coltracepb.ExportTraceServiceRequest{
|
|
|
|
ResourceSpans: mc.spans,
|
|
|
|
}
|
2022-10-18 11:15:51 +00:00
|
|
|
serializedSpans, err := proto.Marshal(pbRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
mc.spans = make([]*tracepb.ResourceSpans, 0)
|
2022-10-18 11:15:51 +00:00
|
|
|
return serializedSpans, nil
|
2022-10-13 10:01:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 23:20:29 +00:00
|
|
|
// NoopOtlpClient is a client implementation for otlptrace.Client that does nothing
|
|
|
|
type NoopOtlpClient struct{}
|
|
|
|
|
|
|
|
func (mc *NoopOtlpClient) Start(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *NoopOtlpClient) Stop(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *NoopOtlpClient) UploadTraces(_ context.Context, _ []*tracepb.ResourceSpans) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spans always returns no traces error
|
|
|
|
func (mc *NoopOtlpClient) Spans() (string, error) {
|
2022-04-11 19:57:50 +00:00
|
|
|
return "", errNoopTracer
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
2022-09-09 04:42:11 +00:00
|
|
|
|
|
|
|
// Spans always returns no traces error
|
2022-10-18 11:15:51 +00:00
|
|
|
func (mc *NoopOtlpClient) ExportProtoSpans() ([]byte, error) {
|
2022-09-09 04:42:11 +00:00
|
|
|
return nil, errNoopTracer
|
|
|
|
}
|
2022-10-13 10:01:25 +00:00
|
|
|
|
|
|
|
func (mc *NoopOtlpClient) ClearSpans() {}
|