Skip to content

Commit 815bbfd

Browse files
add unit test form Timed
1 parent 738752c commit 815bbfd

File tree

5 files changed

+188
-6
lines changed

5 files changed

+188
-6
lines changed

instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/Timed.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* <p>The name should follow the instrument naming rule: <a
4242
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-naming-rule">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-naming-rule</a>
4343
*
44-
* <p>The default name is {@code method.invocations.duration}.
44+
* <p>The default name is {@code method.invocation.duration}.
4545
*/
4646
String value() default "";
4747

instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/CountedExample.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@ public class CountedExample {
1111

1212
public static final String ANOTHER_NAME_COUNT = "another.name.count";
1313
public static final String METRIC_DESCRIPTION = "I am the description.";
14-
public static final String METRIC_UNIT = "kb";
14+
public static final String METRIC_UNIT = "ms";
1515

1616
@Counted
1717
public void defaultExample() {}
1818

1919
@Counted(ANOTHER_NAME_COUNT)
2020
public void exampleWithAnotherName() {}
2121

22-
@Counted(value = "example.with.description", description = METRIC_DESCRIPTION)
22+
@Counted(description = METRIC_DESCRIPTION)
23+
public void exampleWithDescriptionAndDefaultValue() {}
24+
25+
@Counted(unit = METRIC_UNIT)
26+
public void exampleWithUnitAndDefaultValue() {}
27+
28+
@Counted(value = "example.with.description.count", description = METRIC_DESCRIPTION)
2329
public void exampleWithDescription() {}
2430

25-
@Counted(value = "example.with.unit", unit = METRIC_UNIT)
31+
@Counted(value = "example.with.unit.count", unit = METRIC_UNIT)
2632
public void exampleWithUnit() {}
2733

2834
@Counted(additionalAttributes = {"key1", "value1", "key2", "value2"})

instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/CountedInstrumentationTest.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,37 @@ void testExampleWithAnotherName() {
3939
metric -> metric.hasName(ANOTHER_NAME_COUNT));
4040
}
4141

42+
@Test
43+
void testExampleWithDescriptionAndDefaultValue() {
44+
new CountedExample().exampleWithDescriptionAndDefaultValue();
45+
testing.waitAndAssertMetrics(
46+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
47+
metric -> metric.hasName(COUNTED_DEFAULT_NAME).hasDescription(""));
48+
}
49+
50+
@Test
51+
void testExampleWithUnitAndDefaultValue() {
52+
new CountedExample().exampleWithUnitAndDefaultValue();
53+
testing.waitAndAssertMetrics(
54+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
55+
metric -> metric.hasName(COUNTED_DEFAULT_NAME).hasUnit(""));
56+
}
57+
4258
@Test
4359
void testExampleWithDescription() {
4460
new CountedExample().exampleWithDescription();
4561
testing.waitAndAssertMetrics(
4662
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
47-
metric -> metric.hasName("example.with.description").hasDescription(METRIC_DESCRIPTION));
63+
metric ->
64+
metric.hasName("example.with.description.count").hasDescription(METRIC_DESCRIPTION));
4865
}
4966

5067
@Test
5168
void testExampleWithUnit() {
5269
new CountedExample().exampleWithUnit();
5370
testing.waitAndAssertMetrics(
5471
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
55-
metric -> metric.hasName("example.with.unit").hasUnit(METRIC_UNIT));
72+
metric -> metric.hasName("example.with.unit.count").hasUnit(METRIC_UNIT));
5673
}
5774

5875
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.test.annotation;
7+
8+
import io.opentelemetry.instrumentation.annotations.Timed;
9+
10+
public class TimedExample {
11+
public static final String ANOTHER_NAME_HISTOGRAM = "another.name.duration";
12+
public static final String METRIC_DESCRIPTION = "I am the description.";
13+
public static final String METRIC_UNIT = "ms";
14+
15+
@Timed
16+
public void defaultExample() {}
17+
18+
@Timed(ANOTHER_NAME_HISTOGRAM)
19+
public void exampleWithAnotherName() {}
20+
21+
@Timed(description = METRIC_DESCRIPTION)
22+
public void exampleWithDescriptionAndDefaultValue() {}
23+
24+
@Timed(unit = METRIC_UNIT)
25+
public void exampleWithUnitAndDefaultValue() {}
26+
27+
@Timed(value = "example.with.description.duration", description = METRIC_DESCRIPTION)
28+
public void exampleWithDescription() {}
29+
30+
@Timed(value = "example.with.unit.duration", unit = METRIC_UNIT)
31+
public void exampleWithUnit() {}
32+
33+
@Timed(additionalAttributes = {"key1", "value1", "key2", "value2"})
34+
public void exampleWithAdditionalAttributes1() {}
35+
36+
@Timed(additionalAttributes = {"key1", "value1", "key2", "value2", "key3"})
37+
public void exampleWithAdditionalAttributes2() {}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.test.annotation;
7+
8+
import static io.opentelemetry.test.annotation.CountedExample.METRIC_DESCRIPTION;
9+
import static io.opentelemetry.test.annotation.CountedExample.METRIC_UNIT;
10+
import static io.opentelemetry.test.annotation.TimedExample.ANOTHER_NAME_HISTOGRAM;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
import io.opentelemetry.api.common.AttributeKey;
14+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.RegisterExtension;
17+
18+
class TimedInstrumentationTest {
19+
20+
@RegisterExtension
21+
public static final AgentInstrumentationExtension testing =
22+
AgentInstrumentationExtension.create();
23+
24+
public static final String TIMED_DEFAULT_NAME = "method.invocation.duration";
25+
26+
@Test
27+
void testDefaultExample() {
28+
new TimedExample().defaultExample();
29+
testing.waitAndAssertMetrics(
30+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
31+
metric -> metric.hasName(TIMED_DEFAULT_NAME));
32+
}
33+
34+
@Test
35+
void testExampleWithAnotherName() {
36+
new TimedExample().exampleWithAnotherName();
37+
testing.waitAndAssertMetrics(
38+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
39+
metric -> metric.hasName(ANOTHER_NAME_HISTOGRAM));
40+
}
41+
42+
@Test
43+
void testExampleWithDescriptionAndDefaultValue() {
44+
new TimedExample().exampleWithDescriptionAndDefaultValue();
45+
testing.waitAndAssertMetrics(
46+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
47+
metric -> metric.hasName(TIMED_DEFAULT_NAME).hasDescription(""));
48+
}
49+
50+
@Test
51+
void testExampleWithUnitAndDefaultValue() {
52+
new TimedExample().exampleWithUnitAndDefaultValue();
53+
testing.waitAndAssertMetrics(
54+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
55+
metric -> metric.hasName(TIMED_DEFAULT_NAME).hasUnit(""));
56+
}
57+
58+
@Test
59+
void testExampleWithDescription() {
60+
new TimedExample().exampleWithDescription();
61+
testing.waitAndAssertMetrics(
62+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
63+
metric ->
64+
metric.hasName("example.with.description.duration").hasDescription(METRIC_DESCRIPTION));
65+
}
66+
67+
@Test
68+
void testExampleWithUnit() {
69+
new TimedExample().exampleWithUnit();
70+
testing.waitAndAssertMetrics(
71+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
72+
metric -> metric.hasName("example.with.unit.duration").hasUnit(METRIC_UNIT));
73+
}
74+
75+
@Test
76+
void testExampleWithAdditionalAttributes1() {
77+
new TimedExample().exampleWithAdditionalAttributes1();
78+
testing.waitAndAssertMetrics(
79+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
80+
metric ->
81+
metric
82+
.hasName(TIMED_DEFAULT_NAME)
83+
.satisfies(
84+
metricData -> {
85+
assertThat(metricData.getData().getPoints())
86+
.allMatch(
87+
p ->
88+
"value1"
89+
.equals(
90+
p.getAttributes().get(AttributeKey.stringKey("key1")))
91+
&& "value2"
92+
.equals(
93+
p.getAttributes()
94+
.get(AttributeKey.stringKey("key2"))));
95+
}));
96+
}
97+
98+
@Test
99+
void testExampleWithAdditionalAttributes2() {
100+
new TimedExample().exampleWithAdditionalAttributes2();
101+
testing.waitAndAssertMetrics(
102+
"io.opentelemetry.opentelemetry-instrumentation-annotations-1.16",
103+
metric ->
104+
metric
105+
.hasName(TIMED_DEFAULT_NAME)
106+
.satisfies(
107+
metricData -> {
108+
assertThat(metricData.getData().getPoints())
109+
.allMatch(
110+
p ->
111+
"value1"
112+
.equals(
113+
p.getAttributes().get(AttributeKey.stringKey("key1")))
114+
&& "value2"
115+
.equals(
116+
p.getAttributes().get(AttributeKey.stringKey("key2")))
117+
&& null
118+
== p.getAttributes().get(AttributeKey.stringKey("key3")));
119+
}));
120+
}
121+
}

0 commit comments

Comments
 (0)