Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Error Tracking Standalone Config option #30065

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions comp/trace/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,23 @@ func TestLoadEnv(t *testing.T) {
assert.Equal(t, 12.3, cfg.OTLPReceiver.ProbabilisticSampling)
})

env = "DD_APM_ERROR_TRACKING_STANDALONE_ENABLED"
t.Run(env, func(t *testing.T) {
t.Setenv(env, "true")

config := fxutil.Test[Component](t, fx.Options(
corecomp.MockModule(),
fx.Replace(corecomp.MockParams{
Params: corecomp.Params{ConfFilePath: "./testdata/undocumented.yaml"},
}),
MockModule(),
))
cfg := config.Object()

assert.NotNil(t, cfg)
assert.Equal(t, true, cfg.ErrorTrackingStandalone)
})

for _, envKey := range []string{
"DD_IGNORE_RESOURCE", // deprecated
"DD_APM_IGNORE_RESOURCES",
Expand Down
4 changes: 4 additions & 0 deletions comp/trace/config/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ func applyDatadogConfig(c *config.AgentConfig, core corecompcfg.Component) error
c.ProbabilisticSamplerHashSeed = uint32(core.GetInt("apm_config.probabilistic_sampler.hash_seed"))
}

if core.IsSet("apm_config.error_tracking_standalone.enabled") {
c.ErrorTrackingStandalone = core.GetBool("apm_config.error_tracking_standalone.enabled")
}

if core.IsSet("apm_config.max_remote_traces_per_second") {
c.MaxRemoteTPS = core.GetFloat64("apm_config.max_remote_traces_per_second")
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,14 @@ api_key:
## collectors using the probabilistic sampler to ensure consistent sampling.
# hash_seed: 0

## @param error_tracking_standalone - object - optional
## Enables Error Tracking Standalone
##
#error_tracking_standalone:
## @env DD_APM_ERROR_TRACKING_STANDALONE_ENABLED - boolean - optional - default: false
## Enables or disables Error Tracking Standalone
# enabled: false
pgimalac marked this conversation as resolved.
Show resolved Hide resolved


{{- if .InternalProfiling -}}
## @param profiling - custom object - optional
Expand Down
1 change: 1 addition & 0 deletions pkg/config/setup/apm.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func setupAPM(config pkgconfigmodel.Setup) {
config.BindEnv("apm_config.probabilistic_sampler.enabled", "DD_APM_PROBABILISTIC_SAMPLER_ENABLED")
config.BindEnv("apm_config.probabilistic_sampler.sampling_percentage", "DD_APM_PROBABILISTIC_SAMPLER_SAMPLING_PERCENTAGE")
config.BindEnv("apm_config.probabilistic_sampler.hash_seed", "DD_APM_PROBABILISTIC_SAMPLER_HASH_SEED")
config.BindEnv("apm_config.error_tracking_standalone.enabled", "DD_APM_ERROR_TRACKING_STANDALONE_ENABLED")
pgimalac marked this conversation as resolved.
Show resolved Hide resolved

config.BindEnv("apm_config.max_memory", "DD_APM_MAX_MEMORY")
config.BindEnv("apm_config.max_cpu_percent", "DD_APM_MAX_CPU_PERCENT")
Expand Down
51 changes: 50 additions & 1 deletion pkg/trace/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,15 @@ func (a *Agent) ProcessStats(in *pb.ClientStatsPayload, lang, tracerVersion stri
a.ClientStatsAggregator.In <- a.processStats(in, lang, tracerVersion)
}

// sample performs all sampling on the processedTrace modifying it as needed and returning if the trace should be kept and the number of events in the trace
// sample performs all sampling on the processedTrace modifying it as needed and returning if the trace should be kept
// and the number of events in the trace
func (a *Agent) sample(now time.Time, ts *info.TagStats, pt *traceutil.ProcessedTrace) (keep bool, numEvents int) {
// If the agent is set for Error Tracking Standalone only the ErrorSampler is run (bypasses all other samplers).
// Trace chunks that don't contain errors are dropped.
if a.conf.ErrorTrackingStandalone {
return a.errorSampling(now, ts, pt)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this bypass may be too aggressive. The trace-agent needs to run probabilistic samplers to adapt sampling rates returned to the tracer. If this is not run, the tracer will miss updates on sampling rates.

May I get your thoughts on this @ajgajg1134 ?

edit: looking into runSamplers() func in this very same file, I think this may be a better place to put this logic. It already contains conditional statements on which samplers should be run (eg ProbabilisticSampler).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That may be more of a Product question but here is the reasoning:

  • if a user sets a host to ETS, we only want to keep chunks with errors (or exception span events)
  • the error sampler itself may not be needed - likely users would like all their errors and then use exclusion filters - but we can still leave it if they want to sample
  • apart from the priority sampler with manual drop, all sampling decisions could be to keep regardless of the presence of errors - which we don't want as we only need to send chunks with errors

Then I have to admit I didn't know the probabilistic sampler talked to the tracer, but I guess we wouldn't need that if the host is set as ETS and we never run it?

I discussed with @dussault-antoine this week and we concluded that it was fine to bypass all samplers but the error sampler.

Then I don't remember if I considered putting the logic in runSamplers(), maybe that would give better readability?


// We have a `keep` that is different from pt's `DroppedTrace` field as `DroppedTrace` will be sent to intake.
// For example: We want to maintain the overall trace level sampling decision for a trace with Analytics Events
// where a trace might be marked as DroppedTrace true, but we still sent analytics events in that ProcessedTrace.
Expand Down Expand Up @@ -605,6 +612,48 @@ func isManualUserDrop(pt *traceutil.ProcessedTrace) bool {
return dm == manualSampling
}

// errorSampling reports trace chunks with errors and tags spans as Error Tracking Backend Standalone.
// Also sets "DroppedTrace" on the chunk.
func (a *Agent) errorSampling(now time.Time, ts *info.TagStats, pt *traceutil.ProcessedTrace) (keep bool, numEvents int) {
sampled := a.runErrorSampler(now, *pt)
numEvents = len(a.getAnalyzedEvents(pt, ts))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need analyzed spans when only this error sampler is enabled? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I don't know a lot about analyzed spans but as it is set to true when running the error sampler in runSamplers(), I figured I would have it as well

if sampled {
for _, span := range pt.TraceChunk.Spans {
if span.Error != 0 || spanContainsExceptionSpanEvent(span) {
span.Meta["_dd.error_tracking_backend_standalone.error"] = "true"
} else {
span.Meta["_dd.error_tracking_backend_standalone.error"] = "false"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tag is pretty much derived from existing span properties. Can't we avoid the propagation of this tag on every span and resolve the value, if needed, in the backend? It would make the transport more efficient and less costly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are going to need the tag for billing, there is no other way we could know they come from ETS (we don't want to bill them for APM)

}
}
}
pt.TraceChunk.DroppedTrace = !sampled
return sampled, numEvents
}

func spanContainsExceptionSpanEvent(span *pb.Span) bool {
if hasExceptionSpanEvents, ok := span.Meta["_dd.span_events.has_exception"]; ok && hasExceptionSpanEvents == "true" {
return true
}
return false
}

// runErrorSampler runs the agent's configured ErrorSampler if pt contains errors and returns the sampling decision.
func (a *Agent) runErrorSampler(now time.Time, pt traceutil.ProcessedTrace) (keep bool) {
if traceContainsErrorOrExceptionSpanEvent(pt.TraceChunk.Spans) {
return a.ErrorsSampler.Sample(now, pt.TraceChunk.Spans, pt.Root, pt.TracerEnv)
}
return false
}

func traceContainsErrorOrExceptionSpanEvent(trace pb.Trace) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a very similar function already, maybe it's worth extending traceContainsError() adding a boolean parameter to determine whether exceptions should be considered?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that also works! done in ce0d656

for _, span := range trace {
if span.Error != 0 || spanContainsExceptionSpanEvent(span) {
return true
}
}
return false
}

// traceSampling reports whether the chunk should be kept as a trace, setting "DroppedTrace" on the chunk
func (a *Agent) traceSampling(now time.Time, ts *info.TagStats, pt *traceutil.ProcessedTrace) (keep bool, checkAnalyticsEvents bool) {
sampled, check := a.runSamplers(now, ts, *pt)
Expand Down
129 changes: 128 additions & 1 deletion pkg/trace/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ func TestSampling(t *testing.T) {
}
}

func TestSample(t *testing.T) {
func TestSampleTrace(t *testing.T) {
now := time.Now()
cfg := &config.AgentConfig{TargetTPS: 5, ErrorTPS: 1000, Features: make(map[string]struct{})}
genSpan := func(decisionMaker string, priority sampler.SamplingPriority, err int32) traceutil.ProcessedTrace {
Expand Down Expand Up @@ -1357,6 +1357,133 @@ func TestSample(t *testing.T) {
}
}

func TestSample(t *testing.T) {
now := time.Now()
cfg := &config.AgentConfig{TargetTPS: 5, ErrorTPS: 1000, Features: make(map[string]struct{})}
genSpan := func(decisionMaker string, priority sampler.SamplingPriority, err int32, exceptionInSpanEvent bool) traceutil.ProcessedTrace {
root := &pb.Span{
Service: "serv1",
Start: now.UnixNano(),
Duration: (100 * time.Millisecond).Nanoseconds(),
Metrics: map[string]float64{"_top_level": 1},
Error: err, // If 1, the Error Sampler will keep the trace, if 0, it will not be sampled
Meta: map[string]string{},
}
if exceptionInSpanEvent {
root.Meta["_dd.span_events.has_exception"] = "true" // the Error Sampler will keep the trace
}
chunk := testutil.TraceChunkWithSpan(root)
if decisionMaker != "" {
chunk.Tags["_dd.p.dm"] = decisionMaker
}
pt := traceutil.ProcessedTrace{TraceChunk: chunk, Root: root}
pt.TraceChunk.Priority = int32(priority)
return pt
}
statsd := &statsd.NoOpClient{}
tests := map[string]struct {
trace traceutil.ProcessedTrace
etsEnabled bool
keep bool
keepWithFeature bool
}{
"userdrop-error-manual-dm-unsampled": {
trace: genSpan("-4", sampler.PriorityUserDrop, 1, false),
keep: false,
keepWithFeature: false,
},
"userkeep-error-no-dm-sampled": {
trace: genSpan("", sampler.PriorityUserKeep, 1, false),
keep: true,
keepWithFeature: true,
},
"userkeep-error-agent-dm-sampled": {
trace: genSpan("-1", sampler.PriorityUserKeep, 1, false),
keep: true,
keepWithFeature: true,
},
"autodrop-error-sampled": {
trace: genSpan("", sampler.PriorityAutoDrop, 1, false),
keep: true,
keepWithFeature: true,
},
"autodrop-not-sampled": {
trace: genSpan("", sampler.PriorityAutoDrop, 0, false),
keep: false,
keepWithFeature: false,
},
"ets-userdrop-error-manual-dm-unsampled": {
trace: genSpan("-4", sampler.PriorityUserDrop, 1, false),
etsEnabled: true,
keep: true,
keepWithFeature: true,
},
"ets-userdrop-errorspanevent-manual-dm-unsampled": {
trace: genSpan("-4", sampler.PriorityUserDrop, 1, false),
etsEnabled: true,
keep: true,
keepWithFeature: true,
},
"ets-userdrop-manual-dm-unsampled": {
trace: genSpan("-4", sampler.PriorityUserDrop, 0, false),
etsEnabled: true,
keep: false,
keepWithFeature: false,
},
"ets-userkeep-error-no-dm-sampled": {
trace: genSpan("", sampler.PriorityUserKeep, 1, false),
etsEnabled: true,
keep: true,
keepWithFeature: true,
},
"ets-userkeep-error-agent-dm-sampled": {
trace: genSpan("-1", sampler.PriorityUserKeep, 1, false),
etsEnabled: true,
keep: true,
keepWithFeature: true,
},
"ets-autodrop-error-sampled": {
trace: genSpan("", sampler.PriorityAutoDrop, 1, false),
etsEnabled: true,
keep: true,
keepWithFeature: true,
},
"ets-autodrop-errorspanevent-sampled": {
trace: genSpan("", sampler.PriorityAutoDrop, 0, true),
etsEnabled: true,
keep: true,
keepWithFeature: true,
},
"ets-autodrop-not-sampled": {
trace: genSpan("", sampler.PriorityAutoDrop, 0, false),
etsEnabled: true,
keep: false,
keepWithFeature: false,
},
}
for name, tt := range tests {
cfg.ErrorTrackingStandalone = tt.etsEnabled
a := &Agent{
NoPrioritySampler: sampler.NewNoPrioritySampler(cfg, statsd),
ErrorsSampler: sampler.NewErrorsSampler(cfg, statsd),
PrioritySampler: sampler.NewPrioritySampler(cfg, &sampler.DynamicConfig{}, statsd),
RareSampler: sampler.NewRareSampler(config.New(), statsd),
EventProcessor: newEventProcessor(cfg, statsd),
conf: cfg,
}
t.Run(name, func(t *testing.T) {
keep, _ := a.sample(now, info.NewReceiverStats().GetTagStats(info.Tags{}), &tt.trace)
assert.Equal(t, tt.keep, keep)
assert.Equal(t, !tt.keep, tt.trace.TraceChunk.DroppedTrace)
cfg.Features["error_rare_sample_tracer_drop"] = struct{}{}
defer delete(cfg.Features, "error_rare_sample_tracer_drop")
keep, _ = a.sample(now, info.NewReceiverStats().GetTagStats(info.Tags{}), &tt.trace)
assert.Equal(t, tt.keepWithFeature, keep)
assert.Equal(t, !tt.keepWithFeature, tt.trace.TraceChunk.DroppedTrace)
})
}
}

func TestSampleManualUserDropNoAnalyticsEvents(t *testing.T) {
// This test exists to confirm previous behavior where we did not extract nor tag analytics events on
// user manual drop traces
Expand Down
6 changes: 6 additions & 0 deletions pkg/trace/api/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ func (o *OTLPReceiver) convertSpan(rattr map[string]string, lib pcommon.Instrume
if in.Events().Len() > 0 {
transform.SetMetaOTLP(span, "events", transform.MarshalEvents(in.Events()))
}
for i := range in.Events().Len() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were already iterating through events/ checking for exceptions here, may be more efficient to add span.Meta["_dd.span_events.has_exception"] = "true" below that line.

It also seems like Status2Error gets called in both paths you changed, so moving to there would only require adding the logic in 1 place.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can see, there is a check in Status2Error here so that it is only applied to error spans.

The point being to consider non error spans that do contain exception span events in the error sampler (in addition to error spans), I don't think I can move the tag setting there unfortunately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point being to consider non error spans that do contain exception span events in the error sampler

Ah, wasn't aware of this. Agreed you can't add to Status2Error then. May be good to still create a func to avoid duplication but is fine as is too, approving.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into its own func in 7f8a8e5, cleaner indeed!

if in.Events().At(i).Name() == "exception" {
span.Meta["_dd.span_events.has_exception"] = "true"
break
}
}
if in.Links().Len() > 0 {
transform.SetMetaOTLP(span, "_dd.span_links", transform.MarshalLinks(in.Links()))
}
Expand Down
Loading
Loading