-
Notifications
You must be signed in to change notification settings - Fork 629
/
MicrometerAddressResolverGroupMetrics.java
162 lines (140 loc) · 6.95 KB
/
MicrometerAddressResolverGroupMetrics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty5.transport;
import io.micrometer.common.KeyValues;
import io.micrometer.core.instrument.Timer;
import io.micrometer.observation.Observation;
import io.netty5.resolver.AddressResolver;
import io.netty5.resolver.AddressResolverGroup;
import io.netty5.util.concurrent.EventExecutor;
import io.netty5.util.concurrent.Future;
import reactor.netty5.channel.MicrometerChannelMetricsRecorder;
import reactor.netty5.internal.util.MapUtils;
import reactor.netty5.observability.ReactorNettyHandlerContext;
import reactor.util.context.ContextView;
import java.net.SocketAddress;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import static reactor.netty5.Metrics.ADDRESS_RESOLVER;
import static reactor.netty5.Metrics.ERROR;
import static reactor.netty5.Metrics.OBSERVATION_KEY;
import static reactor.netty5.Metrics.OBSERVATION_REGISTRY;
import static reactor.netty5.Metrics.SUCCESS;
import static reactor.netty5.Metrics.formatSocketAddress;
import static reactor.netty5.transport.HostnameResolutionObservations.HostnameResolutionTimeHighCardinalityTags.REACTOR_NETTY_PROTOCOL;
import static reactor.netty5.transport.HostnameResolutionObservations.HostnameResolutionTimeHighCardinalityTags.REACTOR_NETTY_STATUS;
import static reactor.netty5.transport.HostnameResolutionObservations.HostnameResolutionTimeHighCardinalityTags.REACTOR_NETTY_TYPE;
import static reactor.netty5.transport.HostnameResolutionObservations.HostnameResolutionTimeLowCardinalityTags.REMOTE_ADDRESS;
import static reactor.netty5.transport.HostnameResolutionObservations.HostnameResolutionTimeLowCardinalityTags.STATUS;
/**
* @author Marcin Grzejszczak
* @author Violeta Georgieva
* @since 1.1.0
*/
final class MicrometerAddressResolverGroupMetrics<T extends SocketAddress> extends AddressResolverGroupMetrics<T> {
static final ConcurrentMap<Integer, MicrometerAddressResolverGroupMetrics<?>> cache = new ConcurrentHashMap<>();
static MicrometerAddressResolverGroupMetrics<?> getOrCreate(
AddressResolverGroup<?> resolverGroup, MicrometerChannelMetricsRecorder recorder) {
return MapUtils.computeIfAbsent(cache, Objects.hash(resolverGroup, recorder),
key -> new MicrometerAddressResolverGroupMetrics<>(resolverGroup, recorder));
}
MicrometerAddressResolverGroupMetrics(AddressResolverGroup<T> resolverGroup, MicrometerChannelMetricsRecorder recorder) {
super(resolverGroup, recorder);
}
@Override
protected AddressResolver<T> newResolver(EventExecutor executor) {
return new MicrometerDelegatingAddressResolver<>((MicrometerChannelMetricsRecorder) recorder, resolverGroup.getResolver(executor));
}
static final class FutureHandlerContext extends Observation.Context implements ReactorNettyHandlerContext {
static final String CONTEXTUAL_NAME = "hostname resolution";
static final String TYPE = "client";
final String remoteAddress;
final MicrometerChannelMetricsRecorder recorder;
// status is not known beforehand
String status;
FutureHandlerContext(MicrometerChannelMetricsRecorder recorder, String remoteAddress) {
this.recorder = recorder;
this.remoteAddress = remoteAddress;
}
@Override
public Timer getTimer() {
return recorder.getResolveAddressTimer(getName(), remoteAddress, status);
}
@Override
public String getContextualName() {
return CONTEXTUAL_NAME;
}
@Override
public KeyValues getHighCardinalityKeyValues() {
return KeyValues.of(REACTOR_NETTY_PROTOCOL.getKeyName(), recorder.protocol(),
REACTOR_NETTY_STATUS.getKeyName(), status, REACTOR_NETTY_TYPE.getKeyName(), TYPE);
}
@Override
public KeyValues getLowCardinalityKeyValues() {
return KeyValues.of(REMOTE_ADDRESS.getKeyName(), remoteAddress, STATUS.getKeyName(), status);
}
}
static final class MicrometerDelegatingAddressResolver<T extends SocketAddress> extends DelegatingAddressResolver<T> {
final String name;
MicrometerDelegatingAddressResolver(MicrometerChannelMetricsRecorder recorder, AddressResolver<T> resolver) {
super(recorder, resolver);
this.name = recorder.name();
}
Future<List<T>> resolveAll(SocketAddress address, ContextView contextView) {
return resolveAllInternal(address, () -> resolver.resolveAll(address), contextView);
}
Future<List<T>> resolveAllInternal(SocketAddress address, Supplier<Future<List<T>>> resolver, ContextView contextView) {
// Cannot invoke the recorder anymore:
// 1. The recorder is one instance only, it is invoked for all name resolutions that can happen
// 2. The recorder does not have knowledge about name resolution lifecycle
// 3. There is no connection, so we cannot hold the context information in the Channel
//
// Move the implementation from the recorder here
String remoteAddress = formatSocketAddress(address);
FutureHandlerContext handlerContext = new FutureHandlerContext((MicrometerChannelMetricsRecorder) recorder, remoteAddress);
Observation sample = Observation.createNotStarted(name + ADDRESS_RESOLVER, handlerContext, OBSERVATION_REGISTRY);
if (contextView.hasKey(OBSERVATION_KEY)) {
sample.parentObservation(contextView.get(OBSERVATION_KEY));
}
sample.start();
return resolver.get()
.addListener(future -> {
handlerContext.status = future.isSuccess() ? SUCCESS : ERROR;
sample.stop();
});
}
@Override
Future<T> resolveInternal(SocketAddress address, Supplier<Future<T>> resolver) {
// Cannot invoke the recorder anymore:
// 1. The recorder is one instance only, it is invoked for all name resolutions that can happen
// 2. The recorder does not have knowledge about name resolution lifecycle
// 3. There is no connection, so we cannot hold the context information in the Channel
//
// Move the implementation from the recorder here
String remoteAddress = formatSocketAddress(address);
FutureHandlerContext handlerContext = new FutureHandlerContext((MicrometerChannelMetricsRecorder) recorder, remoteAddress);
Observation observation = Observation.start(name + ADDRESS_RESOLVER, handlerContext, OBSERVATION_REGISTRY);
return resolver.get()
.addListener(future -> {
handlerContext.status = future.isSuccess() ? SUCCESS : ERROR;
observation.stop();
});
}
}
}