|
| 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.micrometer.tagged; |
| 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.github.resilience4j.micrometer.BulkheadMetrics; |
| 22 | +import io.micrometer.core.instrument.Gauge; |
| 23 | +import io.micrometer.core.instrument.MeterRegistry; |
| 24 | +import io.micrometer.core.instrument.binder.MeterBinder; |
| 25 | + |
| 26 | +import static java.util.Objects.requireNonNull; |
| 27 | + |
| 28 | +/** |
| 29 | + * A micrometer binder that is used to register bulkhead exposed {@link Metrics metrics}. |
| 30 | + * The main difference from {@link BulkheadMetrics} is that this binder uses tags |
| 31 | + * to distinguish between metrics. |
| 32 | + */ |
| 33 | +public class TaggedBulkheadMetrics implements MeterBinder { |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a new binder that uses given {@code registry} as source of bulkheads. |
| 37 | + * |
| 38 | + * @param registry the source of bulkheads |
| 39 | + */ |
| 40 | + public static TaggedBulkheadMetrics ofBulkheadRegistry(BulkheadRegistry registry) { |
| 41 | + return new TaggedBulkheadMetrics(MetricNames.ofDefaults(), registry.getAllBulkheads()); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a new binder defining custom metric names and |
| 46 | + * using given {@code registry} as source of bulkheads. |
| 47 | + * |
| 48 | + * @param names custom names of the metrics |
| 49 | + * @param registry the source of bulkheads |
| 50 | + */ |
| 51 | + public static TaggedBulkheadMetrics ofBulkheadRegistry(MetricNames names, BulkheadRegistry registry) { |
| 52 | + return new TaggedBulkheadMetrics(names, registry.getAllBulkheads()); |
| 53 | + } |
| 54 | + |
| 55 | + private final MetricNames names; |
| 56 | + private final Iterable<? extends Bulkhead> bulkheads; |
| 57 | + |
| 58 | + private TaggedBulkheadMetrics(MetricNames names, Iterable<? extends Bulkhead> bulkheads) { |
| 59 | + this.names = requireNonNull(names); |
| 60 | + this.bulkheads = requireNonNull(bulkheads); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void bindTo(MeterRegistry registry) { |
| 65 | + for (Bulkhead bulkhead : bulkheads) { |
| 66 | + Gauge.builder(names.getAvailableConcurrentCallsMetricName(), bulkhead, (bh) -> bh.getMetrics().getAvailableConcurrentCalls()) |
| 67 | + .tag(TagNames.NAME, bulkhead.getName()) |
| 68 | + .register(registry); |
| 69 | + Gauge.builder(names.getMaxAllowedConcurrentCallsMetricName(), bulkhead, (bh) -> bh.getMetrics().getMaxAllowedConcurrentCalls()) |
| 70 | + .tag(TagNames.NAME, bulkhead.getName()) |
| 71 | + .register(registry); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** Defines possible configuration for metric names. */ |
| 76 | + public static class MetricNames { |
| 77 | + |
| 78 | + public static final String DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME = "resilience4j_bulkhead_available_concurrent_calls"; |
| 79 | + public static final String DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME = "resilience4j_bulkhead_max_allowed_concurrent_calls"; |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns a builder for creating custom metric names. |
| 83 | + * Note that names have default values, so only desired metrics can be renamed. |
| 84 | + */ |
| 85 | + public static Builder custom() { |
| 86 | + return new Builder(); |
| 87 | + } |
| 88 | + |
| 89 | + /** Returns default metric names. */ |
| 90 | + public static MetricNames ofDefaults() { |
| 91 | + return new MetricNames(); |
| 92 | + } |
| 93 | + |
| 94 | + private String availableConcurrentCallsMetricName = DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME; |
| 95 | + private String maxAllowedConcurrentCallsMetricName = DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME; |
| 96 | + |
| 97 | + private MetricNames() {} |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the metric name for bulkhead concurrent calls, |
| 101 | + * defaults to {@value DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME}. |
| 102 | + */ |
| 103 | + public String getAvailableConcurrentCallsMetricName() { |
| 104 | + return availableConcurrentCallsMetricName; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the metric name for bulkhead max available concurrent calls, |
| 109 | + * defaults to {@value DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME}. |
| 110 | + */ |
| 111 | + public String getMaxAllowedConcurrentCallsMetricName() { |
| 112 | + return maxAllowedConcurrentCallsMetricName; |
| 113 | + } |
| 114 | + |
| 115 | + /** Helps building custom instance of {@link MetricNames}. */ |
| 116 | + public static class Builder { |
| 117 | + |
| 118 | + private final MetricNames metricNames = new MetricNames(); |
| 119 | + |
| 120 | + /** Overrides the default metric name {@value MetricNames#DEFAULT_BULKHEAD_AVAILABLE_CONCURRENT_CALLS_METRIC_NAME} with a given one. */ |
| 121 | + public Builder availableConcurrentCallsMetricName(String availableConcurrentCallsMetricNames) { |
| 122 | + metricNames.availableConcurrentCallsMetricName = requireNonNull(availableConcurrentCallsMetricNames); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** Overrides the default metric name {@value MetricNames#DEFAULT_BULKHEAD_MAX_ALLOWED_CONCURRENT_CALLS_METRIC_NAME} with a given one. */ |
| 127 | + public Builder maxAllowedConcurrentCallsMetricName(String maxAllowedConcurrentCallsMetricName) { |
| 128 | + metricNames.maxAllowedConcurrentCallsMetricName = requireNonNull(maxAllowedConcurrentCallsMetricName); |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /** Builds {@link MetricNames} instance. */ |
| 133 | + public MetricNames build() { |
| 134 | + return metricNames; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments