Skip to content

Commit

Permalink
Simplify code origins. Separate out snapshot generation. (#8263)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly authored Jan 23, 2025
1 parent ac29d6c commit 991929d
Show file tree
Hide file tree
Showing 30 changed files with 459 additions and 673 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -337,6 +338,18 @@ public static void evalContextAndCommit(
}
}

public static void codeOrigin(String probeId) {
try {
ProbeImplementation probe = probeResolver.resolve(probeId);
if (probe != null) {
probe.commit(
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
}
} catch (Exception e) {
LOGGER.debug("Error in codeOrigin: ", e);
}
}

/**
* Commit snapshot based on entry/exit contexts and eventually caught exceptions for given probe
* Ids This is for method probes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.trace.bootstrap.debugger;

import java.util.UUID;

public class ProbeId {
private static final String ID_SEPARATOR = ":";

Expand Down Expand Up @@ -29,6 +31,10 @@ private ProbeId(String id, int version, String encoded) {
this.encoded = encoded;
}

public static ProbeId newId() {
return new ProbeId(UUID.randomUUID().toString(), 0);
}

public String getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.datadog.debugger.agent;

import com.datadog.debugger.probe.ExceptionProbe;
import com.datadog.debugger.probe.LogProbe;
import com.datadog.debugger.probe.MetricProbe;
import com.datadog.debugger.probe.ProbeDefinition;
Expand All @@ -12,6 +11,8 @@
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Stores debugger configuration for a service with: - Probe definitions - filters (allow/deny) -
Expand Down Expand Up @@ -63,46 +64,47 @@ public int hashCode() {
@Json(name = "id")
private final String service;

private final Collection<MetricProbe> metricProbes;
private final Collection<LogProbe> logProbes;
private final Collection<SpanProbe> spanProbes;
private final Collection<TriggerProbe> triggerProbes;
private final Collection<SpanDecorationProbe> spanDecorationProbes;
private transient List<ProbeDefinition> probes = new ArrayList<>();
private Collection<MetricProbe> metricProbes = new ArrayList<>();
private Collection<LogProbe> logProbes = new ArrayList<>();
private Collection<SpanProbe> spanProbes = new ArrayList<>();
private Collection<TriggerProbe> triggerProbes = new ArrayList<>();
private Collection<SpanDecorationProbe> spanDecorationProbes = new ArrayList<>();
private final FilterList allowList;
private final FilterList denyList;
private final LogProbe.Sampling sampling;

public Configuration(String service, Collection<LogProbe> logProbes) {
this(service, null, logProbes, null);
public Configuration(String serviceName, List<? extends ProbeDefinition> probes) {
this(serviceName, probes, null, null, null);
}

public Configuration(
String serviceName,
Collection<MetricProbe> metricProbes,
Collection<LogProbe> logProbes,
Collection<SpanProbe> spanProbes) {
this(serviceName, metricProbes, logProbes, spanProbes, null, null, null, null, null);
}

public Configuration(
String serviceName,
Collection<MetricProbe> metricProbes,
Collection<LogProbe> logProbes,
Collection<SpanProbe> spanProbes,
Collection<TriggerProbe> triggerProbes,
Collection<SpanDecorationProbe> spanDecorationProbes,
List<? extends ProbeDefinition> probes,
FilterList allowList,
FilterList denyList,
LogProbe.Sampling sampling) {
this.service = serviceName;
this.metricProbes = metricProbes;
this.logProbes = logProbes;
this.spanProbes = spanProbes;
this.triggerProbes = triggerProbes;
this.spanDecorationProbes = spanDecorationProbes;
this.allowList = allowList;
this.denyList = denyList;
this.sampling = sampling;
probes.forEach(this::add);
}

private void add(ProbeDefinition p) {
if (p instanceof LogProbe) {
logProbes.add((LogProbe) p);
} else if (p instanceof MetricProbe) {
metricProbes.add((MetricProbe) p);
} else if (p instanceof SpanProbe) {
spanProbes.add((SpanProbe) p);
} else if (p instanceof SpanDecorationProbe) {
spanDecorationProbes.add((SpanDecorationProbe) p);
} else if (p instanceof TriggerProbe) {
triggerProbes.add((TriggerProbe) p);
} else {
probes.add(p);
}
}

public String getService() {
Expand Down Expand Up @@ -141,24 +143,12 @@ public LogProbe.Sampling getSampling() {
return sampling;
}

public Collection<ProbeDefinition> getDefinitions() {
Collection<ProbeDefinition> result = new ArrayList<>();
if (triggerProbes != null) {
result.addAll(triggerProbes);
}
if (metricProbes != null) {
result.addAll(metricProbes);
}
if (logProbes != null) {
result.addAll(logProbes);
}
if (spanProbes != null) {
result.addAll(spanProbes);
}
if (spanDecorationProbes != null) {
result.addAll(spanDecorationProbes);
}
return result;
public List<ProbeDefinition> getDefinitions() {
return Stream.of(
triggerProbes, metricProbes, logProbes, spanProbes, spanDecorationProbes, probes)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

@Generated
Expand All @@ -167,10 +157,8 @@ public String toString() {
return "DebuggerConfiguration{"
+ "service="
+ service
+ ", metricProbes="
+ metricProbes
+ ", logProbes="
+ logProbes
+ ", probes="
+ getDefinitions()
+ ", allowList="
+ allowList
+ ", denyList="
Expand All @@ -183,12 +171,15 @@ public String toString() {
@Generated
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Configuration that = (Configuration) o;
return Objects.equals(service, that.service)
&& Objects.equals(metricProbes, that.metricProbes)
&& Objects.equals(logProbes, that.logProbes)
&& Objects.equals(probes, that.probes)
&& Objects.equals(allowList, that.allowList)
&& Objects.equals(denyList, that.denyList)
&& Objects.equals(sampling, that.sampling);
Expand All @@ -197,7 +188,7 @@ public boolean equals(Object o) {
@Generated
@Override
public int hashCode() {
return Objects.hash(service, metricProbes, logProbes, allowList, denyList, sampling);
return Objects.hash(service, probes, allowList, denyList, sampling);
}

public static Configuration.Builder builder() {
Expand All @@ -206,13 +197,13 @@ public static Configuration.Builder builder() {

public static class Builder {
private String service = null;
private List<MetricProbe> metricProbes = null;
private List<LogProbe> logProbes = null;
private List<SpanProbe> spanProbes = null;
private List<TriggerProbe> triggerProbes = null;
private List<SpanDecorationProbe> spanDecorationProbes = null;

private final List<ProbeDefinition> probes = new ArrayList<>();

private FilterList allowList = null;

private FilterList denyList = null;

private LogProbe.Sampling sampling = null;

public Configuration.Builder setService(String service) {
Expand All @@ -224,53 +215,14 @@ public Configuration.Builder add(Collection<? extends ProbeDefinition> definitio
if (definitions == null) {
return this;
}
for (ProbeDefinition definition : definitions) {
if (definition instanceof MetricProbe) add((MetricProbe) definition);
if (definition instanceof TriggerProbe) add((TriggerProbe) definition);
if (definition instanceof LogProbe) add((LogProbe) definition);
if (definition instanceof SpanProbe) add((SpanProbe) definition);
if (definition instanceof SpanDecorationProbe) add((SpanDecorationProbe) definition);
}
probes.addAll(definitions);
return this;
}

public Configuration.Builder add(MetricProbe probe) {
if (metricProbes == null) {
metricProbes = new ArrayList<>();
public Configuration.Builder add(ProbeDefinition... probes) {
for (ProbeDefinition probe : probes) {
this.probes.add(probe);
}
metricProbes.add(probe);
return this;
}

public Configuration.Builder add(LogProbe probe) {
if (logProbes == null) {
logProbes = new ArrayList<>();
}
logProbes.add(probe);
return this;
}

public Configuration.Builder add(SpanProbe probe) {
if (spanProbes == null) {
spanProbes = new ArrayList<>();
}
spanProbes.add(probe);
return this;
}

public Configuration.Builder add(TriggerProbe probe) {
if (triggerProbes == null) {
triggerProbes = new ArrayList<>();
}
triggerProbes.add(probe);
return this;
}

public Configuration.Builder add(SpanDecorationProbe probe) {
if (spanDecorationProbes == null) {
spanDecorationProbes = new ArrayList<>();
}
spanDecorationProbes.add(probe);
return this;
}

Expand All @@ -281,66 +233,6 @@ public Configuration.Builder add(LogProbe.Sampling newSampling) {
return this;
}

public Configuration.Builder addMetricProbes(Collection<MetricProbe> probes) {
if (probes == null) {
return this;
}
for (MetricProbe probe : probes) {
add(probe);
}
return this;
}

public Configuration.Builder addLogProbes(Collection<LogProbe> probes) {
if (probes == null) {
return this;
}
for (LogProbe probe : probes) {
add(probe);
}
return this;
}

public Builder addExceptionProbes(Collection<ExceptionProbe> probes) {
if (probes == null) {
return this;
}
for (ExceptionProbe probe : probes) {
add(probe);
}
return this;
}

public Configuration.Builder addSpanProbes(Collection<SpanProbe> probes) {
if (probes == null) {
return this;
}
for (SpanProbe probe : probes) {
add(probe);
}
return this;
}

public Configuration.Builder addTriggerProbes(Collection<TriggerProbe> probes) {
if (probes == null) {
return this;
}
for (TriggerProbe probe : probes) {
add(probe);
}
return this;
}

public Configuration.Builder addSpanDecorationProbes(Collection<SpanDecorationProbe> probes) {
if (probes == null) {
return this;
}
for (SpanDecorationProbe probe : probes) {
add(probe);
}
return this;
}

public Configuration.Builder addAllowList(FilterList newAllowList) {
if (newAllowList == null) {
return this;
Expand Down Expand Up @@ -370,32 +262,8 @@ public Configuration.Builder setSampling(LogProbe.Sampling sampling) {
return this;
}

public Configuration.Builder add(Configuration other) {
if (other.service != null) {
this.service = other.service;
}
addMetricProbes(other.getMetricProbes());
addLogProbes(other.getLogProbes());
addSpanProbes(other.getSpanProbes());
addTriggerProbes(other.getTriggerProbes());
addSpanDecorationProbes(other.getSpanDecorationProbes());
addAllowList(other.getAllowList());
addDenyList(other.getDenyList());
add(other.getSampling());
return this;
}

public Configuration build() {
return new Configuration(
service,
metricProbes,
logProbes,
spanProbes,
triggerProbes,
spanDecorationProbes,
allowList,
denyList,
sampling);
return new Configuration(service, probes, allowList, denyList, sampling);
}
}
}
Loading

0 comments on commit 991929d

Please sign in to comment.