private static void zip(List<Tag> list, String... tags) { checkArgument(tags.length % 2 == 0, "tags.length: %s (expected: even)", tags.length); for (int i = 0; i < tags.length;) { list.add(Tag.of(tags[i++], tags[i++])); } }
@Override public void accept(List<Endpoint> endpoints) { final Map<Endpoint, Boolean> endpointsToUpdate = new HashMap<>(); endpoints.forEach(e -> endpointsToUpdate.put(e, true)); endpointGroup.allServers.forEach( conn -> endpointsToUpdate.putIfAbsent(conn.endpoint(), false)); // Update the previously appeared endpoints. healthMap.entrySet().forEach(e -> { final Endpoint authority = e.getKey(); final Boolean healthy = endpointsToUpdate.remove(authority); e.setValue(Boolean.TRUE.equals(healthy)); }); // Process the newly appeared endpoints. endpointsToUpdate.forEach((endpoint, healthy) -> { healthMap.put(endpoint, healthy); final List<Tag> tags = new ArrayList<>(2); tags.add(Tag.of("authority", endpoint.authority())); final String ipAddr = endpoint.hasIpAddr() ? endpoint.ipAddr() : ""; assert ipAddr != null; tags.add(Tag.of("ip", ipAddr)); registry.gauge(idPrefix.name(), idPrefix.tags(tags), this, unused -> healthMap.get(endpoint) ? 1 : 0); }); } }
Tag.of(TAG_SCHEDULER_ID, schedulerId));
@Override public MeterIdPrefix apply(MeterRegistry registry, RequestLog log) { final List<Tag> tags = buildTags(log); // Add the 'httpStatus' tag. final HttpStatus status; if (log.isAvailable(RequestLogAvailability.RESPONSE_HEADERS)) { status = log.status(); } else { status = HttpStatus.UNKNOWN; } tags.add(Tag.of("httpStatus", status.codeAsText())); return new MeterIdPrefix(name, tags); }
private List<Tag> buildTags(RequestLog log) { final RequestContext ctx = log.context(); final Object requestContent = log.requestContent(); String methodName = null; if (requestContent instanceof RpcRequest) { methodName = ((RpcRequest) requestContent).method(); } if (methodName == null) { final HttpHeaders requestHeaders = log.requestHeaders(); final HttpMethod httpMethod = requestHeaders.method(); if (httpMethod != null) { methodName = httpMethod.name(); } } if (methodName == null) { methodName = MoreObjects.firstNonNull(log.method().name(), "__UNKNOWN_METHOD__"); } final List<Tag> tags = new ArrayList<>(4); // method, hostNamePattern, pathMapping, status tags.add(Tag.of("method", methodName)); if (ctx instanceof ServiceRequestContext) { final ServiceRequestContext sCtx = (ServiceRequestContext) ctx; tags.add(Tag.of("hostnamePattern", sCtx.virtualHost().hostnamePattern())); tags.add(Tag.of("pathMapping", sCtx.pathMapping().meterTag())); } return tags; } };
.map(tuple -> Tag.of(tuple.getT1(), tuple.getT2())) .collect(Collectors.toList());
/** * Creates a {@code method} tag based on the {@link HttpServletRequest#getMethod() * method} of the given {@code request}. * @param request the request * @return the method tag whose value is a capitalized method (e.g. GET). */ public static Tag method(HttpServletRequest request) { return (request != null) ? Tag.of("method", request.getMethod()) : METHOD_UNKNOWN; }
/** * Creates a {@code status} {@code Tag} derived from the * {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}. * @param response the response * @return the status tag */ public static Tag status(ClientHttpResponse response) { return Tag.of("status", getStatusMessage(response)); }
private Tag parseTag(String tag) { String[] parts = tag.split(":", 2); if (parts.length != 2) { throw new InvalidEndpointRequestException( "Each tag parameter must be in the form 'key:value' but was: " + tag, "Each tag parameter must be in the form 'key:value'"); } return Tag.of(parts[0], parts[1]); }
/** * Creates a {@code uri} {@code Tag} from the given {@code uriTemplate}. * @param uriTemplate the template * @return the uri tag */ public static Tag uri(String uriTemplate) { String uri = (StringUtils.hasText(uriTemplate) ? uriTemplate : "none"); return Tag.of("uri", ensureLeadingSlash(stripUri(uri))); }
/** * Creates a {@code method} {@code Tag} for the {@link ClientHttpRequest#getMethod() * method} of the given {@code request}. * @param request the request * @return the method tag */ public static Tag method(ClientRequest request) { return Tag.of("method", request.method().name()); }
/** * Creates a {@code method} tag based on the * {@link org.springframework.http.server.reactive.ServerHttpRequest#getMethod() * method} of the {@link ServerWebExchange#getRequest()} request of the given * {@code exchange}. * @param exchange the exchange * @return the method tag whose value is a capitalized method (e.g. GET). */ public static Tag method(ServerWebExchange exchange) { return Tag.of("method", exchange.getRequest().getMethodValue()); }
/** * Creates a {@code uri} {@code Tag} for the URI of the given {@code request}. * @param request the request * @return the uri tag */ public static Tag uri(HttpRequest request) { return Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().toString()))); }
@DisplayName("requestResponse gathers metrics") @Test void requestResponse() { Payload payload = DefaultPayload.create("test-metadata", "test-data"); when(delegate.requestResponse(payload)).thenReturn(Mono.empty()); new MicrometerRSocket(delegate, meterRegistry, Tag.of("test-key", "test-value")) .requestResponse(payload) .as(StepVerifier::create) .verifyComplete(); assertThat(findTimer("request.response", SignalType.ON_COMPLETE).count()).isEqualTo(1); }
@DisplayName("metadataPush gathers metrics") @Test void metadataPush() { Payload payload = DefaultPayload.create("test-metadata", "test-data"); when(delegate.metadataPush(payload)).thenReturn(Mono.empty()); new MicrometerRSocket(delegate, meterRegistry, Tag.of("test-key", "test-value")) .metadataPush(payload) .as(StepVerifier::create) .verifyComplete(); assertThat(findCounter("metadata.push", SignalType.ON_COMPLETE).count()).isEqualTo(1); }
@DisplayName("fireAndForget gathers metrics") @Test void fireAndForget() { Payload payload = DefaultPayload.create("test-metadata", "test-data"); when(delegate.fireAndForget(payload)).thenReturn(Mono.empty()); new MicrometerRSocket(delegate, meterRegistry, Tag.of("test-key", "test-value")) .fireAndForget(payload) .as(StepVerifier::create) .verifyComplete(); assertThat(findCounter("request.fnf", SignalType.ON_COMPLETE).count()).isEqualTo(1); }
@DisplayName("requestChannel gathers metrics") @Test void requestChannel() { Mono<Payload> payload = Mono.just(DefaultPayload.create("test-metadata", "test-data")); when(delegate.requestChannel(payload)).thenReturn(Flux.empty()); new MicrometerRSocket(delegate, meterRegistry, Tag.of("test-key", "test-value")) .requestChannel(payload) .as(StepVerifier::create) .verifyComplete(); assertThat(findCounter("request.channel", SignalType.ON_COMPLETE).count()).isEqualTo(1); }
@DisplayName("requestStream gathers metrics") @Test void requestStream() { Payload payload = DefaultPayload.create("test-metadata", "test-data"); when(delegate.requestStream(payload)).thenReturn(Flux.empty()); new MicrometerRSocket(delegate, meterRegistry, Tag.of("test-key", "test-value")) .requestStream(payload) .as(StepVerifier::create) .verifyComplete(); assertThat(findCounter("request.stream", SignalType.ON_COMPLETE).count()).isEqualTo(1); }