|
| 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