diff --git a/instrumentation/runtime-metrics/library/README.md b/instrumentation/jmx/javaagent/README.md similarity index 100% rename from instrumentation/runtime-metrics/library/README.md rename to instrumentation/jmx/javaagent/README.md diff --git a/instrumentation/runtime-metrics/library/activemq.md b/instrumentation/jmx/javaagent/activemq.md similarity index 100% rename from instrumentation/runtime-metrics/library/activemq.md rename to instrumentation/jmx/javaagent/activemq.md diff --git a/instrumentation/jmx/javaagent/build.gradle.kts b/instrumentation/jmx/javaagent/build.gradle.kts new file mode 100644 index 000000000000..0804b9a57cfe --- /dev/null +++ b/instrumentation/jmx/javaagent/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("otel.javaagent-instrumentation") +} + +dependencies { + implementation(project(":instrumentation:jmx:jmx-engine:library")) + implementation(project(":instrumentation:jmx:jmx-yaml:library")) + + compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") +} diff --git a/instrumentation/runtime-metrics/library/hadoop.md b/instrumentation/jmx/javaagent/hadoop.md similarity index 100% rename from instrumentation/runtime-metrics/library/hadoop.md rename to instrumentation/jmx/javaagent/hadoop.md diff --git a/instrumentation/runtime-metrics/library/jetty.md b/instrumentation/jmx/javaagent/jetty.md similarity index 91% rename from instrumentation/runtime-metrics/library/jetty.md rename to instrumentation/jmx/javaagent/jetty.md index 646edaea29aa..e04622a17918 100644 --- a/instrumentation/runtime-metrics/library/jetty.md +++ b/instrumentation/jmx/javaagent/jetty.md @@ -5,8 +5,8 @@ Here is the list of metrics based on MBeans exposed by Jetty. | Metric Name | Type | Attributes | Description | |--------------------------------|---------------|--------------|------------------------------------------------------| | jetty.session.sessionsCreated | Counter | resource | The number of sessions established in total | -| jetty.session.sessionTimeTotal | UpDownCounter | resource | The total time sessions have been active | -| jetty.session.sessionTimeMax | UpDownCounter | resource | The maximum amount of time a session has been active | +| jetty.session.sessionTimeTotal | Counter | resource | The total time sessions have been active | +| jetty.session.sessionTimeMax | Gauge | resource | The maximum amount of time a session has been active | | jetty.session.sessionTimeMean | Gauge | resource | The mean time sessions remain active | | jetty.threads.busyThreads | UpDownCounter | | The current number of busy threads | | jetty.threads.idleThreads | UpDownCounter | | The current number of idle threads | diff --git a/instrumentation/runtime-metrics/library/kafka-broker.md b/instrumentation/jmx/javaagent/kafka-broker.md similarity index 98% rename from instrumentation/runtime-metrics/library/kafka-broker.md rename to instrumentation/jmx/javaagent/kafka-broker.md index beac186a4f1e..2dddfbb19d82 100644 --- a/instrumentation/runtime-metrics/library/kafka-broker.md +++ b/instrumentation/jmx/javaagent/kafka-broker.md @@ -18,7 +18,7 @@ Broker metrics: | kafka.partition.offline | UpDownCounter | | The number of partitions offline | | kafka.partition.underReplicated | UpDownCounter | | The number of under replicated partitions | | kafka.isr.operation.count | UpDownCounter | operation | The number of in-sync replica shrink and expand operations | -| kafka.lag.max | Counter | | The max lag in messages between follower and leader replicas | +| kafka.lag.max | Gauge | | The max lag in messages between follower and leader replicas | | kafka.controller.active.count | UpDownCounter | | The number of controllers active on the broker | | kafka.leaderElection.count | Counter | | The leader election count | | kafka.leaderElection.unclean.count | Counter | | Unclean leader election count - increasing indicates broker failures | diff --git a/instrumentation/runtime-metrics/library/kafka-consumer.md b/instrumentation/jmx/javaagent/kafka-consumer.md similarity index 100% rename from instrumentation/runtime-metrics/library/kafka-consumer.md rename to instrumentation/jmx/javaagent/kafka-consumer.md diff --git a/instrumentation/runtime-metrics/library/kafka-producer.md b/instrumentation/jmx/javaagent/kafka-producer.md similarity index 100% rename from instrumentation/runtime-metrics/library/kafka-producer.md rename to instrumentation/jmx/javaagent/kafka-producer.md diff --git a/instrumentation/jmx/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java b/instrumentation/jmx/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java new file mode 100644 index 000000000000..6b365c6bffb4 --- /dev/null +++ b/instrumentation/jmx/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java @@ -0,0 +1,89 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.javaagent.jmx; + +import static java.util.logging.Level.CONFIG; +import static java.util.logging.Level.FINE; + +import com.google.auto.service.AutoService; +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.instrumentation.jmx.engine.JmxMetricInsight; +import io.opentelemetry.instrumentation.jmx.engine.MetricConfiguration; +import io.opentelemetry.instrumentation.jmx.yaml.RuleParser; +import io.opentelemetry.javaagent.extension.AgentListener; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import java.io.File; +import java.io.InputStream; +import java.nio.file.Files; + +/** An {@link AgentListener} that enables JMX metrics during agent startup. */ +@AutoService(AgentListener.class) +public class JmxMetricInsightInstaller implements AgentListener { + + @Override + public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredSdk) { + ConfigProperties config = autoConfiguredSdk.getConfig(); + + if (config.getBoolean("otel.jmx.enabled", true)) { + JmxMetricInsight service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), config); + MetricConfiguration conf = buildMetricConfiguration(); + service.start(conf); + } + } + + private static String resourceFor(String platform) { + return "/jmx/rules/" + platform + ".yaml"; + } + + private static void addRulesForPlatform(String platform, MetricConfiguration conf) { + String yamlResource = resourceFor(platform); + try (InputStream inputStream = + JmxMetricInsightInstaller.class.getResourceAsStream(yamlResource)) { + if (inputStream != null) { + JmxMetricInsight.getLogger().log(FINE, "Opened input stream {0}", yamlResource); + RuleParser parserInstance = RuleParser.get(); + parserInstance.addMetricDefs(conf, inputStream); + } else { + JmxMetricInsight.getLogger().log(CONFIG, "No support found for {0}", platform); + } + } catch (Exception e) { + JmxMetricInsight.getLogger().warning(e.getMessage()); + } + } + + private static void buildFromDefaultRules(MetricConfiguration conf) { + String targetSystem = System.getProperty("otel.jmx.target.system", "").trim(); + String[] platforms = targetSystem.length() == 0 ? new String[0] : targetSystem.split(","); + + for (String platform : platforms) { + addRulesForPlatform(platform, conf); + } + } + + private static void buildFromUserRules(MetricConfiguration conf) { + String jmxDir = System.getProperty("otel.jmx.config"); + if (jmxDir != null) { + JmxMetricInsight.getLogger().log(CONFIG, "JMX config file name: {0}", jmxDir); + RuleParser parserInstance = RuleParser.get(); + try (InputStream inputStream = Files.newInputStream(new File(jmxDir.trim()).toPath())) { + parserInstance.addMetricDefs(conf, inputStream); + } catch (Exception e) { + JmxMetricInsight.getLogger().warning(e.getMessage()); + } + } + } + + private static MetricConfiguration buildMetricConfiguration() { + MetricConfiguration conf = new MetricConfiguration(); + + buildFromDefaultRules(conf); + + buildFromUserRules(conf); + + return conf; + } +} diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/activemq.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/activemq.yaml similarity index 100% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/activemq.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/activemq.yaml diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/hadoop.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/hadoop.yaml similarity index 100% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/hadoop.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/hadoop.yaml diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/jetty.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/jetty.yaml similarity index 93% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/jetty.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/jetty.yaml index d387a2d16a8e..610c37ea3f48 100644 --- a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/jetty.yaml +++ b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/jetty.yaml @@ -13,8 +13,10 @@ rules: type: counter desc: The number of sessions established in total sessionTimeTotal: + type: counter desc: The total time sessions have been active sessionTimeMax: + type: gauge desc: The maximum amount of time a session has been active sessionTimeMean: type: gauge @@ -51,4 +53,4 @@ rules: LoggerCount: type: updowncounter unit: 1 - desc: The number of registered loggers by name \ No newline at end of file + desc: The number of registered loggers by name diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-broker.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-broker.yaml similarity index 99% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-broker.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-broker.yaml index 584c90d2db29..f85c58a999e1 100644 --- a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-broker.yaml +++ b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-broker.yaml @@ -159,7 +159,6 @@ rules: mapping: Value: metric: kafka.lag.max - type: counter desc: The max lag in messages between follower and leader replicas unit: '{messages}' diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-consumer.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-consumer.yaml similarity index 100% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-consumer.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-consumer.yaml diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-producer.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-producer.yaml similarity index 100% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/kafka-producer.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/kafka-producer.yaml diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/tomcat.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/tomcat.yaml similarity index 100% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/tomcat.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/tomcat.yaml diff --git a/instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/wildfly.yaml b/instrumentation/jmx/javaagent/src/main/resources/jmx/rules/wildfly.yaml similarity index 100% rename from instrumentation/runtime-metrics/library/src/main/resources/jmx/rules/wildfly.yaml rename to instrumentation/jmx/javaagent/src/main/resources/jmx/rules/wildfly.yaml diff --git a/instrumentation/runtime-metrics/library/tomcat.md b/instrumentation/jmx/javaagent/tomcat.md similarity index 100% rename from instrumentation/runtime-metrics/library/tomcat.md rename to instrumentation/jmx/javaagent/tomcat.md diff --git a/instrumentation/runtime-metrics/library/wildfly.md b/instrumentation/jmx/javaagent/wildfly.md similarity index 100% rename from instrumentation/runtime-metrics/library/wildfly.md rename to instrumentation/jmx/javaagent/wildfly.md diff --git a/instrumentation/jmx/jmx-engine/library/build.gradle.kts b/instrumentation/jmx/jmx-engine/library/build.gradle.kts new file mode 100644 index 000000000000..399a2611f59b --- /dev/null +++ b/instrumentation/jmx/jmx-engine/library/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("otel.library-instrumentation") +} + +dependencies { + + compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") + + testImplementation(project(":testing-common")) +} diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeInfo.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/AttributeInfo.java similarity index 95% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeInfo.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/AttributeInfo.java index 39990a75ee56..01b68bb13869 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeInfo.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/AttributeInfo.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; /** * A class holding relevant information about an MBean attribute which will be used for collecting diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeValueExtractor.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/AttributeValueExtractor.java similarity index 93% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeValueExtractor.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/AttributeValueExtractor.java index d526d8d634e7..24a6ad35176b 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeValueExtractor.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/AttributeValueExtractor.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import static java.util.logging.Level.FINE; import static java.util.logging.Level.INFO; @@ -23,6 +23,9 @@ * immutable. */ public class AttributeValueExtractor implements LabelExtractor { + + private static final Logger logger = Logger.getLogger(AttributeValueExtractor.class.getName()); + // The attribute name to be used during value extraction from MBean private final String baseName; @@ -95,9 +98,8 @@ String getAttributeName() { * @return AttributeInfo if the attribute is properly recognized, or null */ AttributeInfo getAttributeInfo(MBeanServer server, ObjectName objectName) { - if (MetricService.logger.isLoggable(FINE)) { - MetricService.logger.log( - FINE, "Resolving {0} for {1}", new Object[] {getAttributeName(), objectName}); + if (logger.isLoggable(FINE)) { + logger.log(FINE, "Resolving {0} for {1}", new Object[] {getAttributeName(), objectName}); } try { @@ -119,8 +121,8 @@ AttributeInfo getAttributeInfo(MBeanServer server, ObjectName objectName) { // It is fairly normal to get null values, especially during startup, // but it is much more suspicious to get non-numbers Level logLevel = sampleValue == null ? FINE : INFO; - if (MetricService.logger.isLoggable(logLevel)) { - MetricService.logger.log( + if (logger.isLoggable(logLevel)) { + logger.log( logLevel, "Unusable value {0} for attribute {1} and ObjectName {2}", new Object[] { @@ -134,8 +136,8 @@ AttributeInfo getAttributeInfo(MBeanServer server, ObjectName objectName) { } } - if (MetricService.logger.isLoggable(FINE)) { - MetricService.logger.log( + if (logger.isLoggable(FINE)) { + logger.log( FINE, "Cannot find attribute {0} for ObjectName {1}", new Object[] {baseName, objectName}); @@ -143,9 +145,9 @@ AttributeInfo getAttributeInfo(MBeanServer server, ObjectName objectName) { } catch (InstanceNotFoundException e) { // Should not happen. The ObjectName we use has been provided by the MBeanServer we use. - MetricService.logger.log(INFO, "The MBeanServer does not find {0}", objectName); + logger.log(INFO, "The MBeanServer does not find {0}", objectName); } catch (Exception e) { - MetricService.logger.log( + logger.log( FINE, "Exception {0} while inspecting attributes for ObjectName {1}", new Object[] {e, objectName}); diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/BeanFinder.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java similarity index 98% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/BeanFinder.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java index d0f769a0efe0..19e28429e860 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/BeanFinder.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import java.util.ArrayList; diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/BeanPack.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanPack.java similarity index 91% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/BeanPack.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanPack.java index 3bbd7d03c1a9..d0b2143712c8 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/BeanPack.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanPack.java @@ -3,14 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import javax.management.ObjectName; import javax.management.QueryExp; /** * A class describing a set of MBeans which can be used to collect values for a metric. Objects of - * this class are inmutable. + * this class are immutable. */ public class BeanPack { // How to specify the MBean(s) diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/DetectionStatus.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/DetectionStatus.java similarity index 88% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/DetectionStatus.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/DetectionStatus.java index 40a78a440b00..e012295fda19 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/DetectionStatus.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/DetectionStatus.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import java.util.Collection; import javax.management.MBeanServer; @@ -11,7 +11,7 @@ /** * A class encapsulating a set of ObjectNames and the MBeanServer that recognized them. Objects of - * this class are inmutable. + * this class are immutable. */ class DetectionStatus { diff --git a/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/JmxMetricInsight.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/JmxMetricInsight.java new file mode 100644 index 000000000000..2f5a513eb156 --- /dev/null +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/JmxMetricInsight.java @@ -0,0 +1,49 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.jmx.engine; + +import static java.util.logging.Level.CONFIG; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import java.util.logging.Logger; + +/** Collecting and exporting JMX metrics. */ +public class JmxMetricInsight { + + private static final Logger logger = Logger.getLogger(JmxMetricInsight.class.getName()); + + private static final String INSTRUMENTATION_SCOPE = "io.opentelemetry.jmx"; + + private final OpenTelemetry openTelemetry; + private final ConfigProperties configProperties; + + public static JmxMetricInsight createService(OpenTelemetry ot, ConfigProperties config) { + return new JmxMetricInsight(ot, config); + } + + public static Logger getLogger() { + return logger; + } + + private JmxMetricInsight(OpenTelemetry ot, ConfigProperties config) { + openTelemetry = ot; + configProperties = config; + } + + public void start(MetricConfiguration conf) { + if (conf.isEmpty()) { + logger.log( + CONFIG, + "Empty JMX configuration, no metrics will be collected for InstrumentationScope " + + INSTRUMENTATION_SCOPE); + } else { + MetricRegistrar registrar = new MetricRegistrar(openTelemetry, INSTRUMENTATION_SCOPE); + BeanFinder finder = new BeanFinder(registrar, configProperties); + finder.discoverBeans(conf); + } + } +} diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/LabelExtractor.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/LabelExtractor.java similarity index 86% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/LabelExtractor.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/LabelExtractor.java index 967573fa42bd..b31666853b48 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/LabelExtractor.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/LabelExtractor.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import javax.management.MBeanServer; import javax.management.ObjectName; diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricBanner.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricBanner.java similarity index 92% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricBanner.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricBanner.java index 2c74088729da..57b5c41c421d 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricBanner.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricBanner.java @@ -3,13 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; /** * A class providing the user visible characteristics (name, type, description and units) of a * metric to be reported with OpenTelemetry. * - *

Objects of this class are inmutable. + *

Objects of this class are immutable. */ public class MetricBanner { diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricConfiguration.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricConfiguration.java similarity index 90% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricConfiguration.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricConfiguration.java index f1c8c8afc2f6..dee55e6b2ba0 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricConfiguration.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricConfiguration.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import java.util.ArrayList; import java.util.Collection; diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricDef.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricDef.java similarity index 92% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricDef.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricDef.java index 1380deb32576..3d812a0bc513 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricDef.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricDef.java @@ -3,12 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; /** * A class providing a complete definition on how to create an Open Telemetry metric out of the JMX * system: how to extract values from MBeans and how to model, name and label them using - * OpenTelemetry Metric API. Objects of this class are inmutable. + * OpenTelemetry Metric API. Objects of this class are immutable. * *

Example: The JVM provides an MBean with ObjectName "java.lang:type=Threading", and one of the * MBean attributes is "ThreadCount". This MBean can be used to provide a metric showing the current diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricExtractor.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricExtractor.java similarity index 95% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricExtractor.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricExtractor.java index 70ae816e9474..0b43df732b00 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricExtractor.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricExtractor.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; /** * A class holding the info needed to support a single metric: how to define it in OpenTelemetry and diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricLabel.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricLabel.java similarity index 92% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricLabel.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricLabel.java index 1cce4a01eb6b..27083ce104cc 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricLabel.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricLabel.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import javax.management.MBeanServer; import javax.management.ObjectName; @@ -11,7 +11,7 @@ /** * A class representing a metric attribute (label). It is responsible for extracting a label value * (to be reported as a Measurement attribute), and for holding the corresponding label name to be - * used. Objects of this class are inmutable. + * used. Objects of this class are immutable. */ public class MetricLabel { private final String labelName; diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricRegistrar.java b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricRegistrar.java similarity index 94% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricRegistrar.java rename to instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricRegistrar.java index 461839b4de75..d67e1256f1f1 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricRegistrar.java +++ b/instrumentation/jmx/jmx-engine/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/MetricRegistrar.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import static java.util.logging.Level.CONFIG; @@ -18,12 +18,15 @@ import io.opentelemetry.api.metrics.ObservableLongMeasurement; import java.util.Collection; import java.util.function.Consumer; +import java.util.logging.Logger; import javax.management.MBeanServer; import javax.management.ObjectName; /** A class responsible for maintaining the set of metrics to collect and report. */ class MetricRegistrar { + private static final Logger logger = Logger.getLogger(MetricRegistrar.class.getName()); + private final Meter meter; MetricRegistrar(OpenTelemetry openTelemetry, String instrumentationScope) { @@ -80,7 +83,7 @@ void enrollExtractor( } else { builder.buildWithCallback(longTypeCallback(extractor)); } - MetricService.logger.log(CONFIG, "Created Counter for {0}", metricName); + logger.log(CONFIG, "Created Counter for {0}", metricName); } break; @@ -101,7 +104,7 @@ void enrollExtractor( } else { builder.buildWithCallback(longTypeCallback(extractor)); } - MetricService.logger.log(CONFIG, "Created UpDownCounter for {0}", metricName); + logger.log(CONFIG, "Created UpDownCounter for {0}", metricName); } break; @@ -122,7 +125,7 @@ void enrollExtractor( } else { builder.ofLongs().buildWithCallback(longTypeCallback(extractor)); } - MetricService.logger.log(CONFIG, "Created Gauge for {0}", metricName); + logger.log(CONFIG, "Created Gauge for {0}", metricName); } } } diff --git a/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeExtractorTest.java b/instrumentation/jmx/jmx-engine/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/AttributeExtractorTest.java similarity index 99% rename from instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeExtractorTest.java rename to instrumentation/jmx/jmx-engine/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/AttributeExtractorTest.java index 272e7def86ef..249d9243224a 100644 --- a/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/AttributeExtractorTest.java +++ b/instrumentation/jmx/jmx-engine/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/AttributeExtractorTest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; import static org.assertj.core.api.Assertions.assertThat; diff --git a/instrumentation/jmx/jmx-yaml/library/build.gradle.kts b/instrumentation/jmx/jmx-yaml/library/build.gradle.kts new file mode 100644 index 000000000000..7dd35d0e9e8c --- /dev/null +++ b/instrumentation/jmx/jmx-yaml/library/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("otel.library-instrumentation") +} + +dependencies { + implementation("org.yaml:snakeyaml") + implementation(project(":instrumentation:jmx:jmx-engine:library")) + + testImplementation(project(":testing-common")) +} diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/JmxConfig.java b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxConfig.java similarity index 83% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/JmxConfig.java rename to instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxConfig.java index f79f9d02a609..1749a098310c 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/JmxConfig.java +++ b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxConfig.java @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data; +package io.opentelemetry.instrumentation.jmx.yaml; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricConfiguration; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricDef; +import io.opentelemetry.instrumentation.jmx.engine.MetricConfiguration; +import io.opentelemetry.instrumentation.jmx.engine.MetricDef; import java.util.List; /** diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/JmxRule.java b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxRule.java similarity index 91% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/JmxRule.java rename to instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxRule.java index a18694c584ed..10dd9ac96731 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/JmxRule.java +++ b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxRule.java @@ -3,14 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data; - -import io.opentelemetry.instrumentation.runtimemetrics.jmx.AttributeValueExtractor; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.BeanPack; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricBanner; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricDef; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricExtractor; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricLabel; +package io.opentelemetry.instrumentation.jmx.yaml; + +import io.opentelemetry.instrumentation.jmx.engine.AttributeValueExtractor; +import io.opentelemetry.instrumentation.jmx.engine.BeanPack; +import io.opentelemetry.instrumentation.jmx.engine.MetricBanner; +import io.opentelemetry.instrumentation.jmx.engine.MetricDef; +import io.opentelemetry.instrumentation.jmx.engine.MetricExtractor; +import io.opentelemetry.instrumentation.jmx.engine.MetricLabel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -94,10 +94,10 @@ public void setMapping(Map mapping) { } private static Map validateAttributeMapping(Map mapping) { - if (mapping.isEmpty()) { throw new IllegalStateException("No MBean attributes specified"); } + // Make sure that all attribute names are well-formed by creating the corresponding // AttributeValueExtractors Set attrNames = mapping.keySet(); diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/Metric.java b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/Metric.java similarity index 91% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/Metric.java rename to instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/Metric.java index 10bdcdec0098..9d5966c052f4 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/Metric.java +++ b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/Metric.java @@ -3,9 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data; +package io.opentelemetry.instrumentation.jmx.yaml; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricBanner; +import io.opentelemetry.instrumentation.jmx.engine.MetricBanner; /** * A class representing metric definition as a part of YAML metric rule. Objects of this class are diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/MetricStructure.java b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/MetricStructure.java similarity index 94% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/MetricStructure.java rename to instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/MetricStructure.java index c546d90a3a14..4cc2a03b70f2 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/data/MetricStructure.java +++ b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/MetricStructure.java @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data; +package io.opentelemetry.instrumentation.jmx.yaml; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricBanner; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricLabel; +import io.opentelemetry.instrumentation.jmx.engine.MetricBanner; +import io.opentelemetry.instrumentation.jmx.engine.MetricLabel; import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/yaml/RuleParser.java b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/RuleParser.java similarity index 85% rename from instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/yaml/RuleParser.java rename to instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/RuleParser.java index 7ccd418b2de9..d200db1a9537 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/conf/yaml/RuleParser.java +++ b/instrumentation/jmx/jmx-yaml/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/RuleParser.java @@ -3,13 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.yaml; +package io.opentelemetry.instrumentation.jmx.yaml; import static java.util.logging.Level.CONFIG; import static java.util.logging.Level.WARNING; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricConfiguration; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data.JmxConfig; +import io.opentelemetry.instrumentation.jmx.engine.MetricConfiguration; import java.io.InputStream; import java.util.logging.Logger; import org.yaml.snakeyaml.Yaml; @@ -31,12 +30,11 @@ public class RuleParser { // classes we skip explicit checks for nullnes in the field setters, and let the setters // crash with NullPointerException instead. - private static RuleParser theParser = null; + private static final Logger logger = Logger.getLogger(RuleParser.class.getName()); + + private static final RuleParser theParser = new RuleParser(); public static RuleParser get() { - if (theParser == null) { - theParser = new RuleParser(); - } return theParser; } @@ -51,11 +49,10 @@ public JmxConfig loadConfig(InputStream is) throws Exception { * Parse the YAML rules from the specified input stream and add them, after converting to the * internal representation, to the provided metric configuration. * - * @param logger logger to be used for any messages * @param conf the metric configuration * @param is the InputStream with the YAML rules */ - public void addMetricDefs(Logger logger, MetricConfiguration conf, InputStream is) { + public void addMetricDefs(MetricConfiguration conf, InputStream is) { try { JmxConfig config = loadConfig(is); diff --git a/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/RuleParserTest.java b/instrumentation/jmx/jmx-yaml/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/RuleParserTest.java similarity index 97% rename from instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/RuleParserTest.java rename to instrumentation/jmx/jmx-yaml/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/RuleParserTest.java index 3d145c4010f2..d8bdc2dd1393 100644 --- a/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/RuleParserTest.java +++ b/instrumentation/jmx/jmx-yaml/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/RuleParserTest.java @@ -3,14 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.instrumentation.runtimemetrics.jmx; +package io.opentelemetry.instrumentation.jmx.engine; + +// This test is put in the io.opentelemetry.instrumentation.jmx.engine package +// because it needs to access package-private methods from a number of classes. import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data.JmxConfig; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data.JmxRule; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.data.Metric; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.yaml.RuleParser; +import io.opentelemetry.instrumentation.jmx.yaml.JmxConfig; +import io.opentelemetry.instrumentation.jmx.yaml.JmxRule; +import io.opentelemetry.instrumentation.jmx.yaml.Metric; +import io.opentelemetry.instrumentation.jmx.yaml.RuleParser; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.Charset; diff --git a/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java b/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java index fe01ce23d757..025e2298d04a 100644 --- a/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java +++ b/instrumentation/runtime-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/RuntimeMetricsInstaller.java @@ -14,7 +14,6 @@ import io.opentelemetry.instrumentation.runtimemetrics.GarbageCollector; import io.opentelemetry.instrumentation.runtimemetrics.MemoryPools; import io.opentelemetry.instrumentation.runtimemetrics.Threads; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.MetricService; import io.opentelemetry.javaagent.extension.AgentListener; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; @@ -28,25 +27,21 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredSdk) { ConfigProperties config = autoConfiguredSdk.getConfig(); boolean defaultEnabled = config.getBoolean("otel.instrumentation.common.default-enabled", true); - if (config.getBoolean("otel.instrumentation.runtime-metrics.enabled", defaultEnabled)) { - - OpenTelemetry openTelemetry = GlobalOpenTelemetry.get(); + if (!config.getBoolean("otel.instrumentation.runtime-metrics.enabled", defaultEnabled)) { + return; + } - BufferPools.registerObservers(openTelemetry); - Classes.registerObservers(openTelemetry); - Cpu.registerObservers(openTelemetry); - MemoryPools.registerObservers(openTelemetry); - Threads.registerObservers(openTelemetry); + OpenTelemetry openTelemetry = GlobalOpenTelemetry.get(); - if (config.getBoolean( - "otel.instrumentation.runtime-metrics.experimental-metrics.enabled", false)) { - GarbageCollector.registerObservers(openTelemetry); - } - } + BufferPools.registerObservers(openTelemetry); + Classes.registerObservers(openTelemetry); + Cpu.registerObservers(openTelemetry); + MemoryPools.registerObservers(openTelemetry); + Threads.registerObservers(openTelemetry); - if (config.getBoolean("otel.jmx.enabled", true)) { - MetricService service = new MetricService(GlobalOpenTelemetry.get(), config); - service.start(); + if (config.getBoolean( + "otel.instrumentation.runtime-metrics.experimental-metrics.enabled", false)) { + GarbageCollector.registerObservers(openTelemetry); } } } diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricService.java b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricService.java deleted file mode 100644 index 8599adc7b6ae..000000000000 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/jmx/MetricService.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.runtimemetrics.jmx; - -import static java.util.logging.Level.CONFIG; -import static java.util.logging.Level.FINE; - -import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.instrumentation.runtimemetrics.jmx.conf.yaml.RuleParser; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import java.io.File; -import java.io.InputStream; -import java.nio.file.Files; -import java.util.logging.Logger; - -/** Collecting and exporting JMX metrics. */ -public class MetricService { - - static final Logger logger = Logger.getLogger("JMX Metric Insight"); - - private static final String INSTRUMENTATION_SCOPE = "io.opentelemetry.jmx"; - - private final OpenTelemetry openTelemetry; - private final ConfigProperties configProperties; - - public MetricService(OpenTelemetry ot, ConfigProperties config) { - openTelemetry = ot; - configProperties = config; - } - - private static String resourceFor(String platform) { - return "/jmx/rules/" + platform + ".yaml"; - } - - private void addRulesForPlatform(String platform, MetricConfiguration conf) { - - String yamlResource = resourceFor(platform); - try (InputStream inputStream = getClass().getResourceAsStream(yamlResource)) { - if (inputStream != null) { - logger.log(FINE, "Opened input stream {0}", yamlResource); - RuleParser parserInstance = RuleParser.get(); - parserInstance.addMetricDefs(logger, conf, inputStream); - } else { - logger.log(CONFIG, "No support found for {0}", platform); - } - } catch (Exception e) { - logger.warning(e.getMessage()); - } - } - - private void buildFromDefaultRules(MetricConfiguration conf) { - - String targetSystem = System.getProperty("otel.jmx.target.system", "").trim(); - String[] platforms = targetSystem.length() == 0 ? new String[0] : targetSystem.split(","); - - for (String platform : platforms) { - addRulesForPlatform(platform, conf); - } - } - - private static void buildFromUserRules(MetricConfiguration conf) { - String jmxDir = System.getProperty("otel.jmx.config"); - if (jmxDir != null) { - logger.log(CONFIG, "JMX config file name: {0}", jmxDir); - RuleParser parserInstance = RuleParser.get(); - try (InputStream inputStream = Files.newInputStream(new File(jmxDir.trim()).toPath())) { - parserInstance.addMetricDefs(logger, conf, inputStream); - } catch (Exception e) { - logger.warning(e.getMessage()); - } - } - } - - private MetricConfiguration buildMetricConfiguration() { - MetricConfiguration conf = new MetricConfiguration(); - - buildFromDefaultRules(conf); - - buildFromUserRules(conf); - - return conf; - } - - public void start() { - MetricConfiguration conf = buildMetricConfiguration(); - - if (conf.isEmpty()) { - logger.log( - CONFIG, - "Empty JMX configuration, no metrics will be collected for InstrumentationScope " - + INSTRUMENTATION_SCOPE); - } else { - MetricRegistrar registrar = new MetricRegistrar(openTelemetry, INSTRUMENTATION_SCOPE); - BeanFinder finder = new BeanFinder(registrar, configProperties); - finder.discoverBeans(conf); - } - } -} diff --git a/settings.gradle.kts b/settings.gradle.kts index e991b837ca47..39df775d853e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -298,6 +298,9 @@ include(":instrumentation:jetty-httpclient:jetty-httpclient-9.2:library") include(":instrumentation:jetty-httpclient:jetty-httpclient-9.2:testing") include(":instrumentation:jms-1.1:javaagent") include(":instrumentation:jms-1.1:javaagent-unit-tests") +include(":instrumentation:jmx:javaagent") +include(":instrumentation:jmx:jmx-engine:library") +include(":instrumentation:jmx:jmx-yaml:library") include(":instrumentation:jsf:jsf-common:javaagent") include(":instrumentation:jsf:jsf-common:testing") include(":instrumentation:jsf:jsf-mojarra-1.2:javaagent")