|
19 | 19 |
|
20 | 20 | package org.apache.spark.k8s.operator.metrics.source; |
21 | 21 |
|
| 22 | +import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; |
| 23 | +import static org.awaitility.Awaitility.await; |
22 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 25 |
|
| 26 | +import java.nio.ByteBuffer; |
24 | 27 | import java.util.Arrays; |
25 | 28 | import java.util.HashMap; |
26 | 29 | import java.util.List; |
27 | 30 | import java.util.Map; |
28 | 31 |
|
| 32 | +import com.codahale.metrics.Histogram; |
29 | 33 | import com.codahale.metrics.Meter; |
30 | 34 | import com.codahale.metrics.Metric; |
31 | 35 | import io.fabric8.kubernetes.api.model.ConfigMap; |
32 | 36 | import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 37 | +import io.fabric8.kubernetes.client.Config; |
| 38 | +import io.fabric8.kubernetes.client.ConfigBuilder; |
33 | 39 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 40 | +import io.fabric8.kubernetes.client.http.AsyncBody; |
34 | 41 | import io.fabric8.kubernetes.client.http.Interceptor; |
| 42 | +import io.fabric8.kubernetes.client.informers.SharedIndexInformer; |
35 | 43 | import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; |
36 | 44 | import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; |
37 | 45 | import org.junit.jupiter.api.AfterEach; |
|
50 | 58 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
51 | 59 | class KubernetesMetricsInterceptorTest { |
52 | 60 |
|
| 61 | + private static final String CONFIG_MAP_PATH = |
| 62 | + "/api/v1/namespaces/spark-system/configmaps/spark-job-operator-configuration"; |
| 63 | + |
53 | 64 | private KubernetesMockServer mockServer; |
54 | 65 | private KubernetesClient kubernetesClient; |
55 | 66 |
|
@@ -125,6 +136,103 @@ void testWhenKubernetesServerNotWorking() { |
125 | 136 | } |
126 | 137 | } |
127 | 138 |
|
| 139 | + @Test |
| 140 | + @Order(3) |
| 141 | + void testResponseLatency() { |
| 142 | + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); |
| 143 | + List<Interceptor> interceptors = List.of(metricsInterceptor); |
| 144 | + try (KubernetesClient client = |
| 145 | + KubernetesClientFactory.buildKubernetesClient( |
| 146 | + interceptors, kubernetesClient.getConfiguration())) { |
| 147 | + ConfigMap configMap = createConfigMap(); |
| 148 | + mockServer |
| 149 | + .expect() |
| 150 | + .get() |
| 151 | + .delay(50) |
| 152 | + .withPath(CONFIG_MAP_PATH) |
| 153 | + .andReturn(200, configMap) |
| 154 | + .once(); |
| 155 | + client.resource(configMap).get(); |
| 156 | + |
| 157 | + Map<String, Metric> map = metricsInterceptor.metricRegistry().getMetrics(); |
| 158 | + Histogram latency = (Histogram) map.get("http.response.latency.nanos"); |
| 159 | + Assertions.assertEquals(1, latency.getCount()); |
| 160 | + Assertions.assertTrue(latency.getSnapshot().getMax() >= 50_000_000L); |
| 161 | + Assertions.assertTrue(latency.getSnapshot().getMax() < 30_000_000_000L); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + @Order(4) |
| 167 | + void testResponseLatencyOfResentRequest() { |
| 168 | + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); |
| 169 | + List<Interceptor> interceptors = List.of(metricsInterceptor); |
| 170 | + Config config = |
| 171 | + new ConfigBuilder(kubernetesClient.getConfiguration()) |
| 172 | + .withOauthTokenProvider(() -> "token") |
| 173 | + .build(); |
| 174 | + try (KubernetesClient client = |
| 175 | + KubernetesClientFactory.buildKubernetesClient(interceptors, config)) { |
| 176 | + ConfigMap configMap = createConfigMap(); |
| 177 | + mockServer |
| 178 | + .expect() |
| 179 | + .get() |
| 180 | + .delay(200) |
| 181 | + .withPath(CONFIG_MAP_PATH) |
| 182 | + .andReturn(HTTP_UNAUTHORIZED, null) |
| 183 | + .once(); |
| 184 | + mockServer.expect().get().withPath(CONFIG_MAP_PATH).andReturn(200, configMap).once(); |
| 185 | + client.resource(configMap).get(); |
| 186 | + |
| 187 | + Assertions.assertEquals(2, meterCount(metricsInterceptor, "http.response")); |
| 188 | + Assertions.assertEquals(1, meterCount(metricsInterceptor, "http.response.401")); |
| 189 | + Assertions.assertEquals(0, meterCount(metricsInterceptor, "failed")); |
| 190 | + Map<String, Metric> map = metricsInterceptor.metricRegistry().getMetrics(); |
| 191 | + Histogram latency = (Histogram) map.get("http.response.latency.nanos"); |
| 192 | + Assertions.assertEquals(2, latency.getCount()); |
| 193 | + Assertions.assertTrue(latency.getSnapshot().getMax() >= 200_000_000L); |
| 194 | + Assertions.assertTrue(latency.getSnapshot().getMin() < 200_000_000L); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + @Order(5) |
| 200 | + void testResponseLatencyOfWebSocketUpgrade() { |
| 201 | + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); |
| 202 | + List<Interceptor> interceptors = List.of(metricsInterceptor); |
| 203 | + try (KubernetesClient client = |
| 204 | + KubernetesClientFactory.buildKubernetesClient( |
| 205 | + interceptors, kubernetesClient.getConfiguration()); |
| 206 | + SharedIndexInformer<ConfigMap> informer = |
| 207 | + client.configMaps().inNamespace("spark-system").inform()) { |
| 208 | + Map<String, Metric> map = metricsInterceptor.metricRegistry().getMetrics(); |
| 209 | + await() |
| 210 | + .untilAsserted( |
| 211 | + () -> { |
| 212 | + Assertions.assertEquals(2, meterCount(metricsInterceptor, "http.response")); |
| 213 | + Assertions.assertEquals(1, meterCount(metricsInterceptor, "http.response.101")); |
| 214 | + Assertions.assertEquals( |
| 215 | + 1, ((Histogram) map.get("http.response.latency.nanos")).getCount()); |
| 216 | + }); |
| 217 | + Assertions.assertTrue(informer.isWatching()); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + @Order(6) |
| 223 | + void testConsumerUnwrapsDelegate() { |
| 224 | + AsyncBody.Consumer<List<ByteBuffer>> delegate = (value, asyncBody) -> {}; |
| 225 | + AsyncBody.Consumer<List<ByteBuffer>> consumer = |
| 226 | + new KubernetesMetricsInterceptor().consumer(delegate, null); |
| 227 | + Assertions.assertSame(delegate, consumer.unwrap(delegate.getClass())); |
| 228 | + } |
| 229 | + |
| 230 | + private static long meterCount(KubernetesMetricsInterceptor interceptor, String name) { |
| 231 | + Meter meter = interceptor.metricRegistry().getMeters().get(name); |
| 232 | + Assertions.assertNotNull(meter, name); |
| 233 | + return meter.getCount(); |
| 234 | + } |
| 235 | + |
128 | 236 | private static SparkApplication createSparkApplication() { |
129 | 237 | ObjectMeta meta = new ObjectMeta(); |
130 | 238 | meta.setName("sample-spark-application"); |
|
0 commit comments