Skip to content

Commit e198e9a

Browse files
authored
Remove joda time dependency
Use Java 8 Instant class for time instances and Range<Instant> for time ranges.
1 parent 8e15e45 commit e198e9a

File tree

12 files changed

+189
-142
lines changed

12 files changed

+189
-142
lines changed

metrics/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@
3838
<artifactId>error_prone_annotations</artifactId>
3939
<version>2.1.3</version>
4040
</dependency>
41-
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
42-
<dependency>
43-
<groupId>joda-time</groupId>
44-
<artifactId>joda-time</artifactId>
45-
<version>2.9.9</version>
46-
</dependency>
4741
<!-- https://mvnrepository.com/artifact/com.google.re2j/re2j -->
4842
<dependency>
4943
<groupId>com.google.re2j</groupId>

metrics/src/main/java/com/google/monitoring/metrics/Counter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import com.google.common.util.concurrent.AtomicLongMap;
2626
import com.google.common.util.concurrent.Striped;
2727
import com.google.monitoring.metrics.MetricSchema.Kind;
28+
import java.time.Instant;
2829
import java.util.Map.Entry;
2930
import java.util.concurrent.ConcurrentHashMap;
3031
import java.util.concurrent.locks.Lock;
3132
import javax.annotation.concurrent.ThreadSafe;
32-
import org.joda.time.Instant;
3333

3434
/**
3535
* A metric which stores Long values. It is stateful and can be changed in increments.
@@ -145,7 +145,6 @@ final ImmutableList<MetricPoint<Long>> getTimestampedValues(Instant endTimestamp
145145

146146
timestampedValues.add(
147147
MetricPoint.create(this, labelValues, startTimestamp, endTimestamp, entry.getValue()));
148-
149148
}
150149
return timestampedValues.build();
151150
}

metrics/src/main/java/com/google/monitoring/metrics/EventMetric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import com.google.common.collect.Ordering;
2424
import com.google.common.util.concurrent.Striped;
2525
import com.google.monitoring.metrics.MetricSchema.Kind;
26+
import java.time.Instant;
2627
import java.util.Map.Entry;
2728
import java.util.concurrent.ConcurrentHashMap;
2829
import java.util.concurrent.locks.Lock;
29-
import org.joda.time.Instant;
3030

3131
/**
3232
* A metric which stores {@link Distribution} values. The values are stateful and meant to be

metrics/src/main/java/com/google/monitoring/metrics/MetricPoint.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import com.google.auto.value.AutoValue;
1818
import com.google.common.annotations.VisibleForTesting;
1919
import com.google.common.collect.ImmutableList;
20-
import org.joda.time.Instant;
21-
import org.joda.time.Interval;
20+
import com.google.common.collect.Range;
21+
import java.time.Instant;
2222

2323
/**
2424
* Value type class to store a snapshot of a {@link Metric} value for a given label value tuple and
25-
* time {@link Interval}.
25+
* time {@link Range}.
2626
*/
2727
@AutoValue
2828
public abstract class MetricPoint<V> implements Comparable<MetricPoint<V>> {
@@ -39,11 +39,11 @@ public static <V> MetricPoint<V> create(
3939
MetricsUtils.checkLabelValuesLength(metric, labelValues);
4040

4141
return new AutoValue_MetricPoint<>(
42-
metric, labelValues, new Interval(timestamp, timestamp), value);
42+
metric, labelValues, Range.openClosed(timestamp, timestamp), value);
4343
}
4444

4545
/**
46-
* Returns a new {@link MetricPoint} representing a value over an {@link Interval} from {@code
46+
* Returns a new {@link MetricPoint} representing a value over an {@link Range} from {@code
4747
* startTime} to {@code endTime}.
4848
*
4949
* <p>Callers should insure that the count of {@code labelValues} matches the count of labels for
@@ -59,14 +59,14 @@ public static <V> MetricPoint<V> create(
5959
MetricsUtils.checkLabelValuesLength(metric, labelValues);
6060

6161
return new AutoValue_MetricPoint<>(
62-
metric, labelValues, new Interval(startTime, endTime), value);
62+
metric, labelValues, Range.openClosed(startTime, endTime), value);
6363
}
6464

6565
public abstract Metric<V> metric();
6666

6767
public abstract ImmutableList<String> labelValues();
6868

69-
public abstract Interval interval();
69+
public abstract Range<Instant> interval();
7070

7171
public abstract V value();
7272

metrics/src/main/java/com/google/monitoring/metrics/StoredMetric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import com.google.common.collect.ImmutableList.Builder;
2020
import com.google.common.collect.ImmutableSet;
2121
import com.google.monitoring.metrics.MetricSchema.Kind;
22+
import java.time.Instant;
2223
import java.util.Map.Entry;
2324
import java.util.concurrent.ConcurrentHashMap;
2425
import javax.annotation.concurrent.ThreadSafe;
25-
import org.joda.time.Instant;
2626

2727
/**
2828
* A metric which is stateful.

metrics/src/main/java/com/google/monitoring/metrics/VirtualMetric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import com.google.common.collect.ImmutableSet;
2222
import com.google.monitoring.metrics.MetricSchema.Kind;
23+
import java.time.Instant;
2324
import java.util.Map.Entry;
2425
import javax.annotation.concurrent.ThreadSafe;
25-
import org.joda.time.Instant;
2626

2727
/**
2828
* A metric whose value is computed at sample-time.

metrics/src/test/java/com/google/monitoring/metrics/CounterTest.java

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.ImmutableSet;
22-
import org.joda.time.Instant;
22+
import java.time.Instant;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525
import org.junit.runners.JUnit4;
2626

2727
/** Unit tests for {@link Counter}. */
2828
@RunWith(JUnit4.class)
2929
public class CounterTest {
30+
3031
@Test
3132
public void testGetCardinality_reflectsCurrentCardinality() {
3233
Counter counter =
@@ -75,15 +76,17 @@ public void testIncrement_incrementsValues() {
7576
assertThat(counter.getTimestampedValues()).isEmpty();
7677

7778
// use package-private incrementBy once to set the start timestamp predictably.
78-
counter.incrementBy(1, new Instant(1337), ImmutableList.of("test_value1"));
79-
assertThat(counter.getTimestampedValues(new Instant(1337)))
79+
counter.incrementBy(1, Instant.ofEpochMilli(1337), ImmutableList.of("test_value1"));
80+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1337)))
8081
.containsExactly(
81-
MetricPoint.create(counter, ImmutableList.of("test_value1"), new Instant(1337), 1L));
82+
MetricPoint.create(
83+
counter, ImmutableList.of("test_value1"), Instant.ofEpochMilli(1337), 1L));
8284

8385
counter.increment("test_value1");
84-
assertThat(counter.getTimestampedValues(new Instant(1337)))
86+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1337)))
8587
.containsExactly(
86-
MetricPoint.create(counter, ImmutableList.of("test_value1"), new Instant(1337), 2L));
88+
MetricPoint.create(
89+
counter, ImmutableList.of("test_value1"), Instant.ofEpochMilli(1337), 2L));
8790
}
8891

8992
@Test
@@ -97,17 +100,20 @@ public void testIncrementBy_incrementsValues() {
97100

98101
assertThat(counter.getTimestampedValues()).isEmpty();
99102

100-
counter.incrementBy(1, new Instant(1337), ImmutableList.of("test_value1"));
101-
assertThat(counter.getTimestampedValues(new Instant(1337)))
103+
counter.incrementBy(1, Instant.ofEpochMilli(1337), ImmutableList.of("test_value1"));
104+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1337)))
102105
.containsExactly(
103-
MetricPoint.create(counter, ImmutableList.of("test_value1"), new Instant(1337), 1L));
106+
MetricPoint.create(
107+
counter, ImmutableList.of("test_value1"), Instant.ofEpochMilli(1337), 1L));
104108

105-
counter.set(-10L, new Instant(1337), ImmutableList.of("test_value2"));
106-
counter.incrementBy(5, new Instant(1337), ImmutableList.of("test_value2"));
107-
assertThat(counter.getTimestampedValues(new Instant(1337)))
109+
counter.set(-10L, Instant.ofEpochMilli(1337), ImmutableList.of("test_value2"));
110+
counter.incrementBy(5, Instant.ofEpochMilli(1337), ImmutableList.of("test_value2"));
111+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1337)))
108112
.containsExactly(
109-
MetricPoint.create(counter, ImmutableList.of("test_value1"), new Instant(1337), 1L),
110-
MetricPoint.create(counter, ImmutableList.of("test_value2"), new Instant(1337), -5L));
113+
MetricPoint.create(
114+
counter, ImmutableList.of("test_value1"), Instant.ofEpochMilli(1337), 1L),
115+
MetricPoint.create(
116+
counter, ImmutableList.of("test_value2"), Instant.ofEpochMilli(1337), -5L));
111117
}
112118

113119
@Test
@@ -133,24 +139,40 @@ public void testResetAll_resetsAllValuesAndStartTimestamps() {
133139
"vdn",
134140
ImmutableSet.of(LabelDescriptor.create("label1", "bar")));
135141

136-
counter.incrementBy(3, new Instant(1337), ImmutableList.of("foo"));
137-
counter.incrementBy(5, new Instant(1338), ImmutableList.of("moo"));
142+
counter.incrementBy(3, Instant.ofEpochMilli(1337), ImmutableList.of("foo"));
143+
counter.incrementBy(5, Instant.ofEpochMilli(1338), ImmutableList.of("moo"));
138144

139-
assertThat(counter.getTimestampedValues(new Instant(1400)))
145+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1400)))
140146
.containsExactly(
141147
MetricPoint.create(
142-
counter, ImmutableList.of("foo"), new Instant(1337), new Instant(1400), 3L),
148+
counter,
149+
ImmutableList.of("foo"),
150+
Instant.ofEpochMilli(1337),
151+
Instant.ofEpochMilli(1400),
152+
3L),
143153
MetricPoint.create(
144-
counter, ImmutableList.of("moo"), new Instant(1338), new Instant(1400), 5L));
154+
counter,
155+
ImmutableList.of("moo"),
156+
Instant.ofEpochMilli(1338),
157+
Instant.ofEpochMilli(1400),
158+
5L));
145159

146-
counter.reset(new Instant(1339));
160+
counter.reset(Instant.ofEpochMilli(1339));
147161

148-
assertThat(counter.getTimestampedValues(new Instant(1400)))
162+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1400)))
149163
.containsExactly(
150164
MetricPoint.create(
151-
counter, ImmutableList.of("foo"), new Instant(1339), new Instant(1400), 0L),
165+
counter,
166+
ImmutableList.of("foo"),
167+
Instant.ofEpochMilli(1339),
168+
Instant.ofEpochMilli(1400),
169+
0L),
152170
MetricPoint.create(
153-
counter, ImmutableList.of("moo"), new Instant(1339), new Instant(1400), 0L));
171+
counter,
172+
ImmutableList.of("moo"),
173+
Instant.ofEpochMilli(1339),
174+
Instant.ofEpochMilli(1400),
175+
0L));
154176
}
155177

156178
@Test
@@ -162,23 +184,39 @@ public void testReset_resetsValuesAndStartTimestamps() {
162184
"vdn",
163185
ImmutableSet.of(LabelDescriptor.create("label1", "bar")));
164186

165-
counter.incrementBy(3, new Instant(1337), ImmutableList.of("foo"));
166-
counter.incrementBy(5, new Instant(1338), ImmutableList.of("moo"));
187+
counter.incrementBy(3, Instant.ofEpochMilli(1337), ImmutableList.of("foo"));
188+
counter.incrementBy(5, Instant.ofEpochMilli(1338), ImmutableList.of("moo"));
167189

168-
assertThat(counter.getTimestampedValues(new Instant(1400)))
190+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1400)))
169191
.containsExactly(
170192
MetricPoint.create(
171-
counter, ImmutableList.of("foo"), new Instant(1337), new Instant(1400), 3L),
193+
counter,
194+
ImmutableList.of("foo"),
195+
Instant.ofEpochMilli(1337),
196+
Instant.ofEpochMilli(1400),
197+
3L),
172198
MetricPoint.create(
173-
counter, ImmutableList.of("moo"), new Instant(1338), new Instant(1400), 5L));
199+
counter,
200+
ImmutableList.of("moo"),
201+
Instant.ofEpochMilli(1338),
202+
Instant.ofEpochMilli(1400),
203+
5L));
174204

175-
counter.reset(new Instant(1339), ImmutableList.of("foo"));
205+
counter.reset(Instant.ofEpochMilli(1339), ImmutableList.of("foo"));
176206

177-
assertThat(counter.getTimestampedValues(new Instant(1400)))
207+
assertThat(counter.getTimestampedValues(Instant.ofEpochMilli(1400)))
178208
.containsExactly(
179209
MetricPoint.create(
180-
counter, ImmutableList.of("foo"), new Instant(1339), new Instant(1400), 0L),
210+
counter,
211+
ImmutableList.of("foo"),
212+
Instant.ofEpochMilli(1339),
213+
Instant.ofEpochMilli(1400),
214+
0L),
181215
MetricPoint.create(
182-
counter, ImmutableList.of("moo"), new Instant(1338), new Instant(1400), 5L));
216+
counter,
217+
ImmutableList.of("moo"),
218+
Instant.ofEpochMilli(1338),
219+
Instant.ofEpochMilli(1400),
220+
5L));
183221
}
184222
}

0 commit comments

Comments
 (0)