Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ public static void registerInflightWALUploadTasksCountSupplier(

public static CounterMetric buildS3UploadSizeMetric() {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, s3UploadSizeInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, () -> s3UploadSizeInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
}

public static CounterMetric buildS3DownloadSizeMetric() {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, s3DownloadSizeInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, () -> s3DownloadSizeInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
Expand Down Expand Up @@ -435,7 +435,7 @@ public static HistogramMetric buildReadAheadStageTimeMetric(MetricsLevel metrics

public static CounterMetric buildObjectNumMetric() {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, objectNumInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, () -> objectNumInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
Expand All @@ -462,15 +462,15 @@ public static HistogramMetric buildObjectUploadSizeMetric(MetricsLevel metricsLe

public static CounterMetric buildNetworkInboundUsageMetric(ThrottleStrategy strategy) {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, AttributesUtils.buildAttributes(strategy), networkInboundUsageInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, AttributesUtils.buildAttributes(strategy), () -> networkInboundUsageInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
}

public static CounterMetric buildNetworkOutboundUsageMetric(ThrottleStrategy strategy) {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, AttributesUtils.buildAttributes(strategy), networkOutboundUsageInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, AttributesUtils.buildAttributes(strategy), () -> networkOutboundUsageInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
Expand Down Expand Up @@ -523,7 +523,7 @@ public static CounterMetric buildBlockCacheOpsThroughputMetric(String ops) {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, Attributes.builder()
.put(AttributeKey.stringKey("ops"), ops)
.build(), blockCacheOpsThroughput);
.build(), () -> blockCacheOpsThroughput);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
Expand Down Expand Up @@ -561,15 +561,15 @@ public static HistogramMetric buildGetIndexTimeMetric(MetricsLevel metricsLevel,

public static CounterMetric buildCompactionReadSizeMetric() {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, compactionReadSizeInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, () -> compactionReadSizeInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
}

public static CounterMetric buildCompactionWriteSizeMetric() {
synchronized (BASE_ATTRIBUTES_LISTENERS) {
CounterMetric metric = new CounterMetric(metricsConfig, compactionWriteSizeInTotal);
CounterMetric metric = new CounterMetric(metricsConfig, () -> compactionWriteSizeInTotal);
BASE_ATTRIBUTES_LISTENERS.add(metric);
return metric;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongCounter;

import java.util.function.Supplier;

public class CounterMetric extends ConfigurableMetric {
private final LongCounter longCounter;
private final Supplier<LongCounter> longCounterSupplier;

public CounterMetric(MetricsConfig metricsConfig, LongCounter longCounter) {
public CounterMetric(MetricsConfig metricsConfig, Supplier<LongCounter> longCounterSupplier) {
super(metricsConfig, Attributes.empty());
this.longCounter = longCounter;
this.longCounterSupplier = longCounterSupplier;
}

public CounterMetric(MetricsConfig metricsConfig, Attributes extraAttributes, LongCounter longCounter) {
public CounterMetric(MetricsConfig metricsConfig, Attributes extraAttributes, Supplier<LongCounter> longCounterSupplier) {
super(metricsConfig, extraAttributes);
this.longCounter = longCounter;
this.longCounterSupplier = longCounterSupplier;
}

public boolean add(MetricsLevel metricsLevel, long value) {
if (metricsLevel.isWithin(this.metricsLevel)) {
longCounter.add(value, attributes);
longCounterSupplier.get().add(value, attributes);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MetricsWrapperTest {
@Test
public void testConfigurableMetrics() {
CounterMetric metric = new CounterMetric(new MetricsConfig(), Attributes.builder().put("extra", "v").build(),
Mockito.mock(LongCounter.class));
() -> Mockito.mock(LongCounter.class));
Assertions.assertEquals(MetricsLevel.INFO, metric.metricsLevel);

metric.onConfigChange(new MetricsConfig(MetricsLevel.DEBUG, Attributes.builder().put("base", "v2").build()));
Expand All @@ -60,7 +60,7 @@ public void testConfigurableMetrics() {

@Test
public void testMetricsLevel() {
CounterMetric metric = new CounterMetric(new MetricsConfig(MetricsLevel.INFO, null), Mockito.mock(LongCounter.class));
CounterMetric metric = new CounterMetric(new MetricsConfig(MetricsLevel.INFO, null), () -> Mockito.mock(LongCounter.class));
Assertions.assertTrue(metric.add(MetricsLevel.INFO, 1));
Assertions.assertFalse(metric.add(MetricsLevel.DEBUG, 1));
metric.onConfigChange(new MetricsConfig(MetricsLevel.DEBUG, null));
Expand Down