Skip to content

Commit

Permalink
merge (#21)
Browse files Browse the repository at this point in the history
* actuatator counter and gauge name prefix
  • Loading branch information
guanchao-yang authored and luyiisme committed Jun 21, 2018
1 parent bb2a207 commit be02be8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public MetricsRegistryFactory prometheusMetricsRegistryFactory() {
"com.codahale.metrics.MetricRegistry" })
public DropWizardMetricsRegistryFactory dropWizardMetricsRegistryFactory() {
try {
/*
* In order to avoid [com.codahale.metrics.MetricRegistry] class not found.
*/
com.codahale.metrics.MetricRegistry metricRegistry = beanFactory
.getBean(com.codahale.metrics.MetricRegistry.class);
if (metricRegistry == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
*/
public class LookoutSpringBootMetricsImpl implements CounterService, GaugeService {

public static final String LOOKOUT_COUNTER_PREFIX = "counter.";

public static final String LOOKOUT_GAUGE_PREFIX = "gauge.";

private final Registry registry;

private final ConcurrentMap<String, SimpleLookoutGauge> gauges = new ConcurrentHashMap<String, SimpleLookoutGauge>();
private final ConcurrentMap<String, SimpleLookoutGauge> gauges = new ConcurrentHashMap<String, SimpleLookoutGauge>();

public LookoutSpringBootMetricsImpl(Registry registry) {
this.registry = registry;
Expand All @@ -48,6 +52,7 @@ public void increment(String metricName) {
if (StringUtils.isBlank(metricName)) {
return;
}
metricName = wrapName(LOOKOUT_COUNTER_PREFIX, metricName);
Id id = this.registry.createId(metricName);
Counter counter = this.registry.counter(id);
counter.inc();
Expand All @@ -58,6 +63,7 @@ public void decrement(String metricName) {
if (StringUtils.isBlank(metricName)) {
return;
}
metricName = wrapName(LOOKOUT_COUNTER_PREFIX, metricName);
Id id = this.registry.createId(metricName);
Counter counter = this.registry.counter(id);
counter.dec();
Expand All @@ -68,6 +74,7 @@ public void reset(String metricName) {
if (StringUtils.isBlank(metricName)) {
return;
}
metricName = wrapName(LOOKOUT_COUNTER_PREFIX, metricName);
Id id = this.registry.createId(metricName);
this.registry.removeMetric(id);
}
Expand All @@ -77,6 +84,7 @@ public void submit(String metricName, double value) {
if (StringUtils.isBlank(metricName)) {
return;
}
metricName = wrapName(LOOKOUT_GAUGE_PREFIX, metricName);
SimpleLookoutGauge gauge = this.gauges.get(metricName);
if (gauge == null) {
SimpleLookoutGauge newGauge = new SimpleLookoutGauge(value);
Expand All @@ -90,6 +98,16 @@ public void submit(String metricName, double value) {
gauge.setValue(value);
}

private String wrapName(String prefix, String metricName) {
if (StringUtils.isBlank(metricName)) {
throw new RuntimeException("Metric name can't be blank!");
}
if (metricName.startsWith(prefix)) {
return metricName;
}
return prefix + metricName;
}

private final static class SimpleLookoutGauge implements Gauge<Double> {

private volatile double value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ public void testInstance() throws Exception {
*/
@Test
public void testIncrement() throws Exception {
String metricName = "metricName";
this.counterService.increment(metricName);
String originName = "metricName";
String metricName = originName;
this.counterService.increment(originName);
metricName = LookoutSpringBootMetricsImpl.LOOKOUT_COUNTER_PREFIX + metricName;
Metric metric = this.lookoutRegistryMetricReader.findOne(metricName);
assertTrue(metric != null);
assertEquals(1L, metric.getValue());
//decrement
this.counterService.decrement(metricName);
this.counterService.decrement(originName);
metric = this.lookoutRegistryMetricReader.findOne(metricName);
assertTrue(metric != null);
assertEquals(0L, metric.getValue());
Expand All @@ -85,11 +87,13 @@ public void testIncrement() throws Exception {
*/
@Test
public void testReset() throws Exception {
String metricName = "metricName1";
this.counterService.increment(metricName);
String originName = "metricName1";
String metricName = originName;
this.counterService.increment(originName);
metricName = LookoutSpringBootMetricsImpl.LOOKOUT_COUNTER_PREFIX + metricName;
Metric metric = this.lookoutRegistryMetricReader.findOne(metricName);
assertTrue(metric != null);
this.counterService.reset(metricName);
this.counterService.reset(originName);
assertTrue(this.lookoutRegistryMetricReader.findOne(metricName) == null);
}

Expand All @@ -102,6 +106,7 @@ public void testSubmit() throws Exception {
double value = 10;
this.gaugeService.submit(gaugeName, value);
//get
gaugeName = LookoutSpringBootMetricsImpl.LOOKOUT_GAUGE_PREFIX + gaugeName;
Metric metric = this.lookoutRegistryMetricReader.findOne(gaugeName);
assertEquals(value, metric.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.lookout.starter.support.reader;

import com.alipay.lookout.starter.base.AbstractTestBase;
import com.alipay.lookout.starter.support.actuator.LookoutSpringBootMetricsImpl;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -60,7 +61,8 @@ public void testFindOne() throws Exception {
ResponseEntity<String> response = testRestTemplate.getForEntity(restUrl, String.class);
assertTrue(StringUtils.isNotBlank(response.getBody()));
//
Metric metric = this.lookoutRegistryMetricReader.findOne("response." + endpointId);
Metric metric = this.lookoutRegistryMetricReader
.findOne(LookoutSpringBootMetricsImpl.LOOKOUT_GAUGE_PREFIX + "response." + endpointId);
assertTrue(metric != null);
}

Expand Down

0 comments on commit be02be8

Please sign in to comment.