1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.contrib.zpages;
18 
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.common.base.Splitter;
21 import com.sun.net.httpserver.HttpExchange;
22 import com.sun.net.httpserver.HttpHandler;
23 import io.opencensus.common.Scope;
24 import io.opencensus.trace.AttributeValue;
25 import io.opencensus.trace.Tracer;
26 import io.opencensus.trace.Tracing;
27 import java.io.IOException;
28 import java.net.URI;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 
35 /** An {@link HttpHandler} that can be used to render HTML pages using any {@code ZPageHandler}. */
36 final class ZPageHttpHandler implements HttpHandler {
37   private static final Tracer tracer = Tracing.getTracer();
38   private static final String HTTP_SERVER = "HttpServer";
39   private final ZPageHandler zpageHandler;
40   private final String httpServerSpanName;
41 
42   /** Constructs a new {@code ZPageHttpHandler}. */
ZPageHttpHandler(ZPageHandler zpageHandler)43   ZPageHttpHandler(ZPageHandler zpageHandler) {
44     this.zpageHandler = zpageHandler;
45     this.httpServerSpanName = HTTP_SERVER + zpageHandler.getUrlPath();
46     Tracing.getExportComponent()
47         .getSampledSpanStore()
48         .registerSpanNamesForCollection(Arrays.asList(httpServerSpanName));
49   }
50 
51   @Override
handle(HttpExchange httpExchange)52   public final void handle(HttpExchange httpExchange) throws IOException {
53     try (Scope ss =
54         tracer
55             .spanBuilderWithExplicitParent(httpServerSpanName, null)
56             .setRecordEvents(true)
57             .startScopedSpan()) {
58       tracer
59           .getCurrentSpan()
60           .putAttribute(
61               "/http/method ",
62               AttributeValue.stringAttributeValue(httpExchange.getRequestMethod()));
63       httpExchange.sendResponseHeaders(200, 0);
64       zpageHandler.emitHtml(
65           uriQueryToMap(httpExchange.getRequestURI()), httpExchange.getResponseBody());
66     } finally {
67       httpExchange.close();
68     }
69   }
70 
71   @VisibleForTesting
uriQueryToMap(URI uri)72   static Map<String, String> uriQueryToMap(URI uri) {
73     String query = uri.getQuery();
74     if (query == null) {
75       return Collections.emptyMap();
76     }
77     Map<String, String> result = new HashMap<String, String>();
78     for (String param : Splitter.on("&").split(query)) {
79       List<String> splits = Splitter.on("=").splitToList(param);
80       if (splits.size() > 1) {
81         result.put(splits.get(0), splits.get(1));
82       } else {
83         result.put(splits.get(0), "");
84       }
85     }
86     return result;
87   }
88 }
89