forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpExchangeAdapter.java
133 lines (112 loc) · 4.91 KB
/
HttpExchangeAdapter.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
package io.prometheus.metrics.exporter.httpserver;
import com.sun.net.httpserver.HttpExchange;
import io.prometheus.metrics.exporter.common.PrometheusHttpExchange;
import io.prometheus.metrics.exporter.common.PrometheusHttpRequest;
import io.prometheus.metrics.exporter.common.PrometheusHttpResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HttpExchangeAdapter implements PrometheusHttpExchange {
private final HttpExchange httpExchange;
private final HttpRequest request = new HttpRequest();
private final HttpResponse response = new HttpResponse();
private volatile boolean responseSent = false;
public HttpExchangeAdapter(HttpExchange httpExchange) {
this.httpExchange = httpExchange;
}
public class HttpRequest implements PrometheusHttpRequest {
@Override
public String getRequestPath() {
URI requestURI = httpExchange.getRequestURI();
String uri = requestURI.toString();
int qx = uri.indexOf('?');
if (qx != -1) {
uri = uri.substring(0, qx);
}
return uri;
}
@Override
public String getQueryString() {
return httpExchange.getRequestURI().getRawQuery();
}
@Override
public Enumeration<String> getHeaders(String name) {
List<String> headers = httpExchange.getRequestHeaders().get(name);
if (headers == null) {
return Collections.emptyEnumeration();
} else {
return Collections.enumeration(headers);
}
}
@Override
public String getMethod() {
return httpExchange.getRequestMethod();
}
}
public class HttpResponse implements PrometheusHttpResponse {
@Override
public void setHeader(String name, String value) {
httpExchange.getResponseHeaders().set(name, value);
}
@Override
public OutputStream sendHeadersAndGetBody(int statusCode, int contentLength) throws IOException {
if (responseSent) {
throw new IOException("Cannot send multiple HTTP responses for a single HTTP exchange.");
}
responseSent = true;
httpExchange.sendResponseHeaders(statusCode, contentLength);
return httpExchange.getResponseBody();
}
}
@Override
public HttpRequest getRequest() {
return request;
}
@Override
public HttpResponse getResponse() {
return response;
}
@Override
public void handleException(IOException e) throws IOException {
sendErrorResponseWithStackTrace(e);
}
@Override
public void handleException(RuntimeException e) {
sendErrorResponseWithStackTrace(e);
}
private void sendErrorResponseWithStackTrace(Exception requestHandlerException) {
if (!responseSent) {
responseSent = true;
try {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
printWriter.write("An Exception occurred while scraping metrics: ");
requestHandlerException.printStackTrace(new PrintWriter(printWriter));
byte[] stackTrace = stringWriter.toString().getBytes(StandardCharsets.UTF_8);
httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
httpExchange.sendResponseHeaders(500, stackTrace.length);
httpExchange.getResponseBody().write(stackTrace);
} catch (Exception errorWriterException) {
// We want to avoid logging so that we don't mess with application logs when the HTTPServer is used in a Java agent.
// However, if we can't even send an error response to the client there's nothing we can do but logging a message.
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "The Prometheus metrics HTTPServer caught an Exception during scrape and failed to send an error response to the client.", errorWriterException);
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Original Exception that caused the Prometheus scrape error:", requestHandlerException);
}
} else {
// If the exception occurs after response headers have been sent, it's too late to respond with HTTP 500.
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "The Prometheus metrics HTTPServer caught an Exception while trying to send the metrics response.", requestHandlerException);
}
}
@Override
public void close() {
httpExchange.close();
}
}