|
| 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 com.rabbitmq.client.ConnectionFactory; |
| 19 | +import io.micrometer.core.instrument.Clock; |
| 20 | +import io.micrometer.core.instrument.MeterRegistry; |
| 21 | +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; |
| 22 | +import io.micrometer.datadog.DatadogConfig; |
| 23 | +import io.micrometer.datadog.DatadogMeterRegistry; |
| 24 | +import org.apache.commons.cli.Option; |
| 25 | +import org.apache.commons.cli.Options; |
| 26 | + |
| 27 | +import java.time.Duration; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static com.rabbitmq.perf.PerfTest.hasOption; |
| 32 | +import static com.rabbitmq.perf.PerfTest.strArg; |
| 33 | +import static java.lang.Boolean.valueOf; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + */ |
| 38 | +public class DatadogMetrics implements Metrics { |
| 39 | + |
| 40 | + private volatile MeterRegistry registry; |
| 41 | + |
| 42 | + public Options options() { |
| 43 | + Options options = new Options(); |
| 44 | + options.addOption(new Option("mda", "metrics-datadog", false, "enable Datadog metrics")); |
| 45 | + options.addOption(new Option("mdk", "metrics-datadog-api-key", true, "Datadog API key")); |
| 46 | + options.addOption(new Option("mds", "metrics-datadog-step-size", true, "step size (reporting frequency) to use " |
| 47 | + + "in seconds, default is 10 seconds")); |
| 48 | + options.addOption(new Option("mdak", "metrics-datadog-application-key", true, "Datadog application key")); |
| 49 | + options.addOption(new Option("mdh", "metrics-datadog-host-tag", true, "tag that will be mapped to \"host\" when shipping metrics to datadog")); |
| 50 | + options.addOption(new Option("mdd", "metrics-datadog-descriptions", false, "if meter descriptions should be sent to Datadog")); |
| 51 | + options.addOption(new Option("mdu", "metrics-datadog-uri", true, "URI to ship metrics, useful when using " |
| 52 | + + "a proxy, default is https://app.datadoghq.com")); |
| 53 | + return options; |
| 54 | + } |
| 55 | + |
| 56 | + public void configure(CommandLineProxy cmd, CompositeMeterRegistry meterRegistry, ConnectionFactory factory) throws Exception { |
| 57 | + if (isEnabled(cmd)) { |
| 58 | + Map<String, String> dataCfg = new HashMap<>(); |
| 59 | + dataCfg.put("datadog.apiKey", strArg(cmd, "mdk", null)); |
| 60 | + dataCfg.put("datadog.step", strArg(cmd, "mds", "10")); |
| 61 | + dataCfg.put("datadog.applicationKey", strArg(cmd, "mdak", null)); |
| 62 | + dataCfg.put("datadog.hostTag", strArg(cmd, "mdh", null)); |
| 63 | + dataCfg.put("datadog.descriptions", valueOf(hasOption(cmd, "mdd")).toString()); |
| 64 | + dataCfg.put("datadog.uri", strArg(cmd, "mdu", null)); |
| 65 | + |
| 66 | + DatadogConfig config = new DatadogConfig() { |
| 67 | + |
| 68 | + @Override |
| 69 | + public Duration step() { |
| 70 | + return Duration.ofSeconds(Integer.valueOf(dataCfg.get("datadog.step"))); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String get(String k) { |
| 75 | + return dataCfg.get(k); |
| 76 | + } |
| 77 | + }; |
| 78 | + registry = new DatadogMeterRegistry( |
| 79 | + config, |
| 80 | + Clock.SYSTEM, |
| 81 | + new NamedThreadFactory("perf-test-metrics-datadog-") |
| 82 | + ); |
| 83 | + meterRegistry.add(registry); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void close() { |
| 88 | + if (registry != null) { |
| 89 | + registry.close(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String toString() { |
| 95 | + return "Datadog Metrics"; |
| 96 | + } |
| 97 | +} |
0 commit comments