Use uriTemplate attribute in metrics (#1422)
This commit is contained in:
committed by
GitHub
parent
2890f5f682
commit
ac59d1335b
@@ -506,6 +506,12 @@ set the value of the `spring.cloud.loadbalancer.stats.micrometer.enabled` to `tr
|
||||
|
||||
Additional information regarding the service instances, request data, and response data is added to metrics via tags whenever available.
|
||||
|
||||
NOTE: For `WebClient` and `RestClient`-backed load-balancing, we use `uriTemplate` for the `uri` tag whenever available.
|
||||
|
||||
TIP: It is possible to disable adding `path` to `uri` tag by setting `spring.cloud.loadbalancer.stats.include-path` to `false`.
|
||||
|
||||
WARNING: As with `RestTemplate`-backed load-balancing, we don't have access to `uriTemplate`, full path is always used in the `uri` tag. In order to avoid high cardinality issues, if path is a high cardinality value (for example, `/orders/\{id\}`, where `id` takes a big number of values), it is strongly recommended to disable adding path to `uri` tag by setting `spring.cloud.loadbalancer.stats.include-path` to `false`.
|
||||
|
||||
NOTE: For some implementations, such as `BlockingLoadBalancerClient`, request and response data might not be available, as we establish generic types from arguments and might not be able to determine the types and read the data.
|
||||
|
||||
NOTE: The meters are registered in the registry when at least one record is added for a given meter.
|
||||
|
||||
@@ -66,6 +66,7 @@
|
||||
|spring.cloud.loadbalancer.retry.retryable-exceptions | `+++{}+++` | A `Set` of `Throwable` classes that should trigger a retry.
|
||||
|spring.cloud.loadbalancer.retry.retryable-status-codes | `+++{}+++` | A `Set` of status codes that should trigger a retry.
|
||||
|spring.cloud.loadbalancer.service-discovery.timeout | | String representation of Duration of the timeout for calls to service discovery.
|
||||
|spring.cloud.loadbalancer.stats.include-path | `+++true+++` | Indicates whether the {@code path} should be added to {@code uri} tag in metrics. When {@link RestTemplate} is used to execute load-balanced requests with high cardinality paths, setting it to {@code false} is recommended.
|
||||
|spring.cloud.loadbalancer.stats.micrometer.enabled | `+++false+++` | Enables Spring Cloud LoadBalancer Micrometer stats.
|
||||
|spring.cloud.loadbalancer.sticky-session.add-service-instance-cookie | `+++false+++` | Indicates whether a cookie with the newly selected instance should be added by LoadBalancer.
|
||||
|spring.cloud.loadbalancer.sticky-session.instance-id-cookie-name | `+++sc-lb-instance-id+++` | The name of the cookie holding the preferred instance id.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import org.springframework.cloud.commons.util.IdUtils;
|
||||
import org.springframework.core.env.PropertyResolver;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* The base configuration bean for Spring Cloud LoadBalancer.
|
||||
@@ -90,10 +91,20 @@ public class LoadBalancerProperties {
|
||||
|
||||
/**
|
||||
* Properties for
|
||||
* {@link org.springframework.cloud.loadbalancer.core.SubsetServiceInstanceListSupplier}.
|
||||
* {@code org.springframework.cloud.loadbalancer.core.SubsetServiceInstanceListSupplier}.
|
||||
*/
|
||||
private Subset subset = new Subset();
|
||||
|
||||
/**
|
||||
* Enabling X-Forwarded Host and Proto Headers.
|
||||
*/
|
||||
private XForwarded xForwarded = new XForwarded();
|
||||
|
||||
/**
|
||||
* Properties for LoadBalancer metrics.
|
||||
*/
|
||||
private Stats stats = new Stats();
|
||||
|
||||
public HealthCheck getHealthCheck() {
|
||||
return healthCheck;
|
||||
}
|
||||
@@ -134,11 +145,6 @@ public class LoadBalancerProperties {
|
||||
this.hintHeaderName = hintHeaderName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enabling X-Forwarded Host and Proto Headers.
|
||||
*/
|
||||
private XForwarded xForwarded = new XForwarded();
|
||||
|
||||
// TODO: fix spelling in a major release
|
||||
public void setxForwarded(XForwarded xForwarded) {
|
||||
this.xForwarded = xForwarded;
|
||||
@@ -164,6 +170,14 @@ public class LoadBalancerProperties {
|
||||
this.callGetWithRequestOnDelegates = callGetWithRequestOnDelegates;
|
||||
}
|
||||
|
||||
public Stats getStats() {
|
||||
return stats;
|
||||
}
|
||||
|
||||
public void setStats(Stats stats) {
|
||||
this.stats = stats;
|
||||
}
|
||||
|
||||
public static class StickySession {
|
||||
|
||||
/**
|
||||
@@ -539,4 +553,23 @@ public class LoadBalancerProperties {
|
||||
|
||||
}
|
||||
|
||||
public static class Stats {
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code path} should be added to {@code uri} tag in
|
||||
* metrics. When {@link RestTemplate} is used to execute load-balanced requests
|
||||
* with high cardinality paths, setting it to {@code false} is recommended.
|
||||
*/
|
||||
private boolean includePath = true;
|
||||
|
||||
public boolean isIncludePath() {
|
||||
return includePath;
|
||||
}
|
||||
|
||||
public void setIncludePath(boolean includePath) {
|
||||
this.includePath = includePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,8 @@ import io.micrometer.core.instrument.MeterRegistry;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.stats.MicrometerStatsLoadBalancerLifecycle;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -38,8 +40,9 @@ public class LoadBalancerStatsAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(MeterRegistry.class)
|
||||
public MicrometerStatsLoadBalancerLifecycle micrometerStatsLifecycle(MeterRegistry meterRegistry) {
|
||||
return new MicrometerStatsLoadBalancerLifecycle(meterRegistry);
|
||||
public MicrometerStatsLoadBalancerLifecycle micrometerStatsLifecycle(MeterRegistry meterRegistry,
|
||||
ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerFactory) {
|
||||
return new MicrometerStatsLoadBalancerLifecycle(meterRegistry, loadBalancerFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,17 @@
|
||||
|
||||
package org.springframework.cloud.loadbalancer.stats;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.CompletionContext;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.RequestData;
|
||||
import org.springframework.cloud.client.loadbalancer.RequestDataContext;
|
||||
import org.springframework.cloud.client.loadbalancer.ResponseData;
|
||||
@@ -30,17 +36,25 @@ import org.springframework.util.StringUtils;
|
||||
* Utility class for building metrics tags for load-balanced calls.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Jaroslaw Dembek
|
||||
* @since 3.0.0
|
||||
*/
|
||||
final class LoadBalancerTags {
|
||||
class LoadBalancerTags {
|
||||
|
||||
static final String UNKNOWN = "UNKNOWN";
|
||||
|
||||
private LoadBalancerTags() {
|
||||
throw new UnsupportedOperationException("Cannot instantiate utility class");
|
||||
private final LoadBalancerProperties properties;
|
||||
|
||||
// Not using class references in case not in classpath
|
||||
private static final Set<String> URI_TEMPLATE_ATTRIBUTES = Set.of(
|
||||
"org.springframework.web.reactive.function.client.WebClient.uriTemplate",
|
||||
"org.springframework.web.client.RestClient.uriTemplate");
|
||||
|
||||
LoadBalancerTags(LoadBalancerProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
static Iterable<Tag> buildSuccessRequestTags(CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
Iterable<Tag> buildSuccessRequestTags(CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
ServiceInstance serviceInstance = completionContext.getLoadBalancerResponse().getServer();
|
||||
Tags tags = Tags.of(buildServiceInstanceTags(serviceInstance));
|
||||
Object clientResponse = completionContext.getClientResponse();
|
||||
@@ -69,12 +83,23 @@ final class LoadBalancerTags {
|
||||
return responseData.getHttpStatus() != null ? responseData.getHttpStatus().value() : 200;
|
||||
}
|
||||
|
||||
private static String getPath(RequestData requestData) {
|
||||
return requestData.getUrl() != null ? requestData.getUrl().getPath() : UNKNOWN;
|
||||
private String getPath(RequestData requestData) {
|
||||
if (!properties.getStats().isIncludePath()) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
Optional<Object> uriTemplateValue = Optional.ofNullable(requestData.getAttributes())
|
||||
.orElse(Collections.emptyMap())
|
||||
.keySet()
|
||||
.stream()
|
||||
.filter(URI_TEMPLATE_ATTRIBUTES::contains)
|
||||
.map(key -> requestData.getAttributes().get(key))
|
||||
.filter(Objects::nonNull)
|
||||
.findAny();
|
||||
return uriTemplateValue.map(uriTemplate -> (String) uriTemplate)
|
||||
.orElseGet(() -> (requestData.getUrl() != null) ? requestData.getUrl().getPath() : UNKNOWN);
|
||||
}
|
||||
|
||||
static Iterable<Tag> buildDiscardedRequestTags(
|
||||
CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
Iterable<Tag> buildDiscardedRequestTags(CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
if (completionContext.getLoadBalancerRequest().getContext() instanceof RequestDataContext) {
|
||||
RequestData requestData = ((RequestDataContext) completionContext.getLoadBalancerRequest().getContext())
|
||||
.getClientRequest();
|
||||
@@ -92,7 +117,7 @@ final class LoadBalancerTags {
|
||||
return requestData.getUrl() != null ? requestData.getUrl().getHost() : UNKNOWN;
|
||||
}
|
||||
|
||||
static Iterable<Tag> buildFailedRequestTags(CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
Iterable<Tag> buildFailedRequestTags(CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
ServiceInstance serviceInstance = completionContext.getLoadBalancerResponse().getServer();
|
||||
Tags tags = Tags.of(buildServiceInstanceTags(serviceInstance)).and(exception(completionContext.getThrowable()));
|
||||
if (completionContext.getLoadBalancerRequest().getContext() instanceof RequestDataContext) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,31 +27,50 @@ import io.micrometer.core.instrument.Timer;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.CompletionContext;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClientsProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerLifecycle;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.Response;
|
||||
import org.springframework.cloud.client.loadbalancer.TimedRequestContext;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||
|
||||
import static org.springframework.cloud.loadbalancer.stats.LoadBalancerTags.buildDiscardedRequestTags;
|
||||
import static org.springframework.cloud.loadbalancer.stats.LoadBalancerTags.buildFailedRequestTags;
|
||||
import static org.springframework.cloud.loadbalancer.stats.LoadBalancerTags.buildServiceInstanceTags;
|
||||
import static org.springframework.cloud.loadbalancer.stats.LoadBalancerTags.buildSuccessRequestTags;
|
||||
|
||||
/**
|
||||
* An implementation of {@link LoadBalancerLifecycle} that records metrics for
|
||||
* load-balanced calls.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Jaroslaw Dembek
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public class MicrometerStatsLoadBalancerLifecycle implements LoadBalancerLifecycle<Object, Object, ServiceInstance> {
|
||||
|
||||
private final MeterRegistry meterRegistry;
|
||||
|
||||
private final ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerFactory;
|
||||
|
||||
private final ConcurrentHashMap<ServiceInstance, AtomicLong> activeRequestsPerInstance = new ConcurrentHashMap<>();
|
||||
|
||||
public MicrometerStatsLoadBalancerLifecycle(MeterRegistry meterRegistry) {
|
||||
public MicrometerStatsLoadBalancerLifecycle(MeterRegistry meterRegistry,
|
||||
ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerFactory) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
this.loadBalancerFactory = loadBalancerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a MicrometerStatsLoadBalancerLifecycle instance based on the provided
|
||||
* {@link MeterRegistry}.
|
||||
* @param meterRegistry {@link MeterRegistry} to use for Micrometer metrics.
|
||||
* @deprecated in favour of
|
||||
* {@link MicrometerStatsLoadBalancerLifecycle#MicrometerStatsLoadBalancerLifecycle(MeterRegistry, ReactiveLoadBalancer.Factory)}
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public MicrometerStatsLoadBalancerLifecycle(MeterRegistry meterRegistry) {
|
||||
// use default properties when calling deprecated constructor
|
||||
this(meterRegistry, new LoadBalancerClientFactory(new LoadBalancerClientsProperties()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,15 +104,19 @@ public class MicrometerStatsLoadBalancerLifecycle implements LoadBalancerLifecyc
|
||||
|
||||
@Override
|
||||
public void onComplete(CompletionContext<Object, ServiceInstance, Object> completionContext) {
|
||||
ServiceInstance serviceInstance = completionContext.getLoadBalancerResponse().getServer();
|
||||
LoadBalancerProperties properties = serviceInstance != null
|
||||
? loadBalancerFactory.getProperties(serviceInstance.getServiceId())
|
||||
: loadBalancerFactory.getProperties(null);
|
||||
LoadBalancerTags loadBalancerTags = new LoadBalancerTags(properties);
|
||||
long requestFinishedTimestamp = System.nanoTime();
|
||||
if (CompletionContext.Status.DISCARD.equals(completionContext.status())) {
|
||||
Counter.builder("loadbalancer.requests.discard")
|
||||
.tags(buildDiscardedRequestTags(completionContext))
|
||||
.tags(loadBalancerTags.buildDiscardedRequestTags(completionContext))
|
||||
.register(meterRegistry)
|
||||
.increment();
|
||||
return;
|
||||
}
|
||||
ServiceInstance serviceInstance = completionContext.getLoadBalancerResponse().getServer();
|
||||
AtomicLong activeRequestsCounter = activeRequestsPerInstance.get(serviceInstance);
|
||||
if (activeRequestsCounter != null) {
|
||||
activeRequestsCounter.decrementAndGet();
|
||||
@@ -102,7 +125,7 @@ public class MicrometerStatsLoadBalancerLifecycle implements LoadBalancerLifecyc
|
||||
if (requestHasBeenTimed(loadBalancerRequestContext)) {
|
||||
if (CompletionContext.Status.FAILED.equals(completionContext.status())) {
|
||||
Timer.builder("loadbalancer.requests.failed")
|
||||
.tags(buildFailedRequestTags(completionContext))
|
||||
.tags(loadBalancerTags.buildFailedRequestTags(completionContext))
|
||||
.register(meterRegistry)
|
||||
.record(requestFinishedTimestamp
|
||||
- ((TimedRequestContext) loadBalancerRequestContext).getRequestStartTime(),
|
||||
@@ -110,7 +133,7 @@ public class MicrometerStatsLoadBalancerLifecycle implements LoadBalancerLifecyc
|
||||
return;
|
||||
}
|
||||
Timer.builder("loadbalancer.requests.success")
|
||||
.tags(buildSuccessRequestTags(completionContext))
|
||||
.tags(loadBalancerTags.buildSuccessRequestTags(completionContext))
|
||||
.register(meterRegistry)
|
||||
.record(requestFinishedTimestamp
|
||||
- ((TimedRequestContext) loadBalancerRequestContext).getRequestStartTime(),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,11 +18,14 @@ package org.springframework.cloud.loadbalancer.stats;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
@@ -31,26 +34,35 @@ import org.springframework.cloud.client.loadbalancer.DefaultRequest;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultRequestContext;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.RequestData;
|
||||
import org.springframework.cloud.client.loadbalancer.RequestDataContext;
|
||||
import org.springframework.cloud.client.loadbalancer.Response;
|
||||
import org.springframework.cloud.client.loadbalancer.ResponseData;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.MultiValueMapAdapter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.loadbalancer.stats.LoadBalancerTags.UNKNOWN;
|
||||
|
||||
/**
|
||||
* Tests for {@link MicrometerStatsLoadBalancerLifecycle}.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Jaroslaw Dembek
|
||||
*/
|
||||
class MicrometerStatsLoadBalancerLifecycleTests {
|
||||
|
||||
private static final String WEB_CLIENT_URI_TEMPLATE_ATTRIBUTE = "org.springframework.web.reactive.function.client.WebClient.uriTemplate";
|
||||
|
||||
private static final String REST_CLIENT_URI_TEMPLATE_ATTRIBUTE = "org.springframework.web.reactive.function.client.WebClient.uriTemplate";
|
||||
|
||||
MeterRegistry meterRegistry = new SimpleMeterRegistry();
|
||||
|
||||
MicrometerStatsLoadBalancerLifecycle statsLifecycle = new MicrometerStatsLoadBalancerLifecycle(meterRegistry);
|
||||
@@ -80,6 +92,62 @@ class MicrometerStatsLoadBalancerLifecycleTests {
|
||||
Tag.of("serviceInstance.port", "8080"), Tag.of("status", "200"), Tag.of("uri", "/test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAddPathValueWhenDisabled() {
|
||||
ReactiveLoadBalancer.Factory<ServiceInstance> factory = mock(ReactiveLoadBalancer.Factory.class);
|
||||
LoadBalancerProperties properties = new LoadBalancerProperties();
|
||||
properties.getStats().setIncludePath(false);
|
||||
when(factory.getProperties("test")).thenReturn(properties);
|
||||
MicrometerStatsLoadBalancerLifecycle statsLifecycle = new MicrometerStatsLoadBalancerLifecycle(meterRegistry,
|
||||
factory);
|
||||
RequestData requestData = new RequestData(HttpMethod.GET, URI.create("http://test.org/test"), new HttpHeaders(),
|
||||
new HttpHeaders(), new HashMap<>());
|
||||
Request<Object> lbRequest = new DefaultRequest<>(new RequestDataContext(requestData));
|
||||
Response<ServiceInstance> lbResponse = new DefaultResponse(
|
||||
new DefaultServiceInstance("test-1", "test", "test.org", 8080, false, new HashMap<>()));
|
||||
ResponseData responseData = new ResponseData(HttpStatus.OK, new HttpHeaders(),
|
||||
new MultiValueMapAdapter<>(new HashMap<>()), requestData);
|
||||
statsLifecycle.onStartRequest(lbRequest, lbResponse);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.active").gauge().value()).isEqualTo(1);
|
||||
|
||||
statsLifecycle
|
||||
.onComplete(new CompletionContext<>(CompletionContext.Status.SUCCESS, lbRequest, lbResponse, responseData));
|
||||
|
||||
assertThat(meterRegistry.getMeters()).hasSize(2);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.success").timer().getId().getTags())
|
||||
.doesNotContain(Tag.of("uri", "/test"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { WEB_CLIENT_URI_TEMPLATE_ATTRIBUTE, REST_CLIENT_URI_TEMPLATE_ATTRIBUTE })
|
||||
void shouldRecordSuccessfulTimedRequestWithUriTemplate(String attributeName) {
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
String uriTemplate = "/test/{pathParam}/test";
|
||||
attributes.put(attributeName, uriTemplate);
|
||||
RequestData requestData = new RequestData(HttpMethod.GET, URI.create("http://test.org/test/123/test"),
|
||||
new HttpHeaders(), new HttpHeaders(), attributes);
|
||||
Request<Object> lbRequest = new DefaultRequest<>(new RequestDataContext(requestData));
|
||||
Response<ServiceInstance> lbResponse = new DefaultResponse(
|
||||
new DefaultServiceInstance("test-1", "test", "test.org", 8080, false, new HashMap<>()));
|
||||
ResponseData responseData = new ResponseData(HttpStatus.OK, new HttpHeaders(),
|
||||
new MultiValueMapAdapter<>(new HashMap<>()), requestData);
|
||||
statsLifecycle.onStartRequest(lbRequest, lbResponse);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.active").gauge().value()).isEqualTo(1);
|
||||
|
||||
statsLifecycle
|
||||
.onComplete(new CompletionContext<>(CompletionContext.Status.SUCCESS, lbRequest, lbResponse, responseData));
|
||||
|
||||
assertThat(meterRegistry.getMeters()).hasSize(2);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.active").gauge().value()).isEqualTo(0);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.success").timers()).hasSize(1);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.success").timer().count()).isEqualTo(1);
|
||||
assertThat(meterRegistry.get("loadbalancer.requests.success").timer().getId().getTags())
|
||||
.containsExactlyInAnyOrder(Tag.of("method", "GET"), Tag.of("outcome", "SUCCESS"),
|
||||
Tag.of("serviceId", "test"), Tag.of("serviceInstance.host", "test.org"),
|
||||
Tag.of("serviceInstance.instanceId", "test-1"), Tag.of("serviceInstance.port", "8080"),
|
||||
Tag.of("status", "200"), Tag.of("uri", uriTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecordFailedTimedRequest() {
|
||||
RequestData requestData = new RequestData(HttpMethod.GET, URI.create("http://test.org/test"), new HttpHeaders(),
|
||||
|
||||
Reference in New Issue
Block a user