-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring boot runtime metrics for java 8
- Loading branch information
1 parent
4b196cc
commit 5099e45
Showing
10 changed files
with
198 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...y/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/RuntimeMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics.java8; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.JmxRuntimeMetricsUtil; | ||
import java.io.Closeable; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** The entry point class for runtime metrics support using JFR and JMX. */ | ||
public final class RuntimeMetrics implements Closeable { | ||
|
||
private static final Logger logger = Logger.getLogger(RuntimeMetrics.class.getName()); | ||
|
||
private final AtomicBoolean isClosed = new AtomicBoolean(); | ||
private final List<AutoCloseable> observables; | ||
|
||
RuntimeMetrics(List<AutoCloseable> observables) { | ||
this.observables = Collections.unmodifiableList(observables); | ||
} | ||
|
||
/** | ||
* Create and start {@link RuntimeMetrics}. | ||
* | ||
* <p>Listens for select JFR events, extracts data, and records to various metrics. Recording will | ||
* continue until {@link #close()} is called. | ||
* | ||
* @param openTelemetry the {@link OpenTelemetry} instance used to record telemetry | ||
*/ | ||
public static RuntimeMetrics create(OpenTelemetry openTelemetry) { | ||
return new RuntimeMetricsBuilder(openTelemetry).build(); | ||
} | ||
|
||
/** | ||
* Create a builder for configuring {@link RuntimeMetrics}. | ||
* | ||
* @param openTelemetry the {@link OpenTelemetry} instance used to record telemetry | ||
*/ | ||
public static RuntimeMetricsBuilder builder(OpenTelemetry openTelemetry) { | ||
return new RuntimeMetricsBuilder(openTelemetry); | ||
} | ||
|
||
/** Stop recording JFR events. */ | ||
@Override | ||
public void close() { | ||
if (!isClosed.compareAndSet(false, true)) { | ||
logger.log(Level.WARNING, "RuntimeMetrics is already closed"); | ||
return; | ||
} | ||
|
||
JmxRuntimeMetricsUtil.closeObservers(observables); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ain/java/io/opentelemetry/instrumentation/runtimemetrics/java8/RuntimeMetricsBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics.java8; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.JmxRuntimeMetricsFactory; | ||
import java.util.List; | ||
|
||
/** Builder for {@link RuntimeMetrics}. */ | ||
public final class RuntimeMetricsBuilder { | ||
|
||
private final OpenTelemetry openTelemetry; | ||
|
||
private boolean disableJmx = false; | ||
private boolean enableExperimentalJmxTelemetry = false; | ||
|
||
RuntimeMetricsBuilder(OpenTelemetry openTelemetry) { | ||
this.openTelemetry = openTelemetry; | ||
} | ||
|
||
/** Enable all JMX telemetry collection. */ | ||
@CanIgnoreReturnValue | ||
public RuntimeMetricsBuilder enableExperimentalJmxTelemetry() { | ||
enableExperimentalJmxTelemetry = true; | ||
return this; | ||
} | ||
|
||
/** Build and start an {@link RuntimeMetrics} with the config from this builder. */ | ||
public RuntimeMetrics build() { | ||
List<AutoCloseable> observables = | ||
JmxRuntimeMetricsFactory.buildObservables( | ||
openTelemetry, disableJmx, enableExperimentalJmxTelemetry); | ||
return new RuntimeMetrics(observables); | ||
} | ||
|
||
public void startFromInstrumentationConfig(InstrumentationConfig config) { | ||
/* | ||
By default, don't use any JFR metrics. May change this once semantic conventions are updated. | ||
If enabled, default to only the metrics not already covered by runtime-telemetry-java8 | ||
*/ | ||
boolean defaultEnabled = config.getBoolean("otel.instrumentation.common.default-enabled", true); | ||
if (!config.getBoolean("otel.instrumentation.runtime-telemetry.enabled", defaultEnabled)) { | ||
// nothing is enabled | ||
return; | ||
} | ||
|
||
if (config.getBoolean( | ||
"otel.instrumentation.runtime-telemetry.emit-experimental-telemetry", false)) { | ||
this.enableExperimentalJmxTelemetry(); | ||
} | ||
|
||
RuntimeMetrics finalJfrTelemetry = this.build(); | ||
Thread cleanupTelemetry = new Thread(finalJfrTelemetry::close); | ||
Runtime.getRuntime().addShutdownHook(cleanupTelemetry); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...opentelemetry/instrumentation/runtimemetrics/java8/internal/JmxRuntimeMetricsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics.java8.internal; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.Classes; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.Cpu; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.GarbageCollector; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.MemoryPools; | ||
import io.opentelemetry.instrumentation.runtimemetrics.java8.Threads; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public class JmxRuntimeMetricsFactory { | ||
@SuppressWarnings("CatchingUnchecked") | ||
public static List<AutoCloseable> buildObservables( | ||
OpenTelemetry openTelemetry, boolean disableJmx, boolean enableExperimentalJmxTelemetry) { | ||
if (disableJmx) { | ||
return Collections.emptyList(); | ||
} | ||
try { | ||
// Set up metrics gathered by JMX | ||
List<AutoCloseable> observables = new ArrayList<>(); | ||
observables.addAll(Classes.registerObservers(openTelemetry)); | ||
observables.addAll(Cpu.registerObservers(openTelemetry)); | ||
observables.addAll(GarbageCollector.registerObservers(openTelemetry)); | ||
observables.addAll(MemoryPools.registerObservers(openTelemetry)); | ||
observables.addAll(Threads.registerObservers(openTelemetry)); | ||
if (enableExperimentalJmxTelemetry) { | ||
observables.addAll(ExperimentalBufferPools.registerObservers(openTelemetry)); | ||
observables.addAll(ExperimentalCpu.registerObservers(openTelemetry)); | ||
observables.addAll(ExperimentalMemoryPools.registerObservers(openTelemetry)); | ||
} | ||
return observables; | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Error building RuntimeMetrics", e); | ||
} | ||
} | ||
|
||
private JmxRuntimeMetricsFactory() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters