|
| 1 | +/* |
| 2 | + * Copyright 2019 authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.resilience4j.prometheus.collectors; |
| 17 | + |
| 18 | +import io.github.resilience4j.timelimiter.TimeLimiter; |
| 19 | +import io.github.resilience4j.timelimiter.TimeLimiterRegistry; |
| 20 | +import io.prometheus.client.Collector; |
| 21 | +import io.prometheus.client.CollectorRegistry; |
| 22 | +import io.prometheus.client.Counter; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static java.util.Objects.requireNonNull; |
| 28 | + |
| 29 | +/** |
| 30 | + * Collects TimeLimiter exposed events. |
| 31 | + */ |
| 32 | +public class TimeLimiterMetricsCollector extends Collector { |
| 33 | + |
| 34 | + static final String KIND_SUCCESSFUL = "successful"; |
| 35 | + static final String KIND_FAILED = "failed"; |
| 36 | + static final String KIND_TIMEOUT = "timeout"; |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a new collector with custom metric names and |
| 40 | + * using given {@code supplier} as source of time limiters. |
| 41 | + * |
| 42 | + * @param names the custom metric names |
| 43 | + * @param timeLimiterRegistry the source of time limiters |
| 44 | + */ |
| 45 | + public static TimeLimiterMetricsCollector ofTimeLimiterRegistry(TimeLimiterMetricsCollector.MetricNames names, TimeLimiterRegistry timeLimiterRegistry) { |
| 46 | + return new TimeLimiterMetricsCollector(names, timeLimiterRegistry); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new collector using given {@code registry} as source of time limiters. |
| 51 | + * |
| 52 | + * @param timeLimiterRegistry the source of time limiters |
| 53 | + */ |
| 54 | + public static TimeLimiterMetricsCollector ofTimeLimiterRegistry(TimeLimiterRegistry timeLimiterRegistry) { |
| 55 | + return new TimeLimiterMetricsCollector(TimeLimiterMetricsCollector.MetricNames.ofDefaults(), timeLimiterRegistry); |
| 56 | + } |
| 57 | + |
| 58 | + private final MetricNames names; |
| 59 | + private final TimeLimiterRegistry timeLimiterRegistry; |
| 60 | + private final CollectorRegistry collectorRegistry = new CollectorRegistry(true); |
| 61 | + private final Counter callsCounter; |
| 62 | + |
| 63 | + private TimeLimiterMetricsCollector(MetricNames names, TimeLimiterRegistry timeLimiterRegistry) { |
| 64 | + this.names = requireNonNull(names); |
| 65 | + this.timeLimiterRegistry = requireNonNull(timeLimiterRegistry); |
| 66 | + this.callsCounter = Counter.build(names.getCallsMetricName(), |
| 67 | + "Total number of calls by kind") |
| 68 | + .labelNames("name", "kind") |
| 69 | + .create().register(collectorRegistry); |
| 70 | + |
| 71 | + this.timeLimiterRegistry.getAllTimeLimiters() |
| 72 | + .forEach(this::addMetrics); |
| 73 | + this.timeLimiterRegistry.getEventPublisher() |
| 74 | + .onEntryAdded(event -> addMetrics(event.getAddedEntry())); |
| 75 | + } |
| 76 | + |
| 77 | + private void addMetrics(TimeLimiter timeLimiter) { |
| 78 | + String name = timeLimiter.getName(); |
| 79 | + timeLimiter.getEventPublisher() |
| 80 | + .onSuccess(event -> callsCounter.labels(name, KIND_SUCCESSFUL).inc()) |
| 81 | + .onError(event -> callsCounter.labels(name, KIND_FAILED).inc()) |
| 82 | + .onTimeout(event -> callsCounter.labels(name, KIND_TIMEOUT).inc()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public List<MetricFamilySamples> collect() { |
| 87 | + return Collections.list(collectorRegistry.metricFamilySamples()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Defines possible configuration for metric names. |
| 92 | + */ |
| 93 | + public static class MetricNames { |
| 94 | + |
| 95 | + public static final String DEFAULT_CALLS_METRIC_NAME = "resilience4j_timelimiter_calls"; |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns a builder for creating custom metric names. |
| 99 | + * Note that names have default values, so only desired metrics can be renamed. |
| 100 | + */ |
| 101 | + public static Builder custom() { |
| 102 | + return new Builder(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns default metric names. |
| 107 | + */ |
| 108 | + public static MetricNames ofDefaults() { |
| 109 | + return new MetricNames(); |
| 110 | + } |
| 111 | + |
| 112 | + private String callsMetricName = DEFAULT_CALLS_METRIC_NAME; |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns the metric name for calls, defaults to {@value DEFAULT_CALLS_METRIC_NAME}. |
| 116 | + */ |
| 117 | + public String getCallsMetricName() { |
| 118 | + return callsMetricName; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Helps building custom instance of {@link MetricNames}. |
| 123 | + */ |
| 124 | + public static class Builder { |
| 125 | + |
| 126 | + private final MetricNames metricNames = new MetricNames(); |
| 127 | + |
| 128 | + /** |
| 129 | + * Overrides the default metric name {@value MetricNames#DEFAULT_CALLS_METRIC_NAME} with a given one. |
| 130 | + */ |
| 131 | + public Builder callsMetricName(String callsMetricName) { |
| 132 | + metricNames.callsMetricName = requireNonNull(callsMetricName); |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Builds {@link MetricNames} instance. |
| 138 | + */ |
| 139 | + public MetricNames build() { |
| 140 | + return metricNames; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments