-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds prometheus metrics endpoint #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
target/** | ||
rpm/target/** | ||
.idea/** | ||
dependency-reduced-pom.xml | ||
var/ |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||||||
FROM rockylinux:8 | ||||||||||
COPY rpm/target/rpm/com.teragrep-k8s_01/RPMS/noarch/com.teragrep-k8s_01-*.rpm /rpm/ | ||||||||||
RUN dnf -y install jq java-1.8.0-headless /rpm/*.rpm && yum clean all | ||||||||||
RUN dnf -y install jq /rpm/*.rpm && yum clean all | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DL3041: Specify version with ℹ️ Expand to see all @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. |
||||||||||
VOLUME /opt/teragrep/k8s_01/var | ||||||||||
VOLUME /opt/teragrep/k8s_01/etc | ||||||||||
WORKDIR /opt/teragrep/k8s_01 | ||||||||||
|
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DL3040:
dnf clean all
missing after dnf command.ℹ️ Expand to see all @sonatype-lift commands
You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
@sonatype-lift ignore
@sonatype-lift ignoreall
@sonatype-lift exclude <file|issue|path|tool>
file|issue|path|tool
from Lift findings by updating your config.toml fileNote: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.