Skip to content

Commit 6a96f57

Browse files
committed
fix: change gauge from long to double
1 parent 24a4353 commit 6a96f57

10 files changed

Lines changed: 48 additions & 46 deletions

File tree

src/main/java/io/getunleash/FakeUnleash.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public void incrementCounter(String name, long value) {}
5353
public void incrementCounter(String name, Long value, MetricFlagContext ctx) {}
5454

5555
@Override
56-
public void updateGauge(String name, long value) {}
56+
public void updateGauge(String name, double value) {}
5757

5858
@Override
59-
public void updateGauge(String name, long value, MetricFlagContext ctx) {}
59+
public void updateGauge(String name, double value, MetricFlagContext ctx) {}
6060

6161
@Override
6262
public void observeHistogram(String name, double value) {}

src/main/java/io/getunleash/impactmetrics/CounterImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ CollectedMetric collect() {
3838
for (String key : values.keySet()) {
3939
Long value = values.remove(key);
4040
if (value != null) {
41-
samples.add(new NumericMetricSample(parseLabelKey(key), value));
41+
samples.add(new NumericMetricSample(parseLabelKey(key), value.doubleValue()));
4242
}
4343
}
4444

4545
if (samples.isEmpty()) {
46-
samples.add(new NumericMetricSample(Collections.emptyMap(), 0L));
46+
samples.add(new NumericMetricSample(Collections.emptyMap(), 0.0));
4747
}
4848

4949
return new CollectedMetric(

src/main/java/io/getunleash/impactmetrics/Gauge.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import java.util.Map;
44

55
interface Gauge {
6-
void set(long value);
6+
void set(double value);
77

8-
void set(long value, Map<String, String> labels);
8+
void set(double value, Map<String, String> labels);
99

1010
void inc();
1111

12-
void inc(long value);
12+
void inc(double value);
1313

14-
void inc(long value, Map<String, String> labels);
14+
void inc(double value, Map<String, String> labels);
1515

1616
void dec();
1717

18-
void dec(long value);
18+
void dec(double value);
1919

20-
void dec(long value, Map<String, String> labels);
20+
void dec(double value, Map<String, String> labels);
2121
}

src/main/java/io/getunleash/impactmetrics/GaugeImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,60 @@
77

88
class GaugeImpl implements Gauge {
99
private final MetricOptions options;
10-
private final ConcurrentHashMap<String, Long> values = new ConcurrentHashMap<>();
10+
private final ConcurrentHashMap<String, Double> values = new ConcurrentHashMap<>();
1111

1212
GaugeImpl(MetricOptions options) {
1313
this.options = options;
1414
}
1515

1616
@Override
17-
public void set(long value) {
17+
public void set(double value) {
1818
set(value, null);
1919
}
2020

2121
@Override
22-
public void set(long value, Map<String, String> labels) {
22+
public void set(double value, Map<String, String> labels) {
2323
String key = CounterImpl.getLabelKey(labels);
2424
values.put(key, value);
2525
}
2626

2727
@Override
2828
public void inc() {
29-
inc(1, null);
29+
inc(1.0, null);
3030
}
3131

3232
@Override
33-
public void inc(long value) {
33+
public void inc(double value) {
3434
inc(value, null);
3535
}
3636

3737
@Override
38-
public void inc(long value, Map<String, String> labels) {
38+
public void inc(double value, Map<String, String> labels) {
3939
String key = CounterImpl.getLabelKey(labels);
40-
values.compute(key, (k, current) -> (current == null ? 0L : current) + value);
40+
values.compute(key, (k, current) -> (current == null ? 0.0 : current) + value);
4141
}
4242

4343
@Override
4444
public void dec() {
45-
dec(1, null);
45+
dec(1.0, null);
4646
}
4747

4848
@Override
49-
public void dec(long value) {
49+
public void dec(double value) {
5050
dec(value, null);
5151
}
5252

5353
@Override
54-
public void dec(long value, Map<String, String> labels) {
54+
public void dec(double value, Map<String, String> labels) {
5555
String key = CounterImpl.getLabelKey(labels);
56-
values.compute(key, (k, current) -> (current == null ? 0L : current) - value);
56+
values.compute(key, (k, current) -> (current == null ? 0.0 : current) - value);
5757
}
5858

5959
CollectedMetric collect() {
6060
List<MetricSample> samples = new ArrayList<>();
6161

6262
for (String key : values.keySet()) {
63-
Long value = values.remove(key);
63+
Double value = values.remove(key);
6464
if (value != null) {
6565
samples.add(new NumericMetricSample(CounterImpl.parseLabelKey(key), value));
6666
}

src/main/java/io/getunleash/impactmetrics/InMemoryMetricRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void restore(List<CollectedMetric> metrics) {
6666
for (MetricSample sample : metric.getSamples()) {
6767
if (sample instanceof NumericMetricSample) {
6868
NumericMetricSample numericSample = (NumericMetricSample) sample;
69-
counter.inc(numericSample.getValue(), numericSample.getLabels());
69+
counter.inc((long) numericSample.getValue(), numericSample.getLabels());
7070
}
7171
}
7272
} else if (metric.getType() == MetricType.GAUGE) {

src/main/java/io/getunleash/impactmetrics/MetricsAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public interface MetricsAPI {
2020
public void incrementCounter(
2121
String name, @Nullable Long value, @Nullable MetricFlagContext flagContext);
2222

23-
public void updateGauge(String name, long value);
23+
public void updateGauge(String name, double value);
2424

25-
public void updateGauge(String name, long value, @Nullable MetricFlagContext flagContext);
25+
public void updateGauge(String name, double value, @Nullable MetricFlagContext flagContext);
2626

2727
public void observeHistogram(String name, double value);
2828

src/main/java/io/getunleash/impactmetrics/MetricsAPIImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ public void incrementCounter(
108108
}
109109

110110
@Override
111-
public void updateGauge(String name, long value) {
111+
public void updateGauge(String name, double value) {
112112
updateGauge(name, value, null);
113113
}
114114

115115
@Override
116-
public void updateGauge(String name, long value, @Nullable MetricFlagContext flagContext) {
116+
public void updateGauge(String name, double value, @Nullable MetricFlagContext flagContext) {
117117
Gauge gauge = metricRegistry.getGauge(name);
118118
if (gauge == null) {
119119
LOGGER.warn("Gauge {} not defined, this gauge will not be updated.", name);

src/main/java/io/getunleash/impactmetrics/NumericMetricSample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
public class NumericMetricSample implements MetricSample {
77
private final Map<String, String> labels;
8-
private final long value;
8+
private final double value;
99

10-
public NumericMetricSample(Map<String, String> labels, long value) {
10+
public NumericMetricSample(Map<String, String> labels, double value) {
1111
this.labels = labels;
1212
this.value = value;
1313
}
@@ -16,7 +16,7 @@ public Map<String, String> getLabels() {
1616
return labels;
1717
}
1818

19-
public long getValue() {
19+
public double getValue() {
2020
return value;
2121
}
2222

src/test/java/io/getunleash/impactmetrics/MetricTypeTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void should_increment_by_default_value() {
2020

2121
CollectedMetric expected =
2222
new CollectedMetric(
23-
"test_counter", "testing", MetricType.COUNTER, List.of(sample(1L)));
23+
"test_counter", "testing", MetricType.COUNTER, List.of(sample(1.0)));
2424

2525
assertThat(metrics).containsExactly(expected);
2626
}
@@ -40,7 +40,7 @@ public void should_increment_with_custom_value_and_labels() {
4040
"labeled_counter",
4141
"with labels",
4242
MetricType.COUNTER,
43-
List.of(sample(Map.of("foo", "bar"), 5L)));
43+
List.of(sample(Map.of("foo", "bar"), 5.0)));
4444

4545
assertThat(metrics).containsExactly(expected);
4646
}
@@ -61,7 +61,7 @@ public void should_support_gauge_inc_dec_and_set() {
6161
"test_gauge",
6262
"gauge test",
6363
MetricType.GAUGE,
64-
List.of(sample(Map.of("env", "prod"), 10L)));
64+
List.of(sample(Map.of("env", "prod"), 10.0)));
6565

6666
assertThat(metrics).containsExactly(expected);
6767
}
@@ -81,7 +81,7 @@ public void should_store_different_label_combinations_separately() {
8181
assertThat(result.getName()).isEqualTo("multi_label");
8282
assertThat(result.getSamples())
8383
.containsExactlyInAnyOrder(
84-
sample(Map.of("a", "x"), 1L), sample(Map.of("b", "y"), 2L), sample(3L));
84+
sample(Map.of("a", "x"), 1.0), sample(Map.of("b", "y"), 2.0), sample(3.0));
8585
}
8686

8787
@Test
@@ -99,9 +99,9 @@ public void should_track_gauge_values_separately_per_label_set() {
9999
assertThat(result.getName()).isEqualTo("multi_env_gauge");
100100
assertThat(result.getSamples())
101101
.containsExactlyInAnyOrder(
102-
sample(Map.of("env", "prod"), 5L),
103-
sample(Map.of("env", "dev"), -2L),
104-
sample(Map.of("env", "test"), 10L));
102+
sample(Map.of("env", "prod"), 5.0),
103+
sample(Map.of("env", "dev"), -2.0),
104+
sample(Map.of("env", "test"), 10.0));
105105
}
106106

107107
@Test
@@ -114,7 +114,7 @@ public void should_return_zero_value_when_empty() {
114114

115115
CollectedMetric expected =
116116
new CollectedMetric(
117-
"noop_counter", "noop", MetricType.COUNTER, List.of(sample(0L)));
117+
"noop_counter", "noop", MetricType.COUNTER, List.of(sample(0.0)));
118118

119119
assertThat(metrics).containsExactly(expected);
120120
}
@@ -128,12 +128,14 @@ public void should_return_zero_value_after_flushing() {
128128
List<CollectedMetric> firstBatch = registry.collect();
129129

130130
CollectedMetric expectedBatch1 =
131-
new CollectedMetric("flush_test", "flush", MetricType.COUNTER, List.of(sample(1L)));
131+
new CollectedMetric(
132+
"flush_test", "flush", MetricType.COUNTER, List.of(sample(1.0)));
132133
assertThat(firstBatch).containsExactly(expectedBatch1);
133134

134135
List<CollectedMetric> secondBatch = registry.collect();
135136
CollectedMetric expectedBatch2 =
136-
new CollectedMetric("flush_test", "flush", MetricType.COUNTER, List.of(sample(0L)));
137+
new CollectedMetric(
138+
"flush_test", "flush", MetricType.COUNTER, List.of(sample(0.0)));
137139
assertThat(secondBatch).containsExactly(expectedBatch2);
138140
}
139141

@@ -148,14 +150,14 @@ public void should_restore_collected_metrics() {
148150
List<CollectedMetric> flushed = registry.collect();
149151

150152
List<CollectedMetric> afterFlush = registry.collect();
151-
assertThat(afterFlush.get(0).getSamples()).containsExactly(sample(0L));
153+
assertThat(afterFlush.get(0).getSamples()).containsExactly(sample(0.0));
152154

153155
registry.restore(flushed);
154156

155157
List<CollectedMetric> restored = registry.collect();
156158
assertThat(restored.get(0).getSamples())
157159
.containsExactlyInAnyOrder(
158-
sample(Map.of("tag", "a"), 5L), sample(Map.of("tag", "b"), 2L));
160+
sample(Map.of("tag", "a"), 5.0), sample(Map.of("tag", "b"), 2.0));
159161
}
160162

161163
@Test
@@ -293,11 +295,11 @@ private HistogramBucket bucket(double le, long count) {
293295
return new HistogramBucket(le, count);
294296
}
295297

296-
private NumericMetricSample sample(long value) {
298+
private NumericMetricSample sample(double value) {
297299
return sample(Collections.emptyMap(), value);
298300
}
299301

300-
private NumericMetricSample sample(Map<String, String> labels, long value) {
302+
private NumericMetricSample sample(Map<String, String> labels, double value) {
301303
return new NumericMetricSample(labels, value);
302304
}
303305
}

src/test/java/io/getunleash/impactmetrics/MetricsAPITest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public void should_set_gauge_with_valid_parameters() {
137137

138138
UnleashContext context = UnleashContext.builder().build();
139139
MetricFlagContext flagContext = new MetricFlagContext(List.of("featureY"), context);
140-
api.updateGauge("valid_gauge", 10L, flagContext);
140+
api.updateGauge("valid_gauge", 10.0, flagContext);
141141

142-
verify(fakeGauge).set(eq(10L), labelsCaptor.capture());
142+
verify(fakeGauge).set(eq(10.0), labelsCaptor.capture());
143143
Map<String, String> capturedLabels = labelsCaptor.getValue();
144144
assertThat(capturedLabels)
145145
.containsEntry("appName", "my-app")

0 commit comments

Comments
 (0)