|
| 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 static com.google.common.base.Strings.isNullOrEmpty; |
| 20 | + |
| 21 | +import com.google.common.base.Charsets; |
| 22 | +import io.opencensus.trace.config.TraceConfig; |
| 23 | +import io.opencensus.trace.config.TraceParams; |
| 24 | +import io.opencensus.trace.samplers.Samplers; |
| 25 | +import java.io.BufferedWriter; |
| 26 | +import java.io.OutputStream; |
| 27 | +import java.io.OutputStreamWriter; |
| 28 | +import java.io.PrintWriter; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +// TODO(bdrutu): Add tests. |
| 32 | +/** |
| 33 | + * HTML page formatter for tracing config. The page displays information about the current active |
| 34 | + * tracing configuration and allows users to change it. |
| 35 | + */ |
| 36 | +final class TraceConfigzZPageHandler extends ZPageHandler { |
| 37 | + private static final String TRACE_CONFIGZ_URL = "/traceconfigz"; |
| 38 | + private final TraceConfig traceConfig; |
| 39 | + |
| 40 | + private static final String CHANGE = "change"; |
| 41 | + private static final String PERMANENT_CHANGE = "permanently"; |
| 42 | + private static final String RESTORE_DEFAULT_CHANGE = "restore_default"; |
| 43 | + private static final String QUERY_COMPONENT_SAMPLING_PROBABILITY = "samplingprobability"; |
| 44 | + private static final String QUERY_COMPONENT_MAX_NUMBER_OF_ATTRIBUTES = "maxnumberofattributes"; |
| 45 | + private static final String QUERY_COMPONENT_MAX_NUMBER_OF_ANNOTATIONS = "maxnumberofannotations"; |
| 46 | + private static final String QUERY_COMPONENT_MAX_NUMBER_OF_NETWORK_EVENTS = |
| 47 | + "maxnumberofnetworkevents"; |
| 48 | + private static final String QUERY_COMPONENT_MAX_NUMBER_OF_LINKS = "maxnumberoflinks"; |
| 49 | + |
| 50 | + // TODO(bdrutu): Use post. |
| 51 | + // TODO(bdrutu): Refactor this to not use a big "printf". |
| 52 | + private static final String TRACECONFIGZ_FORM_BODY = |
| 53 | + "<form action=/traceconfigz method=get>%n" |
| 54 | + // Permanently changes table. |
| 55 | + + "<table>%n" |
| 56 | + + "<td colspan=\"3\"><b>Permanently change</b> " |
| 57 | + + "<input type=\"hidden\" name=\"%s\" value=\"%s\"></td>%n" |
| 58 | + + "<tr><td>SamplingProbability to</td> " |
| 59 | + + "<td><input type=text size=10 name=%s value=\"\"></td> <td>(%s)</td>%n" |
| 60 | + + "<tr><td>MaxNumberOfAttributes to</td> " |
| 61 | + + "<td><input type=text size=10 name=%s value=\"\"></td> <td>(%d)</td>%n" |
| 62 | + + "<tr><td>MaxNumberOfAnnotations to</td>" |
| 63 | + + "<td><input type=text size=10 name=%s value=\"\"></td> <td>(%d)</td>%n" |
| 64 | + + "<tr><td>MaxNumberOfNetworkEvents to</td> " |
| 65 | + + "<td><input type=text size=10 name=%s value=\"\"></td> <td>(%d)</td>%n" |
| 66 | + + "<tr><td>MaxNumberOfLinks to</td>" |
| 67 | + + "<td><input type=text size=10 name=%s value=\"\"></td> <td>(%d)</td>%n" |
| 68 | + + "</table>%n" |
| 69 | + // Submit button. |
| 70 | + + "<input type=submit value=Submit>%n" |
| 71 | + + "</form>"; |
| 72 | + |
| 73 | + private static final String RESTORE_DEFAULT_FORM_BODY = |
| 74 | + "<form action=/traceconfigz method=get>%n" |
| 75 | + // Restore to default. |
| 76 | + + "<b>Restore default</b> %n" |
| 77 | + + "<input type=\"hidden\" name=\"%s\" value=\"%s\"></td>%n" |
| 78 | + + "</br>%n" |
| 79 | + // Reset button. |
| 80 | + + "<input type=submit value=Reset>%n" |
| 81 | + + "</form>"; |
| 82 | + |
| 83 | + static TraceConfigzZPageHandler create(TraceConfig traceConfig) { |
| 84 | + return new TraceConfigzZPageHandler(traceConfig); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getUrlPath() { |
| 89 | + return TRACE_CONFIGZ_URL; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void emitHtml(Map<String, String> queryMap, OutputStream outputStream) { |
| 94 | + PrintWriter out = |
| 95 | + new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, Charsets.UTF_8))); |
| 96 | + out.write("<!DOCTYPE html>\n"); |
| 97 | + out.write("<html lang=\"en\"><head>\n"); |
| 98 | + out.write("<meta charset=\"utf-8\">\n"); |
| 99 | + out.write("<title>TraceConfigZ</title>\n"); |
| 100 | + out.write("<link rel=\"shortcut icon\" href=\"//www.opencensus.io/favicon.ico\"/>\n"); |
| 101 | + out.write("</head>\n"); |
| 102 | + out.write("<body>\n"); |
| 103 | + try { |
| 104 | + // Work that can throw exceptions. |
| 105 | + maybeApplyChanges(queryMap); |
| 106 | + } finally { |
| 107 | + // TODO(bdrutu): Maybe display to the page if an exception happened. |
| 108 | + // Display the page in any case. |
| 109 | + out.printf( |
| 110 | + TRACECONFIGZ_FORM_BODY, |
| 111 | + CHANGE, |
| 112 | + PERMANENT_CHANGE, |
| 113 | + QUERY_COMPONENT_SAMPLING_PROBABILITY, |
| 114 | + "0.0001", // TODO(bdrutu): Get this from the default sampler (if possible). |
| 115 | + QUERY_COMPONENT_MAX_NUMBER_OF_ATTRIBUTES, |
| 116 | + TraceParams.DEFAULT.getMaxNumberOfAttributes(), |
| 117 | + QUERY_COMPONENT_MAX_NUMBER_OF_ANNOTATIONS, |
| 118 | + TraceParams.DEFAULT.getMaxNumberOfAnnotations(), |
| 119 | + QUERY_COMPONENT_MAX_NUMBER_OF_NETWORK_EVENTS, |
| 120 | + TraceParams.DEFAULT.getMaxNumberOfNetworkEvents(), |
| 121 | + QUERY_COMPONENT_MAX_NUMBER_OF_LINKS, |
| 122 | + TraceParams.DEFAULT.getMaxNumberOfLinks()); |
| 123 | + out.write("<br>\n"); |
| 124 | + out.printf(RESTORE_DEFAULT_FORM_BODY, CHANGE, RESTORE_DEFAULT_CHANGE); |
| 125 | + out.write("<br>\n"); |
| 126 | + emitTraceParamsTable(traceConfig.getActiveTraceParams(), out); |
| 127 | + out.write("</body>\n"); |
| 128 | + out.write("</html>\n"); |
| 129 | + out.close(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // If this is a supported change (currently only permanent changes are supported) apply it. |
| 134 | + private void maybeApplyChanges(Map<String, String> queryMap) { |
| 135 | + String changeStr = queryMap.get(CHANGE); |
| 136 | + if (PERMANENT_CHANGE.equals(changeStr)) { |
| 137 | + TraceParams.Builder traceParamsBuilder = traceConfig.getActiveTraceParams().toBuilder(); |
| 138 | + String samplingProbabilityStr = queryMap.get(QUERY_COMPONENT_SAMPLING_PROBABILITY); |
| 139 | + if (!isNullOrEmpty(samplingProbabilityStr)) { |
| 140 | + double samplingProbability = Double.parseDouble(samplingProbabilityStr); |
| 141 | + traceParamsBuilder.setSampler(Samplers.probabilitySampler(samplingProbability)); |
| 142 | + } |
| 143 | + String maxNumberOfAttributesStr = queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_ATTRIBUTES); |
| 144 | + if (!isNullOrEmpty(maxNumberOfAttributesStr)) { |
| 145 | + int maxNumberOfAttributes = Integer.parseInt(maxNumberOfAttributesStr); |
| 146 | + traceParamsBuilder.setMaxNumberOfAttributes(maxNumberOfAttributes); |
| 147 | + } |
| 148 | + String maxNumberOfAnnotationsStr = queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_ANNOTATIONS); |
| 149 | + if (!isNullOrEmpty(maxNumberOfAnnotationsStr)) { |
| 150 | + int maxNumberOfAnnotations = Integer.parseInt(maxNumberOfAnnotationsStr); |
| 151 | + traceParamsBuilder.setMaxNumberOfAnnotations(maxNumberOfAnnotations); |
| 152 | + } |
| 153 | + String maxNumberOfNetworkEventsStr = |
| 154 | + queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_NETWORK_EVENTS); |
| 155 | + if (!isNullOrEmpty(maxNumberOfNetworkEventsStr)) { |
| 156 | + int maxNumberOfNetworkEvents = Integer.parseInt(maxNumberOfNetworkEventsStr); |
| 157 | + traceParamsBuilder.setMaxNumberOfNetworkEvents(maxNumberOfNetworkEvents); |
| 158 | + } |
| 159 | + String maxNumverOfLinksStr = queryMap.get(QUERY_COMPONENT_MAX_NUMBER_OF_LINKS); |
| 160 | + if (!isNullOrEmpty(maxNumverOfLinksStr)) { |
| 161 | + int maxNumberOfLinks = Integer.parseInt(maxNumverOfLinksStr); |
| 162 | + traceParamsBuilder.setMaxNumberOfLinks(maxNumberOfLinks); |
| 163 | + } |
| 164 | + traceConfig.updateActiveTraceParams(traceParamsBuilder.build()); |
| 165 | + } else if (RESTORE_DEFAULT_CHANGE.equals(changeStr)) { |
| 166 | + traceConfig.updateActiveTraceParams(TraceParams.DEFAULT); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Prints a table to a PrintWriter that shows existing trace parameters. |
| 171 | + private static void emitTraceParamsTable(TraceParams params, PrintWriter out) { |
| 172 | + out.write( |
| 173 | + "<b>Active tracing parameters:</b><br>\n" |
| 174 | + + "<table rules=\"all\" frame=\"border\">\n" |
| 175 | + + " <tr style=\"background: #eee\">\n" |
| 176 | + + " <td><b>Name</b></td>\n" |
| 177 | + + " <td><b>Value</b></td>\n" |
| 178 | + + " </tr>\n"); |
| 179 | + out.printf( |
| 180 | + " <tr>%n <td>Sampler</td>%n <td>%s</td>%n </tr>%n", |
| 181 | + params.getSampler().getDescription()); |
| 182 | + out.printf( |
| 183 | + " <tr>%n <td>MaxNumberOfAttributes</td>%n <td>%d</td>%n </tr>%n", |
| 184 | + params.getMaxNumberOfAttributes()); |
| 185 | + out.printf( |
| 186 | + " <tr>%n <td>MaxNumberOfAnnotations</td>%n <td>%d</td>%n </tr>%n", |
| 187 | + params.getMaxNumberOfAnnotations()); |
| 188 | + out.printf( |
| 189 | + " <tr>%n <td>MaxNumberOfNetworkEvents</td>%n <td>%d</td>%n </tr>%n", |
| 190 | + params.getMaxNumberOfNetworkEvents()); |
| 191 | + out.printf( |
| 192 | + " <tr>%n <td>MaxNumberOfLinks</td>%n <td>%d</td>%n </tr>%n", |
| 193 | + params.getMaxNumberOfLinks()); |
| 194 | + |
| 195 | + out.write("</table>\n"); |
| 196 | + } |
| 197 | + |
| 198 | + private TraceConfigzZPageHandler(TraceConfig traceConfig) { |
| 199 | + this.traceConfig = traceConfig; |
| 200 | + } |
| 201 | +} |
0 commit comments