Skip to content

Commit

Permalink
additional tests for various monitor types
Browse files Browse the repository at this point in the history
Adds tests for ContentualCounter, ContextualTimer,
PeakRateCounter, and MinGauge. The PeakRateCounter
and MinGauge are examples of custom meters.
  • Loading branch information
brharrington committed May 10, 2018
1 parent 00eea64 commit beea181
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2013 Netflix, Inc.
/*
* Copyright 2011-2018 Netflix, Inc.
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -118,7 +118,7 @@ private static ObjectNameMapper getObjectNameMapper(Properties props) {

return mapper;
}

private static Properties loadProps() {
String registryClassProp = System.getProperty(REGISTRY_CLASS_PROP);
String registryNameProp = System.getProperty(REGISTRY_NAME_PROP);
Expand Down Expand Up @@ -150,6 +150,7 @@ public Collection<Monitor<?>> getRegisteredMonitors() {
*/
@Override
public void register(Monitor<?> monitor) {
SpectatorContext.register(monitor);
registry.register(monitor);
}

Expand All @@ -158,6 +159,7 @@ public void register(Monitor<?> monitor) {
*/
@Override
public void unregister(Monitor<?> monitor) {
SpectatorContext.unregister(monitor);
registry.unregister(monitor);
}

Expand Down
72 changes: 72 additions & 0 deletions servo-core/src/main/java/com/netflix/servo/SpectatorContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@
*/
package com.netflix.servo;

import com.netflix.servo.monitor.CompositeMonitor;
import com.netflix.servo.monitor.Monitor;
import com.netflix.servo.monitor.MonitorConfig;
import com.netflix.servo.monitor.SpectatorMonitor;
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Gauge;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Meter;
import com.netflix.spectator.api.NoopRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Timer;
import com.netflix.spectator.api.patterns.PolledMeter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

Expand All @@ -47,6 +56,8 @@ private SpectatorContext() {
}
);

private static final Logger LOGGER = LoggerFactory.getLogger(SpectatorContext.class);

private static volatile Registry registry = new NoopRegistry();

/**
Expand Down Expand Up @@ -106,4 +117,65 @@ public static PolledMeter.Builder polledGauge(MonitorConfig config) {
.withId(createId(config))
.scheduleOn(GAUGE_POOL);
}

/** Register a custom monitor. */
public static void register(Monitor<?> monitor) {
PolledMeter.monitorMeter(registry, new ServoMeter(monitor));
}

/** Unregister a custom monitor. */
public static void unregister(Monitor<?> monitor) {
PolledMeter.remove(registry, createId(monitor.getConfig()));
}

private static class ServoMeter implements Meter {

private final Id id;
private final Monitor<?> monitor;

ServoMeter(Monitor<?> monitor) {
this.id = createId(monitor.getConfig());
this.monitor = monitor;
}

@Override public Id id() {
return id;
}

private void addMeasurements(Monitor<?> m, List<Measurement> measurements) {
// Skip any that will report directly
if (!(m instanceof SpectatorMonitor)) {
if (m instanceof CompositeMonitor<?>) {
CompositeMonitor<?> cm = (CompositeMonitor<?>) m;
for (Monitor<?> v : cm.getMonitors()) {
addMeasurements(v, measurements);
}
} else {
try {
Object obj = m.getValue();
if (obj instanceof Number) {
double value = ((Number) obj).doubleValue();
// timestamp will get ignored as the value will get forwarded to a gauge
Measurement v = new Measurement(createId(m.getConfig()), 0L, value);
measurements.add(v);
}
} catch (Throwable t) {
LOGGER.warn("Exception while querying user defined gauge ({}), "
+ "the value will be ignored. The owner of the user defined "
+ "function should fix it to not propagate an exception.", m.getConfig(), t);
}
}
}
}

@Override public Iterable<Measurement> measure() {
List<Measurement> measurements = new ArrayList<>();
addMeasurements(monitor, measurements);
return measurements;
}

@Override public boolean hasExpired() {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package com.netflix.servo.monitor;

import com.netflix.servo.DefaultMonitorRegistry;
import com.netflix.servo.SpectatorContext;
import com.netflix.servo.annotations.DataSourceType;
import com.netflix.servo.annotations.Monitor;
import com.netflix.servo.stats.StatsConfig;
import com.netflix.servo.tag.BasicTagList;
import com.netflix.servo.tag.TagList;
import com.netflix.servo.util.Clock;
import com.netflix.servo.util.ManualClock;
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Registry;
Expand All @@ -31,10 +35,6 @@
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class SpectatorIntegrationTest {

Expand Down Expand Up @@ -104,6 +104,35 @@ public void testAnnotatedCounter() {
assertEquals(1, registry.counter(id).count());
}

@Test
public void testContextualCounter() {
TagList context = BasicTagList.of("a", "1");
ContextualCounter c = new ContextualCounter(CONFIG, () -> context, BasicCounter::new);
c.increment();
Id id = ID.withTag("a", "1");
assertEquals(1, registry.counter(id).count());
}

@Test
public void testPeakRateCounter() {
PeakRateCounter c = new PeakRateCounter(CONFIG);
DefaultMonitorRegistry.getInstance().register(c);
c.increment();
PolledMeter.update(registry);
registry.stream().forEach(m -> System.out.println(m.id()));
assertEquals(1.0, registry.gauge(ID.withTag("type", "GAUGE")).value());
}

@Test
public void testPeakRateCounterRemove() {
PeakRateCounter c = new PeakRateCounter(CONFIG);
DefaultMonitorRegistry.getInstance().register(c);
DefaultMonitorRegistry.getInstance().unregister(c);
c.increment();
PolledMeter.update(registry);
assertEquals(0, registry.stream().count());
}

@Test
public void testDoubleGauge() {
DoubleGauge c = new DoubleGauge(CONFIG);
Expand Down Expand Up @@ -151,6 +180,27 @@ public void testDoubleMaxGauge() {
assertEquals(42.0, registry.maxGauge(ID).value(), 1e-12);
}

@Test
public void testMinGauge() {
ManualClock clock = new ManualClock(0);
MinGauge g = new MinGauge(CONFIG, clock);
DefaultMonitorRegistry.getInstance().register(g);
g.update(42);
clock.set(60000);
PolledMeter.update(registry);
assertEquals(42.0, registry.gauge(ID.withTag("type", "GAUGE")).value());
}

@Test
public void testMinGaugeRemove() {
MinGauge g = new MinGauge(CONFIG);
DefaultMonitorRegistry.getInstance().register(g);
DefaultMonitorRegistry.getInstance().unregister(g);
g.update(42);
PolledMeter.update(registry);
assertEquals(0, registry.stream().count());
}

@Test
public void testBasicDistributionSummaryRecord() {
BasicDistributionSummary d = new BasicDistributionSummary(CONFIG);
Expand Down Expand Up @@ -227,6 +277,18 @@ public void testStatsTimerRecordMillis() {
assertEquals(42.0, registry.gauge(id.withTag("statistic", "avg")).value(), 1e-12);
}

@Test
public void testContextualTimerRecordMillis() {
TagList context = BasicTagList.of("a", "1");
ContextualTimer d = new ContextualTimer(CONFIG, () -> context, BasicTimer::new);
d.record(42, TimeUnit.NANOSECONDS);
Id id = ID.withTag("unit", "MILLISECONDS").withTag("a", "1");
assertEquals(1, registry.counter(id.withTag(Statistic.count)).count());
assertEquals(42e-6, registry.counter(id.withTag(Statistic.totalTime)).actualCount(), 1e-12);
assertEquals(42e-6 * 42e-6, registry.counter(id.withTag(Statistic.totalOfSquares)).actualCount(), 1e-12);
assertEquals(42e-6, registry.maxGauge(id.withTag(Statistic.max)).value(), 1e-12);
}

public static class AnnotateExample {

private long count = 0;
Expand Down

0 comments on commit beea181

Please sign in to comment.