parent
26fdeaac46
commit
1acbe9b162
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
@ -15,7 +15,7 @@
|
||||
<properties>
|
||||
<dep.ver.metrics>3.1.2</dep.ver.metrics>
|
||||
<dep.ver.servlet>3.1.0</dep.ver.servlet>
|
||||
<netflix.servo.ver>0.12.16</netflix.servo.ver>
|
||||
<netflix.servo.ver>0.12.17</netflix.servo.ver>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -44,10 +44,24 @@
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${dep.ver.servlet}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.netflix.servo</groupId>
|
||||
<artifactId>servo-core</artifactId>
|
||||
<version>${netflix.servo.ver}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.servo</groupId>
|
||||
<artifactId>servo-atlas</artifactId>
|
||||
<version>${netflix.servo.ver}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-smile</artifactId>
|
||||
<version>2.8.9</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,95 @@
|
||||
package com.baeldung.metrics.servo;
|
||||
|
||||
import com.netflix.servo.DefaultMonitorRegistry;
|
||||
import com.netflix.servo.monitor.BasicCounter;
|
||||
import com.netflix.servo.monitor.Counter;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
import com.netflix.servo.publish.BasicMetricFilter;
|
||||
import com.netflix.servo.publish.MonitorRegistryMetricPoller;
|
||||
import com.netflix.servo.publish.PollRunnable;
|
||||
import com.netflix.servo.publish.PollScheduler;
|
||||
import com.netflix.servo.publish.atlas.AtlasMetricObserver;
|
||||
import com.netflix.servo.publish.atlas.BasicAtlasConfig;
|
||||
import com.netflix.servo.tag.BasicTagList;
|
||||
import org.apache.commons.lang.math.RandomUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.apache.http.client.methods.RequestBuilder.get;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class AtlasObserverLiveTest {
|
||||
|
||||
private final String atlasUri = "http://localhost:7101/api/v1";
|
||||
|
||||
@Before
|
||||
public void prepareScheduler() {
|
||||
System.setProperty("servo.pollers", "1000");
|
||||
System.setProperty("servo.atlas.batchSize", "1");
|
||||
System.setProperty("servo.atlas.uri", atlasUri + "/publish");
|
||||
AtlasMetricObserver observer = new AtlasMetricObserver(new BasicAtlasConfig(), BasicTagList.of("servo", "counter"));
|
||||
|
||||
PollRunnable task = new PollRunnable(new MonitorRegistryMetricPoller(), new BasicMetricFilter(true), observer);
|
||||
PollScheduler
|
||||
.getInstance()
|
||||
.start();
|
||||
PollScheduler
|
||||
.getInstance()
|
||||
.addPoller(task, 1, SECONDS);
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopScheduler() {
|
||||
if (PollScheduler
|
||||
.getInstance()
|
||||
.isStarted()) {
|
||||
PollScheduler
|
||||
.getInstance()
|
||||
.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private String atlasValuesOfTag(String tagname) throws Exception {
|
||||
HttpEntity entity = HttpClients
|
||||
.createDefault()
|
||||
.execute(get()
|
||||
.setUri(atlasUri + "/tags/" + tagname)
|
||||
.build())
|
||||
.getEntity();
|
||||
return new BufferedReader(new InputStreamReader(entity.getContent())).readLine();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAtlasAndCounter_whenRegister_thenPublishedToAtlas() throws Exception {
|
||||
Counter counter = new BasicCounter(MonitorConfig
|
||||
.builder("test")
|
||||
.withTag("servo", "counter")
|
||||
.build());
|
||||
DefaultMonitorRegistry
|
||||
.getInstance()
|
||||
.register(counter);
|
||||
assertThat(atlasValuesOfTag("servo"), not(containsString("counter")));
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
counter.increment(RandomUtils.nextInt(10));
|
||||
SECONDS.sleep(1);
|
||||
counter.increment(-1 * RandomUtils.nextInt(10));
|
||||
SECONDS.sleep(1);
|
||||
}
|
||||
|
||||
assertThat(atlasValuesOfTag("servo"), containsString("counter"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user