53
53
import com .arpnetworking .steno .LoggerFactory ;
54
54
import com .google .common .base .Charsets ;
55
55
import com .google .common .base .Stopwatch ;
56
+ import com .google .common .collect .ImmutableList ;
56
57
import com .google .common .io .Resources ;
57
58
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
58
59
import scala .compat .java8 .FutureConverters ;
59
60
import scala .concurrent .Future ;
60
61
import scala .concurrent .duration .FiniteDuration ;
61
62
62
- import java .util .List ;
63
+ import java .util .Objects ;
63
64
import java .util .Optional ;
64
65
import java .util .concurrent .CompletableFuture ;
65
66
import java .util .concurrent .CompletionStage ;
@@ -87,7 +88,7 @@ public Routes(
87
88
final PeriodicMetrics metrics ,
88
89
final String healthCheckPath ,
89
90
final String statusPath ,
90
- final List <SupplementalRoutes > supplementalRoutes ) {
91
+ final ImmutableList <SupplementalRoutes > supplementalRoutes ) {
91
92
_actorSystem = actorSystem ;
92
93
_metrics = metrics ;
93
94
_healthCheckPath = healthCheckPath ;
@@ -126,6 +127,14 @@ public CompletionStage<HttpResponse> apply(final HttpRequest request) {
126
127
createMetricName (request , REQUEST_METRIC ),
127
128
requestTimer .elapsed (TimeUnit .NANOSECONDS ),
128
129
Optional .of (Units .NANOSECOND ));
130
+
131
+ final int responseStatusClass = response .status ().intValue () / 100 ;
132
+ for (final int i : STATUS_CLASSES ) {
133
+ _metrics .recordCounter (
134
+ createMetricName (request , String .format ("%s/%dxx" , STATUS_METRIC , i )),
135
+ responseStatusClass == i ? 1 : 0 );
136
+ }
137
+
129
138
final LogBuilder log = LOGGER .trace ()
130
139
.setEvent ("http.in" )
131
140
.addData ("method" , request .method ())
@@ -141,12 +150,12 @@ public CompletionStage<HttpResponse> apply(final HttpRequest request) {
141
150
142
151
private CompletionStage <HttpResponse > process (final HttpRequest request ) {
143
152
final String path = request .getUri ().path ();
144
- if (HttpMethods . GET . equals (request .method ())) {
145
- if (TELEMETRY_STREAM_V1_PATH .equals (path )) {
153
+ if (Objects . equals (HttpMethods . GET , request .method ())) {
154
+ if (Objects .equals (TELEMETRY_STREAM_V1_PATH , path )) {
146
155
return getHttpResponseForTelemetry (request , TELEMETRY_V1_FACTORY );
147
- } else if (TELEMETRY_STREAM_V2_PATH .equals (path )) {
156
+ } else if (Objects .equals (TELEMETRY_STREAM_V2_PATH , path )) {
148
157
return getHttpResponseForTelemetry (request , TELEMETRY_V2_FACTORY );
149
- } else if (_healthCheckPath .equals (path )) {
158
+ } else if (Objects .equals (_healthCheckPath , path )) {
150
159
return ask ("/user/status" , Status .IS_HEALTHY , Boolean .FALSE )
151
160
.thenApply (
152
161
isHealthy -> HttpResponse .create ()
@@ -159,16 +168,16 @@ private CompletionStage<HttpResponse> process(final HttpRequest request) {
159
168
"{\" status\" :\" "
160
169
+ (isHealthy ? HEALTHY_STATE : UNHEALTHY_STATE )
161
170
+ "\" }" )));
162
- } else if (_statusPath .equals (path )) {
171
+ } else if (Objects .equals (_statusPath , path )) {
163
172
return CompletableFuture .completedFuture (
164
173
HttpResponse .create ()
165
174
.withStatus (StatusCodes .OK )
166
175
.withEntity (JSON_CONTENT_TYPE , ByteString .fromString (STATUS_JSON )));
167
176
}
168
- } else if (HttpMethods . POST . equals (request .method ())) {
169
- if (path .equals (COLLECTD_V1_SOURCE_PREFIX )) {
177
+ } else if (Objects . equals (HttpMethods . POST , request .method ())) {
178
+ if (Objects .equals (path , COLLECTD_V1_SOURCE_PREFIX )) {
170
179
return dispatchHttpRequest (request , ACTOR_COLLECTD_V1 );
171
- } else if (path .equals (APP_V1_SOURCE_PREFIX )) {
180
+ } else if (Objects .equals (path , APP_V1_SOURCE_PREFIX )) {
172
181
return dispatchHttpRequest (request , ACTOR_APP_V1 );
173
182
}
174
183
}
@@ -274,7 +283,7 @@ private String createMetricName(final HttpRequest request, final String actionPa
274
283
private final String _healthCheckPath ;
275
284
private final String _statusPath ;
276
285
@ SuppressFBWarnings ("SE_BAD_FIELD" )
277
- private final List <SupplementalRoutes > _supplementalRoutes ;
286
+ private final ImmutableList <SupplementalRoutes > _supplementalRoutes ;
278
287
279
288
private static final Logger LOGGER = LoggerFactory .getLogger (Routes .class );
280
289
@@ -291,6 +300,8 @@ private String createMetricName(final HttpRequest request, final String actionPa
291
300
private static final String REST_SERVICE_METRIC_ROOT = "rest_service/" ;
292
301
private static final String BODY_SIZE_METRIC = "body_size" ;
293
302
private static final String REQUEST_METRIC = "request" ;
303
+ private static final String STATUS_METRIC = "status" ;
304
+ private static final ImmutableList <Integer > STATUS_CLASSES = ImmutableList .of (2 , 3 , 4 , 5 );
294
305
295
306
// Ping
296
307
private static final HttpHeader PING_CACHE_CONTROL_HEADER = CacheControl .create (
0 commit comments