Skip to content

Feature: Added support to handle multiple java application jmx scrapi… #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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,47 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.jmxmetrics;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class ConfigParser {

public static Map<String, Properties> parseConfig(String filePath) throws IOException {
Map<String, Properties> configMap = new HashMap<>();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
String currentSection = null;
Properties currentProps = null;

while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue; // Skip empty lines and comments
}

if (line.endsWith(":")) {
// Start of a new section
currentSection = line.substring(0, line.length() - 1).trim();
currentProps = new Properties();
configMap.put(currentSection, currentProps);
} else if (currentSection != null) {
// Parse key-value pairs within the current section
String[] parts = line.split("=", 2);
if (parts.length == 2) {
String key = parts[0].trim();
String value = parts[1].trim();
currentProps.setProperty(key, value);
}
}
}

reader.close();
return configMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package io.opentelemetry.contrib.jmxmetrics;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

Expand Down Expand Up @@ -80,6 +82,8 @@ class JmxConfig {

final boolean aggregateAcrossMBeans;

private final Map<String, Properties> multiConfig;

JmxConfig(final Properties props) {
properties = new Properties();
// putAll() instead of using constructor defaults
Expand Down Expand Up @@ -132,10 +136,50 @@ class JmxConfig {
System.setProperty(key, value);
}
});
this.multiConfig = new HashMap<>();
}

public JmxConfig(final Map<String, Properties> config) {
this.multiConfig = new HashMap<>(config);
this.properties = new Properties();
this.serviceUrl = null;
this.groovyScript = null;
this.targetSystem = null;
this.targetSystems = null;
this.intervalMilliseconds = 0;
this.metricsExporterType = null;
this.otlpExporterEndpoint = null;
this.prometheusExporterHost = null;
this.prometheusExporterPort = 0;
this.username = null;
this.password = null;
this.realm = null;
this.remoteProfile = null;
this.registrySsl = false;
this.aggregateAcrossMBeans = false;

// this(new Properties());
}

public JmxConfig getConfig(String key) {
Properties props = multiConfig.get(key);
if (props == null) {
throw new ConfigurationException("No configuration found for " + key);
}
return new JmxConfig(props);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (String key : properties.stringPropertyNames()) {
sb.append(key).append(" = ").append(properties.getProperty(key)).append("\n");
}
return sb.toString();
}

JmxConfig() {
this(new Properties());
public Set<String> getConfigKeys() {
return multiConfig.keySet();
}

private int getProperty(final String key, final int dfault) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -37,6 +38,11 @@ class JmxMetrics {
runner = new GroovyRunner(config, jmxClient, new GroovyMetricEnvironment(config));
}

@Override
public String toString() {
return "JmxMetrics{" + "config=" + config + ", runner=" + runner + ", exec=" + exec + '}';
}

private void start() {
exec.scheduleWithFixedDelay(
new Runnable() {
Expand Down Expand Up @@ -106,19 +112,52 @@ public static void loadPropertiesFromPath(Properties props, String path) {
* @param args - must be of the form "-config {jmx_config_path,'-'}"
*/
public static void main(final String[] args) {
JmxConfig config = getConfigFromArgs(args);
config.validate();

final JmxMetrics jmxMetrics = new JmxMetrics(config);
jmxMetrics.start();

Runtime.getRuntime()
.addShutdownHook(
new Thread() {
@Override
public void run() {
jmxMetrics.shutdown();
}
});
// JmxConfig config = getConfigFromArgs(args);
// config.validate();
//
// final JmxMetrics jmxMetrics = new JmxMetrics(config);
// jmxMetrics.start();
//
// Runtime.getRuntime()
// .addShutdownHook(
// new Thread() {
// @Override
// public void run() {
// jmxMetrics.shutdown();
// }
// });

try {
Map<String, Properties> configMap = ConfigParser.parseConfig("/tmp/config.properties");

JmxConfig multiConfig = new JmxConfig(configMap);

for (String key : multiConfig.getConfigKeys()) {

JmxConfig config = multiConfig.getConfig(key);
System.out.println("Config: " + config);
System.out.println("Key: " + key);

final JmxMetrics jmxMetrics = new JmxMetrics(config);

System.out.println("JmxMetrics: " + jmxMetrics);

config.validate();
jmxMetrics.start();

Runtime.getRuntime()
.addShutdownHook(
new Thread() {
@Override
public void run() {
jmxMetrics.shutdown();
}
});
}

} catch (IOException e) {
System.out.println("Failed to read config properties from stdin: " + e.getMessage());
System.exit(1);
}
}
}

This file was deleted.

Loading