From cde993191e76b0a2e465e8bb171cf888fe6dc87f Mon Sep 17 00:00:00 2001 From: Shawn Pearce Date: Thu, 12 Nov 2015 09:45:43 -0800 Subject: [PATCH] Test DropWizard MetricMaker glue Simple test cases to verify the MetricMaker glues onto DropWizard as expected. Here we mostly assume DropWizard works as advertised and does not itself need to be heavily tested. Change-Id: I9f8286386e15a9f6d3eba6dc211b5bbf3669ff3f --- .../java/com/google/gerrit/pgm/Daemon.java | 3 +- gerrit-server/BUCK | 1 + .../dropwizard/DropWizardMetricMaker.java | 7 +- .../metrics/proc/ProcMetricModuleTest.java | 159 ++++++++++++++++++ .../gerrit/httpd/WebAppInitializer.java | 3 +- 5 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 gerrit-server/src/test/java/com/google/gerrit/metrics/proc/ProcMetricModuleTest.java diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java index 67cf369574..ebbb7658de 100644 --- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java +++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java @@ -325,7 +325,8 @@ public class Daemon extends SiteProgram { private Injector createSysInjector() { final List modules = new ArrayList<>(); modules.add(SchemaVersionCheck.module()); - modules.add(new DropWizardMetricMaker.Module()); + modules.add(new DropWizardMetricMaker.ApiModule()); + modules.add(new DropWizardMetricMaker.RestModule()); modules.add(new LogFileCompressor.Module()); modules.add(new WorkQueue.Module()); modules.add(new ChangeHookRunner.Module()); diff --git a/gerrit-server/BUCK b/gerrit-server/BUCK index 107b3f1635..b192432585 100644 --- a/gerrit-server/BUCK +++ b/gerrit-server/BUCK @@ -195,6 +195,7 @@ java_test( '//lib:args4j', '//lib:grappa', '//lib:guava', + '//lib/dropwizard:dropwizard-core', '//lib/guice:guice-assistedinject', '//lib/joda:joda-time', '//lib/prolog:runtime', diff --git a/gerrit-server/src/main/java/com/google/gerrit/metrics/dropwizard/DropWizardMetricMaker.java b/gerrit-server/src/main/java/com/google/gerrit/metrics/dropwizard/DropWizardMetricMaker.java index 58abed8a4f..0a3bc5eb5d 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/metrics/dropwizard/DropWizardMetricMaker.java +++ b/gerrit-server/src/main/java/com/google/gerrit/metrics/dropwizard/DropWizardMetricMaker.java @@ -60,7 +60,7 @@ import java.util.concurrent.TimeUnit; */ @Singleton public class DropWizardMetricMaker extends MetricMaker { - public static class Module extends RestApiModule { + public static class ApiModule extends RestApiModule { @Override protected void configure() { bind(MetricRegistry.class).in(Scopes.SINGLETON); @@ -69,7 +69,12 @@ public class DropWizardMetricMaker extends MetricMaker { install(new ProcMetricModule()); install(new JGitMetricModule()); + } + } + public static class RestModule extends RestApiModule { + @Override + protected void configure() { DynamicMap.mapOf(binder(), METRIC_KIND); child(CONFIG_KIND, "metrics").to(MetricsCollection.class); get(METRIC_KIND).to(GetMetric.class); diff --git a/gerrit-server/src/test/java/com/google/gerrit/metrics/proc/ProcMetricModuleTest.java b/gerrit-server/src/test/java/com/google/gerrit/metrics/proc/ProcMetricModuleTest.java new file mode 100644 index 0000000000..d52e74792f --- /dev/null +++ b/gerrit-server/src/test/java/com/google/gerrit/metrics/proc/ProcMetricModuleTest.java @@ -0,0 +1,159 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.metrics.proc; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.gerrit.common.Version; +import com.google.gerrit.lifecycle.LifecycleManager; +import com.google.gerrit.metrics.CallbackMetric0; +import com.google.gerrit.metrics.Counter0; +import com.google.gerrit.metrics.Counter1; +import com.google.gerrit.metrics.Description; +import com.google.gerrit.metrics.Field; +import com.google.gerrit.metrics.MetricMaker; +import com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker; +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.Injector; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Metric; +import com.codahale.metrics.MetricRegistry; + +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +public class ProcMetricModuleTest { + @Inject + MetricMaker metrics; + + @Inject + MetricRegistry registry; + + @Test + public void testConstantBuildLabel() { + Gauge buildLabel = gauge("build/label"); + assertThat(buildLabel.getValue()).isEqualTo(Version.getVersion()); + } + + @Test + public void testProcUptime() { + Gauge birth = gauge("proc/birth_timestamp"); + assertThat(birth.getValue()).isLessThan( + TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis())); + + Gauge uptime = gauge("proc/uptime"); + assertThat(uptime.getValue()).isAtLeast(1L); + } + + @Test + public void testCounter0() { + Counter0 cntr = metrics.newCounter( + "test/count", + new Description("simple test") + .setCumulative()); + + Counter raw = get("test/count", Counter.class); + assertThat(raw.getCount()).isEqualTo(0); + + cntr.increment(); + assertThat(raw.getCount()).isEqualTo(1); + + cntr.incrementBy(5); + assertThat(raw.getCount()).isEqualTo(6); + } + + @Test + public void testCounter1() { + Counter1 cntr = metrics.newCounter( + "test/count", + new Description("simple test") + .setCumulative(), + Field.ofString("action")); + + Counter total = get("test/count_total", Counter.class); + assertThat(total.getCount()).isEqualTo(0); + + cntr.increment("passed"); + Counter passed = get("test/count/passed", Counter.class); + assertThat(total.getCount()).isEqualTo(1); + assertThat(passed.getCount()).isEqualTo(1); + + cntr.incrementBy("failed", 5); + Counter failed = get("test/count/failed", Counter.class); + assertThat(total.getCount()).isEqualTo(6); + assertThat(passed.getCount()).isEqualTo(1); + assertThat(failed.getCount()).isEqualTo(5); + } + + @Test + public void testCallbackMetric0() { + final CallbackMetric0 cntr = metrics.newCallbackMetric( + "test/count", + Long.class, + new Description("simple test") + .setCumulative()); + + final AtomicInteger invocations = new AtomicInteger(0); + metrics.newTrigger(cntr, new Runnable() { + @Override + public void run() { + invocations.getAndIncrement(); + cntr.set(42L); + } + }); + + // Triggers run immediately with DropWizard binding. + assertThat(invocations.get()).isEqualTo(1); + + Gauge raw = gauge("test/count"); + assertThat(raw.getValue()).isEqualTo(42); + + // Triggers are debounced to avoid being fired too frequently. + assertThat(invocations.get()).isEqualTo(1); + } + + @SuppressWarnings({"unchecked", "cast"}) + private Gauge gauge(String name) { + return (Gauge) get(name, Gauge.class); + } + + private M get(String name, Class type) { + Metric m = registry.getMetrics().get(name); + assertThat(m).named(name).isNotNull(); + assertThat(m).named(name).isInstanceOf(type); + + @SuppressWarnings("unchecked") + M result = (M) m; + return result; + } + + @Before + public void setup() { + Injector injector = + Guice.createInjector(new DropWizardMetricMaker.ApiModule()); + + LifecycleManager mgr = new LifecycleManager(); + mgr.add(injector); + mgr.start(); + + injector.injectMembers(this); + } +} diff --git a/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java b/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java index de80303507..10e3f9c482 100644 --- a/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java +++ b/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java @@ -289,7 +289,8 @@ public class WebAppInitializer extends GuiceServletContextListener private Injector createSysInjector() { final List modules = new ArrayList<>(); - modules.add(new DropWizardMetricMaker.Module()); + modules.add(new DropWizardMetricMaker.ApiModule()); + modules.add(new DropWizardMetricMaker.RestModule()); modules.add(new WorkQueue.Module()); modules.add(new ChangeHookRunner.Module()); modules.add(new ReceiveCommitsExecutorModule());