From 85ba38151886d5d7d3dd2cba4e28952148953557 Mon Sep 17 00:00:00 2001 From: Vijay Samuel Date: Mon, 17 Apr 2017 23:57:15 -0700 Subject: [PATCH] Adding dropwizard docker container for integration testing --- metricbeat/docs/modules/dropwizard.asciidoc | 2 +- .../module/dropwizard/_meta/.dockerignore | 1 + metricbeat/module/dropwizard/_meta/Dockerfile | 12 ++--- .../module/dropwizard/_meta/docs.asciidoc | 2 +- metricbeat/module/dropwizard/_meta/env | 2 +- metricbeat/module/dropwizard/_meta/main.go | 22 -------- .../module/dropwizard/_meta/test/.gitignore | 1 + .../module/dropwizard/_meta/test/pom.xml | 40 +++++++++++++++ .../MetricsServletContextListener.java | 48 ++++++++++++++++++ .../java/io/test/dropwizard/TestServlet.java | 39 +++++++++++++++ .../test/src/main/webapp/WEB-INF/web.xml | 50 +++++++++++++++++++ .../dropwizard/collector/_meta/data.json | 12 +---- .../collector/collector_integration_test.go | 24 ++++++--- .../tests/system/config/metricbeat.yml.j2 | 4 ++ metricbeat/tests/system/test_dropwizard.py | 5 +- 15 files changed, 212 insertions(+), 52 deletions(-) create mode 100644 metricbeat/module/dropwizard/_meta/.dockerignore delete mode 100644 metricbeat/module/dropwizard/_meta/main.go create mode 100644 metricbeat/module/dropwizard/_meta/test/.gitignore create mode 100644 metricbeat/module/dropwizard/_meta/test/pom.xml create mode 100644 metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/MetricsServletContextListener.java create mode 100644 metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/TestServlet.java create mode 100644 metricbeat/module/dropwizard/_meta/test/src/main/webapp/WEB-INF/web.xml diff --git a/metricbeat/docs/modules/dropwizard.asciidoc b/metricbeat/docs/modules/dropwizard.asciidoc index ccc486971d1..eb0ab3f4767 100644 --- a/metricbeat/docs/modules/dropwizard.asciidoc +++ b/metricbeat/docs/modules/dropwizard.asciidoc @@ -5,7 +5,7 @@ This file is generated! See scripts/docs_collector.py [[metricbeat-module-dropwizard]] == dropwizard Module -This is the dropwizard Module. +This is the http://dropwizard.io[Dropwizard] Module. diff --git a/metricbeat/module/dropwizard/_meta/.dockerignore b/metricbeat/module/dropwizard/_meta/.dockerignore new file mode 100644 index 00000000000..bb0e316a479 --- /dev/null +++ b/metricbeat/module/dropwizard/_meta/.dockerignore @@ -0,0 +1 @@ +test/target diff --git a/metricbeat/module/dropwizard/_meta/Dockerfile b/metricbeat/module/dropwizard/_meta/Dockerfile index e8286a730a9..4c544812b27 100644 --- a/metricbeat/module/dropwizard/_meta/Dockerfile +++ b/metricbeat/module/dropwizard/_meta/Dockerfile @@ -1,8 +1,8 @@ -FROM golang:1.7 -RUN apt-get update && apt-get install curl -COPY main.go /go +FROM maven:3.3-jdk-8 +COPY test /test -HEALTHCHECK CMD curl -f http://localhost:9090/metrics/metrics -EXPOSE 9090 +HEALTHCHECK CMD curl -f http://localhost:8080/test/helloworld +EXPOSE 8080 -CMD go run /go/main.go +WORKDIR /test +CMD mvn jetty:run diff --git a/metricbeat/module/dropwizard/_meta/docs.asciidoc b/metricbeat/module/dropwizard/_meta/docs.asciidoc index fa3955edee1..21ade09313d 100644 --- a/metricbeat/module/dropwizard/_meta/docs.asciidoc +++ b/metricbeat/module/dropwizard/_meta/docs.asciidoc @@ -1,4 +1,4 @@ == dropwizard Module -This is the dropwizard Module. +This is the http://dropwizard.io[Dropwizard] Module. diff --git a/metricbeat/module/dropwizard/_meta/env b/metricbeat/module/dropwizard/_meta/env index c4e2d405211..771807862fd 100644 --- a/metricbeat/module/dropwizard/_meta/env +++ b/metricbeat/module/dropwizard/_meta/env @@ -1,2 +1,2 @@ DROPWIZARD_HOST=dropwizard -DROPWIZARD_PORT=9090 +DROPWIZARD_PORT=8080 diff --git a/metricbeat/module/dropwizard/_meta/main.go b/metricbeat/module/dropwizard/_meta/main.go deleted file mode 100644 index a174370976b..00000000000 --- a/metricbeat/module/dropwizard/_meta/main.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" -) - -var response = `{"version":"4.0.0","gauges":{"my_gauge{this=that}":{"value":565}},"counters":{"my_counter":{"count":565},"my_counter2{this=that}":{"count":565}},"histograms":{"my_hist":{"count":565,"max":564,"mean":563.3148706761577,"min":0,"p50":564.0,"p75":564.0,"p95":564.0,"p98":564.0,"p99":564.0,"p999":564.0,"stddev":1.0747916190718627}},"meters":{"my_meter":{"count":0,"m1_rate":0.0,"m5_rate":0.0,"m15_rate":0.0,"mean_rate":0.0,"units":"events/second"}},"timers":{"my_timer{this=that}":{"count":0,"max":0.0,"mean":0.0,"min":0.0,"p50":0.0,"p75":0.0,"p95":0.0,"p98":0.0,"p99":0.0,"p999":0.0,"stddev":0.0,"m1_rate":0.0,"m5_rate":0.0,"m15_rate":0.0,"mean_rate":0.0,"duration_units":"seconds","rate_units":"calls/second"}}}` - -func sendResponse(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, response) -} - -func main() { - - http.HandleFunc("/metrics/metrics", sendResponse) // set router - err := http.ListenAndServe(":9090", nil) // set listen port - if err != nil { - log.Fatal("ListenAndServe: ", err) - } -} diff --git a/metricbeat/module/dropwizard/_meta/test/.gitignore b/metricbeat/module/dropwizard/_meta/test/.gitignore new file mode 100644 index 00000000000..eb5a316cbd1 --- /dev/null +++ b/metricbeat/module/dropwizard/_meta/test/.gitignore @@ -0,0 +1 @@ +target diff --git a/metricbeat/module/dropwizard/_meta/test/pom.xml b/metricbeat/module/dropwizard/_meta/test/pom.xml new file mode 100644 index 00000000000..430125802b2 --- /dev/null +++ b/metricbeat/module/dropwizard/_meta/test/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + io.test.dropwizard + test + 1.0-SNAPSHOT + war + Test Webapp for dropwizard metrics + + + javax.servlet + servlet-api + 2.5 + provided + + + io.dropwizard.metrics + metrics-servlets + 3.1.0 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + org.mortbay.jetty + maven-jetty-plugin + + + + diff --git a/metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/MetricsServletContextListener.java b/metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/MetricsServletContextListener.java new file mode 100644 index 00000000000..264d8d8e6d5 --- /dev/null +++ b/metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/MetricsServletContextListener.java @@ -0,0 +1,48 @@ +package io.test.dropwizard; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import com.codahale.metrics.servlets.MetricsServlet; + +/** + * + * MetricsServletContextListener is a listener class that needs to be added to all assertion + * web application's web.xml in order to expose the MetricsRegistry which maintains all the + * metrics that are being tracked. + * + */ + +public class MetricsServletContextListener extends MetricsServlet.ContextListener { + + public static MetricRegistry METRIC_REGISTRY = new MetricRegistry(); + + static { + Counter c = new Counter(); + c.inc(); + METRIC_REGISTRY.register("my_counter{this=that}", c); + METRIC_REGISTRY.register("my_meter{this=that}", new Meter()); + + METRIC_REGISTRY.register("my_timer", new Timer()); + METRIC_REGISTRY.histogram("my_histogram"); + METRIC_REGISTRY.register("my_gauge", new Gauge() { + + @Override + public Integer getValue() { + // TODO Auto-generated method stub + return null; + } + + }); + + } + + @Override + protected MetricRegistry getMetricRegistry() { + return METRIC_REGISTRY; + } + + +} diff --git a/metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/TestServlet.java b/metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/TestServlet.java new file mode 100644 index 00000000000..e651c4aa446 --- /dev/null +++ b/metricbeat/module/dropwizard/_meta/test/src/main/java/io/test/dropwizard/TestServlet.java @@ -0,0 +1,39 @@ +package io.test.dropwizard; + +import java.io.Closeable; +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +@SuppressWarnings("serial") +public class TestServlet extends HttpServlet { + @SuppressWarnings("unchecked") + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + PrintWriter out = null; + try { + out = new PrintWriter(resp.getWriter()); + out.println("hello world"); + out.flush(); + } finally { + close(out); + } + } + + private static void close(Closeable c) { + if (c == null) { + return; + } + + try { + c.close(); + } catch (IOException ignore) { + /* ignore */ + } + } +} diff --git a/metricbeat/module/dropwizard/_meta/test/src/main/webapp/WEB-INF/web.xml b/metricbeat/module/dropwizard/_meta/test/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..783c271fac4 --- /dev/null +++ b/metricbeat/module/dropwizard/_meta/test/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,50 @@ + + + Test Dropwizard + + + HelloServlet + io.test.dropwizard.TestServlet + + + + HelloServlet + /helloworld + + + + io.test.dropwizard.MetricsServletContextListener + + + + CodahaleMetrics + com.codahale.metrics.servlets.MetricsServlet + + metrics-uri + /metrics + + + ping-uri + /ping + + + healthcheck-uri + /health + + + threads-uri + /threads + + + + + CodahaleMetrics + /metrics/* + + + + diff --git a/metricbeat/module/dropwizard/collector/_meta/data.json b/metricbeat/module/dropwizard/collector/_meta/data.json index 733cca69b3e..bee1cd731e3 100644 --- a/metricbeat/module/dropwizard/collector/_meta/data.json +++ b/metricbeat/module/dropwizard/collector/_meta/data.json @@ -6,17 +6,7 @@ "version": "6.0.0-alpha1" }, "dropwizard": { - "test": { - "counter1": { - "count": 1337 - }, - "gauge1": { - "value": 1337 - }, - "tags": { - "this": "that" - } - } + "test": {} }, "metricset": { "host": "localhost:8080", diff --git a/metricbeat/module/dropwizard/collector/collector_integration_test.go b/metricbeat/module/dropwizard/collector/collector_integration_test.go index b89ebe23433..d5825dcc8d3 100644 --- a/metricbeat/module/dropwizard/collector/collector_integration_test.go +++ b/metricbeat/module/dropwizard/collector/collector_integration_test.go @@ -15,16 +15,20 @@ func TestFetch(t *testing.T) { f := mbtest.NewEventsFetcher(t, getConfig()) events, err := f.Fetch() + hasTag := false + doesntHaveTag := false for _, event := range events { - ok, _ := event.HasKey("my_counter") + + ok, _ := event.HasKey("my_histogram") if ok { _, err := event.GetValue("tags") if err == nil { t.Fatal("write", "my_counter not supposed to have tags") } + doesntHaveTag = true } - ok, _ = event.HasKey("my_counter2") + ok, _ = event.HasKey("my_counter") if ok { tagsRaw, err := event.GetValue("tags") if err != nil { @@ -35,10 +39,13 @@ func TestFetch(t *testing.T) { t.Fatal("write", "unable to cast tags to common.MapStr") } else { assert.Equal(t, len(tags), 1) + hasTag = true } } } } + assert.Equal(t, hasTag, true) + assert.Equal(t, doesntHaveTag, true) if !assert.NoError(t, err) { t.FailNow() } @@ -67,17 +74,18 @@ func getEnvPort() string { port := os.Getenv("DROPWIZARD_PORT") if len(port) == 0 { - port = "9090" + port = "8080" } return port } func getConfig() map[string]interface{} { return map[string]interface{}{ - "module": "dropwizard", - "metricsets": []string{"collector"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, - "namespace": "testnamespace", - "enabled": true, + "module": "dropwizard", + "metricsets": []string{"collector"}, + "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "namespace": "testnamespace", + "metrics_path": "/test/metrics", + "enabled": true, } } diff --git a/metricbeat/tests/system/config/metricbeat.yml.j2 b/metricbeat/tests/system/config/metricbeat.yml.j2 index 33eb3e6a428..2665e2f07ee 100644 --- a/metricbeat/tests/system/config/metricbeat.yml.j2 +++ b/metricbeat/tests/system/config/metricbeat.yml.j2 @@ -37,6 +37,10 @@ metricbeat.modules: period: {{ m.period }} {% endif -%} + {% if m.path -%} + metrics_path: {{ m.path }} + {% endif -%} + {% if m.timeout -%} timeout: {{ m.timeout }} {% endif -%} diff --git a/metricbeat/tests/system/test_dropwizard.py b/metricbeat/tests/system/test_dropwizard.py index f4f2e1f7db8..4c202daea8a 100644 --- a/metricbeat/tests/system/test_dropwizard.py +++ b/metricbeat/tests/system/test_dropwizard.py @@ -15,11 +15,12 @@ def test_dropwizard(self): "name": "dropwizard", "metricsets": ["collector"], "hosts": self.get_hosts(), + "path": "/test/metrics", "period": "1s", "namespace": "test", }]) proc = self.start_beat() - self.wait_until(lambda: self.output_lines() > 0, max_timeout=20) + self.wait_until(lambda: self.output_lines() > 0, max_timeout=10) proc.check_kill_and_wait() output = self.read_output_json() @@ -27,4 +28,4 @@ def test_dropwizard(self): def get_hosts(self): return [os.getenv('DROPWIZARD_HOST', 'localhost') + ':' + - os.getenv('DROPWIZARD_PORT', '9090')] + os.getenv('DROPWIZARD_PORT', '8080')]