Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d081c29
add test code to test yaml jmx metrics
SylvainJuge Feb 25, 2025
e1f60cd
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Mar 27, 2025
02e8a41
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday Mar 28, 2025
dfae1ac
Tomcat metrics updated
robsunday Apr 1, 2025
e272d84
Tomcat test added
robsunday Apr 4, 2025
2d2c264
tomcat.active_session.count support added in test
robsunday Apr 4, 2025
15ad839
Comment added to test
robsunday Apr 4, 2025
297fb6b
Checkstyle error fix
robsunday Apr 4, 2025
e8fdc29
Attr name fixed
robsunday Apr 4, 2025
185e60d
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday Apr 7, 2025
8a9bb0f
Test fixed.
robsunday Apr 7, 2025
1cc4080
Test fixed.
robsunday Apr 8, 2025
747ec32
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday Apr 8, 2025
816b9ed
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday Apr 22, 2025
910e837
Thread count related metrics fixed after code review
robsunday Apr 24, 2025
0dc6775
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday Apr 24, 2025
1dcd568
Readme updates
robsunday Apr 24, 2025
38fb785
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday May 14, 2025
99104ee
Update instrumentation/jmx-metrics/library/src/main/resources/jmx/rul…
robsunday May 14, 2025
f8cc647
Update instrumentation/jmx-metrics/library/src/main/resources/jmx/rul…
robsunday May 14, 2025
299b35c
Code review changes
robsunday May 14, 2025
ee7faec
Fixed tomcat.md
robsunday May 15, 2025
5bdbabd
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday May 23, 2025
b5fdd29
tomcat.request.duration.total -> tomcat.request.duration.sum
robsunday May 23, 2025
6f69f76
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday May 23, 2025
c3248f1
Update instrumentation/jmx-metrics/library/tomcat.md
robsunday May 27, 2025
7c4afcb
tomcat.request_processor.name -> tomcat.request.processor.name
robsunday May 27, 2025
aee5f06
Merge branch 'main' into jmx-unit-semconv-alignment
robsunday May 27, 2025
616014e
Update instrumentation/jmx-metrics/library/tomcat.md
robsunday May 29, 2025
8ad8974
Indentation fixed
robsunday May 29, 2025
9f07105
Unit descriptions tuning
robsunday May 29, 2025
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
4 changes: 2 additions & 2 deletions instrumentation/jmx-metrics/javaagent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ No targets are enabled by default. The supported target environments are listed
- [camel](camel.md)
- [jetty](jetty.md)
- [kafka-broker](kafka-broker.md)
- [tomcat](tomcat.md)
- [tomcat](../library/tomcat.md)
- [wildfly](wildfly.md)
- [hadoop](hadoop.md)

Expand Down Expand Up @@ -302,7 +302,7 @@ rules:
unit: By
metricAttribute:
jvm.memory.pool.name : param(name)
jvm.memory.type: lowercase(beanattr(type))
jvm.memory.type: lowercase(beanattr(Type))
```

For now, only the `lowercase` transformation is supported, other additions might be added in the future if needed.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import java.util.Set;
import org.junit.jupiter.api.Test;

/**
* TODO: This test will eventually go away when all yaml files are moved from javaagent to library
* directory. When yaml file is moved from javaagent to library then appropriate item must be
* removed from JmxMetricInsightInstallerTest#FILES_TO_BE_TESTED and corresponding test must be
* added in the library.
*/
class JmxMetricInsightInstallerTest {
private static final String PATH_TO_ALL_EXISTING_RULES = "src/main/resources/jmx/rules";
private static final Set<String> FILES_TO_BE_TESTED =
Expand All @@ -32,7 +38,6 @@ class JmxMetricInsightInstallerTest {
"hadoop.yaml",
"jetty.yaml",
"kafka-broker.yaml",
"tomcat.yaml",
"wildfly.yaml"));

@Test
Expand Down
13 changes: 0 additions & 13 deletions instrumentation/jmx-metrics/javaagent/tomcat.md

This file was deleted.

26 changes: 26 additions & 0 deletions instrumentation/jmx-metrics/library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
id("otel.library-instrumentation")
}
Expand All @@ -6,4 +8,28 @@ dependencies {
implementation("org.snakeyaml:snakeyaml-engine")

testImplementation(project(":testing-common"))
testImplementation("org.testcontainers:testcontainers")

testImplementation("org.testcontainers:junit-jupiter")
testImplementation("com.linecorp.armeria:armeria-junit5:1.31.3")
testImplementation("com.linecorp.armeria:armeria-junit5:1.31.3")
testImplementation("com.linecorp.armeria:armeria-grpc:1.31.3")
testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.5.0-alpha")
}

tasks {
test {
// get packaged agent jar for testing
val shadowTask = project(":javaagent").tasks.named<ShadowJar>("shadowJar").get()

dependsOn(shadowTask)

inputs.files(layout.files(shadowTask))
.withPropertyName("javaagent")
.withNormalizer(ClasspathNormalizer::class)

doFirst {
jvmArgs("-Dio.opentelemetry.javaagent.path=${shadowTask.archiveFile.get()}")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
# For Tomcat, the default JMX domain is "Catalina:", however with some deployments like embedded in spring-boot
# we can have the "Tomcat:" domain used, thus we use both MBean names for the metrics.

rules:
- beans:
- Catalina:type=GlobalRequestProcessor,name=*
- Tomcat:type=GlobalRequestProcessor,name=*
prefix: tomcat.
metricAttribute:
tomcat.request_processor.name: param(name)
mapping:
errorCount:
metric: error.count
type: counter
unit: "{error}"
desc: The number of errors.
requestCount:
metric: request.count
type: counter
unit: "{request}"
desc: The number of requests processed.
maxTime:
metric: request.duration.max
type: gauge
sourceUnit: ms
unit: s
desc: The longest request processing time.
processingTime:
metric: request.processing_time
type: counter
sourceUnit: ms
unit: s
desc: Total time for processing all requests.
bytesReceived:
metric: &metric network.io
type: &type counter
unit: &unit By
desc: &desc The number of bytes transmitted.
metricAttribute:
network.io.direction: const(receive)
bytesSent:
metric: *metric
type: *type
unit: *unit
desc: *desc
metricAttribute:
network.io.direction: const(transmit)

- beans:
- Catalina:type=Manager,host=localhost,context=*
- Tomcat:type=Manager,host=localhost,context=*
prefix: tomcat.
metricAttribute:
tomcat.context: param(context)
mapping:
activeSessions:
metric: active_session.count
type: updowncounter
unit: "{session}"
desc: The number of active sessions.

- beans:
- Catalina:type=ThreadPool,name=*
- Tomcat:type=ThreadPool,name=*
unit: "{thread}"
prefix: tomcat.
type: updowncounter
metricAttribute:
tomcat.thread_pool.name: param(name)
mapping:
currentThreadCount:
metric: &metric thread.count
desc: &desc Thread count of the thread pool.
metricAttribute:
tomcat.thread.state: const(idle)
currentThreadsBusy:
metric: *metric
desc: *desc
metricAttribute:
tomcat.thread.state: const(busy)

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.jmx.rules;

import static io.opentelemetry.instrumentation.jmx.rules.assertions.JmxAssertj.assertThat;
import static org.assertj.core.api.Assertions.fail;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.instrumentation.jmx.rules.assertions.MetricAssert;
import io.opentelemetry.proto.metrics.v1.Metric;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class MetricsVerifier {

private final Map<String, Consumer<Metric>> assertions = new HashMap<>();
private boolean strictMode = true;

private MetricsVerifier() {}

/**
* Create instance of MetricsVerifier configured to fail verification if any metric was not
* verified because there is no assertion defined for it. This behavior can be changed by calling
* {@link #disableStrictMode()} method.
*
* @return new instance of MetricsVerifier
* @see #disableStrictMode()
*/
public static MetricsVerifier create() {
return new MetricsVerifier();
}

/**
* Disable strict checks of metric assertions. It means that all metrics checks added after
* calling this method will not enforce asserting all metric properties and will not detect
* duplicate property assertions. Also, there will be no error reported if any of metrics was
* skipped because no assertion was added for it.
*
* @return this
* @see #verify(List)
* @see #add(String, Consumer)
*/
@CanIgnoreReturnValue
public MetricsVerifier disableStrictMode() {
strictMode = false;
return this;
}

/**
* Add assertion for given metric
*
* @param metricName name of metric to be verified by provided assertion
* @param assertion an assertion to verify properties of the metric
* @return this
*/
@CanIgnoreReturnValue
public MetricsVerifier add(String metricName, Consumer<MetricAssert> assertion) {
if (assertions.containsKey(metricName)) {
throw new IllegalArgumentException("Duplicate assertion for metric " + metricName);
}
assertions.put(
metricName,
metric -> {
MetricAssert metricAssert = assertThat(metric);
metricAssert.setStrict(strictMode);
assertion.accept(metricAssert);
metricAssert.strictCheck();
});
return this;
}

/**
* Execute all defined assertions against provided list of metrics. Error is reported if any of
* defined assertions failed. Error is also reported if any of expected metrics was not present in
* the metrics list, unless strict mode is disabled with {@link #disableStrictMode()}.
*
* @param metrics list of metrics to be verified
* @see #add(String, Consumer)
* @see #disableStrictMode()
*/
public void verify(List<Metric> metrics) {
verifyAllExpectedMetricsWereReceived(metrics);

Set<String> unverifiedMetrics = new HashSet<>();

for (Metric metric : metrics) {
String metricName = metric.getName();
Consumer<Metric> assertion = assertions.get(metricName);

if (assertion != null) {
assertion.accept(metric);
} else {
unverifiedMetrics.add(metricName);
}
}

if (strictMode && !unverifiedMetrics.isEmpty()) {
fail("Metrics received but not verified because no assertion exists: " + unverifiedMetrics);
}
}

private void verifyAllExpectedMetricsWereReceived(List<Metric> metrics) {
Set<String> receivedMetricNames =
metrics.stream().map(Metric::getName).collect(Collectors.toSet());
Set<String> assertionNames = new HashSet<>(assertions.keySet());

assertionNames.removeAll(receivedMetricNames);
if (!assertionNames.isEmpty()) {
fail(
"Metrics expected but not received: "
+ assertionNames
+ "\nReceived only: "
+ receivedMetricNames);
}
}
}
Loading
Loading