19
19
import io .micrometer .observation .Observation .Context ;
20
20
import io .micrometer .observation .Observation .Event ;
21
21
import io .micrometer .observation .ObservationHandler ;
22
+ import io .micrometer .observation .tck .InvalidObservationException .EventName ;
23
+ import io .micrometer .observation .tck .InvalidObservationException .HistoryElement ;
22
24
25
+ import java .util .ArrayList ;
26
+ import java .util .Collections ;
27
+ import java .util .List ;
23
28
import java .util .function .Consumer ;
24
29
import java .util .function .Predicate ;
25
30
@@ -52,6 +57,7 @@ class ObservationValidator implements ObservationHandler<Context> {
52
57
53
58
@ Override
54
59
public void onStart (Context context ) {
60
+ addHistoryElement (context , EventName .START );
55
61
Status status = context .get (Status .class );
56
62
if (status != null ) {
57
63
consumer .accept (new ValidationResult ("Invalid start: Observation has already been started" , context ));
@@ -63,31 +69,37 @@ public void onStart(Context context) {
63
69
64
70
@ Override
65
71
public void onError (Context context ) {
72
+ addHistoryElement (context , EventName .ERROR );
66
73
checkIfObservationWasStartedButNotStopped ("Invalid error signal" , context );
67
74
}
68
75
69
76
@ Override
70
77
public void onEvent (Event event , Context context ) {
78
+ addHistoryElement (context , EventName .EVENT );
71
79
checkIfObservationWasStartedButNotStopped ("Invalid event signal" , context );
72
80
}
73
81
74
82
@ Override
75
83
public void onScopeOpened (Context context ) {
84
+ addHistoryElement (context , EventName .SCOPE_OPEN );
76
85
checkIfObservationWasStartedButNotStopped ("Invalid scope opening" , context );
77
86
}
78
87
79
88
@ Override
80
89
public void onScopeClosed (Context context ) {
90
+ addHistoryElement (context , EventName .SCOPE_CLOSE );
81
91
checkIfObservationWasStartedButNotStopped ("Invalid scope closing" , context );
82
92
}
83
93
84
94
@ Override
85
95
public void onScopeReset (Context context ) {
96
+ addHistoryElement (context , EventName .SCOPE_RESET );
86
97
checkIfObservationWasStartedButNotStopped ("Invalid scope resetting" , context );
87
98
}
88
99
89
100
@ Override
90
101
public void onStop (Context context ) {
102
+ addHistoryElement (context , EventName .STOP );
91
103
Status status = checkIfObservationWasStartedButNotStopped ("Invalid stop" , context );
92
104
if (status != null ) {
93
105
status .markStopped ();
@@ -99,6 +111,14 @@ public boolean supportsContext(Context context) {
99
111
return supportsContextPredicate .test (context );
100
112
}
101
113
114
+ private void addHistoryElement (Context context , EventName eventName ) {
115
+ if (!context .containsKey (History .class )) {
116
+ context .put (History .class , new History ());
117
+ }
118
+ History history = context .get (History .class );
119
+ history .addHistoryElement (eventName );
120
+ }
121
+
102
122
@ Nullable
103
123
private Status checkIfObservationWasStartedButNotStopped (String prefix , Context context ) {
104
124
Status status = context .get (Status .class );
@@ -113,7 +133,9 @@ else if (status.isStopped()) {
113
133
}
114
134
115
135
private static void throwInvalidObservationException (ValidationResult validationResult ) {
116
- throw new InvalidObservationException (validationResult .getMessage (), validationResult .getContext ());
136
+ History history = validationResult .getContext ().getOrDefault (History .class , new History ());
137
+ throw new InvalidObservationException (validationResult .getMessage (), validationResult .getContext (),
138
+ history .getHistoryElements ());
117
139
}
118
140
119
141
static class ValidationResult {
@@ -156,4 +178,18 @@ void markStopped() {
156
178
157
179
}
158
180
181
+ static class History {
182
+
183
+ private final List <HistoryElement > historyElements = new ArrayList <>();
184
+
185
+ private void addHistoryElement (EventName eventName ) {
186
+ historyElements .add (new HistoryElement (eventName ));
187
+ }
188
+
189
+ List <HistoryElement > getHistoryElements () {
190
+ return Collections .unmodifiableList (historyElements );
191
+ }
192
+
193
+ }
194
+
159
195
}
0 commit comments