Skip to content

Commit 5653e4c

Browse files
szaluzhskiyNikitaNovozhilovWork
authored andcommitted
Add default micrometer reporter (#432)
Add default micrometer reporter Co-authored-by: NikitaNovozhilovWork <[email protected]>
1 parent 1ecbdcd commit 5653e4c

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dependencies {
5757
compile group: 'com.uber.m3', name: 'tally-core', version: '0.4.0'
5858
compile group: 'com.google.guava', name: 'guava', version: '28.1-jre'
5959
compile group: 'com.cronutils', name: 'cron-utils', version: '9.0.0'
60+
compile group: 'io.micrometer', name: 'micrometer-core', version: '1.1.2'
6061

6162
testCompile group: 'junit', name: 'junit', version: '4.12'
6263
testCompile group: 'com.googlecode.junit-toolbox', name: 'junit-toolbox', version: '2.4'
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.reporter;
19+
20+
import com.uber.m3.tally.Buckets;
21+
import com.uber.m3.tally.Capabilities;
22+
import com.uber.m3.tally.CapableOf;
23+
import com.uber.m3.tally.StatsReporter;
24+
import com.uber.m3.util.Duration;
25+
import io.micrometer.core.instrument.Metrics;
26+
import io.micrometer.core.instrument.Tag;
27+
import java.util.Map;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.stream.Collectors;
30+
31+
public class CadenceClientStatsReporter implements StatsReporter {
32+
33+
@Override
34+
public Capabilities capabilities() {
35+
return CapableOf.REPORTING;
36+
}
37+
38+
@Override
39+
public void flush() {
40+
// NOOP
41+
}
42+
43+
@Override
44+
public void close() {
45+
// NOOP
46+
}
47+
48+
@Override
49+
public void reportCounter(String name, Map<String, String> tags, long value) {
50+
Metrics.counter(name, getTags(tags)).increment(value);
51+
}
52+
53+
@Override
54+
public void reportGauge(String name, Map<String, String> tags, double value) {
55+
// NOOP
56+
}
57+
58+
@Override
59+
public void reportTimer(String name, Map<String, String> tags, Duration interval) {
60+
Metrics.timer(name, getTags(tags)).record(interval.getNanos(), TimeUnit.NANOSECONDS);
61+
}
62+
63+
@Override
64+
public void reportHistogramValueSamples(
65+
String name,
66+
Map<String, String> tags,
67+
Buckets buckets,
68+
double bucketLowerBound,
69+
double bucketUpperBound,
70+
long samples) {
71+
// NOOP
72+
}
73+
74+
@Override
75+
public void reportHistogramDurationSamples(
76+
String name,
77+
Map<String, String> tags,
78+
Buckets buckets,
79+
Duration bucketLowerBound,
80+
Duration bucketUpperBound,
81+
long samples) {
82+
// NOOP
83+
}
84+
85+
private Iterable<Tag> getTags(Map<String, String> tags) {
86+
return tags.entrySet()
87+
.stream()
88+
.map(entry -> Tag.of(entry.getKey(), entry.getValue()))
89+
.collect(Collectors.toList());
90+
}
91+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.reporter;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
import com.uber.m3.tally.CapableOf;
23+
import com.uber.m3.util.Duration;
24+
import com.uber.m3.util.ImmutableMap;
25+
import io.micrometer.core.instrument.Metrics;
26+
import io.micrometer.core.instrument.Tag;
27+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
28+
import java.util.Arrays;
29+
import java.util.Map;
30+
import java.util.concurrent.TimeUnit;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
35+
public class CadenceClientStatsReporterTest {
36+
37+
private static final String DEFAULT_REPORT_NAME = "cadence_workflow_start";
38+
private static final Map<String, String> DEFAULT_REPORT_TAGS =
39+
ImmutableMap.of("Domain", "domain_name", "TaskList", "task_list");
40+
private static final long DEFAULT_COUNT = 10;
41+
private static final Duration DEFAULT_DURATION = Duration.ofSeconds(10);
42+
43+
private CadenceClientStatsReporter cadenceClientStatsReporter = new CadenceClientStatsReporter();
44+
45+
@Before
46+
public void init() {
47+
Metrics.addRegistry(new SimpleMeterRegistry());
48+
}
49+
50+
@After
51+
public void cleanup() {
52+
Metrics.globalRegistry.getMeters().forEach(Metrics.globalRegistry::remove);
53+
}
54+
55+
@Test
56+
public void testReporterCapabilitiesShouldReturnReporting() {
57+
assertEquals(CapableOf.REPORTING, cadenceClientStatsReporter.capabilities());
58+
}
59+
60+
@Test
61+
public void testCounterShouldCallMetricRegistryForMonitoredCounterCadenceAction() {
62+
callDefaultCounter();
63+
64+
assertEquals(
65+
Arrays.asList(Tag.of("Domain", "domain_name"), Tag.of("TaskList", "task_list")),
66+
Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).counter().getId().getTags());
67+
assertEquals(10, Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).counter().count(), 0);
68+
}
69+
70+
@Test
71+
public void testTimerShouldCallMetricRegistryForMonitoredCounterCadenceAction() {
72+
callDefaultTimer();
73+
74+
assertEquals(
75+
Arrays.asList(Tag.of("Domain", "domain_name"), Tag.of("TaskList", "task_list")),
76+
Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).timer().getId().getTags());
77+
assertEquals(
78+
10, Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).timer().totalTime(TimeUnit.SECONDS), 0);
79+
}
80+
81+
private void callDefaultCounter() {
82+
cadenceClientStatsReporter.reportCounter(
83+
DEFAULT_REPORT_NAME, DEFAULT_REPORT_TAGS, DEFAULT_COUNT);
84+
}
85+
86+
private void callDefaultTimer() {
87+
cadenceClientStatsReporter.reportTimer(
88+
DEFAULT_REPORT_NAME, DEFAULT_REPORT_TAGS, DEFAULT_DURATION);
89+
}
90+
}

0 commit comments

Comments
 (0)