Skip to content

Commit

Permalink
Enable oshi ProcessMetrics in javaagent (and refactor oshi instrument…
Browse files Browse the repository at this point in the history
…ation) (open-telemetry#5281)

* Enable oshi ProcessMetrics in javaagent (and refactor oshi instrumentation)

* Get rid of double async instrument registration logs

* spotless
  • Loading branch information
Mateusz Rzeszutek authored and RashmiRam committed May 23, 2022
1 parent 4f67a02 commit e9c4be6
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 149 deletions.
8 changes: 7 additions & 1 deletion instrumentation/oshi/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ dependencies {

library("com.github.oshi:oshi-core:5.3.1")

testImplementation("com.google.guava:guava")
testImplementation(project(":instrumentation:oshi:testing"))
}

tasks {
withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.oshi.experimental-metrics.enabled=true")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.oshi;

import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.oshi.ProcessMetrics;
import io.opentelemetry.instrumentation.oshi.SystemMetrics;
import java.util.concurrent.atomic.AtomicBoolean;

public final class MetricsRegistration {

private static final AtomicBoolean registered = new AtomicBoolean();

public static void register() {
if (registered.compareAndSet(false, true)) {
SystemMetrics.registerObservers();

// ProcessMetrics don't follow the spec
if (Config.get()
.getBoolean("otel.instrumentation.oshi.experimental-metrics.enabled", false)) {
ProcessMetrics.registerObservers();
}
}
}

private MetricsRegistration() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.instrumentation.oshi.SystemMetrics;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -44,7 +43,7 @@ public static class GetCurrentPlatformEnumAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter() {
SystemMetrics.registerObservers();
MetricsRegistration.register();
}
}
}

This file was deleted.

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

package io.opentelemetry.javaagent.instrumentation.oshi;

import io.opentelemetry.instrumentation.oshi.AbstractProcessMetricsTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

class ProcessMetricsTest extends AbstractProcessMetricsTest {

@RegisterExtension
public static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Override
protected void registerMetrics() {}

@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.oshi;

import io.opentelemetry.instrumentation.oshi.AbstractSystemMetricsTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

class SystemMetricsTest extends AbstractSystemMetricsTest {

@RegisterExtension
public static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Override
protected void registerMetrics() {}

@Override
protected InstrumentationExtension testing() {
return testing;
}
}
4 changes: 1 addition & 3 deletions instrumentation/oshi/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ plugins {
dependencies {
library("com.github.oshi:oshi-core:5.3.1")

testImplementation("io.opentelemetry:opentelemetry-sdk-metrics")
testImplementation(project(":testing-common"))
testImplementation("org.assertj:assertj-core")
testImplementation(project(":instrumentation:oshi:testing"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ private ProcessMetrics() {}
/** Register observers for java runtime metrics. */
public static void registerObservers() {
// TODO(anuraaga): registerObservers should accept an OpenTelemetry instance
Meter meter = GlobalOpenTelemetry.get().getMeterProvider().get(ProcessMetrics.class.getName());
Meter meter = GlobalOpenTelemetry.get().getMeterProvider().get("io.opentelemetry.oshi");
SystemInfo systemInfo = new SystemInfo();
OperatingSystem osInfo = systemInfo.getOperatingSystem();
OSProcess processInfo = osInfo.getProcess(osInfo.getProcessId());

meter
.upDownCounterBuilder("runtime.java.memory")
.setDescription("Runtime Java memory")
.setUnit("bytes")
.setUnit("By")
.buildWithCallback(
r -> {
processInfo.updateAttributes();
Expand All @@ -41,12 +41,12 @@ public static void registerObservers() {
meter
.gaugeBuilder("runtime.java.cpu_time")
.setDescription("Runtime Java CPU time")
.setUnit("seconds")
.setUnit("ms")
.buildWithCallback(
r -> {
processInfo.updateAttributes();
r.record(processInfo.getUserTime() * 1000, Attributes.of(TYPE_KEY, "user"));
r.record(processInfo.getKernelTime() * 1000, Attributes.of(TYPE_KEY, "system"));
r.record(processInfo.getUserTime(), Attributes.of(TYPE_KEY, "user"));
r.record(processInfo.getKernelTime(), Attributes.of(TYPE_KEY, "system"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ private SystemMetrics() {}
/** Register observers for system metrics. */
public static void registerObservers() {
// TODO(anuraaga): registerObservers should accept an OpenTelemetry instance
Meter meter =
GlobalOpenTelemetry.get().getMeterProvider().get("io.opentelemetry.instrumentation.oshi");
Meter meter = GlobalOpenTelemetry.get().getMeterProvider().get("io.opentelemetry.oshi");
SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hal = systemInfo.getHardware();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,22 @@

package io.opentelemetry.instrumentation.oshi;

import static io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.junit.jupiter.api.Test;
class ProcessMetricsTest extends AbstractProcessMetricsTest {

public class ProcessMetricsTest extends AbstractMetricsTest {
@RegisterExtension
public static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();

@Test
public void test() {
@Override
protected void registerMetrics() {
ProcessMetrics.registerObservers();
}

waitAndAssertMetrics(
metric ->
metric
.hasName("runtime.java.memory")
.hasUnit("bytes")
.hasLongSum()
.points()
.anySatisfy(point -> assertThat(point.getValue()).isPositive()),
metric ->
metric
.hasName("runtime.java.cpu_time")
.hasUnit("seconds")
.hasDoubleGauge()
.points()
.anySatisfy(point -> assertThat(point.getValue()).isPositive()));
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,22 @@

package io.opentelemetry.instrumentation.oshi;

import static io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.junit.jupiter.api.Test;
class SystemMetricsTest extends AbstractSystemMetricsTest {

public class SystemMetricsTest extends AbstractMetricsTest {
@RegisterExtension
public static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();

@Test
public void test() {
@Override
protected void registerMetrics() {
SystemMetrics.registerObservers();
}

waitAndAssertMetrics(
metric ->
metric
.hasName("system.memory.usage")
.hasUnit("By")
.hasLongSum()
.points()
.anySatisfy(point -> assertThat(point.getValue()).isPositive()),
metric ->
metric
.hasName("system.memory.utilization")
.hasUnit("1")
.hasDoubleGauge()
.points()
.anySatisfy(point -> assertThat(point.getValue()).isPositive()),
metric -> metric.hasName("system.network.io").hasUnit("By").hasLongSum(),
metric -> metric.hasName("system.network.packets").hasUnit("packets").hasLongSum(),
metric -> metric.hasName("system.network.errors").hasUnit("errors").hasLongSum(),
metric -> metric.hasName("system.disk.operations").hasUnit("operations").hasLongSum());
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
7 changes: 7 additions & 0 deletions instrumentation/oshi/testing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("otel.java-conventions")
}

dependencies {
api(project(":testing-common"))
}
Loading

0 comments on commit e9c4be6

Please sign in to comment.