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 String toString() { if (tags.isEmpty()) { return name; } final StringBuilder buf = new StringBuilder(); buf.append(name).append('{'); tags.forEach(tag -> buf.append(tag.getKey()).append('=') .append(tag.getValue()).append(',')); buf.setCharAt(buf.length() - 1, '}'); return buf.toString(); } }
@Override public void process(Exchange exchange) { Message in = exchange.getIn(); String defaultMetricsName = simple(exchange, getEndpoint().getMetricsName(), String.class); String finalMetricsName = getStringHeader(in, HEADER_METRIC_NAME, defaultMetricsName); Iterable<Tag> defaultTags = getEndpoint().getTags(); Iterable<Tag> headerTags = getTagHeader(in, HEADER_METRIC_TAGS, Tags.empty()); Iterable<Tag> finalTags = Tags.concat(defaultTags, headerTags).stream() .map(tag -> Tag.of( simple(exchange, tag.getKey(), String.class), simple(exchange, tag.getValue(), String.class))) .reduce(Tags.empty(), Tags::and, Tags::and) .and(Tags.of( CAMEL_CONTEXT_TAG, getEndpoint().getCamelContext().getName())); try { doProcess(exchange, finalMetricsName, finalTags); } catch (Exception e) { exchange.setException(e); } finally { clearMetricsHeaders(in); } }
@ReadOperation public MetricResponse metric(@Selector String requiredMetricName, @Nullable List<String> tag) { List<Tag> tags = parseTags(tag); Collection<Meter> meters = findFirstMatchingMeters(this.registry, requiredMetricName, tags); if (meters.isEmpty()) { return null; } Map<Statistic, Double> samples = getSamples(meters); Map<String, Set<String>> availableTags = getAvailableTags(meters); tags.forEach((t) -> availableTags.remove(t.getKey())); Meter.Id meterId = meters.iterator().next().getId(); return new MetricResponse(requiredMetricName, meterId.getDescription(), meterId.getBaseUnit(), asList(samples, Sample::new), asList(availableTags, AvailableTag::new)); }
String generatePrometheusFriendlyName(Meter.Id id) { String name = ""; Optional<Tag> group = findTagByKey(id, "group"); if (group.isPresent()) { name += group.get().getValue(); } Optional<Tag> service = findTagByKey(id, "service"); if (service.isPresent()) { name += "." + service.get().getValue(); } Optional<Tag> method = findTagByKey(id, "method"); if (method.isPresent()) { name += "." + method.get().getValue(); } if (name.isEmpty()) { return name; } else { return name + "." + id.getName(); } }
private Optional<Tag> findTagByKey(Meter.Id id, String key) { return id.getTags().stream().filter(tag -> tag.getKey().equals(key)).findFirst(); } }
@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); }); } }
private static String measurementName(Meter.Id id, Measurement measurement) { final StringBuilder buf = new StringBuilder(); // Append name. buf.append(id.getName()); // Append statistic. buf.append('#'); buf.append(measurement.getStatistic().getTagValueRepresentation()); // Append tags if there are any. final Iterator<Tag> tagsIterator = id.getTags().iterator(); if (tagsIterator.hasNext()) { buf.append('{'); tagsIterator.forEachRemaining(tag -> buf.append(tag.getKey()).append('=') .append(tag.getValue()).append(',')); buf.setCharAt(buf.length() - 1, '}'); } return buf.toString(); }
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); }
/** * Logs a sorted and readable list of meters using the debug level. Useful for debugging. * * @param meters The meters to be logged. */ public static void logMeters(final Collection<? extends Meter> meters) { if (!log.isDebugEnabled()) { return; } // The original collection is usually unmodifiable final List<Meter> sortedMeters = new ArrayList<>(meters); Collections.sort(sortedMeters, METER_COMPARATOR); log.debug("Found meters:"); for (final Meter meter : sortedMeters) { final Id id = meter.getId(); final String type = id.getType().name(); final String name = id.getName(); final Map<String, String> tagMap = new LinkedHashMap<>(); // Tags are already sorted for (final Tag tag : id.getTags()) { tagMap.put(tag.getKey(), tag.getValue()); } log.debug("- {} {} {}", type, name, tagMap); } }
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());