Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
IbraheemA committed Sep 23, 2024
1 parent ab06928 commit 8f6fe76
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
1 change: 0 additions & 1 deletion pkg/api/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func GetDCAAuthToken() string {

// Validate validates an http request
func Validate(w http.ResponseWriter, r *http.Request) error {
return nil
var err error
auth := r.Header.Get("Authorization")
if auth == "" {
Expand Down
35 changes: 18 additions & 17 deletions pkg/trace/api/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ var _ (ptraceotlp.GRPCServer) = (*OTLPReceiver)(nil)
// data on two ports for both plain HTTP and gRPC.
type OTLPReceiver struct {
ptraceotlp.UnimplementedGRPCServer
wg sync.WaitGroup // waits for a graceful shutdown
grpcsrv *grpc.Server // the running GRPC server on a started receiver, if enabled
out chan<- *Payload // the outgoing payload channel
conf *config.AgentConfig // receiver config
cidProvider IDProvider // container ID provider
statsd statsd.ClientInterface
timing timing.Reporter
wg sync.WaitGroup // waits for a graceful shutdown
grpcsrv *grpc.Server // the running GRPC server on a started receiver, if enabled
out chan<- *Payload // the outgoing payload channel
conf *config.AgentConfig // receiver config
cidProvider IDProvider // container ID provider
statsd statsd.ClientInterface
timing timing.Reporter
ignoreResNames map[string]struct{}
}

// NewOTLPReceiver returns a new OTLPReceiver which sends any incoming traces down the out channel.
Expand All @@ -64,8 +65,12 @@ func NewOTLPReceiver(out chan<- *Payload, cfg *config.AgentConfig, statsd statsd
if cfg.HasFeature("enable_otlp_compute_top_level_by_span_kind") {
computeTopLevelBySpanKindVal = 1.0
}
ignoreResNames := make(map[string]struct{})
for _, resName := range cfg.Ignore["resource"] {
ignoreResNames[resName] = struct{}{}
}
_ = statsd.Gauge("datadog.trace_agent.otlp.compute_top_level_by_span_kind", computeTopLevelBySpanKindVal, nil, 1)
return &OTLPReceiver{out: out, conf: cfg, cidProvider: NewIDProvider(cfg.ContainerProcRoot), statsd: statsd, timing: timing}
return &OTLPReceiver{out: out, conf: cfg, cidProvider: NewIDProvider(cfg.ContainerProcRoot), statsd: statsd, timing: timing, ignoreResNames: ignoreResNames}
}

// Start starts the OTLPReceiver, if any of the servers were configured as active.
Expand Down Expand Up @@ -190,9 +195,8 @@ func (o *OTLPReceiver) SetOTelAttributeTranslator(attrstrans *attributes.Transla
func (o *OTLPReceiver) ReceiveResourceSpans(ctx context.Context, rspans ptrace.ResourceSpans, httpHeader http.Header) source.Source {
if o.conf.HasFeature("enable_receive_resource_spans_v2") {
return o.receiveResourceSpansV2(ctx, rspans, httpHeader)
} else {
return o.receiveResourceSpansV1(ctx, rspans, httpHeader)
}
return o.receiveResourceSpansV1(ctx, rspans, httpHeader)
}

func (o *OTLPReceiver) receiveResourceSpansV2(ctx context.Context, rspans ptrace.ResourceSpans, httpHeader http.Header) source.Source {
Expand Down Expand Up @@ -225,18 +229,14 @@ func (o *OTLPReceiver) receiveResourceSpansV2(ctx context.Context, rspans ptrace

// 2. Transform OTLP spans to DD spans. If metadata was missing in resource attributes, attempt to get it from spans
topLevelByKind := o.conf.HasFeature("enable_otlp_compute_top_level_by_span_kind")
ignoreResNames := make(map[string]struct{})
for _, resName := range o.conf.Ignore["resource"] {
ignoreResNames[resName] = struct{}{}
}
tracesByID := make(map[uint64]pb.Trace)
priorityByID := make(map[uint64]sampler.SamplingPriority)
var spancount int64
for j := 0; j < rspans.ScopeSpans().Len(); j++ {
libspans := rspans.ScopeSpans().At(j)
for k := 0; k < libspans.Spans().Len(); k++ {
otelspan := libspans.Spans().At(k)
if _, exists := ignoreResNames[traceutil.GetOTelResource(otelspan, otelres)]; exists {
if _, exists := o.ignoreResNames[traceutil.GetOTelResource(otelspan, otelres)]; exists {
continue
}

Expand All @@ -251,8 +251,9 @@ func (o *OTLPReceiver) receiveResourceSpansV2(ctx context.Context, rspans ptrace
}
if env == "" {
// no env at resource level, try the first span
if v := ddspan.Meta["env"]; v != "" {
env = v
// TODO(songy23): use AttributeDeploymentEnvironmentName once collector version upgrade is unblocked
if spanEnv := traceutil.GetOTelAttrVal(otelspan.Attributes(), false, "deployment.environment.name", semconv.AttributeDeploymentEnvironment); spanEnv != "" {
env = spanEnv
}
}
if containerID == "" {
Expand Down
11 changes: 3 additions & 8 deletions pkg/trace/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func OtelSpanToDDSpan(
for k, v := range rattr {
if k == "service.name" || k == "operation.name" || k == "resource.name" || k == "span.type" {
continue
} else if k == "analytics.event" {
}
if k == "analytics.event" {
if v, err := strconv.ParseBool(v); err == nil {
if v {
traceutil.SetMetric(ddspan, sampler.KeySamplingRateEventExtraction, 1)
Expand Down Expand Up @@ -160,12 +161,6 @@ func OtelSpanToDDSpan(
return true
})

if _, ok := ddspan.Meta["env"]; !ok {
// TODO(songy23): use AttributeDeploymentEnvironmentName once collector version upgrade is unblocked
if _, env := GetFirstFromMap(ddspan.Meta, "deployment.environment.name", semconv.AttributeDeploymentEnvironment); env != "" {
traceutil.SetMeta(ddspan, "env", traceutil.NormalizeTag(env))
}
}
if otelspan.TraceState().AsRaw() != "" {
traceutil.SetMeta(ddspan, "w3c.tracestate", otelspan.TraceState().AsRaw())
}
Expand Down Expand Up @@ -368,7 +363,7 @@ func Status2Error(status ptrace.Status, events ptrace.SpanEventSlice, span *pb.S
}
}

// getFirstFromMap checks each key in the given keys in the map and returns the first key-value pair whose
// GetFirstFromMap checks each key in the given keys in the map and returns the first key-value pair whose
// key matches, or empty strings if none matches.
func GetFirstFromMap(m map[string]string, keys ...string) (string, string) {
for _, key := range keys {
Expand Down

0 comments on commit 8f6fe76

Please sign in to comment.