|
| 1 | +/* |
| 2 | + * Copyright 2019 Yevhenii Voievodin |
| 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.bulkhead.Bulkhead; |
| 19 | +import io.github.resilience4j.bulkhead.Bulkhead.Metrics; |
| 20 | +import io.github.resilience4j.bulkhead.BulkheadRegistry; |
| 21 | +import io.prometheus.client.Collector; |
| 22 | +import io.prometheus.client.GaugeMetricFamily; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.function.Supplier; |
| 27 | + |
| 28 | +import static java.util.Arrays.asList; |
| 29 | +import static java.util.Collections.singletonList; |
| 30 | +import static java.util.Objects.requireNonNull; |
| 31 | + |
| 32 | +/** Collects bulkhead exposed {@link Metrics}. */ |
| 33 | +public class BulkheadMetricsCollector extends Collector { |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a new collector with custom metric names and |
| 37 | + * using given {@code supplier} as source of bulkheads. |
| 38 | + * |
| 39 | + * @param names the custom metric names |
| 40 | + * @param supplier the supplier of bulkheads, note that supplier will be called one every {@link #collect()} |
| 41 | + */ |
| 42 | + public static BulkheadMetricsCollector ofSupplier(MetricNames names, Supplier<? extends Iterable<? extends Bulkhead>> supplier) { |
| 43 | + return new BulkheadMetricsCollector(names, supplier); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new collector using given {@code supplier} as source of bulkheads. |
| 48 | + * |
| 49 | + * @param supplier the supplier of bulkheads, note that supplier will be called one very {@link #collect()} |
| 50 | + */ |
| 51 | + public static BulkheadMetricsCollector ofSupplier(Supplier<? extends Iterable<? extends Bulkhead>> supplier) { |
| 52 | + return new BulkheadMetricsCollector(MetricNames.ofDefaults(), supplier); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new collector using given {@code registry} as source of bulkheads. |
| 57 | + * |
| 58 | + * @param registry the source of bulkheads |
| 59 | + */ |
| 60 | + public static BulkheadMetricsCollector ofBulkheadRegistry(BulkheadRegistry registry) { |
| 61 | + return new BulkheadMetricsCollector(MetricNames.ofDefaults(), registry::getAllBulkheads); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a new collector using given {@code bulkheads} iterable as source of bulkheads. |
| 66 | + * |
| 67 | + * @param bulkheads the source of bulkheads |
| 68 | + */ |
| 69 | + public static BulkheadMetricsCollector ofIterable(Iterable<? extends Bulkhead> bulkheads) { |
| 70 | + return new BulkheadMetricsCollector(MetricNames.ofDefaults(), () -> bulkheads); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates a new collector for a given {@code bulkhead}. |
| 75 | + * |
| 76 | + * @param bulkhead the bulkhead to collect metrics for |
| 77 | + */ |
| 78 | + public static BulkheadMetricsCollector ofBulkhead(Bulkhead bulkhead) { |
| 79 | + return ofIterable(singletonList(bulkhead)); |
| 80 | + } |
| 81 | + |
| 82 | + private final MetricNames names; |
| 83 | + private final Supplier<? extends Iterable<? extends Bulkhead>> bulkheadsSupplier; |
| 84 | + |
| 85 | + private BulkheadMetricsCollector(MetricNames names, Supplier<? extends Iterable<? extends Bulkhead>> bulkheadsSupplier) { |
| 86 | + this.names = Objects.requireNonNull(names); |
| 87 | + this.bulkheadsSupplier = Objects.requireNonNull(bulkheadsSupplier); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public List<MetricFamilySamples> collect() { |
| 92 | + GaugeMetricFamily availableCallsFamily = new GaugeMetricFamily( |
| 93 | + names.getAvailableConcurrentCallsMetricName(), |
| 94 | + "The number of available concurrent calls", |
| 95 | + LabelNames.NAME |
| 96 | + ); |
| 97 | + GaugeMetricFamily maxAllowedCallsFamily = new GaugeMetricFamily( |
| 98 | + names.getMaxAllowedConcurrentCallsMetricName(), |
| 99 | + "The maximum number of allowed concurrent calls", |
| 100 | + LabelNames.NAME |
| 101 | + ); |
| 102 | + |
| 103 | + for (Bulkhead bulkhead: bulkheadsSupplier.get()) { |
| 104 | + List<String> labelValues = singletonList(bulkhead.getName()); |
| 105 | + availableCallsFamily.addMetric(labelValues, bulkhead.getMetrics().getAvailableConcurrentCalls()); |
| 106 | + maxAllowedCallsFamily.addMetric(labelValues, bulkhead.getMetrics().getMaxAllowedConcurrentCalls()); |
| 107 | + } |
| 108 | + |
| 109 | + return asList(availableCallsFamily, maxAllowedCallsFamily); |
| 110 | + } |
| 111 | + |
| 112 | + /** Defines possible configuration for metric names. */ |
| 113 | + public static class MetricNames { |
| 114 | + |
| 115 | + public static final String DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME = "resilience4j_bulkhead_available_concurrent_calls"; |
| 116 | + public static final String DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME = "resilience4j_bulkhead_max_allowed_concurrent_calls"; |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns a builder for creating custom metric names. |
| 120 | + * Note that names have default values, so only desired metrics can be renamed. |
| 121 | + */ |
| 122 | + public static Builder custom() { |
| 123 | + return new Builder(); |
| 124 | + } |
| 125 | + |
| 126 | + /** Returns default metric names. */ |
| 127 | + public static MetricNames ofDefaults() { |
| 128 | + return new MetricNames(); |
| 129 | + } |
| 130 | + |
| 131 | + private String availableConcurrentCallsMetricName = DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME; |
| 132 | + private String maxAllowedConcurrentCallsMetricName = DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME; |
| 133 | + |
| 134 | + private MetricNames() {} |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the metric name for bulkhead concurrent calls, |
| 138 | + * defaults to {@value DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME}. |
| 139 | + */ |
| 140 | + public String getAvailableConcurrentCallsMetricName() { |
| 141 | + return availableConcurrentCallsMetricName; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns the metric name for bulkhead max available concurrent calls, |
| 146 | + * defaults to {@value DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME}. |
| 147 | + */ |
| 148 | + public String getMaxAllowedConcurrentCallsMetricName() { |
| 149 | + return maxAllowedConcurrentCallsMetricName; |
| 150 | + } |
| 151 | + |
| 152 | + /** Helps building custom instance of {@link MetricNames}. */ |
| 153 | + public static class Builder { |
| 154 | + |
| 155 | + private final MetricNames metricNames = new MetricNames(); |
| 156 | + |
| 157 | + /** Overrides the default metric name {@value MetricNames#DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME} with a given one. */ |
| 158 | + public Builder availableConcurrentCallsMetricName(String availableConcurrentCallsMetricNames) { |
| 159 | + metricNames.availableConcurrentCallsMetricName = requireNonNull(availableConcurrentCallsMetricNames); |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + /** Overrides the default metric name {@value MetricNames#DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME} with a given one. */ |
| 164 | + public Builder maxAllowedConcurrentCallsMetricName(String maxAllowedConcurrentCallsMetricName) { |
| 165 | + metricNames.maxAllowedConcurrentCallsMetricName = requireNonNull(maxAllowedConcurrentCallsMetricName); |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
| 169 | + /** Builds {@link MetricNames} instance. */ |
| 170 | + public MetricNames build() { |
| 171 | + return metricNames; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments