@BeforeClass public static void init() { registry = new DefaultRegistry(); Spectator.globalRegistry().add(registry); }
@Test public void testGuage() { DefaultRegistry registry = new DefaultRegistry(); SpectatorMetricRegistry metricRegistry = new SpectatorMetricRegistry(registry, registry.createId("foo")); metricRegistry.registerGauge("bar", () -> 10); PolledMeter.update(registry); Assert.assertEquals(10.0, registry.gauge(registry.createId("foo.bar")).value(), 0); }
@Test public void testIgnoreNan() { Id id = idB.withTag("tagA", "Z"); Measurement measure = new Measurement(id, 100, Double.NaN); Meter meter = new TestMeter("ignoreZ", measure); DefaultRegistry registry = new DefaultRegistry(clock); registry.register(meter); HashMap<String, MetricValues> expect = new HashMap<>(); PolledMeter.update(registry); Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll)); }
@Test public void testMeterToKind() { DefaultRegistry r = new DefaultRegistry(clock); Assertions.assertEquals( "Counter", MetricsController.meterToKind(r, r.counter(idAXY))); Assertions.assertEquals( "Timer", MetricsController.meterToKind(r, r.timer(idBXY))); Assertions.assertEquals( "TestMeter", MetricsController.meterToKind(r, meterA)); }
@Test public void testEncodeSimpleRegistry() { DefaultRegistry registry = new DefaultRegistry(clock); Counter counterA = registry.counter(idAXY); Counter counterB = registry.counter(idBXY); counterA.increment(4); counterB.increment(10); List<TaggedDataPoints> expectedTaggedDataPointsA = Arrays.asList( new TaggedDataPoints( Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagB", "Y")), Arrays.asList(new DataPoint(clock.wallTime(), 4)))); List<TaggedDataPoints> expectedTaggedDataPointsB = Arrays.asList( new TaggedDataPoints( Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagB", "Y")), Arrays.asList(new DataPoint(clock.wallTime(), 10)))); HashMap<String, MetricValues> expect = new HashMap<String, MetricValues>(); expect.put("idA", new MetricValues("Counter", expectedTaggedDataPointsA)); expect.put("idB", new MetricValues("Counter", expectedTaggedDataPointsB)); Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll)); }
@Override protected Gauge newMaxGauge(Id id) { return new DefaultMaxGauge(clock(), id); } }
@Test public void publishPayloadNoCommonTags() throws Exception { Id id = registry.createId("foo"); Measurement m = new Measurement(id, 42L, 3.0); PublishPayload p = new PublishPayload(Collections.emptyMap(), Collections.singletonList(m)); String json = mapper.writeValueAsString(p); String tags = "{\"name\":\"foo\",\"atlas.dstype\":\"gauge\"}"; String mjson = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}"; String expected = "{\"tags\":{},\"metrics\":[" + mjson + "]}"; Assertions.assertEquals(expected, json); }
@Test public void testEncodeCombinedRegistry() { // Multiple occurrences of measurements in the same registry // (confirm these are handled within the registry itself). Measurement measureBXY2 = new Measurement(idBXY, 5, 5.5); Meter meterB2 = new TestMeter("ignoreB", measureBXY2); DefaultRegistry registry = new DefaultRegistry(clock); registry.register(meterB); registry.register(meterB2); List<TaggedDataPoints> expectedTaggedDataPoints = Arrays.asList( new TaggedDataPoints( Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagB", "Y")), Arrays.asList(new DataPoint(clock.wallTime(), 50.5 + 5.5)))); HashMap<String, MetricValues> expect = new HashMap<>(); expect.put("idB", new MetricValues("Counter", expectedTaggedDataPoints)); PolledMeter.update(registry); Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll)); }
@Override protected Gauge newMaxGauge(Id id) { return new DefaultMaxGauge(clock(), id); } }
@Test public void publishPayloadWithCommonTags() throws Exception { Id id = registry.createId("foo"); Measurement m = new Measurement(id, 42L, 3.0); PublishPayload p = new PublishPayload(Collections.singletonMap("a", "b"), Collections.singletonList(m)); String json = mapper.writeValueAsString(p); String tags = "{\"name\":\"foo\",\"atlas.dstype\":\"gauge\"}"; String mjson = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}"; String expected = "{\"tags\":{\"a\":\"b\"},\"metrics\":[" + mjson + "]}"; Assertions.assertEquals(expected, json); } }
@BeforeMethod public void before() { DefaultMonitorRegistry.getInstance().getRegisteredMonitors().forEach( m -> DefaultMonitorRegistry.getInstance().unregister(m) ); registry = new DefaultRegistry(); SpectatorContext.setRegistry(registry); }
Meter meterA2 = new TestMeter("ignoreA", measureAXY2); DefaultRegistry registry = new DefaultRegistry(clock); registry.register(meterA); registry.register(meterA2);
@Test public void testUnregister() { DefaultRegistry registry = new DefaultRegistry(); SpectatorMetricRegistry metricRegistry = new SpectatorMetricRegistry(registry, registry.createId("foo")); metricRegistry.registerGauge("bar", () -> 10); metricRegistry.registerGauge("bar", () -> 20); PolledMeter.update(registry); Assert.assertEquals(20.0, registry.gauge(registry.createId("foo.bar")).value(), 0); } }
@Override protected Counter newCounter(Id id) { return new DefaultCounter(clock(), id); }
@Test public void explicitDsType() throws Exception { Id id = registry.createId("foo", "atlas.dstype", "rate"); Measurement m = new Measurement(id, 42L, 3.0); String json = mapper.writeValueAsString(m); String tags = "{\"name\":\"foo\",\"atlas.dstype\":\"rate\"}"; String expected = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}"; Assertions.assertEquals(expected, json); }
@Override protected void configure() { bind(IConfiguration.class).toInstance(new FakeConfiguration("fake-app")); bind(IBackupRestoreConfig.class).to(FakeBackupRestoreConfig.class); bind(InstanceInfo.class) .toInstance(new FakeInstanceInfo("fakeInstance1", "az1", "us-east-1")); bind(IPriamInstanceFactory.class).to(FakePriamInstanceFactory.class); bind(SchedulerFactory.class).to(StdSchedulerFactory.class).in(Scopes.SINGLETON); bind(IMembership.class) .toInstance( new FakeMembership( ImmutableList.of( "fakeInstance1", "fakeInstance2", "fakeInstance3"))); bind(ICredential.class).to(FakeCredentials.class).in(Scopes.SINGLETON); bind(IBackupFileSystem.class).to(NullBackupFileSystem.class); bind(Sleeper.class).to(FakeSleeper.class); bind(Registry.class).toInstance(new DefaultRegistry()); } }
@Override protected DistributionSummary newDistributionSummary(Id id) { return new DefaultDistributionSummary(clock(), id); }
@Test public void invalidName() throws Exception { Id id = registry.createId("f@%", "bar", "baz"); Measurement m = new Measurement(id, 42L, 3.0); String json = mapper.writeValueAsString(m); String tags = "{\"name\":\"f__\",\"bar\":\"baz\",\"atlas.dstype\":\"gauge\"}"; String expected = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}"; Assertions.assertEquals(expected, json); }
bind(ICassandraProcess.class).to(FakeCassandraProcess.class); bind(IPostRestoreHook.class).to(FakePostRestoreHook.class); bind(Registry.class).toInstance(new DefaultRegistry()); bind(IMetaProxy.class).annotatedWith(Names.named("v1")).to(MetaV1Proxy.class); bind(IMetaProxy.class).annotatedWith(Names.named("v2")).to(MetaV2Proxy.class);
@Override protected Timer newTimer(Id id) { return new DefaultTimer(clock(), id); }