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

add runtime metrics for buffer pool #6177

Merged
merged 4 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.runtimemetrics.BufferPools;
import io.opentelemetry.instrumentation.runtimemetrics.Classes;
import io.opentelemetry.instrumentation.runtimemetrics.Cpu;
import io.opentelemetry.instrumentation.runtimemetrics.GarbageCollector;
Expand All @@ -33,6 +34,7 @@ public void afterAgent(Config config, AutoConfiguredOpenTelemetrySdk unused) {
Classes.registerObservers(GlobalOpenTelemetry.get());
Cpu.registerObservers(GlobalOpenTelemetry.get());
MemoryPools.registerObservers(GlobalOpenTelemetry.get());
BufferPools.registerObservers(GlobalOpenTelemetry.get());
laurit marked this conversation as resolved.
Show resolved Hide resolved
Threads.registerObservers(GlobalOpenTelemetry.get());

if (config.getBoolean(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.runtimemetrics;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
import java.lang.management.BufferPoolMXBean;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* Registers measurements that generate metrics about buffer pools.
*
* <p>Example usage:
*
* <pre>{@code
* BufferPools.registerObservers(GlobalOpenTelemetry.get());
* }</pre>
*
* <p>Example metrics being exported:
*
* <pre>
* process.runtime.jvm.buffer.usage.usage{pool="buffer_pool"} 500
* process.runtime.jvm.buffer.usage.max{pool="buffer_pool"} 1500
* process.runtime.jvm.buffer.usage.count{pool="buffer_pool"} 15
* </pre>
*/
public final class BufferPools {
private static final AttributeKey<String> POOL_KEY = AttributeKey.stringKey("pool");

/** Register observers for java runtime buffer pool metrics. */
public static void registerObservers(OpenTelemetry openTelemetry) {

List<BufferPoolMXBean> bufferBeans =
ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
Meter meter = openTelemetry.getMeter("io.opentelemetry.runtime-metrics");

meter
.upDownCounterBuilder("process.runtime.jvm.buffer.usage")
.setDescription("Memory that the Java virtual machine is using for this buffer pool")
.setUnit("By")
.buildWithCallback(callback(bufferBeans, BufferPoolMXBean::getMemoryUsed));

meter
.upDownCounterBuilder("process.runtime.jvm.buffer.max")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use limit per naming conventinos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

.setDescription("Total capacity of the buffers in this pool")
.setUnit("By")
.buildWithCallback(callback(bufferBeans, BufferPoolMXBean::getTotalCapacity));

meter
.upDownCounterBuilder("process.runtime.jvm.buffer.count")
.setDescription("Total capacity of the buffers in this pool")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.setDescription("Total capacity of the buffers in this pool")
.setDescription("The number of buffers in the pool")

See getCount

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

.setUnit("buffers")
.buildWithCallback(callback(bufferBeans, BufferPoolMXBean::getCount));
}

static Consumer<ObservableLongMeasurement> callback(
List<BufferPoolMXBean> bufferPools, Function<BufferPoolMXBean, Long> extractor) {
List<Attributes> attributeSets = new ArrayList<>(bufferPools.size());
for (BufferPoolMXBean pool : bufferPools) {
attributeSets.add(Attributes.builder().put(POOL_KEY, pool.getName()).build());
}
return measurement -> {
for (int i = 0; i < bufferPools.size(); i++) {
Attributes attributes = attributeSets.get(i);
long value = extractor.apply(bufferPools.get(i));
if (value != -1) {
measurement.record(value, attributes);
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.runtimemetrics;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
import java.lang.management.BufferPoolMXBean;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class BufferPoolsTest {
@Spy private ObservableLongMeasurement measurement;
@Mock private BufferPoolMXBean bufferPoolBean;
private List<BufferPoolMXBean> beans;

@BeforeEach
void setup() {
when(bufferPoolBean.getName()).thenReturn("buffer_pool_1");
beans = Arrays.asList(bufferPoolBean);
}

@Test
void callback_Records() {
when(bufferPoolBean.getMemoryUsed()).thenReturn(1L);
Consumer<ObservableLongMeasurement> callback =
BufferPools.callback(beans, BufferPoolMXBean::getMemoryUsed);
callback.accept(measurement);
verify(measurement).record(1, Attributes.builder().put("pool", "buffer_pool_1").build());
}

@Test
void callback_SkipRecord() {
when(bufferPoolBean.getMemoryUsed()).thenReturn(-1L);
Consumer<ObservableLongMeasurement> callback =
BufferPools.callback(beans, BufferPoolMXBean::getMemoryUsed);
callback.accept(measurement);
verify(measurement, never()).record(eq(-1), any());
}
}