|
| 1 | +package io.getunleash.impactmetrics; |
| 2 | + |
| 3 | +import io.getunleash.lang.Nullable; |
| 4 | +import io.getunleash.variant.Variant; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +public class MetricsAPI { |
| 13 | + private static final Logger LOGGER = LoggerFactory.getLogger(MetricsAPI.class); |
| 14 | + |
| 15 | + private final ImpactMetricRegistry metricRegistry; |
| 16 | + private final VariantResolver variantResolver; |
| 17 | + private final StaticContext staticContext; |
| 18 | + |
| 19 | + public MetricsAPI( |
| 20 | + ImpactMetricRegistry metricRegistry, |
| 21 | + VariantResolver variantResolver, |
| 22 | + StaticContext staticContext) { |
| 23 | + this.metricRegistry = metricRegistry; |
| 24 | + this.variantResolver = variantResolver; |
| 25 | + this.staticContext = staticContext; |
| 26 | + } |
| 27 | + |
| 28 | + public void defineCounter(String name, String help) { |
| 29 | + if (name == null || name.isEmpty() || help == null || help.isEmpty()) { |
| 30 | + LOGGER.warn("Counter name or help cannot be empty: {}, {}", name, help); |
| 31 | + return; |
| 32 | + } |
| 33 | + List<String> labelNames = List.of("featureName", "appName", "environment"); |
| 34 | + metricRegistry.counter(new MetricOptions(name, help, labelNames)); |
| 35 | + } |
| 36 | + |
| 37 | + public void defineGauge(String name, String help) { |
| 38 | + if (name == null || name.isEmpty() || help == null || help.isEmpty()) { |
| 39 | + LOGGER.warn("Gauge name or help cannot be empty: {}, {}", name, help); |
| 40 | + return; |
| 41 | + } |
| 42 | + List<String> labelNames = List.of("featureName", "appName", "environment"); |
| 43 | + metricRegistry.gauge(new MetricOptions(name, help, labelNames)); |
| 44 | + } |
| 45 | + |
| 46 | + public void defineHistogram(String name, String help, @Nullable List<Double> buckets) { |
| 47 | + if (name == null || name.isEmpty() || help == null || help.isEmpty()) { |
| 48 | + LOGGER.warn("Histogram name or help cannot be empty: {}, {}", name, help); |
| 49 | + return; |
| 50 | + } |
| 51 | + List<String> labelNames = List.of("featureName", "appName", "environment"); |
| 52 | + List<Double> bucketList = buckets != null ? buckets : new ArrayList<>(); |
| 53 | + metricRegistry.histogram(new BucketMetricOptions(name, help, labelNames, bucketList)); |
| 54 | + } |
| 55 | + |
| 56 | + private Map<String, String> getFlagLabels(@Nullable MetricFlagContext flagContext) { |
| 57 | + Map<String, String> flagLabels = new HashMap<>(); |
| 58 | + if (flagContext != null) { |
| 59 | + for (String flag : flagContext.getFlagNames()) { |
| 60 | + Variant variant = |
| 61 | + variantResolver.getVariantForImpactMetrics(flag, flagContext.getContext()); |
| 62 | + |
| 63 | + if (variant.isEnabled()) { |
| 64 | + flagLabels.put(flag, variant.getName()); |
| 65 | + } else if (variant.isFeatureEnabled()) { |
| 66 | + flagLabels.put(flag, "enabled"); |
| 67 | + } else { |
| 68 | + flagLabels.put(flag, "disabled"); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return flagLabels; |
| 73 | + } |
| 74 | + |
| 75 | + public void incrementCounter(String name) { |
| 76 | + incrementCounter(name, null, null); |
| 77 | + } |
| 78 | + |
| 79 | + public void incrementCounter(String name, long value) { |
| 80 | + incrementCounter(name, value, null); |
| 81 | + } |
| 82 | + |
| 83 | + public void incrementCounter( |
| 84 | + String name, @Nullable Long value, @Nullable MetricFlagContext flagContext) { |
| 85 | + Counter counter = metricRegistry.getCounter(name); |
| 86 | + if (counter == null) { |
| 87 | + LOGGER.warn("Counter {} not defined, this counter will not be incremented.", name); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + Map<String, String> flagLabels = getFlagLabels(flagContext); |
| 92 | + Map<String, String> labels = new HashMap<>(flagLabels); |
| 93 | + labels.put("appName", staticContext.getAppName()); |
| 94 | + labels.put("environment", staticContext.getEnvironment()); |
| 95 | + |
| 96 | + counter.inc(value != null ? value : 1L, labels); |
| 97 | + } |
| 98 | + |
| 99 | + public void updateGauge(String name, long value) { |
| 100 | + updateGauge(name, value, null); |
| 101 | + } |
| 102 | + |
| 103 | + public void updateGauge(String name, long value, @Nullable MetricFlagContext flagContext) { |
| 104 | + Gauge gauge = metricRegistry.getGauge(name); |
| 105 | + if (gauge == null) { |
| 106 | + LOGGER.warn("Gauge {} not defined, this gauge will not be updated.", name); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + Map<String, String> flagLabels = getFlagLabels(flagContext); |
| 111 | + Map<String, String> labels = new HashMap<>(flagLabels); |
| 112 | + labels.put("appName", staticContext.getAppName()); |
| 113 | + labels.put("environment", staticContext.getEnvironment()); |
| 114 | + |
| 115 | + gauge.set(value, labels); |
| 116 | + } |
| 117 | + |
| 118 | + public void observeHistogram(String name, double value) { |
| 119 | + observeHistogram(name, value, null); |
| 120 | + } |
| 121 | + |
| 122 | + public void observeHistogram( |
| 123 | + String name, double value, @Nullable MetricFlagContext flagContext) { |
| 124 | + Histogram histogram = metricRegistry.getHistogram(name); |
| 125 | + if (histogram == null) { |
| 126 | + LOGGER.warn("Histogram {} not defined, this histogram will not be updated.", name); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + Map<String, String> flagLabels = getFlagLabels(flagContext); |
| 131 | + Map<String, String> labels = new HashMap<>(flagLabels); |
| 132 | + labels.put("appName", staticContext.getAppName()); |
| 133 | + labels.put("environment", staticContext.getEnvironment()); |
| 134 | + |
| 135 | + histogram.observe(value, labels); |
| 136 | + } |
| 137 | +} |
0 commit comments