|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.oap.meter.analyzer.v2; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import org.apache.skywalking.oap.meter.analyzer.v2.dsl.Sample; |
| 23 | +import org.apache.skywalking.oap.meter.analyzer.v2.dsl.SampleFamily; |
| 24 | +import org.apache.skywalking.oap.meter.analyzer.v2.dsl.SampleFamilyBuilder; |
| 25 | +import org.apache.skywalking.oap.server.core.analysis.meter.MeterEntity; |
| 26 | +import org.apache.skywalking.oap.server.core.analysis.meter.MeterSystem; |
| 27 | +import org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue; |
| 28 | +import org.apache.skywalking.oap.server.core.analysis.meter.function.BucketedValues; |
| 29 | +import org.apache.skywalking.oap.server.core.analysis.meter.function.PercentileArgument; |
| 30 | +import org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor; |
| 31 | +import org.apache.skywalking.oap.server.core.config.NamingControl; |
| 32 | +import org.apache.skywalking.oap.server.core.config.group.EndpointNameGrouping; |
| 33 | +import org.junit.jupiter.api.AfterAll; |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 37 | +import org.mockito.ArgumentCaptor; |
| 38 | +import org.mockito.Mock; |
| 39 | +import org.mockito.MockedStatic; |
| 40 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 41 | + |
| 42 | +import static com.google.common.collect.ImmutableMap.of; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 46 | +import static org.mockito.ArgumentMatchers.any; |
| 47 | +import static org.mockito.ArgumentMatchers.anyString; |
| 48 | +import static org.mockito.ArgumentMatchers.eq; |
| 49 | +import static org.mockito.Mockito.doNothing; |
| 50 | +import static org.mockito.Mockito.mock; |
| 51 | +import static org.mockito.Mockito.mockStatic; |
| 52 | +import static org.mockito.Mockito.verify; |
| 53 | +import static org.mockito.Mockito.when; |
| 54 | + |
| 55 | +/** |
| 56 | + * End-to-end coverage for Analyzer building histogram / percentile meters when a bucket |
| 57 | + * is labeled {@code le="+Inf"} (Prometheus) or {@code Infinity} (Java). FunctionTest only |
| 58 | + * asserts the MAL expression result; this hits {@link Analyzer#analyse} which parses {@code le} |
| 59 | + * again while assembling {@link BucketedValues}. |
| 60 | + */ |
| 61 | +@ExtendWith(MockitoExtension.class) |
| 62 | +public class AnalyzerHistogramInfTest { |
| 63 | + |
| 64 | + @Mock |
| 65 | + private MeterSystem meterSystem; |
| 66 | + |
| 67 | + @BeforeAll |
| 68 | + public static void setUp() { |
| 69 | + MeterEntity.setNamingControl(new NamingControl(512, 512, 512, new EndpointNameGrouping())); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + public static void tearDown() { |
| 74 | + MeterEntity.setNamingControl(null); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void analysePercentileAcceptsPrometheusPlusInfBucket() { |
| 79 | + final AcceptableValue<PercentileArgument> sink = mock(AcceptableValue.class); |
| 80 | + when(meterSystem.buildMetrics(anyString(), eq(PercentileArgument.class))).thenReturn(sink); |
| 81 | + doNothing().when(meterSystem).create(anyString(), anyString(), any()); |
| 82 | + doNothing().when(meterSystem).doStreamingCalculation(any()); |
| 83 | + |
| 84 | + final Analyzer analyzer = Analyzer.build( |
| 85 | + "http_latency_percentile", |
| 86 | + null, |
| 87 | + "http_request_duration_bucket.histogram().histogram_percentile([75, 99])" |
| 88 | + + ".service(['service'], Layer.GENERAL)", |
| 89 | + meterSystem |
| 90 | + ); |
| 91 | + |
| 92 | + final ImmutableMap<String, SampleFamily> input = of( |
| 93 | + "http_request_duration_bucket", |
| 94 | + SampleFamilyBuilder.newBuilder( |
| 95 | + sample("0.025", 100), |
| 96 | + sample("0.75", 22), |
| 97 | + sample("1.25", 30), |
| 98 | + sample("+Inf", 40) |
| 99 | + ).build() |
| 100 | + ); |
| 101 | + |
| 102 | + try (MockedStatic<MetricsStreamProcessor> ignored = mockStatic(MetricsStreamProcessor.class)) { |
| 103 | + final MetricsStreamProcessor processor = mock(MetricsStreamProcessor.class); |
| 104 | + ignored.when(MetricsStreamProcessor::getInstance).thenReturn(processor); |
| 105 | + assertDoesNotThrow(() -> analyzer.analyse(input)); |
| 106 | + } |
| 107 | + |
| 108 | + final ArgumentCaptor<PercentileArgument> captor = ArgumentCaptor.forClass(PercentileArgument.class); |
| 109 | + verify(sink).accept(any(MeterEntity.class), captor.capture()); |
| 110 | + final BucketedValues buckets = captor.getValue().getBucketedValues(); |
| 111 | + assertArrayEquals( |
| 112 | + new long[] {25L, 750L, 1250L, Long.MAX_VALUE}, |
| 113 | + buckets.getBuckets() |
| 114 | + ); |
| 115 | + assertArrayEquals(new long[] {100L, 22L, 30L, 40L}, buckets.getValues()); |
| 116 | + assertArrayEquals(new int[] {75, 99}, captor.getValue().getRanks()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void analyseHistogramAcceptsJavaInfinityAndNegInfBuckets() { |
| 121 | + final AcceptableValue<BucketedValues> sink = mock(AcceptableValue.class); |
| 122 | + when(meterSystem.buildMetrics(anyString(), eq(BucketedValues.class))).thenReturn(sink); |
| 123 | + doNothing().when(meterSystem).create(anyString(), anyString(), any()); |
| 124 | + doNothing().when(meterSystem).doStreamingCalculation(any()); |
| 125 | + |
| 126 | + final Analyzer analyzer = Analyzer.build( |
| 127 | + "http_latency_histogram", |
| 128 | + null, |
| 129 | + "http_request_duration_bucket.histogram().service(['service'], Layer.GENERAL)", |
| 130 | + meterSystem |
| 131 | + ); |
| 132 | + |
| 133 | + final ImmutableMap<String, SampleFamily> input = of( |
| 134 | + "http_request_duration_bucket", |
| 135 | + SampleFamilyBuilder.newBuilder( |
| 136 | + sample("-Inf", 1), |
| 137 | + sample("0.025", 100), |
| 138 | + sample("Infinity", 120) |
| 139 | + ).build() |
| 140 | + ); |
| 141 | + |
| 142 | + try (MockedStatic<MetricsStreamProcessor> ignored = mockStatic(MetricsStreamProcessor.class)) { |
| 143 | + final MetricsStreamProcessor processor = mock(MetricsStreamProcessor.class); |
| 144 | + ignored.when(MetricsStreamProcessor::getInstance).thenReturn(processor); |
| 145 | + assertDoesNotThrow(() -> analyzer.analyse(input)); |
| 146 | + } |
| 147 | + |
| 148 | + final ArgumentCaptor<BucketedValues> captor = ArgumentCaptor.forClass(BucketedValues.class); |
| 149 | + verify(sink).accept(any(MeterEntity.class), captor.capture()); |
| 150 | + assertArrayEquals( |
| 151 | + new long[] {Long.MIN_VALUE, 25L, Long.MAX_VALUE}, |
| 152 | + captor.getValue().getBuckets() |
| 153 | + ); |
| 154 | + assertEquals(3, captor.getValue().getValues().length); |
| 155 | + } |
| 156 | + |
| 157 | + private static Sample sample(final String le, final double value) { |
| 158 | + return Sample.builder() |
| 159 | + .name("http_request_duration_bucket") |
| 160 | + .labels(of("service", "ds-api", "le", le)) |
| 161 | + .value(value) |
| 162 | + .timestamp(1_700_000_000_000L) |
| 163 | + .build(); |
| 164 | + } |
| 165 | +} |
0 commit comments