Skip to content

Commit d0b5c8e

Browse files
authored
Add micrometer ContextTimer implementation (#144)
* Declare new module for micrometer * Add micrometer ContextTimer implementation Signed-off-by: Sjoerd Talsma <sjoerd@talsma-ict.nl>
1 parent 4f03358 commit d0b5c8e

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
io.micrometer.core.annotation
2+
io.micrometer.core.aop
3+
io.micrometer.core.instrument
4+
io.micrometer.core.instrument.binder
5+
io.micrometer.core.instrument.binder.cache
6+
io.micrometer.core.instrument.binder.db
7+
io.micrometer.core.instrument.binder.httpcomponents
8+
io.micrometer.core.instrument.binder.hystrix
9+
io.micrometer.core.instrument.binder.jetty
10+
io.micrometer.core.instrument.binder.jpa
11+
io.micrometer.core.instrument.binder.jvm
12+
io.micrometer.core.instrument.binder.kafka
13+
io.micrometer.core.instrument.binder.logging
14+
io.micrometer.core.instrument.binder.mongodb
15+
io.micrometer.core.instrument.binder.okhttp3
16+
io.micrometer.core.instrument.binder.system
17+
io.micrometer.core.instrument.binder.tomcat
18+
io.micrometer.core.instrument.composite
19+
io.micrometer.core.instrument.config
20+
io.micrometer.core.instrument.cumulative
21+
io.micrometer.core.instrument.distribution
22+
io.micrometer.core.instrument.distribution.pause
23+
io.micrometer.core.instrument.dropwizard
24+
io.micrometer.core.instrument.internal
25+
io.micrometer.core.instrument.logging
26+
io.micrometer.core.instrument.noop
27+
io.micrometer.core.instrument.push
28+
io.micrometer.core.instrument.search
29+
io.micrometer.core.instrument.simple
30+
io.micrometer.core.instrument.step
31+
io.micrometer.core.instrument.util
32+
io.micrometer.core.ipc.http
33+
io.micrometer.core.lang
34+
io.micrometer.core.util.internal.logging

context-propagation-bom/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@
9494
<classifier>sources</classifier>
9595
</dependency>
9696

97+
<dependency>
98+
<groupId>${project.groupId}</groupId>
99+
<artifactId>context-propagation-micrometer</artifactId>
100+
<version>${project.version}</version>
101+
</dependency>
102+
<dependency>
103+
<groupId>${project.groupId}</groupId>
104+
<artifactId>context-propagation-micrometer</artifactId>
105+
<version>${project.version}</version>
106+
<classifier>javadoc</classifier>
107+
</dependency>
108+
<dependency>
109+
<groupId>${project.groupId}</groupId>
110+
<artifactId>context-propagation-micrometer</artifactId>
111+
<version>${project.version}</version>
112+
<classifier>sources</classifier>
113+
</dependency>
114+
97115
<dependency>
98116
<groupId>${project.groupId}</groupId>
99117
<artifactId>mdc-propagation</artifactId>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2016-2019 Talsma ICT
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
23+
<modelVersion>4.0.0</modelVersion>
24+
<parent>
25+
<groupId>nl.talsmasoftware.context</groupId>
26+
<artifactId>context-propagation-root</artifactId>
27+
<version>1.0.8-SNAPSHOT</version>
28+
</parent>
29+
30+
<!-- Artifact identification -->
31+
<artifactId>context-propagation-micrometer</artifactId>
32+
<name>Context propagation (micrometer)</name>
33+
<packaging>jar</packaging>
34+
35+
<properties>
36+
<project.moduleName>${project.groupId}.metrics</project.moduleName>
37+
<root.basedir>${project.parent.basedir}</root.basedir>
38+
39+
<micrometer.version>1.3.2</micrometer.version>
40+
</properties>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>${project.groupId}</groupId>
45+
<artifactId>context-propagation</artifactId>
46+
<version>${project.version}</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>io.micrometer</groupId>
51+
<artifactId>micrometer-core</artifactId>
52+
<version>${micrometer.version}</version>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016-2019 Talsma ICT
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+
package nl.talsmasoftware.context.micrometer;
17+
18+
import io.micrometer.core.instrument.Metrics;
19+
import nl.talsmasoftware.context.timing.ContextTimer;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
/**
24+
* {@linkplain ContextTimer} using a {@linkplain io.micrometer.core.instrument.Timer micrometer Timer}
25+
* to {@linkplain io.micrometer.core.instrument.Timer#record(long, TimeUnit) record} the context switching durations.
26+
*/
27+
public class MicrometerContextTimer implements ContextTimer {
28+
29+
/**
30+
* Updates the {@linkplain io.micrometer.core.instrument.Timer micrometer Timer} specified by the
31+
* {@code type} and {@code method} with the given {@code duration}.
32+
*
33+
* @param type The class being called
34+
* @param method The method being called
35+
* @param duration The duration of the method
36+
* @param unit The unit of the duration
37+
*/
38+
@Override
39+
public void update(Class<?> type, String method, long duration, TimeUnit unit) {
40+
Metrics.timer(type.getName() + "." + method).record(duration, unit);
41+
}
42+
43+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nl.talsmasoftware.context.micrometer.MicrometerContextTimer
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2016-2019 Talsma ICT
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+
package nl.talsmasoftware.context.micrometer;
17+
18+
import io.micrometer.core.instrument.Metrics;
19+
import io.micrometer.core.instrument.Timer;
20+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
21+
import nl.talsmasoftware.context.ContextManagers;
22+
import nl.talsmasoftware.context.ContextSnapshot;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
import static org.hamcrest.Matchers.is;
31+
import static org.hamcrest.Matchers.notNullValue;
32+
import static org.hamcrest.number.IsCloseTo.closeTo;
33+
34+
public class MicrometerContextTimerTest {
35+
SimpleMeterRegistry registry;
36+
37+
@Before
38+
public void setupRegistry() {
39+
registry = new SimpleMeterRegistry();
40+
Metrics.addRegistry(registry);
41+
}
42+
43+
@After
44+
public void shutdownRegistry() {
45+
Metrics.removeRegistry(registry);
46+
registry.clear();
47+
registry.close();
48+
}
49+
50+
@Test
51+
public void testCreateSnapshotInFreshApplication() {
52+
Timer timer = Metrics.timer(ContextManagers.class.getName() + ".createContextSnapshot");
53+
assertThat(timer.count(), is(0L));
54+
55+
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
56+
assertThat(snapshot, is(notNullValue()));
57+
58+
assertThat(timer.count(), is(1L));
59+
}
60+
61+
@Test
62+
public void testTiming() {
63+
Timer timer = Metrics.timer(MicrometerContextTimerTest.class.getName() + ".testTiming");
64+
new MicrometerContextTimer().update(MicrometerContextTimerTest.class, "testTiming", 43, TimeUnit.MILLISECONDS);
65+
assertThat(timer.count(), is(1L));
66+
assertThat(timer.mean(TimeUnit.NANOSECONDS), closeTo(43000000.0d, 0.001d));
67+
}
68+
69+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<module>context-propagation-java5</module>
4040
<module>context-propagation-java8</module>
4141
<module>context-propagation-metrics</module>
42+
<module>context-propagation-micrometer</module>
4243
<module>locale-context</module>
4344
<module>mdc-propagation</module>
4445
<module>servletrequest-propagation</module>
@@ -216,6 +217,10 @@
216217
<url>https://javadoc.io/page/javax.servlet/javax.servlet-api/${servlet-api.version}</url>
217218
<location>${root.basedir}/.mvn/javadoc/servlet-api</location>
218219
</offlineLink>
220+
<offlineLink>
221+
<url>https://www.javadoc.io/page/io.micrometer/micrometer-core/1.3.2</url>
222+
<location>${root.basedir}/.mvn/javadoc/micrometer-core</location>
223+
</offlineLink>
219224
</offlineLinks>
220225
</configuration>
221226
</plugin>

0 commit comments

Comments
 (0)