|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or 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 | + * https://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 org.springframework.modulith.observability.support; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.Counter; |
| 19 | +import io.micrometer.core.instrument.Counter.Builder; |
| 20 | + |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.SortedSet; |
| 23 | +import java.util.TreeSet; |
| 24 | +import java.util.function.BiConsumer; |
| 25 | +import java.util.function.BiFunction; |
| 26 | +import java.util.function.Function; |
| 27 | + |
| 28 | +import org.springframework.modulith.observability.ModulithEventMetrics; |
| 29 | +import org.springframework.util.Assert; |
| 30 | + |
| 31 | +/** |
| 32 | + * A factory to create {@link Builder} instances for {@link Counter}s eventually. Target for dependency injection via |
| 33 | + * the {@link ModulithEventMetricsCustomizer} interface to allow users to augment the counters with additional |
| 34 | + * information. |
| 35 | + * |
| 36 | + * @author Oliver Drotbohm |
| 37 | + * @author Marcin Grzejszczak |
| 38 | + * @since 1.4 |
| 39 | + */ |
| 40 | +public class CrossModuleEventCounterFactory implements ModulithEventMetrics { |
| 41 | + |
| 42 | + private final SortedSet<ModulithMetricsCustomizer> customizers = new TreeSet<>(); |
| 43 | + private final SortedSet<ModulithMetricsCustomizer> creators = new TreeSet<>(); |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a {@link Builder} instance for the given event applying registered customizers. |
| 47 | + * |
| 48 | + * @param event must not be {@literal null}. |
| 49 | + * @return will never be {@literal null}. |
| 50 | + */ |
| 51 | + Builder createCounterBuilder(Object event) { |
| 52 | + |
| 53 | + Assert.notNull(event, "Event must not be null!"); |
| 54 | + |
| 55 | + // Use most specific creator (default order as defined in ModulithMetricsCustomizer) |
| 56 | + var creator = creators.stream() |
| 57 | + .filter(it -> it.supports(event)) |
| 58 | + .findFirst() |
| 59 | + .orElse(ModulithMetricsCustomizer.DEFAULT); |
| 60 | + |
| 61 | + var builder = creator.createBuilder(event); |
| 62 | + |
| 63 | + return customizers.stream() |
| 64 | + .sorted(Comparator.reverseOrder()) // Inverted order (most specific last) |
| 65 | + .filter(it -> it.supports(event)) |
| 66 | + .reduce(builder, (it, customizer) -> customizer.augment(event, it), (l, r) -> r); |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + * (non-Javadoc) |
| 71 | + * @see org.springframework.modulith.observability.api.ModulithEventMetricsCustomizer#customize(java.lang.Class, java.util.function.Function) |
| 72 | + */ |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + @Override |
| 75 | + public <T> ModulithEventMetrics customize(Class<T> type, Function<T, Builder> factory) { |
| 76 | + |
| 77 | + creators.add(new ModulithMetricsCustomizer(type, (Function<Object, Builder>) factory)); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /* |
| 82 | + * (non-Javadoc) |
| 83 | + * @see org.springframework.modulith.observability.api.ModulithEventMetricsCustomizer#customize(java.lang.Class, java.util.function.BiConsumer) |
| 84 | + */ |
| 85 | + @Override |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + public <T> CrossModuleEventCounterFactory customize(Class<T> type, BiConsumer<T, Builder> consumer) { |
| 88 | + |
| 89 | + customizers.add(new ModulithMetricsCustomizer(type, (BiConsumer<Object, Builder>) consumer)); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + private static class ModulithMetricsCustomizer implements Comparable<ModulithMetricsCustomizer> { |
| 94 | + |
| 95 | + private static final BiConsumer<Object, Builder> NO_OP = (event, builder) -> {}; |
| 96 | + private static final Function<Object, Builder> DEFAULT_FACTORY = event -> Counter |
| 97 | + .builder(event.getClass().getName()); |
| 98 | + |
| 99 | + public static final ModulithMetricsCustomizer DEFAULT = new ModulithMetricsCustomizer(Object.class, NO_OP); |
| 100 | + |
| 101 | + private final Class<?> type; |
| 102 | + private final Function<Object, Builder> creator; |
| 103 | + private final BiFunction<Object, Builder, Builder> customizer; |
| 104 | + |
| 105 | + public ModulithMetricsCustomizer(Class<?> type, Function<Object, Builder> creator) { |
| 106 | + |
| 107 | + this.type = type; |
| 108 | + this.creator = creator; |
| 109 | + this.customizer = (event, builder) -> builder; |
| 110 | + } |
| 111 | + |
| 112 | + public ModulithMetricsCustomizer(Class<?> type, BiConsumer<Object, Builder> creator) { |
| 113 | + |
| 114 | + this.type = type; |
| 115 | + this.creator = DEFAULT_FACTORY; |
| 116 | + this.customizer = (event, builder) -> { |
| 117 | + creator.accept(event, builder); |
| 118 | + return builder; |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + public Builder createBuilder(Object event) { |
| 123 | + return creator.apply(event); |
| 124 | + } |
| 125 | + |
| 126 | + public boolean supports(Object event) { |
| 127 | + return type.isInstance(event); |
| 128 | + } |
| 129 | + |
| 130 | + public Builder augment(Object event, Builder builder) { |
| 131 | + return customizer.apply(event, builder); |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * (non-Javadoc) |
| 136 | + * @see java.lang.Comparable#compareTo(java.lang.Object) |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public int compareTo(ModulithMetricsCustomizer that) { |
| 140 | + |
| 141 | + if (this.type.isAssignableFrom(that.type)) { |
| 142 | + return 1; |
| 143 | + } |
| 144 | + if (that.type.isAssignableFrom(this.type)) { |
| 145 | + return -1; |
| 146 | + } |
| 147 | + |
| 148 | + // If classes are not in the same hierarchy, sort by name for consistency |
| 149 | + return this.type.getName().compareTo(that.type.getName()); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments