This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exemplar: Refactor to be under common.
- Loading branch information
Showing
10 changed files
with
194 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2019, OpenCensus Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opencensus.common; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
/*>>> | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
*/ | ||
|
||
/** | ||
* An example point that may be used to annotate aggregated distribution values, associated with a | ||
* histogram bucket. | ||
* | ||
* @since 0.20 | ||
*/ | ||
@Immutable | ||
@AutoValue | ||
public abstract class Exemplar { | ||
|
||
Exemplar() {} | ||
|
||
/** | ||
* Returns value of the {@link Exemplar} point. | ||
* | ||
* @return value of the {@code Exemplar} point. | ||
* @since 0.20 | ||
*/ | ||
public abstract double getValue(); | ||
|
||
/** | ||
* Returns the time that this {@link Exemplar}'s value was recorded. | ||
* | ||
* @return the time that this {@code Exemplar}'s value was recorded. | ||
* @since 0.20 | ||
*/ | ||
public abstract Timestamp getTimestamp(); | ||
|
||
/** | ||
* Returns the contextual information about the example value. | ||
* | ||
* @return the contextual information about the example value. | ||
* @since 0.20 | ||
*/ | ||
public abstract Map<String, AttachmentValue> getAttachments(); | ||
|
||
/** | ||
* Creates an {@link Exemplar}. | ||
* | ||
* @param value value of the {@link Exemplar} point. | ||
* @param timestamp the time that this {@code Exemplar}'s value was recorded. | ||
* @param attachments the contextual information about the example value. | ||
* @return an {@code Exemplar}. | ||
* @since 0.20 | ||
*/ | ||
public static Exemplar create( | ||
double value, Timestamp timestamp, Map<String, AttachmentValue> attachments) { | ||
checkNotNull(attachments, "attachments"); | ||
Map<String, AttachmentValue> attachmentsCopy = | ||
Collections.unmodifiableMap(new HashMap<String, AttachmentValue>(attachments)); | ||
for (Entry<String, AttachmentValue> entry : attachmentsCopy.entrySet()) { | ||
checkNotNull(entry.getKey(), "key of attachments"); | ||
checkNotNull(entry.getValue(), "value of attachments"); | ||
} | ||
return new AutoValue_Exemplar(value, timestamp, attachmentsCopy); | ||
} | ||
|
||
// TODO(songy23): shade the internal Utils jar and remove this duplicated method. | ||
private static <T /*>>> extends @NonNull Object*/> T checkNotNull( | ||
T arg, @javax.annotation.Nullable Object errorMessage) { | ||
if (arg == null) { | ||
throw new NullPointerException(String.valueOf(errorMessage)); | ||
} | ||
return arg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2019, OpenCensus Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opencensus.common; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import io.opencensus.common.AttachmentValue.AttachmentValueString; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Unit tests for {@link Exemplar}. */ | ||
@RunWith(JUnit4.class) | ||
public class ExemplarTest { | ||
|
||
private static final double TOLERANCE = 1e-6; | ||
private static final Timestamp TIMESTAMP_1 = Timestamp.create(1, 0); | ||
private static final AttachmentValue ATTACHMENT_VALUE = AttachmentValueString.create("value"); | ||
private static final Map<String, AttachmentValue> ATTACHMENTS = | ||
Collections.singletonMap("key", ATTACHMENT_VALUE); | ||
|
||
@Rule public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void testExemplar() { | ||
Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP_1, ATTACHMENTS); | ||
assertThat(exemplar.getValue()).isWithin(TOLERANCE).of(15.0); | ||
assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP_1); | ||
assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS); | ||
} | ||
|
||
@Test | ||
public void testExemplar_PreventNullTimestamp() { | ||
thrown.expect(NullPointerException.class); | ||
Exemplar.create(15, null, ATTACHMENTS); | ||
} | ||
|
||
@Test | ||
public void testExemplar_PreventNullAttachments() { | ||
thrown.expect(NullPointerException.class); | ||
thrown.expectMessage("attachments"); | ||
Exemplar.create(15, TIMESTAMP_1, null); | ||
} | ||
|
||
@Test | ||
public void testExemplar_PreventNullAttachmentKey() { | ||
Map<String, AttachmentValue> attachments = Collections.singletonMap(null, ATTACHMENT_VALUE); | ||
thrown.expect(NullPointerException.class); | ||
thrown.expectMessage("key of attachment"); | ||
Exemplar.create(15, TIMESTAMP_1, attachments); | ||
} | ||
|
||
@Test | ||
public void testExemplar_PreventNullAttachmentValue() { | ||
Map<String, AttachmentValue> attachments = Collections.singletonMap("key", null); | ||
thrown.expect(NullPointerException.class); | ||
thrown.expectMessage("value of attachment"); | ||
Exemplar.create(15, TIMESTAMP_1, attachments); | ||
} | ||
} |
Oops, something went wrong.