Skip to content

Commit

Permalink
fix(s3stream): fix metrics level check (#954)
Browse files Browse the repository at this point in the history
Signed-off-by: Shichao Nie <[email protected]>
  • Loading branch information
SCNieh authored Mar 5, 2024
1 parent 2b82088 commit dae9460
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit dae9460

Please sign in to comment.