|
47 | 47 | import com.newrelic.agent.transport.HttpError; |
48 | 48 | import com.newrelic.agent.transport.HttpResponseCode; |
49 | 49 | import com.newrelic.agent.utilization.UtilizationData; |
| 50 | +import com.newrelic.api.agent.NewRelic; |
50 | 51 | import org.json.simple.JSONStreamAware; |
51 | 52 |
|
52 | 53 | import java.lang.management.ManagementFactory; |
@@ -237,6 +238,9 @@ private Map<String, Object> getSettings(boolean sendEnvironmentInfo) { |
237 | 238 | } |
238 | 239 | settings.put("services", ServiceFactory.getServicesConfiguration()); |
239 | 240 |
|
| 241 | + // the sysprops and envvars above an in unmodifiable collections, so just change it here |
| 242 | + updateSamplingTargetSettings(settings); |
| 243 | + |
240 | 244 | return settings; |
241 | 245 | } |
242 | 246 |
|
@@ -969,6 +973,58 @@ private void handle503Error(Exception e) { |
969 | 973 | } |
970 | 974 | } |
971 | 975 |
|
| 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 | + |
972 | 1028 | @Override |
973 | 1029 | protected void doStop() { |
974 | 1030 | removeHarvestablesFromServices(appName); |
|
0 commit comments