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

[wip] JFR Event Streaming Support #22

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
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
9 changes: 6 additions & 3 deletions .github/workflows/deploy-snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ jobs:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
- name: Set up JDK 17
uses: actions/setup-java@v2.3.0
with:
java-version: 1.8
java-version: '17'
distribution: 'zulu'
- name: install deps
run: deps/install.sh
- name: Configure Maven
env:
M2_SETTINGS_XML: ${{ secrets.M2_SETTINGS_XML }}
Expand Down
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/dependency-reduced-pom.xml
/target/
/.classpath
/.settings
/.project
/pom.xml.releaseBackup
/release.properties
dependency-reduced-pom.xml
target/
.classpath
.settings
.project
pom.xml.releaseBackup
release.properties
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "deps/janino"]
path = deps/janino
url = https://github.com/janino-compiler/janino
56 changes: 50 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Scriptable JMX Exporter
=======================

Java agent to scrape and expose MBeans to Prometheus. Formerly, java-prometheus-metrics-agent.
Java agent to scrape and expose MBeans to Prometheus.

[![GitHub Actions](https://github.com/eiiches/scriptable-jmx-exporter/workflows/test/badge.svg)](https://github.com/eiiches/scriptable-jmx-exporter/actions)

Expand All @@ -22,6 +22,8 @@ Features
- Decomposing complex MBean name into metric labels.
- Performance. Goal is to enable a large number of metrics (~50k) at shorter intervals (>1s) without impacting workloads.
- See our [benchmark](#benchmark).
- Java Flight Recorder (JFR) Event Streaming (>=Java 14)
- You can aggregate JFR events into custom metrics.


#### Why another exporter? There's a bunch of exporters and there's even the official one.
Expand All @@ -32,8 +34,8 @@ I needed something that can scrape many MBeans with a small number of rules. Wri

#### Requirements

* Java 8 or newer

* Java 8 (Minimum)
* JFR Event Streaming support requires Java 14 or newer

Quick Start
------------
Expand Down Expand Up @@ -126,6 +128,8 @@ declarations: |
public static void foo() {
log("foo");
}
public static Counter jvmAllocationRate = Counter.builder("jvm_allocation_rate")
.register(Registry.getInstance());
rules:
- pattern:
# Drop less useful attributes JVM exposes.
Expand All @@ -144,6 +148,14 @@ rules:
# Default rule to cover the rest.
- transform: |
V1.transform(in, out, "type");
flight_recorder_events:
- name: jdk.ObjectAllocationInNewTLAB
handler: |
jvmAllocationRate.increment(event.getLong("tlabSize"));
- name: jdk.ObjectAllocationOutsideTLAB
handler: |
jvmAllocationRate.increment(event.getLong("allocationSize"));

```

This YAML is mapped to [Config](src/main/java/net/thisptr/jmx/exporter/agent/config/Config.java) class using Jackson data-binding and validated by Hibernate validator.
Expand All @@ -169,8 +181,8 @@ These options can be overridden by URL parameters. E.g. `/metrics?minimum_respon

### Declarations

You can define static classes and methods for use in transform scripts, condition expressions, etc. They will be automatically imported and available so you don't have to manually write `import` statements.
Make sure to add `public static` in the declarations; otherwise, the classes and methods won't be accessible.
The classes, methods and variables defined here will be automatically imported and can be referenced from other places (e.g. MBean transform scripts, condition expression, and JFR event handlers).
You can also define and register custom metrics here.

```yaml
declarations: |
Expand All @@ -183,9 +195,14 @@ declarations: |
public static class Foo {
// ...
}

public static Counter jvmAllocationRate = Counter.builder("jvm_allocation_rate")
.register(Registry.getInstance());
```

### Rule Configuration
> NOTE: Make sure to use `public static` modifiers.

### MBean Rule Configuration

Rules are searched in order and a first match is used for each attribute.

Expand Down Expand Up @@ -219,6 +236,33 @@ The following variables are accessible from a condition expression.

* `mbeanInfo.getClassName().endsWith("JmxReporter$Timer")`

### Flight Recorder Events

You can subscribe events from Java Flight Recorder (JFR) event stream.

```yaml
flight_recorder_events:
- name: .*
handler: |
Counter.builder("java_flight_recorder_events")
.tags("type", event.getEventType().getName())
.register(Registry.getInstance())
.increment();
```

| Config Key | Default | Description |
|-|-|-|
| `flight_recorder_events[].name` | - | The type of the event to subscribe. E.g. `jdk.ObjectAllocationInNewTLAB` |
| `flight_recorder_events[].handler` | - | The script to execute when the event is generated. |

The following variables are accessible from the handler script.

| Variable Name | Type | Description |
|-|-|-|
| `event` | [jdk.jfr.consumer.RecordedEvent](https://docs.oracle.com/en/java/javase/14/docs/api/jdk.jfr/jdk/jfr/consumer/RecordedEvent.html) | The generated event |

You can use custom JFR events too. Refer to [Creating and Recording Your First Event](https://docs.oracle.com/en/java/javase/14/jfapi/creating-and-recording-your-first-event.html) to learn how to create custom events.

Scripting
---------

Expand Down
24 changes: 24 additions & 0 deletions deps/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
set -euo pipefail

scriptpath="$(readlink -f "$0")"
scriptdir="$(dirname "$scriptpath")"
cd "$scriptdir"

git submodule update --force --recursive --init janino
git -C janino clean -fdx
base_version="$(git -C janino rev-parse HEAD | cut -c 1-7)"
patch_version="$(cat janino.patch.d/*.patch | sha1sum | cut -c 1-5)"
version="0.0.0-$base_version.$patch_version"

for patch in janino.patch.d/*.patch; do
patch="$(readlink -f "$patch")"
git -C janino am "$patch"
done

(cd janino/janino-parent \
&& mvn versions:set -DnewVersion="$version" \
&& mvn clean install)

git submodule update --force --recursive --init janino
git -C janino clean -fdx
1 change: 1 addition & 0 deletions deps/janino
Submodule janino added at a796a9
27 changes: 27 additions & 0 deletions deps/janino.patch.d/0001-compile-with-target-1.7-source-1.7.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
From 5e8bee8cc526ab149881a036cb950e6d6c3d2de5 Mon Sep 17 00:00:00 2001
From: Eiichi Sato <sato.eiichi@gmail.com>
Date: Mon, 18 Oct 2021 02:58:26 +0900
Subject: [PATCH] compile with -target 1.7 -source 1.7

---
janino-parent/pom.xml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/janino-parent/pom.xml b/janino-parent/pom.xml
index 04b4d50b..5c387b7c 100644
--- a/janino-parent/pom.xml
+++ b/janino-parent/pom.xml
@@ -13,8 +13,8 @@
<!--
JANINO still retains backwards compatibility with JRE 6:
-->
- <maven.compiler.source>1.6</maven.compiler.source>
- <maven.compiler.target>1.6</maven.compiler.target>
+ <maven.compiler.source>1.7</maven.compiler.source>
+ <maven.compiler.target>1.7</maven.compiler.target>

<!--
If you want to enforce a particular JAVAC executable to be used, set these two properties:
--
2.33.1

9 changes: 9 additions & 0 deletions deps/reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -euo pipefail

scriptpath="$(readlink -f "$0")"
scriptdir="$(dirname "$scriptpath")"
cd "$scriptdir"

git submodule update --force --recursive --init janino
git -C janino clean -fdx
2 changes: 1 addition & 1 deletion examples/apache-cassandra/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
- '9639:9639'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter-typed.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/apache-flink/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- '9249:9249'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter-typed.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/apache-flume/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
- '44444:44444'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/apache-hbase/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- '16030:16030'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/apache-ignite/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- '6379:6379'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/apache-solr/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- '8983:8983'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter-untyped.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/apache-tomcat/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- '8080:8080'
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter.yaml
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmark-kafka/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
volumes:
- 'kafka_data:/bitnami'
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./jmx_prometheus_javaagent-0.13.0.jar
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-jvm-metrics/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
image: openjdk:14.0.1
volumes:
- type: bind
source: ../../target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
source: ../../scriptable-jmx-exporter/target/scriptable-jmx-exporter-1.0.0-SNAPSHOT.jar
target: /opt/java-agents/scriptable-jmx-exporter.jar
- type: bind
source: ./scriptable-jmx-exporter.yaml
Expand Down
Loading