-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds prometheus metrics endpoint (#24)
* Adds prometheus metrics endpoint * Adds some metrics using dropwizard tools * Make metricsregistry to not be singleton * Private final jettyServer
- Loading branch information
1 parent
537b55e
commit 3a4d23e
Showing
11 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
target/** | ||
rpm/target/** | ||
.idea/** | ||
dependency-reduced-pom.xml | ||
var/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Kubernetes log forwarder k8s_01 | ||
Copyright (C) 2023 Suomen Kanuuna Oy | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.teragrep.k8s_01; | ||
|
||
import com.codahale.metrics.*; | ||
import com.codahale.metrics.jmx.JmxReporter; | ||
import com.codahale.metrics.jvm.*; | ||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.dropwizard.DropwizardExports; | ||
import io.prometheus.client.exporter.MetricsServlet; | ||
import io.prometheus.client.hotspot.DefaultExports; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.codahale.metrics.MetricRegistry.name; | ||
|
||
public class PrometheusMetrics { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusMetrics.class); | ||
private final Server jettyServer; | ||
public PrometheusMetrics(int port) { | ||
LOGGER.info("Starting prometheus metrics server on port {}", port); | ||
// prometheus-exporter | ||
jettyServer = new Server(port); | ||
ServletContextHandler context = new ServletContextHandler(); | ||
context.setContextPath("/"); | ||
jettyServer.setHandler(context); | ||
|
||
MetricsServlet metricsServlet = new MetricsServlet(); | ||
ServletHolder servletHolder = new ServletHolder(metricsServlet); | ||
context.addServlet(servletHolder, "/metrics"); | ||
setupDropWizard(); | ||
// Add metrics about CPU, JVM memory etc. | ||
DefaultExports.initialize(); | ||
// Start the webserver. | ||
try { | ||
jettyServer.start(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
static void setupDropWizard() { | ||
MetricRegistry metricRegistry = new MetricRegistry(); | ||
|
||
// Totals | ||
metricRegistry.register(name("total", "reconnects"), new Counter()); | ||
metricRegistry.register(name("total", "connections"), new Counter()); | ||
|
||
// Throughput meters | ||
metricRegistry.register(name("throughput", "bytes"), new Meter(new SlidingTimeWindowMovingAverages())); | ||
metricRegistry.register(name("throughput", "records"), new Meter(new SlidingTimeWindowMovingAverages())); | ||
metricRegistry.register(name("throughput", "errors"), new Meter(new SlidingTimeWindowMovingAverages())); | ||
|
||
// Misc | ||
metricRegistry.register(name("jvm", "vm"), new JvmAttributeGaugeSet()); | ||
metricRegistry.register(name("jvm", "memory"), new MemoryUsageGaugeSet()); | ||
metricRegistry.register(name("jvm", "threads"), new ThreadStatesGaugeSet()); | ||
metricRegistry.register(name("jvm", "gc"), new GarbageCollectorMetricSet()); | ||
SharedMetricRegistries.add("default", metricRegistry); | ||
|
||
// Add to Prometheus metrics | ||
CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry)); | ||
|
||
// Enable JMX listener | ||
JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build(); | ||
jmxReporter.start(); | ||
} | ||
|
||
public void close() { | ||
LOGGER.info("Closing prometheus metrics server"); | ||
try { | ||
jettyServer.stop(); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to stop jettyServer:", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.