|
| 1 | +package datadog.trace.api.telemetry; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.ArrayBlockingQueue; |
| 8 | +import java.util.concurrent.BlockingQueue; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +public class ConfigInversionMetricCollector |
| 13 | + implements MetricCollector<ConfigInversionMetricCollector.ConfigInversionMetric> { |
| 14 | + private static final Logger log = LoggerFactory.getLogger(ConfigInversionMetricCollector.class); |
| 15 | + private static final String CONFIG_INVERSION_KEY_TAG = "config_name:"; |
| 16 | + private static final String CONFIG_INVERSION_METRIC_NAME = "untracked.config.detected"; |
| 17 | + private static final String NAMESPACE = "tracers"; |
| 18 | + private static final ConfigInversionMetricCollector INSTANCE = |
| 19 | + new ConfigInversionMetricCollector(); |
| 20 | + |
| 21 | + private final BlockingQueue<ConfigInversionMetricCollector.ConfigInversionMetric> metricsQueue; |
| 22 | + |
| 23 | + private ConfigInversionMetricCollector() { |
| 24 | + this.metricsQueue = new ArrayBlockingQueue<>(RAW_QUEUE_SIZE); |
| 25 | + } |
| 26 | + |
| 27 | + public static ConfigInversionMetricCollector getInstance() { |
| 28 | + return INSTANCE; |
| 29 | + } |
| 30 | + |
| 31 | + public void setUndocumentedEnvVarMetric(String configName) { |
| 32 | + setMetricConfigInversionMetric(CONFIG_INVERSION_KEY_TAG + configName); |
| 33 | + } |
| 34 | + |
| 35 | + private void setMetricConfigInversionMetric(final String... tags) { |
| 36 | + if (!metricsQueue.offer( |
| 37 | + new ConfigInversionMetricCollector.ConfigInversionMetric( |
| 38 | + NAMESPACE, true, CONFIG_INVERSION_METRIC_NAME, "count", 1, tags))) { |
| 39 | + log.debug("Unable to add telemetry metric {} for {}", CONFIG_INVERSION_METRIC_NAME, tags[0]); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void prepareMetrics() { |
| 45 | + // Nothing to do here |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Collection<ConfigInversionMetricCollector.ConfigInversionMetric> drain() { |
| 50 | + if (this.metricsQueue.isEmpty()) { |
| 51 | + return Collections.emptyList(); |
| 52 | + } |
| 53 | + List<ConfigInversionMetricCollector.ConfigInversionMetric> drained = |
| 54 | + new ArrayList<>(this.metricsQueue.size()); |
| 55 | + this.metricsQueue.drainTo(drained); |
| 56 | + return drained; |
| 57 | + } |
| 58 | + |
| 59 | + public static class ConfigInversionMetric extends MetricCollector.Metric { |
| 60 | + public ConfigInversionMetric( |
| 61 | + String namespace, |
| 62 | + boolean common, |
| 63 | + String metricName, |
| 64 | + String type, |
| 65 | + Number value, |
| 66 | + final String... tags) { |
| 67 | + super(namespace, common, metricName, type, value, tags); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments