|
| 1 | +package com.baeldung.metrics.servo; |
| 2 | + |
| 3 | +import com.netflix.servo.DefaultMonitorRegistry; |
| 4 | +import com.netflix.servo.monitor.BasicCounter; |
| 5 | +import com.netflix.servo.monitor.Counter; |
| 6 | +import com.netflix.servo.monitor.MonitorConfig; |
| 7 | +import com.netflix.servo.publish.BasicMetricFilter; |
| 8 | +import com.netflix.servo.publish.MonitorRegistryMetricPoller; |
| 9 | +import com.netflix.servo.publish.PollRunnable; |
| 10 | +import com.netflix.servo.publish.PollScheduler; |
| 11 | +import com.netflix.servo.publish.atlas.AtlasMetricObserver; |
| 12 | +import com.netflix.servo.publish.atlas.BasicAtlasConfig; |
| 13 | +import com.netflix.servo.tag.BasicTagList; |
| 14 | +import org.apache.commons.lang.math.RandomUtils; |
| 15 | +import org.apache.http.HttpEntity; |
| 16 | +import org.apache.http.impl.client.HttpClients; |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import java.io.BufferedReader; |
| 22 | +import java.io.InputStreamReader; |
| 23 | + |
| 24 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 25 | +import static org.apache.http.client.methods.RequestBuilder.get; |
| 26 | +import static org.hamcrest.CoreMatchers.containsString; |
| 27 | +import static org.hamcrest.CoreMatchers.not; |
| 28 | +import static org.junit.Assert.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author aiet |
| 32 | + */ |
| 33 | +public class AtlasObserverLiveTest { |
| 34 | + |
| 35 | + private final String atlasUri = "http://localhost:7101/api/v1"; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void prepareScheduler() { |
| 39 | + System.setProperty("servo.pollers", "1000"); |
| 40 | + System.setProperty("servo.atlas.batchSize", "1"); |
| 41 | + System.setProperty("servo.atlas.uri", atlasUri + "/publish"); |
| 42 | + AtlasMetricObserver observer = new AtlasMetricObserver(new BasicAtlasConfig(), BasicTagList.of("servo", "counter")); |
| 43 | + |
| 44 | + PollRunnable task = new PollRunnable(new MonitorRegistryMetricPoller(), new BasicMetricFilter(true), observer); |
| 45 | + PollScheduler |
| 46 | + .getInstance() |
| 47 | + .start(); |
| 48 | + PollScheduler |
| 49 | + .getInstance() |
| 50 | + .addPoller(task, 1, SECONDS); |
| 51 | + } |
| 52 | + |
| 53 | + @After |
| 54 | + public void stopScheduler() { |
| 55 | + if (PollScheduler |
| 56 | + .getInstance() |
| 57 | + .isStarted()) { |
| 58 | + PollScheduler |
| 59 | + .getInstance() |
| 60 | + .stop(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private String atlasValuesOfTag(String tagname) throws Exception { |
| 65 | + HttpEntity entity = HttpClients |
| 66 | + .createDefault() |
| 67 | + .execute(get() |
| 68 | + .setUri(atlasUri + "/tags/" + tagname) |
| 69 | + .build()) |
| 70 | + .getEntity(); |
| 71 | + return new BufferedReader(new InputStreamReader(entity.getContent())).readLine(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void givenAtlasAndCounter_whenRegister_thenPublishedToAtlas() throws Exception { |
| 76 | + Counter counter = new BasicCounter(MonitorConfig |
| 77 | + .builder("test") |
| 78 | + .withTag("servo", "counter") |
| 79 | + .build()); |
| 80 | + DefaultMonitorRegistry |
| 81 | + .getInstance() |
| 82 | + .register(counter); |
| 83 | + assertThat(atlasValuesOfTag("servo"), not(containsString("counter"))); |
| 84 | + |
| 85 | + for (int i = 0; i < 3; i++) { |
| 86 | + counter.increment(RandomUtils.nextInt(10)); |
| 87 | + SECONDS.sleep(1); |
| 88 | + counter.increment(-1 * RandomUtils.nextInt(10)); |
| 89 | + SECONDS.sleep(1); |
| 90 | + } |
| 91 | + |
| 92 | + assertThat(atlasValuesOfTag("servo"), containsString("counter")); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments