Skip to content

Commit 0d8da28

Browse files
Merge pull request #2471 from newrelic/Sampling-Target
Updates to support sampling_target.
2 parents 2ad7f9e + 1f4c034 commit 0d8da28

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

newrelic-agent/src/main/java/com/newrelic/agent/RPMService.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.newrelic.agent.transport.HttpError;
4848
import com.newrelic.agent.transport.HttpResponseCode;
4949
import com.newrelic.agent.utilization.UtilizationData;
50+
import com.newrelic.api.agent.NewRelic;
5051
import org.json.simple.JSONStreamAware;
5152

5253
import java.lang.management.ManagementFactory;
@@ -237,6 +238,9 @@ private Map<String, Object> getSettings(boolean sendEnvironmentInfo) {
237238
}
238239
settings.put("services", ServiceFactory.getServicesConfiguration());
239240

241+
// the sysprops and envvars above an in unmodifiable collections, so just change it here
242+
updateSamplingTargetSettings(settings);
243+
240244
return settings;
241245
}
242246

@@ -969,6 +973,58 @@ private void handle503Error(Exception e) {
969973
}
970974
}
971975

976+
private void updateSamplingTargetSettings (Map<String, Object> settings) {
977+
// the new distributed_tracing.sampler.adaptive_sampling_target is meant to be per minute
978+
// but, the harvest cycle is 12 times per minute right now,
979+
// so we need to divide this number by 12 until that changes
980+
// there are 2 spots we need to do this:
981+
// - settings.distributed_tracing_sampler_adaptive_sampling_target
982+
// - settings.distributed_tracing.sampler.adaptive_sampling_target
983+
if (settings == null) return;
984+
try {
985+
// local settings
986+
Map<String, Object> dtConfig = (Map<String, Object>) settings.get("distributed_tracing");
987+
if (dtConfig != null) {
988+
Map<String, Object> samplerConfig = (Map<String, Object>) dtConfig.get("sampler");
989+
if (samplerConfig != null) {
990+
Integer adaptiveSamplingTarget = toInt(samplerConfig.get("adaptive_sampling_target"));
991+
if (adaptiveSamplingTarget != null) {
992+
Integer newAdaptiveSamplingTarget = (int) Math.ceil(adaptiveSamplingTarget.doubleValue() / 12.0);
993+
NewRelic.getAgent()
994+
.getLogger()
995+
.log(Level.FINE, "Updating local setting adaptive_sampling_target from " + adaptiveSamplingTarget + " to " +
996+
newAdaptiveSamplingTarget);
997+
samplerConfig.put("adaptive_sampling_target", newAdaptiveSamplingTarget);
998+
}
999+
}
1000+
}
1001+
} catch (Exception e) {
1002+
NewRelic.getAgent().getLogger().log(Level.WARNING, "Unable to parse local setting adaptive_sampling_target setting: {0}", e);
1003+
}
1004+
1005+
try {
1006+
// env var
1007+
if (settings.containsKey("distributed_tracing_sampler_adaptive_sampling_target")) {
1008+
Integer adaptiveSamplingTarget = toInt(settings.get("distributed_tracing_sampler_adaptive_sampling_target"));
1009+
Integer newAdaptiveSamplingTarget = (int) Math.ceil(adaptiveSamplingTarget.doubleValue() / 12.0);
1010+
NewRelic.getAgent()
1011+
.getLogger()
1012+
.log(Level.FINE, "Updating environment variable adaptive_sampling_target from " + adaptiveSamplingTarget + " to " +
1013+
newAdaptiveSamplingTarget);
1014+
settings.put("distributed_tracing_sampler_adaptive_sampling_target", newAdaptiveSamplingTarget);
1015+
}
1016+
} catch (Exception e) {
1017+
NewRelic.getAgent().getLogger().log(Level.WARNING, "Unable to parse environment variable adaptive_sampling_target setting: {0}", e);
1018+
}
1019+
}
1020+
1021+
private static Integer toInt(Object o) {
1022+
if (o instanceof Number) {
1023+
return ((Number) o).intValue();
1024+
}
1025+
return ((Double)Double.parseDouble((String)o)).intValue();
1026+
}
1027+
9721028
@Override
9731029
protected void doStop() {
9741030
removeHarvestablesFromServices(appName);

newrelic-agent/src/main/java/com/newrelic/agent/config/DistributedTracingConfig.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
package com.newrelic.agent.config;
99

10+
import com.newrelic.api.agent.NewRelic;
11+
1012
import java.util.Map;
13+
import java.util.logging.Level;
1114

1215
public class DistributedTracingConfig extends BaseConfig {
1316

1417
private static final boolean DEFAULT_DISTRIBUTED_TRACING = true;
18+
private static final Integer DEFAULT_ADAPTIVE_SAMPLING_TARGET = 120;
1519
private static final String SYSTEM_PROPERTY_ROOT = "newrelic.config.distributed_tracing.";
1620

1721
public static final String ENABLED = "enabled";
@@ -26,7 +30,13 @@ public class DistributedTracingConfig extends BaseConfig {
2630
public static final String REMOTE_PARENT_NOT_SAMPLED = "remote_parent_not_sampled";
2731
public static final String SAMPLE_ALWAYS_ON = "always_on";
2832
public static final String SAMPLE_ALWAYS_OFF = "always_off";
29-
public static final String SAMPLE_DEFAULT = "default";
33+
34+
// note: there is no special logic for these yet, as they are just the fallback if the other values don't exist
35+
// if we have to add special logic, we should treat one as an alias of the other
36+
public static final String SAMPLE_DEFAULT = "default"; // same as 'adaptive_sampling'
37+
public static final String SAMPLE_ADAPTIVE_SAMPLING = "adaptive_sampling"; // same as 'default'
38+
39+
public static final String ADAPTIVE_SAMPLING_TARGET = "adaptive_sampling_target"; // see below
3040

3141
private final boolean enabled;
3242
private final String trustedAccountKey;
@@ -36,6 +46,11 @@ public class DistributedTracingConfig extends BaseConfig {
3646
private final String remoteParentSampled;
3747
private final String remoteParentNotSampled;
3848

49+
// will be passed up on connect and come back down as just 'sampling_target'
50+
// which will get assigned to transaction_events.target_samples_stored
51+
// so, even though it's not directly used in code here, it is necessary
52+
private final Integer adaptiveSamplingTarget;
53+
3954
DistributedTracingConfig(Map<String, Object> props) {
4055
super(props, SYSTEM_PROPERTY_ROOT);
4156
this.enabled = getProperty(ENABLED, DEFAULT_DISTRIBUTED_TRACING);
@@ -45,6 +60,7 @@ public class DistributedTracingConfig extends BaseConfig {
4560
this.includeNewRelicHeader = !getProperty(EXCLUDE_NEWRELIC_HEADER, false);
4661

4762
BaseConfig samplerConfig = new BaseConfig(nestedProps(SAMPLER), SYSTEM_PROPERTY_ROOT+SAMPLER+".");
63+
this.adaptiveSamplingTarget = samplerConfig.getProperty(ADAPTIVE_SAMPLING_TARGET, DEFAULT_ADAPTIVE_SAMPLING_TARGET);
4864
this.remoteParentSampled = samplerConfig.getProperty(REMOTE_PARENT_SAMPLED, SAMPLE_DEFAULT);
4965
this.remoteParentNotSampled = samplerConfig.getProperty(REMOTE_PARENT_NOT_SAMPLED, SAMPLE_DEFAULT);
5066
}

0 commit comments

Comments
 (0)