-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathFrontend.java
79 lines (67 loc) · 2.99 KB
/
Frontend.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
package brave.example;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.google.common.net.HostAndPort;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ext.RuntimeDelegate;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.nio.charset.StandardCharsets.UTF_8;
public final class Frontend {
static final Logger LOGGER = LoggerFactory.getLogger(Frontend.class);
@Path("")
public static class Resource {
final Session session;
final PreparedStatement preparedStatement;
Resource(Session session) {
this.session = session;
// Normal backend example returns the date, possibly with "userName" baggage field.
// There's no current way to select baggage inside CQL, so we skip it and just print the date.
this.preparedStatement = session.prepare("SELECT toTimestamp(now()) from system.local");
}
@GET public String callBackend() {
Date date = session.execute(preparedStatement.bind()).one().getTimestamp(0);
return date + "\n";
}
}
/** Fake /health endpoint that allows us to ensure our HEALTHCHECK doesn't start traces. */
public static class HealthCheck implements HttpHandler {
static final byte[] body = "ok\n".getBytes(UTF_8);
@Override public void handle(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(200, body.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(body);
}
}
}
public static void main(String[] args) throws Exception {
String contactPointString = System.getProperty("backend.endpoint", "127.0.0.1:9042");
HostAndPort parsed = HostAndPort.fromString(contactPointString).withDefaultPort(9042);
LOGGER.info("Using contact point: {}", parsed);
Cluster cluster = Cluster.builder()
.addContactPointsWithPorts(new InetSocketAddress(parsed.getHost(), parsed.getPort()))
.build();
Runtime.getRuntime().addShutdownHook(new Thread(cluster::close));
TracingConfiguration tracing = TracingConfiguration.create("frontend");
ResourceConfig config = new ResourceConfig();
config.register(new Resource(tracing.tracingSession(cluster.connect(), "backend")));
config.register(tracing.tracingListener());
HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(config, HttpHandler.class);
HttpServer server = HttpServer.create(new InetSocketAddress(8081), 0);
Runtime.getRuntime().addShutdownHook(new Thread(() -> server.stop(0)));
server.createContext("/", handler);
server.createContext("/health", new HealthCheck());
server.start();
Thread.currentThread().join();
}
}