forked from rsocket/rsocket-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservationRequesterRSocketProxy.java
More file actions
208 lines (190 loc) · 7.28 KB
/
Copy pathObservationRequesterRSocketProxy.java
File metadata and controls
208 lines (190 loc) · 7.28 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2013-2021 the original author or authors.
*
* 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 io.rsocket.micrometer.observation;
import io.micrometer.common.util.StringUtils;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.docs.ObservationDocumentation;
import io.netty.buffer.ByteBuf;
import io.rsocket.Payload;
import io.rsocket.RSocket;
import io.rsocket.frame.FrameType;
import io.rsocket.metadata.RoutingMetadata;
import io.rsocket.metadata.WellKnownMimeType;
import io.rsocket.util.RSocketProxy;
import java.util.Iterator;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.annotation.Nullable;
import reactor.util.context.ContextView;
/**
* Tracing representation of a {@link RSocketProxy} for the requester.
*
* @author Marcin Grzejszczak
* @author Oleh Dokuka
* @since 1.1.4
*/
public class ObservationRequesterRSocketProxy extends RSocketProxy {
/** Aligned with ObservationThreadLocalAccessor#KEY */
private static final String MICROMETER_OBSERVATION_KEY = "micrometer.observation";
private final ObservationRegistry observationRegistry;
@Nullable private final RSocketRequesterObservationConvention observationConvention;
public ObservationRequesterRSocketProxy(RSocket source, ObservationRegistry observationRegistry) {
this(source, observationRegistry, null);
}
public ObservationRequesterRSocketProxy(
RSocket source,
ObservationRegistry observationRegistry,
RSocketRequesterObservationConvention observationConvention) {
super(source);
this.observationRegistry = observationRegistry;
this.observationConvention = observationConvention;
}
@Override
public Mono<Void> fireAndForget(Payload payload) {
return setObservation(
super::fireAndForget,
payload,
FrameType.REQUEST_FNF,
RSocketObservationDocumentation.RSOCKET_REQUESTER_FNF);
}
@Override
public Mono<Payload> requestResponse(Payload payload) {
return setObservation(
super::requestResponse,
payload,
FrameType.REQUEST_RESPONSE,
RSocketObservationDocumentation.RSOCKET_REQUESTER_REQUEST_RESPONSE);
}
<T> Mono<T> setObservation(
Function<Payload, Mono<T>> input,
Payload payload,
FrameType frameType,
ObservationDocumentation observation) {
return Mono.deferContextual(
contextView -> observe(input, payload, frameType, observation, contextView));
}
private String route(Payload payload) {
if (payload.hasMetadata()) {
try {
ByteBuf extracted =
CompositeMetadataUtils.extract(
payload.sliceMetadata(), WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString());
final RoutingMetadata routingMetadata = new RoutingMetadata(extracted);
final Iterator<String> iterator = routingMetadata.iterator();
return iterator.next();
} catch (Exception e) {
}
}
return null;
}
private <T> Mono<T> observe(
Function<Payload, Mono<T>> input,
Payload payload,
FrameType frameType,
ObservationDocumentation obs,
ContextView contextView) {
String route = route(payload);
RSocketContext rSocketContext =
new RSocketContext(
payload, payload.sliceMetadata(), frameType, route, RSocketContext.Side.REQUESTER);
Observation parentObservation = contextView.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
Observation observation =
obs.observation(
this.observationConvention,
new DefaultRSocketRequesterObservationConvention(rSocketContext),
() -> rSocketContext,
observationRegistry)
.parentObservation(parentObservation);
setContextualName(frameType, route, observation);
observation.start();
Payload newPayload = payload;
if (rSocketContext.modifiedPayload != null) {
newPayload = rSocketContext.modifiedPayload;
}
return input
.apply(newPayload)
.doOnError(observation::error)
.doFinally(signalType -> observation.stop())
.contextWrite(context -> context.put(MICROMETER_OBSERVATION_KEY, observation));
}
@Override
public Flux<Payload> requestStream(Payload payload) {
return observationFlux(
super::requestStream,
payload,
FrameType.REQUEST_STREAM,
RSocketObservationDocumentation.RSOCKET_REQUESTER_REQUEST_STREAM);
}
@Override
public Flux<Payload> requestChannel(Publisher<Payload> inbound) {
return Flux.from(inbound)
.switchOnFirst(
(firstSignal, flux) -> {
final Payload firstPayload = firstSignal.get();
if (firstPayload != null) {
return observationFlux(
p -> super.requestChannel(flux.skip(1).startWith(p)),
firstPayload,
FrameType.REQUEST_CHANNEL,
RSocketObservationDocumentation.RSOCKET_REQUESTER_REQUEST_CHANNEL);
}
return flux;
});
}
private Flux<Payload> observationFlux(
Function<Payload, Flux<Payload>> input,
Payload payload,
FrameType frameType,
ObservationDocumentation obs) {
return Flux.deferContextual(
contextView -> {
String route = route(payload);
RSocketContext rSocketContext =
new RSocketContext(
payload,
payload.sliceMetadata(),
frameType,
route,
RSocketContext.Side.REQUESTER);
Observation parentObservation =
contextView.getOrDefault(MICROMETER_OBSERVATION_KEY, null);
Observation newObservation =
obs.observation(
this.observationConvention,
new DefaultRSocketRequesterObservationConvention(rSocketContext),
() -> rSocketContext,
this.observationRegistry)
.parentObservation(parentObservation);
setContextualName(frameType, route, newObservation);
newObservation.start();
return input
.apply(rSocketContext.modifiedPayload)
.doOnError(newObservation::error)
.doFinally(signalType -> newObservation.stop())
.contextWrite(context -> context.put(MICROMETER_OBSERVATION_KEY, newObservation));
});
}
private void setContextualName(FrameType frameType, String route, Observation newObservation) {
if (StringUtils.isNotBlank(route)) {
newObservation.contextualName(frameType.name() + " " + route);
} else {
newObservation.contextualName(frameType.name());
}
}
}