diff --git a/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/CounterMetric.java b/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/CounterMetric.java index ea800fb29..51d167340 100644 --- a/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/CounterMetric.java +++ b/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/CounterMetric.java @@ -29,9 +29,11 @@ public CounterMetric(MetricsConfig metricsConfig, Attributes extraAttributes, Lo this.longCounter = longCounter; } - public void add(MetricsLevel metricsLevel, long value) { - if (metricsLevel.isWithin(metricsLevel)) { + public boolean add(MetricsLevel metricsLevel, long value) { + if (metricsLevel.isWithin(this.metricsLevel)) { longCounter.add(value, attributes); + return true; } + return false; } } diff --git a/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/HistogramMetric.java b/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/HistogramMetric.java index 671763de8..b923a8839 100644 --- a/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/HistogramMetric.java +++ b/s3stream/src/main/java/com/automq/stream/s3/metrics/wrapper/HistogramMetric.java @@ -28,9 +28,11 @@ public HistogramMetric(MetricsConfig metricsConfig, Attributes extraAttributes, this.longHistogram = longHistogram; } - public void record(MetricsLevel metricsLevel, long value) { - if (metricsLevel.isWithin(metricsLevel)) { + public boolean record(MetricsLevel metricsLevel, long value) { + if (metricsLevel.isWithin(this.metricsLevel)) { longHistogram.record(value, attributes); + return true; } + return false; } } diff --git a/s3stream/src/test/java/com/automq/stream/s3/metrics/MetricsLevelTest.java b/s3stream/src/test/java/com/automq/stream/s3/metrics/MetricsLevelTest.java new file mode 100644 index 000000000..f2ef07f59 --- /dev/null +++ b/s3stream/src/test/java/com/automq/stream/s3/metrics/MetricsLevelTest.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.automq.stream.s3.metrics; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class MetricsLevelTest { + + @Test + public void testIsWithin() { + Assertions.assertTrue(MetricsLevel.INFO.isWithin(MetricsLevel.INFO)); + Assertions.assertFalse(MetricsLevel.DEBUG.isWithin(MetricsLevel.INFO)); + + Assertions.assertTrue(MetricsLevel.INFO.isWithin(MetricsLevel.DEBUG)); + Assertions.assertTrue(MetricsLevel.DEBUG.isWithin(MetricsLevel.DEBUG)); + } +} diff --git a/s3stream/src/test/java/com/automq/stream/s3/metrics/wrapper/MetricsWrapperTest.java b/s3stream/src/test/java/com/automq/stream/s3/metrics/wrapper/MetricsWrapperTest.java new file mode 100644 index 000000000..7512a0a7d --- /dev/null +++ b/s3stream/src/test/java/com/automq/stream/s3/metrics/wrapper/MetricsWrapperTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.automq.stream.s3.metrics.wrapper; + +import com.automq.stream.s3.metrics.MetricsConfig; +import com.automq.stream.s3.metrics.MetricsLevel; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.LongHistogram; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class MetricsWrapperTest { + + @Test + public void testConfigurableMetrics() { + CounterMetric metric = new CounterMetric(new MetricsConfig(), Attributes.builder().put("extra", "v").build(), + Mockito.mock(LongCounter.class)); + Assertions.assertEquals(MetricsLevel.INFO, metric.metricsLevel); + + metric.onConfigChange(new MetricsConfig(MetricsLevel.DEBUG, Attributes.builder().put("base", "v2").build())); + Assertions.assertEquals(MetricsLevel.DEBUG, metric.metricsLevel); + Assertions.assertEquals(Attributes.builder().put("extra", "v").put("base", "v2").build(), metric.attributes); + + HistogramMetric histogramMetric = new HistogramMetric(new MetricsConfig(), Attributes.builder().put("extra", "v").build(), + Mockito.mock(LongHistogram.class)); + Assertions.assertEquals(MetricsLevel.INFO, histogramMetric.metricsLevel); + + histogramMetric.onConfigChange(new MetricsConfig(MetricsLevel.DEBUG, Attributes.builder().put("base", "v2").build())); + Assertions.assertEquals(MetricsLevel.DEBUG, histogramMetric.metricsLevel); + Assertions.assertEquals(Attributes.builder().put("extra", "v").put("base", "v2").build(), histogramMetric.attributes); + } + + @Test + public void testMetricsLevel() { + 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)); + Assertions.assertTrue(metric.add(MetricsLevel.INFO, 1)); + Assertions.assertTrue(metric.add(MetricsLevel.DEBUG, 1)); + + HistogramMetric histogramMetric = new HistogramMetric(new MetricsConfig(MetricsLevel.INFO, null), Mockito.mock(LongHistogram.class)); + Assertions.assertTrue(histogramMetric.record(MetricsLevel.INFO, 1)); + Assertions.assertFalse(histogramMetric.record(MetricsLevel.DEBUG, 1)); + histogramMetric.onConfigChange(new MetricsConfig(MetricsLevel.DEBUG, null)); + Assertions.assertTrue(histogramMetric.record(MetricsLevel.INFO, 1)); + Assertions.assertTrue(histogramMetric.record(MetricsLevel.DEBUG, 1)); + } +}