Skip to content
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

Demo plugin improvement #108

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</formats>
<files>
<file>
<source>../build-plugin-demo/target/demo-plugin-${project.parent.version}.jar</source>
<source>../build-plugin-demo/target/pluginDir/demo-plugin-${project.parent.version}.jar</source>
<outputDirectory>plugins</outputDirectory>
</file>
</files>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</formats>
<files>
<file>
<source>../build-plugin-demo/target/demo-plugin-${project.parent.version}.jar</source>
<source>../build-plugin-demo/target/pluginDir/demo-plugin-${project.parent.version}.jar</source>
<outputDirectory>plugins</outputDirectory>
</file>
</files>
Expand Down
3 changes: 3 additions & 0 deletions build/build-plugin-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/pluginDir</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;

import static com.baidu.demo.plugin.util.VariableUtil.getContext;
import static com.baidu.demo.plugin.util.VariableUtil.getPort;

@Slf4j
public class DemoPlugin extends BifroMQPlugin<DemoPluginContext> {
private static final String PLUGIN_PROMETHEUS_PORT = "plugin.prometheus.port";
private static final String PLUGIN_PROMETHEUS_CONTEXT = "plugin.prometheus.context";
private final PrometheusMeterRegistry registry;
private final HttpServer prometheusExportServer;
private final Thread serverThread;
private final int exportPort;

/**
* Constructor to be used by plugin manager for plugin instantiation. Your plugins have to provide constructor with
Expand All @@ -44,6 +46,7 @@ public class DemoPlugin extends BifroMQPlugin<DemoPluginContext> {
*/
public DemoPlugin(BifroMQPluginDescriptor context) {
super(context);
exportPort = getPort();
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
registry.config().meterFilter(new MeterFilter() {
@Override
Expand All @@ -68,8 +71,8 @@ public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticC
});
Metrics.addRegistry(registry);
try {
prometheusExportServer = HttpServer.create(new InetSocketAddress(port()), 0);
prometheusExportServer.createContext(contextPath(), httpExchange -> {
prometheusExportServer = HttpServer.create(new InetSocketAddress(exportPort), 0);
prometheusExportServer.createContext(getContext(), httpExchange -> {
String response = registry.scrape();
httpExchange.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = httpExchange.getResponseBody()) {
Expand All @@ -85,30 +88,13 @@ public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticC
@Override
protected void doStart() {
serverThread.start();
log.debug("Prometheus exporter started");
log.info("Prometheus exporter started on port {}", exportPort);
}

@Override
protected void doStop() {
prometheusExportServer.stop(0);
Metrics.removeRegistry(registry);
log.debug("Prometheus exporter stopped");
}

private int port() {
String prometheusPort = System.getProperty(PLUGIN_PROMETHEUS_PORT, "9090");
try {
return Integer.parseUnsignedInt(prometheusPort);
} catch (Throwable e) {
return 9090;
}
}

private String contextPath() {
String ctx = System.getProperty(PLUGIN_PROMETHEUS_CONTEXT, "/metrics");
if (ctx.startsWith("/")) {
return ctx;
}
return "/" + ctx;
log.info("Prometheus exporter stopped");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baidu.demo.plugin.util;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VariableUtil {
private static final String PLUGIN_PROMETHEUS_PORT = "PLUGIN_PROMETHEUS_PORT";
private static final String PLUGIN_PROMETHEUS_CONTEXT = "PLUGIN_PROMETHEUS_CONTEXT";

private static final int DEFAULT_PORT = 9090;
private static final String DEFAULT_CONTEXT = "/metrics";

public static int getPort() {
String prometheusPort = System.getenv(PLUGIN_PROMETHEUS_PORT);
if (prometheusPort == null) {
prometheusPort = System.getProperty(PLUGIN_PROMETHEUS_PORT, "9090");
}
try {
return Integer.parseUnsignedInt(prometheusPort);
} catch (Throwable e) {
log.error("Parse prometheus port: {} error, use default port", prometheusPort, e);
return DEFAULT_PORT;
popduke marked this conversation as resolved.
Show resolved Hide resolved
}
}

public static String getContext() {
String ctx = System.getenv(PLUGIN_PROMETHEUS_CONTEXT);
if (ctx == null) {
ctx = System.getProperty(PLUGIN_PROMETHEUS_CONTEXT, DEFAULT_CONTEXT);
}
return ctx.startsWith("/") ? ctx : "/" + ctx;
}
}
Loading