Skip to content

Commit 90c87bf

Browse files
remove timed.unit
1 parent 3ea13f7 commit 90c87bf

File tree

4 files changed

+6
-56
lines changed
  • instrumentation-annotations-incubator/src/main/java/io/opentelemetry/instrumentation/annotations/incubator
  • instrumentation/opentelemetry-instrumentation-annotations/opentelemetry-instrumentation-annotations-incubator/javaagent/src

4 files changed

+6
-56
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.lang.annotation.Retention;
1010
import java.lang.annotation.RetentionPolicy;
1111
import java.lang.annotation.Target;
12-
import java.util.concurrent.TimeUnit;
1312

1413
/**
1514
* This annotation creates a {@link io.opentelemetry.api.metrics.DoubleHistogram Histogram}
@@ -50,11 +49,4 @@
5049
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description">https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description</a>
5150
*/
5251
String description() default "";
53-
54-
/**
55-
* The unit for the metric.
56-
*
57-
* <p>Default is seconds.
58-
*/
59-
TimeUnit unit() default TimeUnit.SECONDS;
6052
}

instrumentation/opentelemetry-instrumentation-annotations/opentelemetry-instrumentation-annotations-incubator/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/incubator/TimedHelper.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,51 +57,30 @@ private static MethodTimer getMethodTimer(Method method) {
5757
return timers.get(method.getDeclaringClass()).computeIfAbsent(method, MethodTimer::new);
5858
}
5959

60-
private static String timeUnitToString(TimeUnit timeUnit) {
61-
switch (timeUnit) {
62-
case NANOSECONDS:
63-
return "ns";
64-
case MICROSECONDS:
65-
return "us";
66-
case MILLISECONDS:
67-
return "ms";
68-
case SECONDS:
69-
return "s";
70-
case MINUTES:
71-
return "min";
72-
case HOURS:
73-
return "h";
74-
case DAYS:
75-
return "d";
76-
}
77-
throw new IllegalArgumentException("Unsupported time unit " + timeUnit);
78-
}
79-
80-
private static double getDuration(long startNanoTime, TimeUnit unit) {
60+
private static double getDurationInSecond(long startNanoTime) {
8161
long nanoDelta = System.nanoTime() - startNanoTime;
82-
return (double) nanoDelta / NANOSECONDS.convert(1, unit);
62+
return (double) nanoDelta / NANOSECONDS.convert(1, TimeUnit.SECONDS);
8363
}
8464

8565
private static class MethodTimer {
86-
private final TimeUnit unit;
8766
private final DoubleHistogram histogram;
8867
private final MetricAttributeHelper attributeHelper;
8968

9069
MethodTimer(Method method) {
9170
Timed timedAnnotation = method.getAnnotation(Timed.class);
92-
unit = timedAnnotation.unit();
9371
histogram =
9472
METER
9573
.histogramBuilder(timedAnnotation.value())
9674
.setDescription(timedAnnotation.description())
97-
.setUnit(timeUnitToString(unit))
75+
.setUnit("s")
9876
.build();
9977
attributeHelper = new MetricAttributeHelper(method);
10078
}
10179

10280
void record(Object returnValue, Object[] arguments, Throwable throwable, long startNanoTime) {
103-
double duration = getDuration(startNanoTime, unit);
104-
histogram.record(duration, attributeHelper.getAttributes(returnValue, arguments, throwable));
81+
double durationInSecond = getDurationInSecond(startNanoTime);
82+
histogram.record(
83+
durationInSecond, attributeHelper.getAttributes(returnValue, arguments, throwable));
10584
}
10685
}
10786

instrumentation/opentelemetry-instrumentation-annotations/opentelemetry-instrumentation-annotations-incubator/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/incubator/timed/TimedExample.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.opentelemetry.instrumentation.annotations.incubator.StaticAttribute;
1111
import io.opentelemetry.instrumentation.annotations.incubator.Timed;
1212
import java.util.concurrent.CompletableFuture;
13-
import java.util.concurrent.TimeUnit;
1413

1514
public class TimedExample {
1615
public static final String METRIC_NAME = "name.duration";
@@ -23,11 +22,6 @@ public void exampleWithName() {}
2322
@Timed(value = "example.with.description.duration", description = METRIC_DESCRIPTION)
2423
public void exampleWithDescription() {}
2524

26-
@Timed(value = "example.with.unit.duration", unit = TimeUnit.MILLISECONDS)
27-
public void exampleWithUnit() throws InterruptedException {
28-
Thread.sleep(2000);
29-
}
30-
3125
@Timed("example.with.static.attributes.duration")
3226
@StaticAttribute(name = "key1", value = "value1")
3327
@StaticAttribute(name = "key2", value = "value2")

instrumentation/opentelemetry-instrumentation-annotations/opentelemetry-instrumentation-annotations-incubator/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/incubator/timed/TimedInstrumentationTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,6 @@ void testExampleWithDescription() {
4141
metric.hasName("example.with.description.duration").hasDescription(METRIC_DESCRIPTION));
4242
}
4343

44-
@Test
45-
void testExampleWithUnit() throws InterruptedException {
46-
new TimedExample().exampleWithUnit();
47-
testing.waitAndAssertMetrics(
48-
TIMED_INSTRUMENTATION_NAME,
49-
metric ->
50-
metric
51-
.hasName("example.with.unit.duration")
52-
.hasUnit("ms")
53-
.satisfies(
54-
metricData ->
55-
assertThat(metricData.getHistogramData().getPoints())
56-
.allMatch(p -> p.getMax() < 5000 && p.getMin() > 0)));
57-
}
58-
5944
@Test
6045
void testExampleWithStaticAttributes() {
6146
new TimedExample().exampleWithStaticAttributes();

0 commit comments

Comments
 (0)