26
26
import java .util .concurrent .FutureTask ;
27
27
import java .util .concurrent .ThreadFactory ;
28
28
import java .util .concurrent .atomic .AtomicInteger ;
29
+ import java .util .logging .Logger ;
29
30
import java .util .zip .GZIPOutputStream ;
30
31
31
32
import com .sun .net .httpserver .Authenticator ;
@@ -68,6 +69,10 @@ protected ByteArrayOutputStream initialValue()
68
69
* Handles Metrics collections from the given registry.
69
70
*/
70
71
public static class HTTPMetricHandler implements HttpHandler {
72
+
73
+ private static final Logger LOGGER = Logger .getLogger (HTTPMetricHandler .class .getName ());
74
+ private static final String INTERNAL_SERVER_ERROR = "Internal Server Error" ;
75
+
71
76
private final CollectorRegistry registry ;
72
77
private final LocalByteArray response = new LocalByteArray ();
73
78
private final Supplier <Predicate <String >> sampleNameFilterSupplier ;
@@ -83,49 +88,63 @@ public HTTPMetricHandler(CollectorRegistry registry, Supplier<Predicate<String>>
83
88
}
84
89
85
90
@ Override
86
- public void handle (HttpExchange t ) throws IOException {
87
- String query = t .getRequestURI ().getRawQuery ();
88
- String contextPath = t .getHttpContext ().getPath ();
89
- ByteArrayOutputStream response = this .response .get ();
90
- response .reset ();
91
- OutputStreamWriter osw = new OutputStreamWriter (response , Charset .forName ("UTF-8" ));
92
- if ("/-/healthy" .equals (contextPath )) {
93
- osw .write (HEALTHY_RESPONSE );
94
- } else {
95
- String contentType = TextFormat .chooseContentType (t .getRequestHeaders ().getFirst ("Accept" ));
96
- t .getResponseHeaders ().set ("Content-Type" , contentType );
97
- Predicate <String > filter = sampleNameFilterSupplier == null ? null : sampleNameFilterSupplier .get ();
98
- filter = SampleNameFilter .restrictToNamesEqualTo (filter , parseQuery (query ));
99
- if (filter == null ) {
100
- TextFormat .writeFormat (contentType , osw , registry .metricFamilySamples ());
91
+ public void handle (HttpExchange httpExchange ) {
92
+ try {
93
+ String query = httpExchange .getRequestURI ().getRawQuery ();
94
+ String contextPath = httpExchange .getHttpContext ().getPath ();
95
+ ByteArrayOutputStream response = this .response .get ();
96
+ response .reset ();
97
+ OutputStreamWriter osw = new OutputStreamWriter (response , Charset .forName ("UTF-8" ));
98
+ if ("/-/healthy" .equals (contextPath )) {
99
+ osw .write (HEALTHY_RESPONSE );
101
100
} else {
102
- TextFormat .writeFormat (contentType , osw , registry .filteredMetricFamilySamples (filter ));
101
+ String contentType = TextFormat .chooseContentType (httpExchange .getRequestHeaders ().getFirst ("Accept" ));
102
+ httpExchange .getResponseHeaders ().set ("Content-Type" , contentType );
103
+ Predicate <String > filter = sampleNameFilterSupplier == null ? null : sampleNameFilterSupplier .get ();
104
+ filter = SampleNameFilter .restrictToNamesEqualTo (filter , parseQuery (query ));
105
+ if (filter == null ) {
106
+ TextFormat .writeFormat (contentType , osw , registry .metricFamilySamples ());
107
+ } else {
108
+ TextFormat .writeFormat (contentType , osw , registry .filteredMetricFamilySamples (filter ));
109
+ }
103
110
}
104
- }
105
111
106
- osw .close ();
112
+ osw .close ();
113
+
114
+ if (shouldUseCompression (httpExchange )) {
115
+ httpExchange .getResponseHeaders ().set ("Content-Encoding" , "gzip" );
116
+ httpExchange .sendResponseHeaders (HttpURLConnection .HTTP_OK , 0 );
117
+ final GZIPOutputStream os = new GZIPOutputStream (httpExchange .getResponseBody ());
118
+ try {
119
+ response .writeTo (os );
120
+ } finally {
121
+ os .close ();
122
+ }
123
+ } else {
124
+ long contentLength = response .size ();
125
+ if (contentLength > 0 ) {
126
+ httpExchange .getResponseHeaders ().set ("Content-Length" , String .valueOf (contentLength ));
127
+ }
128
+ if (httpExchange .getRequestMethod ().equals ("HEAD" )) {
129
+ contentLength = -1 ;
130
+ }
131
+ httpExchange .sendResponseHeaders (HttpURLConnection .HTTP_OK , contentLength );
132
+ response .writeTo (httpExchange .getResponseBody ());
133
+ }
134
+ httpExchange .close ();
135
+ } catch (Throwable throwable ) {
136
+ LOGGER .warning ("Exception handling request, " + throwable .getMessage ());
137
+ LOGGER .throwing (HTTPMetricHandler .class .getName (), "handle" , throwable );
107
138
108
- if (shouldUseCompression (t )) {
109
- t .getResponseHeaders ().set ("Content-Encoding" , "gzip" );
110
- t .sendResponseHeaders (HttpURLConnection .HTTP_OK , 0 );
111
- final GZIPOutputStream os = new GZIPOutputStream (t .getResponseBody ());
112
139
try {
113
- response .writeTo (os );
114
- } finally {
115
- os .close ();
116
- }
117
- } else {
118
- long contentLength = response .size ();
119
- if (contentLength > 0 ) {
120
- t .getResponseHeaders ().set ("Content-Length" , String .valueOf (contentLength ));
121
- }
122
- if (t .getRequestMethod ().equals ("HEAD" )) {
123
- contentLength = -1 ;
140
+ byte [] internalServerErrorBytes = INTERNAL_SERVER_ERROR .getBytes ("UTF-8" );
141
+ httpExchange .getRequestHeaders ().set ("Content-Type" , "text/plain" );
142
+ httpExchange .sendResponseHeaders (HttpURLConnection .HTTP_INTERNAL_ERROR , internalServerErrorBytes .length );
143
+ httpExchange .getResponseBody ().write (internalServerErrorBytes );
144
+ } catch (Throwable throwable2 ) {
145
+ // Ignore since the response may already be partially committed
124
146
}
125
- t .sendResponseHeaders (HttpURLConnection .HTTP_OK , contentLength );
126
- response .writeTo (t .getResponseBody ());
127
147
}
128
- t .close ();
129
148
}
130
149
}
131
150
0 commit comments