TUN-5990: Add otlp span export to response header

This commit is contained in:
Devin Carr
2022-04-11 12:57:50 -07:00
parent 8a07a900fd
commit f81b0ee9e8
4 changed files with 44 additions and 12 deletions

View File

@@ -16,7 +16,8 @@ const (
)
var (
errNoTraces = errors.New("no traces recorded to be exported")
errNoTraces = errors.New("no traces recorded to be exported")
errNoopTracer = errors.New("noop tracer has no traces")
)
type InMemoryClient interface {
@@ -86,5 +87,5 @@ func (mc *NoopOtlpClient) UploadTraces(_ context.Context, _ []*tracepb.ResourceS
// Spans always returns no traces error
func (mc *NoopOtlpClient) Spans() (string, error) {
return "", errNoTraces
return "", errNoopTracer
}

View File

@@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"github.com/rs/zerolog"
otelContrib "go.opentelemetry.io/contrib/propagators/Jaeger"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
@@ -23,11 +24,14 @@ const (
tracerContextName = "cf-trace-id"
tracerContextNameOverride = "uber-trace-id"
IntCloudflaredTracingHeader = "cf-int-cloudflared-tracing"
)
var (
Http2TransportAttribute = trace.WithAttributes(TransportAttributeKey.String("http2"))
QuicTransportAttribute = trace.WithAttributes(TransportAttributeKey.String("quic"))
CanonicalCloudflaredTracingHeader = http.CanonicalHeaderKey(IntCloudflaredTracingHeader)
Http2TransportAttribute = trace.WithAttributes(TransportAttributeKey.String("http2"))
QuicTransportAttribute = trace.WithAttributes(TransportAttributeKey.String("quic"))
TransportAttributeKey = attribute.Key("transport")
TrafficAttributeKey = attribute.Key("traffic")
@@ -75,8 +79,26 @@ func (cft *TracedRequest) Tracer() trace.Tracer {
}
// Spans returns the spans as base64 encoded protobuf otlp traces.
func (cft *TracedRequest) Spans() (string, error) {
return cft.exporter.Spans()
func (cft *TracedRequest) AddSpans(headers http.Header, log *zerolog.Logger) {
enc, err := cft.exporter.Spans()
switch err {
case nil:
break
case errNoTraces:
log.Error().Err(err).Msgf("expected traces to be available")
return
case errNoopTracer:
return // noop tracer has no traces
default:
log.Error().Err(err)
return
}
// No need to add header if no traces
if enc == "" {
log.Error().Msgf("no traces provided and no error from exporter")
return
}
headers[CanonicalCloudflaredTracingHeader] = []string{enc}
}
// EndWithStatus will set a status for the span and then end it.