Skip to content

Commit

Permalink
Allows passing an OpenTelemetry instance to registerObservers() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenarros committed Mar 30, 2022
1 parent 32c3f45 commit c1d75a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.oshi;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
Expand All @@ -21,8 +22,10 @@ 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("io.opentelemetry.oshi");
registerObservers(GlobalOpenTelemetry.get());
}
public static void registerObservers(OpenTelemetry openTelemetry) {
Meter meter = openTelemetry.getMeterProvider().get("io.opentelemetry.oshi");
SystemInfo systemInfo = new SystemInfo();
OperatingSystem osInfo = systemInfo.getOperatingSystem();
OSProcess processInfo = osInfo.getProcess(osInfo.getProcessId());
Expand All @@ -49,4 +52,4 @@ public static void registerObservers() {
r.record(processInfo.getKernelTime(), Attributes.of(TYPE_KEY, "system"));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.oshi;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
Expand All @@ -29,8 +30,10 @@ 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.oshi");
registerObservers(GlobalOpenTelemetry.get());
}
public static void registerObservers(OpenTelemetry openTelemetry) {
Meter meter = openTelemetry.getMeterProvider().get("io.opentelemetry.oshi");
SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hal = systemInfo.getHardware();

Expand Down Expand Up @@ -136,4 +139,4 @@ public static void registerObservers() {
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.runtimemetrics;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
Expand Down Expand Up @@ -35,10 +36,13 @@ public final class GarbageCollector {

/** Register all observers provided by this module. */
public static void registerObservers() {
// TODO(anuraaga): registerObservers should accept an OpenTelemetry instance
registerObservers(GlobalOpenTelemetry.get());
}

public static void registerObservers(OpenTelemetry openTelemetry) {
List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
Meter meter =
GlobalOpenTelemetry.get().getMeterProvider().get(GarbageCollector.class.getName());
openTelemetry.getMeterProvider().get(GarbageCollector.class.getName());
List<Attributes> labelSets = new ArrayList<>(garbageCollectors.size());
for (GarbageCollectorMXBean gc : garbageCollectors) {
labelSets.add(Attributes.of(GC_KEY, gc.getName()));
Expand Down Expand Up @@ -69,4 +73,4 @@ public static void registerObservers() {
}

private GarbageCollector() {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.runtimemetrics;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
Expand Down Expand Up @@ -59,9 +60,12 @@ public final class MemoryPools {

/** Register only the "area" measurements. */
public static void registerMemoryAreaObservers() {
// TODO(anuraaga): registerObservers should accept an OpenTelemetry instance
registerMemoryPoolObservers(GlobalOpenTelemetry.get());
}

public static void registerMemoryAreaObservers(OpenTelemetry openTelemetry) {
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
Meter meter = GlobalOpenTelemetry.get().getMeterProvider().get(MemoryPools.class.getName());
Meter meter = openTelemetry.getMeterProvider().get(MemoryPools.class.getName());
meter
.upDownCounterBuilder("runtime.jvm.memory.area")
.setDescription("Bytes of a given JVM memory area.")
Expand All @@ -75,9 +79,12 @@ public static void registerMemoryAreaObservers() {

/** Register only the "pool" measurements. */
public static void registerMemoryPoolObservers() {
// TODO(anuraaga): registerObservers should accept an OpenTelemetry instance
registerMemoryPoolObservers(GlobalOpenTelemetry.get());
}

public static void registerMemoryPoolObservers(OpenTelemetry openTelemetry) {
List<MemoryPoolMXBean> poolBeans = ManagementFactory.getMemoryPoolMXBeans();
Meter meter = GlobalOpenTelemetry.get().getMeterProvider().get(MemoryPools.class.getName());
Meter meter = openTelemetry.getMeterProvider().get(MemoryPools.class.getName());
List<Attributes> usedLabelSets = new ArrayList<>(poolBeans.size());
List<Attributes> committedLabelSets = new ArrayList<>(poolBeans.size());
List<Attributes> maxLabelSets = new ArrayList<>(poolBeans.size());
Expand Down Expand Up @@ -112,6 +119,12 @@ public static void registerObservers() {
registerMemoryPoolObservers();
}

/** Register all measurements provided by this module. */
public static void registerObservers(OpenTelemetry openTelemetry) {
registerMemoryAreaObservers(openTelemetry);
registerMemoryPoolObservers(openTelemetry);
}

static void recordHeap(ObservableLongMeasurement measurement, MemoryUsage usage) {
record(measurement, usage, USED_HEAP, COMMITTED_HEAP, MAX_HEAP);
}
Expand Down Expand Up @@ -139,4 +152,4 @@ private static void record(
}

private MemoryPools() {}
}
}

0 comments on commit c1d75a6

Please sign in to comment.