62
62
63
63
import java .util .Objects ;
64
64
import java .util .Optional ;
65
+ import java .util .UUID ;
65
66
import java .util .concurrent .CompletableFuture ;
66
67
import java .util .concurrent .CompletionStage ;
67
68
import java .util .concurrent .TimeUnit ;
69
+ import java .util .stream .Collectors ;
70
+ import java .util .stream .StreamSupport ;
68
71
69
72
/**
70
73
* Http server routes.
@@ -117,7 +120,21 @@ public CompletionStage<HttpResponse> apply(final HttpRequest request) {
117
120
createMetricName (request , BODY_SIZE_METRIC ),
118
121
request .entity ().getContentLengthOption ().orElse (0L ),
119
122
Optional .of (Units .BYTE ));
120
- // TODO(vkoskela): Add a request UUID and include in MDC. [MAI-462]
123
+ final UUID requestId = UUID .randomUUID ();
124
+ if (LOGGER .isTraceEnabled ()) {
125
+ LOGGER .trace ()
126
+ .setEvent ("http.in.start" )
127
+ .addContext ("requestId" , requestId )
128
+ .addData ("method" , request .method ().toString ())
129
+ .addData ("url" , request .getUri ().toString ())
130
+ .addData (
131
+ "headers" ,
132
+ StreamSupport .stream (request .getHeaders ().spliterator (), false )
133
+ .map (h -> h .name () + "=" + h .value ())
134
+ .collect (Collectors .toList ()))
135
+ .log ();
136
+ }
137
+ // TODO(ville): Add a request UUID and include in MDC.
121
138
LOGGER .trace ()
122
139
.setEvent ("http.in.start" )
123
140
.addData ("method" , request .method ())
@@ -132,23 +149,41 @@ public CompletionStage<HttpResponse> apply(final HttpRequest request) {
132
149
requestTimer .elapsed (TimeUnit .NANOSECONDS ),
133
150
Optional .of (Units .NANOSECOND ));
134
151
135
- final int responseStatusClass = response .status ().intValue () / 100 ;
152
+ final int responseStatus ;
153
+ if (response != null ) {
154
+ responseStatus = response .status ().intValue ();
155
+ } else {
156
+ // TODO(ville): Figure out how to intercept post-exception mapping.
157
+ responseStatus = 599 ;
158
+ }
159
+ final int responseStatusClass = responseStatus / 100 ;
136
160
for (final int i : STATUS_CLASSES ) {
137
161
_metrics .recordCounter (
138
162
createMetricName (request , String .format ("%s/%dxx" , STATUS_METRIC , i )),
139
163
responseStatusClass == i ? 1 : 0 );
140
164
}
141
165
142
- final LogBuilder log = LOGGER .trace ()
143
- .setEvent ("http.in" )
144
- .addData ("method" , request .method ())
145
- .addData ("url" , request .getUri ())
146
- .addData ("status" , response .status ().intValue ())
147
- .addData ("headers" , request .getHeaders ());
148
- if (failure != null ) {
149
- log .setEvent ("http.in.error" ).addData ("exception" , failure );
166
+ final LogBuilder log ;
167
+ if (failure != null || responseStatusClass == 5 ) {
168
+ log = LOGGER .info ().setEvent ("http.in.failure" );
169
+ if (failure != null ) {
170
+ log .setThrowable (failure );
171
+ }
172
+ if (!LOGGER .isTraceEnabled () && LOGGER .isInfoEnabled ()) {
173
+ log .addData ("method" , request .method ().toString ())
174
+ .addData ("url" , request .getUri ().toString ())
175
+ .addData (
176
+ "headers" ,
177
+ StreamSupport .stream (request .getHeaders ().spliterator (), false )
178
+ .map (h -> h .name () + "=" + h .value ())
179
+ .collect (Collectors .toList ()));
180
+ }
181
+ } else {
182
+ log = LOGGER .trace ().setEvent ("http.in.complete" );
150
183
}
151
- log .log ();
184
+ log .addContext ("requestId" , requestId )
185
+ .addData ("status" , responseStatus )
186
+ .log ();
152
187
});
153
188
}
154
189
0 commit comments