|
| 1 | +// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.perf; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.Gauge; |
| 19 | +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; |
| 20 | +import org.apache.commons.cli.CommandLine; |
| 21 | +import org.apache.commons.cli.CommandLineParser; |
| 22 | +import org.apache.commons.cli.GnuParser; |
| 23 | +import org.apache.commons.cli.Options; |
| 24 | +import org.eclipse.jetty.server.Connector; |
| 25 | +import org.eclipse.jetty.server.Request; |
| 26 | +import org.eclipse.jetty.server.Server; |
| 27 | +import org.eclipse.jetty.server.ServerConnector; |
| 28 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 29 | +import org.eclipse.jetty.server.handler.ContextHandler; |
| 30 | +import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import javax.servlet.http.HttpServletRequest; |
| 36 | +import javax.servlet.http.HttpServletResponse; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.concurrent.CountDownLatch; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | +import java.util.concurrent.atomic.AtomicInteger; |
| 41 | +import java.util.concurrent.atomic.AtomicReference; |
| 42 | +import java.util.stream.Collectors; |
| 43 | + |
| 44 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 46 | + |
| 47 | +/** |
| 48 | + * |
| 49 | + */ |
| 50 | +public class DatadogMetricsTest { |
| 51 | + |
| 52 | + static final int NB_REQUESTS = 5; |
| 53 | + final AtomicReference<String> apiKey = new AtomicReference<>(); |
| 54 | + final AtomicReference<String> appKey = new AtomicReference<>(); |
| 55 | + final AtomicReference<String> content = new AtomicReference<>(); |
| 56 | + final AtomicReference<String> description = new AtomicReference<>(); |
| 57 | + final CountDownLatch latch = new CountDownLatch(NB_REQUESTS); |
| 58 | + int port; |
| 59 | + Server server; |
| 60 | + DatadogMetrics metrics; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + public void init() throws Exception { |
| 64 | + port = TestUtils.randomNetworkPort(); |
| 65 | + server = startMockDatadogService(); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + public void tearDown() throws Exception { |
| 70 | + if (metrics != null) { |
| 71 | + metrics.close(); |
| 72 | + } |
| 73 | + if (server != null) { |
| 74 | + server.stop(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void datadog() throws Exception { |
| 80 | + DatadogMetrics metrics = new DatadogMetrics(); |
| 81 | + Options options = metrics.options(); |
| 82 | + |
| 83 | + CommandLineParser parser = new GnuParser(); |
| 84 | + CommandLine rawCmd = parser.parse( |
| 85 | + options, |
| 86 | + ("--metrics-datadog-uri http://localhost:" + port + "/datadog " |
| 87 | + + "--metrics-datadog-api-key APIKEY --metrics-datadog-application-key APPKEY " |
| 88 | + + "--metrics-datadog-step-size 1 --metrics-datadog-host-tag host --metrics-datadog-descriptions") |
| 89 | + .split(" ") |
| 90 | + ); |
| 91 | + CommandLineProxy cmd = new CommandLineProxy(options, rawCmd, name -> null); |
| 92 | + CompositeMeterRegistry registry = new CompositeMeterRegistry(); |
| 93 | + registry.config().commonTags("host", "test"); |
| 94 | + AtomicInteger gauge = new AtomicInteger(42); |
| 95 | + Gauge.builder("dummy", gauge, g -> g.doubleValue()).description("this is a dummy meter").register(registry); |
| 96 | + metrics.configure(cmd, registry, null); |
| 97 | + |
| 98 | + assertTrue(latch.await(10, TimeUnit.SECONDS), NB_REQUESTS + " metrics requests should have been sent by now"); |
| 99 | + |
| 100 | + assertEquals("APIKEY", apiKey.get()); |
| 101 | + assertEquals("APPKEY", appKey.get()); |
| 102 | + assertTrue(description.get().contains("\"description\":\"this is a dummy meter\"")); |
| 103 | + assertTrue(content.get().contains("\"metric\":\"dummy\"")); |
| 104 | + assertTrue(content.get().contains("42.0")); |
| 105 | + assertTrue(content.get().contains("\"host\":\"test\"")); |
| 106 | + |
| 107 | + metrics.close(); |
| 108 | + } |
| 109 | + |
| 110 | + private Server startMockDatadogService() throws Exception { |
| 111 | + QueuedThreadPool threadPool = new QueuedThreadPool(); |
| 112 | + // difference between those 2 should be high enough to avoid a warning |
| 113 | + threadPool.setMinThreads(2); |
| 114 | + threadPool.setMaxThreads(12); |
| 115 | + server = new Server(threadPool); |
| 116 | + ServerConnector connector = new ServerConnector(server); |
| 117 | + connector.setPort(port); |
| 118 | + server.setConnectors(new Connector[] { connector }); |
| 119 | + |
| 120 | + ContextHandler context = new ContextHandler(); |
| 121 | + context.setContextPath("/datadog"); |
| 122 | + context.setHandler(new AbstractHandler() { |
| 123 | + |
| 124 | + @Override |
| 125 | + public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) |
| 126 | + throws IOException { |
| 127 | + if (request.getParameter("api_key") != null) { |
| 128 | + apiKey.set(request.getParameter("api_key")); |
| 129 | + } |
| 130 | + if (request.getParameter("application_key") != null) { |
| 131 | + appKey.set(request.getParameter("application_key")); |
| 132 | + } |
| 133 | + |
| 134 | + String body = request.getReader().lines().collect(Collectors.joining("\n")); |
| 135 | + DatadogMetricsTest.this.content.set(body); |
| 136 | + |
| 137 | + if (body.contains("\"description\"")) { |
| 138 | + description.set(body); |
| 139 | + } |
| 140 | + |
| 141 | + request.setHandled(true); |
| 142 | + latch.countDown(); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + server.setHandler(context); |
| 147 | + |
| 148 | + server.setStopTimeout(1000); |
| 149 | + server.start(); |
| 150 | + return server; |
| 151 | + } |
| 152 | +} |
0 commit comments