|
| 1 | +/* |
| 2 | + * Copyright 2023 Greptime Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.greptime.metrics; |
| 18 | + |
| 19 | +import com.codahale.metrics.MetricRegistry; |
| 20 | +import io.greptime.common.Lifecycle; |
| 21 | +import io.prometheus.client.CollectorRegistry; |
| 22 | +import io.prometheus.client.dropwizard.DropwizardExports; |
| 23 | +import io.prometheus.client.exporter.HTTPServer; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.InetSocketAddress; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +public class MetricsExporter implements Lifecycle<Void> { |
| 31 | + |
| 32 | + private static final Logger LOG = LoggerFactory.getLogger(MetricsExporter.class); |
| 33 | + |
| 34 | + private final CollectorRegistry prometheusMetricRegistry; |
| 35 | + |
| 36 | + private final int port; |
| 37 | + private HTTPServer server; |
| 38 | + |
| 39 | + private final AtomicBoolean started = new AtomicBoolean(false); |
| 40 | + |
| 41 | + public MetricsExporter(int port, MetricRegistry dropwizardMetricRegistry) { |
| 42 | + this.port = port; |
| 43 | + this.prometheusMetricRegistry = new CollectorRegistry(); |
| 44 | + this.prometheusMetricRegistry.register(new DropwizardExports(dropwizardMetricRegistry)); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean init(Void opts) { |
| 49 | + if (this.started.compareAndSet(false, true)) { |
| 50 | + try { |
| 51 | + this.server = new HTTPServer(new InetSocketAddress(this.port), this.prometheusMetricRegistry); |
| 52 | + LOG.info("Metrics exporter started at `http://localhost:{}/metrics`", this.port); |
| 53 | + return true; |
| 54 | + } catch (IOException e) { |
| 55 | + this.started.set(false); |
| 56 | + LOG.error("Failed to start metrics exporter", e); |
| 57 | + throw new RuntimeException("Failed to start metrics exporter", e); |
| 58 | + } |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void shutdownGracefully() { |
| 65 | + if (this.started.compareAndSet(true, false)) { |
| 66 | + if (this.server != null) { |
| 67 | + this.server.close(); |
| 68 | + LOG.info("Metrics exporter stopped"); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments