From 99f017035e91e5fec1ffcadd73d34de63ef738ed Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Mon, 20 Jun 2022 12:03:03 +0200 Subject: [PATCH] Adapt serializer and trace OTLP code to new changes --- .../internal/serializerexporter/exporter.go | 20 +++++---- pkg/trace/api/otlp.go | 41 ++++++++++--------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/pkg/otlp/internal/serializerexporter/exporter.go b/pkg/otlp/internal/serializerexporter/exporter.go index fd06b86c33f2f..e973095976e56 100644 --- a/pkg/otlp/internal/serializerexporter/exporter.go +++ b/pkg/otlp/internal/serializerexporter/exporter.go @@ -9,6 +9,7 @@ import ( "context" "fmt" + "github.com/DataDog/datadog-agent/pkg/otlp/model/source" "github.com/DataDog/datadog-agent/pkg/otlp/model/translator" "github.com/DataDog/datadog-agent/pkg/serializer" "github.com/DataDog/datadog-agent/pkg/tagger/collectors" @@ -51,14 +52,19 @@ func newDefaultConfig() config.Exporter { } } -var _ translator.HostnameProvider = (*hostnameProviderFunc)(nil) +var _ source.Provider = (*sourceProviderFunc)(nil) -// hostnameProviderFunc is an adapter to allow the use of a function as a translator.HostnameProvider. -type hostnameProviderFunc func(context.Context) (string, error) +// sourceProviderFunc is an adapter to allow the use of a function as a translator.HostnameProvider. +type sourceProviderFunc func(context.Context) (string, error) -// Hostname calls f. -func (f hostnameProviderFunc) Hostname(ctx context.Context) (string, error) { - return f(ctx) +// Source calls f and wraps in a source struct. +func (f sourceProviderFunc) Source(ctx context.Context) (source.Source, error) { + hostname, err := f(ctx) + if err != nil { + return source.Source{}, err + } + + return source.Source{Kind: source.HostnameKind, Identifier: hostname}, nil } // exporter translate OTLP metrics into the Datadog format and sends @@ -81,7 +87,7 @@ func translatorFromConfig(logger *zap.Logger, cfg *exporterConfig) (*translator. } options := []translator.Option{ - translator.WithFallbackHostnameProvider(hostnameProviderFunc(util.GetHostname)), + translator.WithFallbackSourceProvider(sourceProviderFunc(util.GetHostname)), translator.WithHistogramMode(histogramMode), translator.WithDeltaTTL(cfg.Metrics.DeltaTTL), } diff --git a/pkg/trace/api/otlp.go b/pkg/trace/api/otlp.go index c93242b128ebd..d89f06147346f 100644 --- a/pkg/trace/api/otlp.go +++ b/pkg/trace/api/otlp.go @@ -20,6 +20,7 @@ import ( "time" "github.com/DataDog/datadog-agent/pkg/otlp/model/attributes" + "github.com/DataDog/datadog-agent/pkg/otlp/model/source" "github.com/DataDog/datadog-agent/pkg/trace/api/apiutil" "github.com/DataDog/datadog-agent/pkg/trace/config" "github.com/DataDog/datadog-agent/pkg/trace/info" @@ -207,17 +208,8 @@ func (o *OTLPReceiver) processRequest(protocol string, header http.Header, in pt } } -// OTLPIngestSummary returns a summary of the received resource spans. -type OTLPIngestSummary struct { - // Hostname indicates the hostname of the passed resource spans. - Hostname string - // Tags returns a set of Datadog-specific tags which are relevant for identifying - // the source of the passed resource spans. - Tags []string -} - // ReceiveResourceSpans processes the given rspans and sends them to writer. -func (o *OTLPReceiver) ReceiveResourceSpans(rspans ptrace.ResourceSpans, header http.Header, protocol string) OTLPIngestSummary { +func (o *OTLPReceiver) ReceiveResourceSpans(rspans ptrace.ResourceSpans, header http.Header, protocol string) source.Source { // each rspans is coming from a different resource and should be considered // a separate payload; typically there is only one item in this slice attr := rspans.Resource().Attributes() @@ -226,15 +218,15 @@ func (o *OTLPReceiver) ReceiveResourceSpans(rspans ptrace.ResourceSpans, header rattr[k] = v.AsString() return true }) - hostname, hostok := attributes.HostnameFromAttributes(attr, o.conf.OTLPReceiver.UsePreviewHostnameLogic) + src, srcok := attributes.SourceFromAttributes(attr, o.conf.OTLPReceiver.UsePreviewHostnameLogic) hostFromMap := func(m map[string]string, key string) { // hostFromMap sets the hostname to m[key] if it is set. if v, ok := m[key]; ok { - hostname = v - hostok = true + src = source.Source{Kind: source.HostnameKind, Identifier: v} + srcok = true } } - if !hostok { + if !srcok { hostFromMap(rattr, "_dd.hostname") } env := rattr[string(semconv.AttributeDeploymentEnvironment)] @@ -272,7 +264,7 @@ func (o *OTLPReceiver) ReceiveResourceSpans(rspans ptrace.ResourceSpans, header tracesByID[traceID] = pb.Trace{} } ddspan := o.convertSpan(rattr, lib, span) - if !hostok { + if !srcok { // if we didn't find a hostname at the resource level // try and see if the span has a hostname set hostFromMap(ddspan.Meta, "_dd.hostname") @@ -318,9 +310,21 @@ func (o *OTLPReceiver) ReceiveResourceSpans(rspans ptrace.ResourceSpans, header if env == "" { env = o.conf.DefaultEnv } - if !hostok { + + // Get the hostname or set to empty + // if source is empty + var hostname string + if srcok { + switch src.Kind { + case source.HostnameKind: + hostname = src.Identifier + default: + hostname = "" + } + } else { hostname = o.conf.Hostname } + p.TracerPayload = &pb.TracerPayload{ Hostname: hostname, Chunks: traceChunks, @@ -341,10 +345,7 @@ func (o *OTLPReceiver) ReceiveResourceSpans(rspans ptrace.ResourceSpans, header default: log.Warn("Payload in channel full. Dropped 1 payload.") } - return OTLPIngestSummary{ - Hostname: hostname, - Tags: attributes.RunningTagsFromAttributes(attr), - } + return src } // marshalEvents marshals events into JSON.