/
SimpleObservation.java
262 lines (217 loc) · 9.01 KB
/
SimpleObservation.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright 2022 VMware, Inc.
*
* 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.micrometer.observation;
import io.micrometer.common.KeyValue;
import io.micrometer.common.lang.Nullable;
import io.micrometer.common.util.StringUtils;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.stream.Collectors;
/**
* Default implementation of {@link Observation}.
*
* @author Jonatan Ivanov
* @author Tommy Ludwig
* @author Marcin Grzejszczak
* @since 1.10.0
*/
class SimpleObservation implements Observation {
private final ObservationRegistry registry;
private final Context context;
@SuppressWarnings("rawtypes")
private final Collection<ObservationConvention> conventions;
@SuppressWarnings("rawtypes")
private final Deque<ObservationHandler> handlers;
private final Collection<ObservationFilter> filters;
SimpleObservation(@Nullable String name, ObservationRegistry registry, Context context) {
this.registry = registry;
this.context = context;
this.context.setName(name);
this.conventions = getConventionsFromConfig(registry, context);
this.handlers = getHandlersFromConfig(registry, context);
this.filters = registry.observationConfig().getObservationFilters();
}
SimpleObservation(ObservationConvention<? extends Context> convention, ObservationRegistry registry,
Context context) {
this((String) null, registry, context); // name is set later in start()
if (convention.supportsContext(context)) {
this.conventions.add(convention);
}
else {
throw new IllegalStateException(
"Convention [" + convention + "] doesn't support context [" + context + "]");
}
}
private static Collection<ObservationConvention> getConventionsFromConfig(ObservationRegistry registry,
Context context) {
return registry.observationConfig().getObservationConventions().stream()
.filter(convention -> convention.supportsContext(context)).collect(Collectors.toList());
}
private static Deque<ObservationHandler> getHandlersFromConfig(ObservationRegistry registry, Context context) {
return registry.observationConfig().getObservationHandlers().stream()
.filter(handler -> handler.supportsContext(context)).collect(Collectors.toCollection(ArrayDeque::new));
}
@Override
public Observation contextualName(@Nullable String contextualName) {
this.context.setContextualName(contextualName);
return this;
}
@Override
public Observation parentObservation(@Nullable Observation parentObservation) {
this.context.setParentObservation(parentObservation);
return this;
}
@Override
public Observation lowCardinalityKeyValue(KeyValue keyValue) {
this.context.addLowCardinalityKeyValue(keyValue);
return this;
}
@Override
public Observation highCardinalityKeyValue(KeyValue keyValue) {
this.context.addHighCardinalityKeyValue(keyValue);
return this;
}
@Override
public Observation observationConvention(ObservationConvention<?> convention) {
if (convention.supportsContext(context)) {
this.conventions.add(convention);
}
return this;
}
@Override
public Observation error(Throwable error) {
this.context.setError(error);
notifyOnError();
return this;
}
@Override
public Observation event(Event event) {
notifyOnEvent(event);
return this;
}
@Override
public Observation start() {
// We want to rename with the first matching convention
boolean nameChanged = false;
for (ObservationConvention convention : this.conventions) {
this.context.addLowCardinalityKeyValues(convention.getLowCardinalityKeyValues(context));
this.context.addHighCardinalityKeyValues(convention.getHighCardinalityKeyValues(context));
if (!nameChanged) {
String newName = convention.getName();
if (StringUtils.isNotBlank(newName)) {
this.context.setName(newName);
nameChanged = true;
}
}
}
notifyOnObservationStarted();
return this;
}
@Override
public Context getContext() {
return this.context;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void stop() {
// We want to rename with the first matching convention
boolean contextualNameChanged = false;
for (ObservationConvention convention : this.conventions) {
this.context.addLowCardinalityKeyValues(convention.getLowCardinalityKeyValues(context));
this.context.addHighCardinalityKeyValues(convention.getHighCardinalityKeyValues(context));
if (!contextualNameChanged) {
String newContextualName = convention.getContextualName(context);
if (StringUtils.isNotBlank(newContextualName)) {
this.context.setContextualName(newContextualName);
contextualNameChanged = true;
}
}
}
Observation.Context modifiedContext = this.context;
for (ObservationFilter filter : this.filters) {
modifiedContext = filter.map(modifiedContext);
}
notifyOnObservationStopped(modifiedContext);
}
@Override
public Scope openScope() {
Scope scope = new SimpleScope(this.registry, this);
notifyOnScopeOpened();
return scope;
}
@Override
public String toString() {
return "{" + "name=" + this.context.getName() + "(" + this.context.getContextualName() + ")" + ", error="
+ this.context.getError() + ", context=" + this.context + '}';
}
@SuppressWarnings("unchecked")
private void notifyOnObservationStarted() {
this.handlers.forEach(handler -> handler.onStart(this.context));
}
@SuppressWarnings("unchecked")
private void notifyOnError() {
this.handlers.forEach(handler -> handler.onError(this.context));
}
@SuppressWarnings("unchecked")
private void notifyOnEvent(Event event) {
this.handlers.forEach(handler -> handler.onEvent(event, this.context));
}
@SuppressWarnings("unchecked")
private void notifyOnScopeOpened() {
this.handlers.forEach(handler -> handler.onScopeOpened(this.context));
}
@SuppressWarnings("unchecked")
private void notifyOnScopeClosed() {
// We're closing from end till the beginning - e.g. we opened scope with handlers
// with ids 1,2,3 and we need to close the scope in order 3,2,1
this.handlers.descendingIterator().forEachRemaining(handler -> handler.onScopeClosed(this.context));
}
@SuppressWarnings("unchecked")
private void notifyOnObservationStopped(Observation.Context context) {
// We're closing from end till the beginning - e.g. we started with handlers with
// ids 1,2,3 and we need to call close on 3,2,1
this.handlers.descendingIterator().forEachRemaining(handler -> handler.onStop(context));
}
static class SimpleScope implements Scope {
private final ObservationRegistry registry;
private final SimpleObservation currentObservation;
@Nullable
private final Observation.Scope previousObservationScope;
@Nullable
private final ObservationView previousParentObservationView;
SimpleScope(ObservationRegistry registry, SimpleObservation current) {
this.registry = registry;
this.currentObservation = current;
this.previousObservationScope = registry.getCurrentObservationScope();
this.previousParentObservationView = current.context.getParentObservation();
if (this.previousObservationScope != null) {
current.context.setParentObservation(this.previousObservationScope.getCurrentObservation());
}
this.registry.setCurrentObservationScope(this);
}
@Override
public Observation getCurrentObservation() {
return this.currentObservation;
}
@Override
public void close() {
this.registry.setCurrentObservationScope(previousObservationScope);
this.currentObservation.notifyOnScopeClosed();
this.currentObservation.context.setParentObservation(this.previousParentObservationView);
}
}
}