Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 70f9701

Browse files
authored
Exemplar: Refactor to be under metrics.data. (#1791)
* Exemplar: Refactor to be under common. * Update all impl and contrib. * Move Exemplar and AttachmentValue to metrics.data. * Add a note in CHANGELOG.
1 parent b47f070 commit 70f9701

File tree

29 files changed

+240
-210
lines changed

29 files changed

+240
-210
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- Modified default value to false for publicEndpoint property in Http-Servlet.
77
- Add a generic `AttachmentValue` class to support `Exemplar`.
88
- Add Elasticsearch Trace Exporter.
9+
- Add `metrics.data` package to hold common classes shared between stats and metrics.
10+
- Refactor `Exemplar` and `AttachmentValue` to be under `metrics.data`. Note that this is a breaking change
11+
if you're using the `Exemplar` classes or APIs in the previous releases.
912

1013
# 0.19.0 - 2019-01-28
1114
- Add an artifact `opencensus-contrib-http-jetty-client` for instrumenting jetty http client. Add extractor for Jetty Client.

api/src/main/java/io/opencensus/stats/AttachmentValue.java renamed to api/src/main/java/io/opencensus/metrics/data/AttachmentValue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opencensus.stats;
17+
package io.opencensus.metrics.data;
1818

1919
import com.google.auto.value.AutoValue;
20-
import io.opencensus.stats.AggregationData.DistributionData.Exemplar;
2120
import javax.annotation.concurrent.Immutable;
2221

2322
/**
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.metrics.data;
18+
19+
import com.google.auto.value.AutoValue;
20+
import io.opencensus.common.Timestamp;
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.Map.Entry;
25+
import javax.annotation.concurrent.Immutable;
26+
27+
/*>>>
28+
import org.checkerframework.checker.nullness.qual.NonNull;
29+
*/
30+
31+
/**
32+
* An example point that may be used to annotate aggregated distribution values, associated with a
33+
* histogram bucket.
34+
*
35+
* @since 0.20
36+
*/
37+
@Immutable
38+
@AutoValue
39+
public abstract class Exemplar {
40+
41+
Exemplar() {}
42+
43+
/**
44+
* Returns value of the {@link Exemplar} point.
45+
*
46+
* @return value of the {@code Exemplar} point.
47+
* @since 0.20
48+
*/
49+
public abstract double getValue();
50+
51+
/**
52+
* Returns the time that this {@link Exemplar}'s value was recorded.
53+
*
54+
* @return the time that this {@code Exemplar}'s value was recorded.
55+
* @since 0.20
56+
*/
57+
public abstract Timestamp getTimestamp();
58+
59+
/**
60+
* Returns the contextual information about the example value.
61+
*
62+
* @return the contextual information about the example value.
63+
* @since 0.20
64+
*/
65+
public abstract Map<String, AttachmentValue> getAttachments();
66+
67+
/**
68+
* Creates an {@link Exemplar}.
69+
*
70+
* @param value value of the {@link Exemplar} point.
71+
* @param timestamp the time that this {@code Exemplar}'s value was recorded.
72+
* @param attachments the contextual information about the example value.
73+
* @return an {@code Exemplar}.
74+
* @since 0.20
75+
*/
76+
public static Exemplar create(
77+
double value, Timestamp timestamp, Map<String, AttachmentValue> attachments) {
78+
checkNotNull(attachments, "attachments");
79+
Map<String, AttachmentValue> attachmentsCopy =
80+
Collections.unmodifiableMap(new HashMap<String, AttachmentValue>(attachments));
81+
for (Entry<String, AttachmentValue> entry : attachmentsCopy.entrySet()) {
82+
checkNotNull(entry.getKey(), "key of attachments");
83+
checkNotNull(entry.getValue(), "value of attachments");
84+
}
85+
return new AutoValue_Exemplar(value, timestamp, attachmentsCopy);
86+
}
87+
88+
// TODO(songy23): shade the internal Utils jar and remove this duplicated method.
89+
private static <T /*>>> extends @NonNull Object*/> T checkNotNull(
90+
T arg, @javax.annotation.Nullable Object errorMessage) {
91+
if (arg == null) {
92+
throw new NullPointerException(String.valueOf(errorMessage));
93+
}
94+
return arg;
95+
}
96+
}

api/src/main/java/io/opencensus/metrics/export/Distribution.java

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
import com.google.auto.value.AutoValue;
2020
import io.opencensus.common.ExperimentalApi;
2121
import io.opencensus.common.Function;
22-
import io.opencensus.common.Timestamp;
2322
import io.opencensus.internal.Utils;
23+
import io.opencensus.metrics.data.Exemplar;
2424
import java.util.ArrayList;
2525
import java.util.Collections;
26-
import java.util.HashMap;
2726
import java.util.List;
28-
import java.util.Map;
29-
import java.util.Map.Entry;
3027
import javax.annotation.Nullable;
3128
import javax.annotation.concurrent.Immutable;
3229

@@ -284,62 +281,4 @@ public static Bucket create(long count, Exemplar exemplar) {
284281
@Nullable
285282
public abstract Exemplar getExemplar();
286283
}
287-
288-
/**
289-
* An example point that may be used to annotate aggregated distribution values, associated with a
290-
* histogram bucket.
291-
*
292-
* @since 0.17
293-
*/
294-
@Immutable
295-
@AutoValue
296-
public abstract static class Exemplar {
297-
298-
Exemplar() {}
299-
300-
/**
301-
* Returns value of the {@link Exemplar} point.
302-
*
303-
* @return value of the {@code Exemplar} point.
304-
* @since 0.17
305-
*/
306-
public abstract double getValue();
307-
308-
/**
309-
* Returns the time that this {@link Exemplar}'s value was recorded.
310-
*
311-
* @return the time that this {@code Exemplar}'s value was recorded.
312-
* @since 0.17
313-
*/
314-
public abstract Timestamp getTimestamp();
315-
316-
/**
317-
* Returns the contextual information about the example value, represented as a string map.
318-
*
319-
* @return the contextual information about the example value.
320-
* @since 0.17
321-
*/
322-
public abstract Map<String, String> getAttachments();
323-
324-
/**
325-
* Creates an {@link Exemplar}.
326-
*
327-
* @param value value of the {@link Exemplar} point.
328-
* @param timestamp the time that this {@code Exemplar}'s value was recorded.
329-
* @param attachments the contextual information about the example value.
330-
* @return an {@code Exemplar}.
331-
* @since 0.17
332-
*/
333-
public static Exemplar create(
334-
double value, Timestamp timestamp, Map<String, String> attachments) {
335-
Utils.checkNotNull(attachments, "attachments");
336-
Map<String, String> attachmentsCopy =
337-
Collections.unmodifiableMap(new HashMap<String, String>(attachments));
338-
for (Entry<String, String> entry : attachmentsCopy.entrySet()) {
339-
Utils.checkNotNull(entry.getKey(), "key of attachments");
340-
Utils.checkNotNull(entry.getValue(), "value of attachments");
341-
}
342-
return new AutoValue_Distribution_Exemplar(value, timestamp, attachmentsCopy);
343-
}
344-
}
345284
}

api/src/main/java/io/opencensus/stats/AggregationData.java

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818

1919
import com.google.auto.value.AutoValue;
2020
import io.opencensus.common.Function;
21-
import io.opencensus.common.Timestamp;
2221
import io.opencensus.internal.Utils;
22+
import io.opencensus.metrics.data.Exemplar;
2323
import java.util.ArrayList;
2424
import java.util.Collections;
25-
import java.util.HashMap;
2625
import java.util.List;
27-
import java.util.Map;
28-
import java.util.Map.Entry;
2926
import javax.annotation.concurrent.Immutable;
3027

3128
/**
@@ -445,65 +442,6 @@ public final <T> T match(
445442
Function<? super AggregationData, T> defaultFunction) {
446443
return p3.apply(this);
447444
}
448-
449-
/**
450-
* An example point that may be used to annotate aggregated distribution values, associated with
451-
* a histogram bucket.
452-
*
453-
* @since 0.16
454-
*/
455-
@Immutable
456-
@AutoValue
457-
public abstract static class Exemplar {
458-
459-
Exemplar() {}
460-
461-
/**
462-
* Returns value of the {@link Exemplar} point.
463-
*
464-
* @return value of the {@code Exemplar} point.
465-
* @since 0.16
466-
*/
467-
public abstract double getValue();
468-
469-
/**
470-
* Returns the time that this {@link Exemplar}'s value was recorded.
471-
*
472-
* @return the time that this {@code Exemplar}'s value was recorded.
473-
* @since 0.16
474-
*/
475-
public abstract Timestamp getTimestamp();
476-
477-
/**
478-
* Returns the contextual information about the example value.
479-
*
480-
* @return the contextual information about the example value.
481-
* @since 0.20
482-
*/
483-
public abstract Map<String, AttachmentValue> getAttachments();
484-
485-
/**
486-
* Creates an {@link Exemplar}.
487-
*
488-
* @param value value of the {@link Exemplar} point.
489-
* @param timestamp the time that this {@code Exemplar}'s value was recorded.
490-
* @param attachments the contextual information about the example value.
491-
* @return an {@code Exemplar}.
492-
* @since 0.20
493-
*/
494-
public static Exemplar create(
495-
double value, Timestamp timestamp, Map<String, AttachmentValue> attachments) {
496-
Utils.checkNotNull(attachments, "attachments");
497-
Map<String, AttachmentValue> attachmentsCopy =
498-
Collections.unmodifiableMap(new HashMap<String, AttachmentValue>(attachments));
499-
for (Entry<String, AttachmentValue> entry : attachmentsCopy.entrySet()) {
500-
Utils.checkNotNull(entry.getKey(), "key of attachments");
501-
Utils.checkNotNull(entry.getValue(), "value of attachments");
502-
}
503-
return new AutoValue_AggregationData_DistributionData_Exemplar(
504-
value, timestamp, attachmentsCopy);
505-
}
506-
}
507445
}
508446

509447
/**

api/src/main/java/io/opencensus/stats/MeasureMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package io.opencensus.stats;
1818

1919
import io.opencensus.internal.Utils;
20-
import io.opencensus.stats.AttachmentValue.AttachmentValueString;
20+
import io.opencensus.metrics.data.AttachmentValue;
21+
import io.opencensus.metrics.data.AttachmentValue.AttachmentValueString;
2122
import io.opencensus.stats.Measure.MeasureDouble;
2223
import io.opencensus.stats.Measure.MeasureLong;
2324
import io.opencensus.tags.TagContext;

api/src/test/java/io/opencensus/stats/AttachmentValueTest.java renamed to api/src/test/java/io/opencensus/metrics/data/AttachmentValueTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opencensus.stats;
17+
package io.opencensus.metrics.data;
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import io.opencensus.stats.AttachmentValue.AttachmentValueString;
21+
import io.opencensus.metrics.data.AttachmentValue.AttachmentValueString;
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
2525
import org.junit.runner.RunWith;
2626
import org.junit.runners.JUnit4;
2727

28-
/** Unit tests for {@link AttachmentValue}. */
28+
/** Unit tests for {@link io.opencensus.metrics.data.AttachmentValue}. */
2929
@RunWith(JUnit4.class)
3030
public class AttachmentValueTest {
3131

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.metrics.data;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import io.opencensus.common.Timestamp;
22+
import io.opencensus.metrics.data.AttachmentValue.AttachmentValueString;
23+
import java.util.Collections;
24+
import java.util.Map;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Unit tests for {@link io.opencensus.metrics.data.Exemplar}. */
32+
@RunWith(JUnit4.class)
33+
public class ExemplarTest {
34+
35+
private static final double TOLERANCE = 1e-6;
36+
private static final Timestamp TIMESTAMP_1 = Timestamp.create(1, 0);
37+
private static final AttachmentValue ATTACHMENT_VALUE = AttachmentValueString.create("value");
38+
private static final Map<String, AttachmentValue> ATTACHMENTS =
39+
Collections.singletonMap("key", ATTACHMENT_VALUE);
40+
41+
@Rule public ExpectedException thrown = ExpectedException.none();
42+
43+
@Test
44+
public void testExemplar() {
45+
Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP_1, ATTACHMENTS);
46+
assertThat(exemplar.getValue()).isWithin(TOLERANCE).of(15.0);
47+
assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP_1);
48+
assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS);
49+
}
50+
51+
@Test
52+
public void testExemplar_PreventNullTimestamp() {
53+
thrown.expect(NullPointerException.class);
54+
Exemplar.create(15, null, ATTACHMENTS);
55+
}
56+
57+
@Test
58+
public void testExemplar_PreventNullAttachments() {
59+
thrown.expect(NullPointerException.class);
60+
thrown.expectMessage("attachments");
61+
Exemplar.create(15, TIMESTAMP_1, null);
62+
}
63+
64+
@Test
65+
public void testExemplar_PreventNullAttachmentKey() {
66+
Map<String, AttachmentValue> attachments = Collections.singletonMap(null, ATTACHMENT_VALUE);
67+
thrown.expect(NullPointerException.class);
68+
thrown.expectMessage("key of attachment");
69+
Exemplar.create(15, TIMESTAMP_1, attachments);
70+
}
71+
72+
@Test
73+
public void testExemplar_PreventNullAttachmentValue() {
74+
Map<String, AttachmentValue> attachments = Collections.singletonMap("key", null);
75+
thrown.expect(NullPointerException.class);
76+
thrown.expectMessage("value of attachment");
77+
Exemplar.create(15, TIMESTAMP_1, attachments);
78+
}
79+
}

0 commit comments

Comments
 (0)