|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.metrics; |
| 6 | + |
| 7 | +import com.timgroup.statsd.NonBlockingStatsDClientBuilder; |
| 8 | +import com.timgroup.statsd.StatsDClient; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | + |
| 11 | +/** |
| 12 | + * Light wrapper around the DogsStatsD client to make using the client slightly more ergonomic. |
| 13 | + * <p> |
| 14 | + * This class mainly exists to help Airbyte instrument/debug application on Airbyte Cloud. |
| 15 | + * <p> |
| 16 | + * Open source users are free to turn this on and consume the same metrics. |
| 17 | + */ |
| 18 | +@Slf4j |
| 19 | +public class DogstatsdMetricSingleton { |
| 20 | + |
| 21 | + private static DogstatsdMetricSingleton instance; |
| 22 | + private final StatsDClient statsDClient; |
| 23 | + private final boolean instancePublish; |
| 24 | + |
| 25 | + public DogstatsdMetricSingleton(final String appName, final boolean publish) { |
| 26 | + instancePublish = publish; |
| 27 | + statsDClient = new NonBlockingStatsDClientBuilder() |
| 28 | + .prefix(appName) |
| 29 | + .hostname(System.getenv("DD_AGENT_HOST")) |
| 30 | + .port(Integer.parseInt(System.getenv("DD_DOGSTATSD_PORT"))) |
| 31 | + .build(); |
| 32 | + } |
| 33 | + |
| 34 | + public static synchronized DogstatsdMetricSingleton getInstance() { |
| 35 | + if (instance == null) { |
| 36 | + throw new RuntimeException("You must initialize configuration with the initialize() method before getting an instance."); |
| 37 | + } |
| 38 | + return instance; |
| 39 | + } |
| 40 | + |
| 41 | + public synchronized static void initialize(final String appName, final boolean publish) { |
| 42 | + if (instance != null) { |
| 43 | + throw new RuntimeException("You cannot initialize configuration more than once."); |
| 44 | + } |
| 45 | + if (publish) { |
| 46 | + log.info("Starting DogStatsD client.."); |
| 47 | + // The second constructor argument ('true') makes this server start as a separate daemon thread. |
| 48 | + // http://prometheus.github.io/client_java/io/prometheus/client/exporter/HTTPServer.html#HTTPServer-int-boolean- |
| 49 | + instance = new DogstatsdMetricSingleton(appName, publish); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Increment or decrement a counter. |
| 55 | + * |
| 56 | + * @param name of counter. |
| 57 | + * @param amt to adjust. |
| 58 | + * @param tags |
| 59 | + */ |
| 60 | + public void count(final String name, final double amt, final String... tags) { |
| 61 | + if (instancePublish) { |
| 62 | + log.info("publishing count, name: {}, value: {}", name, amt); |
| 63 | + statsDClient.count(name, amt, tags); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Record the latest value for a gauge. |
| 69 | + * |
| 70 | + * @param name of gauge. |
| 71 | + * @param val to record. |
| 72 | + * @param tags |
| 73 | + */ |
| 74 | + public void gauge(final String name, final double val, final String... tags) { |
| 75 | + if (instancePublish) { |
| 76 | + log.info("publishing gauge, name: {}, value: {}", name, val); |
| 77 | + statsDClient.gauge(name, val, tags); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Submit a single execution time aggregated locally by the Agent. Use this if approximate stats are |
| 83 | + * sufficient. |
| 84 | + * |
| 85 | + * @param name of histogram. |
| 86 | + * @param val of time to record. |
| 87 | + * @param tags |
| 88 | + */ |
| 89 | + public void recordTimeLocal(final String name, final double val, final String... tags) { |
| 90 | + if (instancePublish) { |
| 91 | + log.info("recording histogram, name: {}, value: {}", name, val); |
| 92 | + statsDClient.histogram(name, val, tags); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Submit a single execution time aggregated globally by Datadog. Use this for precise stats. |
| 98 | + * |
| 99 | + * @param name of distribution. |
| 100 | + * @param val of time to record. |
| 101 | + * @param tags |
| 102 | + */ |
| 103 | + public void recordTimeGlobal(final String name, final double val, final String... tags) { |
| 104 | + if (instancePublish) { |
| 105 | + log.info("recording distribution, name: {}, value: {}", name, val); |
| 106 | + statsDClient.distribution(name, val, tags); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Wrapper of {@link #recordTimeGlobal(String, double, String...)} with a runnable for convenience. |
| 112 | + * |
| 113 | + * @param name |
| 114 | + * @param runnable |
| 115 | + * @param tags |
| 116 | + */ |
| 117 | + public void recordTimeGlobal(final String name, final Runnable runnable, final String... tags) { |
| 118 | + final long start = System.currentTimeMillis(); |
| 119 | + runnable.run(); |
| 120 | + final long end = System.currentTimeMillis(); |
| 121 | + final long val = end - start; |
| 122 | + recordTimeGlobal(name, val, tags); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments