|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.api.client.util; |
| 16 | + |
| 17 | +import com.google.api.client.http.HttpHeaders; |
| 18 | +import com.google.api.client.http.HttpRequest; |
| 19 | +import com.google.api.client.http.HttpStatusCodes; |
| 20 | +import com.google.common.annotations.VisibleForTesting; |
| 21 | + |
| 22 | +import io.opencensus.contrib.http.util.HttpPropagationUtil; |
| 23 | +import io.opencensus.trace.BlankSpan; |
| 24 | +import io.opencensus.trace.EndSpanOptions; |
| 25 | +import io.opencensus.trace.NetworkEvent; |
| 26 | +import io.opencensus.trace.NetworkEvent.Type; |
| 27 | +import io.opencensus.trace.Span; |
| 28 | +import io.opencensus.trace.Status; |
| 29 | +import io.opencensus.trace.Tracer; |
| 30 | +import io.opencensus.trace.Tracing; |
| 31 | +import io.opencensus.trace.propagation.TextFormat; |
| 32 | + |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.concurrent.atomic.AtomicLong; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
| 37 | +import javax.annotation.Nullable; |
| 38 | + |
| 39 | +/** |
| 40 | + * Utilities for Census monitoring and tracing. |
| 41 | + * |
| 42 | + * @author Hailong Wen |
| 43 | + * @since 1.24 |
| 44 | + */ |
| 45 | +public class OpenCensusUtils { |
| 46 | + |
| 47 | + private static final Logger logger = Logger.getLogger(OpenCensusUtils.class.getName()); |
| 48 | + |
| 49 | + /** |
| 50 | + * Span name for tracing {@link HttpRequest#execute()}. |
| 51 | + */ |
| 52 | + public static final String SPAN_NAME_HTTP_REQUEST_EXECUTE = |
| 53 | + "Sent." + HttpRequest.class.getName() + ".execute"; |
| 54 | + |
| 55 | + /** |
| 56 | + * OpenCensus tracing component. When no OpenCensus implementation is provided, it will return a |
| 57 | + * no-op tracer. |
| 58 | + */ |
| 59 | + private static Tracer tracer = Tracing.getTracer(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Sequence id generator for message event. |
| 63 | + */ |
| 64 | + private static AtomicLong idGenerator = new AtomicLong(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Whether spans should be recorded locally. Defaults to true. |
| 68 | + */ |
| 69 | + private static volatile boolean isRecordEvent = true; |
| 70 | + |
| 71 | + /** |
| 72 | + * {@link TextFormat} used in tracing context propagation. |
| 73 | + */ |
| 74 | + @Nullable |
| 75 | + @VisibleForTesting |
| 76 | + static volatile TextFormat propagationTextFormat = null; |
| 77 | + |
| 78 | + /** |
| 79 | + * {@link TextFormat.Setter} for {@link #propagationTextFormat}. |
| 80 | + */ |
| 81 | + @Nullable |
| 82 | + @VisibleForTesting |
| 83 | + static volatile TextFormat.Setter propagationTextFormatSetter = null; |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets the {@link TextFormat} used in context propagation. |
| 87 | + * |
| 88 | + * <p>This API allows users of google-http-client to specify other text format, or disable context |
| 89 | + * propagation by setting it to {@code null}. It should be used along with {@link |
| 90 | + * #setPropagationTextFormatSetter} for setting purpose. </p> |
| 91 | + * |
| 92 | + * @param textFormat the text format. |
| 93 | + */ |
| 94 | + public static void setPropagationTextFormat(@Nullable TextFormat textFormat) { |
| 95 | + propagationTextFormat = textFormat; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets the {@link TextFormat.Setter} used in context propagation. |
| 100 | + * |
| 101 | + * <p>This API allows users of google-http-client to specify other text format setter, or disable |
| 102 | + * context propagation by setting it to {@code null}. It should be used along with {@link |
| 103 | + * #setPropagationTextFormat} for setting purpose. </p> |
| 104 | + * |
| 105 | + * @param textFormatSetter the {@code TextFormat.Setter} for the text format. |
| 106 | + */ |
| 107 | + public static void setPropagationTextFormatSetter(@Nullable TextFormat.Setter textFormatSetter) { |
| 108 | + propagationTextFormatSetter = textFormatSetter; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets whether spans should be recorded locally. |
| 113 | + * |
| 114 | + * <p> This API allows users of google-http-client to turn on/off local span collection. </p> |
| 115 | + * |
| 116 | + * @param recordEvent record span locally if true. |
| 117 | + */ |
| 118 | + public static void setIsRecordEvent(boolean recordEvent) { |
| 119 | + isRecordEvent = recordEvent; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the tracing component of OpenCensus. |
| 124 | + * |
| 125 | + * @return the tracing component of OpenCensus. |
| 126 | + */ |
| 127 | + public static Tracer getTracer() { |
| 128 | + return tracer; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns whether spans should be recorded locally. |
| 133 | + * |
| 134 | + * @return whether spans should be recorded locally. |
| 135 | + */ |
| 136 | + public static boolean isRecordEvent() { |
| 137 | + return isRecordEvent; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Propagate information of current tracing context. This information will be injected into HTTP |
| 142 | + * header. |
| 143 | + * |
| 144 | + * @param span the span to be propagated. |
| 145 | + * @param headers the headers used in propagation. |
| 146 | + */ |
| 147 | + public static void propagateTracingContext(Span span, HttpHeaders headers) { |
| 148 | + Preconditions.checkArgument(span != null, "span should not be null."); |
| 149 | + Preconditions.checkArgument(headers != null, "headers should not be null."); |
| 150 | + if (propagationTextFormat != null && propagationTextFormatSetter != null) { |
| 151 | + if (!span.equals(BlankSpan.INSTANCE)) { |
| 152 | + propagationTextFormat.inject(span.getContext(), headers, propagationTextFormatSetter); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns an {@link EndSpanOptions} to end a http span according to the status code. |
| 159 | + * |
| 160 | + * @param statusCode the status code, can be null to represent no valid response is returned. |
| 161 | + * @return an {@code EndSpanOptions} that best suits the status code. |
| 162 | + */ |
| 163 | + public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) { |
| 164 | + // Always sample the span, but optionally export it. |
| 165 | + EndSpanOptions.Builder builder = EndSpanOptions.builder(); |
| 166 | + if (statusCode == null) { |
| 167 | + builder.setStatus(Status.UNKNOWN); |
| 168 | + } else if (!HttpStatusCodes.isSuccess(statusCode)) { |
| 169 | + switch (statusCode) { |
| 170 | + case HttpStatusCodes.STATUS_CODE_BAD_REQUEST: |
| 171 | + builder.setStatus(Status.INVALID_ARGUMENT); |
| 172 | + break; |
| 173 | + case HttpStatusCodes.STATUS_CODE_UNAUTHORIZED: |
| 174 | + builder.setStatus(Status.UNAUTHENTICATED); |
| 175 | + break; |
| 176 | + case HttpStatusCodes.STATUS_CODE_FORBIDDEN: |
| 177 | + builder.setStatus(Status.PERMISSION_DENIED); |
| 178 | + break; |
| 179 | + case HttpStatusCodes.STATUS_CODE_NOT_FOUND: |
| 180 | + builder.setStatus(Status.NOT_FOUND); |
| 181 | + break; |
| 182 | + case HttpStatusCodes.STATUS_CODE_PRECONDITION_FAILED: |
| 183 | + builder.setStatus(Status.FAILED_PRECONDITION); |
| 184 | + break; |
| 185 | + case HttpStatusCodes.STATUS_CODE_SERVER_ERROR: |
| 186 | + builder.setStatus(Status.UNAVAILABLE); |
| 187 | + break; |
| 188 | + default: |
| 189 | + builder.setStatus(Status.UNKNOWN); |
| 190 | + } |
| 191 | + } else { |
| 192 | + builder.setStatus(Status.OK); |
| 193 | + } |
| 194 | + return builder.build(); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Records a new message event which contains the size of the request content. Note that the size |
| 199 | + * represents the message size in application layer, i.e., content-length. |
| 200 | + * |
| 201 | + * @param span The {@code span} in which the send event occurs. |
| 202 | + * @param size Size of the request. |
| 203 | + */ |
| 204 | + public static void recordSentMessageEvent(Span span, long size) { |
| 205 | + recordMessageEvent(span, size, Type.SENT); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Records a new message event which contains the size of the response content. Note that the size |
| 210 | + * represents the message size in application layer, i.e., content-length. |
| 211 | + * |
| 212 | + * @param span The {@code span} in which the receive event occurs. |
| 213 | + * @param size Size of the response. |
| 214 | + */ |
| 215 | + public static void recordReceivedMessageEvent(Span span, long size) { |
| 216 | + recordMessageEvent(span, size, Type.RECV); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Records a message event of a certain {@link NetworkEvent.Type}. This method is package |
| 221 | + * protected since {@link NetworkEvent} might be deprecated in future releases. |
| 222 | + * |
| 223 | + * @param span The {@code span} in which the event occurs. |
| 224 | + * @param size Size of the message. |
| 225 | + * @param eventType The {@code NetworkEvent.Type} of the message event. |
| 226 | + */ |
| 227 | + @VisibleForTesting |
| 228 | + static void recordMessageEvent(Span span, long size, Type eventType) { |
| 229 | + Preconditions.checkArgument(span != null, "span should not be null."); |
| 230 | + if (size < 0) { |
| 231 | + size = 0; |
| 232 | + } |
| 233 | + NetworkEvent event = NetworkEvent |
| 234 | + .builder(eventType, idGenerator.getAndIncrement()) |
| 235 | + .setUncompressedMessageSize(size) |
| 236 | + .build(); |
| 237 | + span.addNetworkEvent(event); |
| 238 | + } |
| 239 | + |
| 240 | + static { |
| 241 | + try { |
| 242 | + propagationTextFormat = HttpPropagationUtil.getCloudTraceFormat(); |
| 243 | + propagationTextFormatSetter = new TextFormat.Setter<HttpHeaders>() { |
| 244 | + @Override |
| 245 | + public void put(HttpHeaders carrier, String key, String value) { |
| 246 | + carrier.set(key, value); |
| 247 | + } |
| 248 | + }; |
| 249 | + } catch (Exception e) { |
| 250 | + logger.log( |
| 251 | + Level.WARNING, "Cannot initialize default OpenCensus HTTP propagation text format.", e); |
| 252 | + } |
| 253 | + |
| 254 | + try { |
| 255 | + Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection( |
| 256 | + Collections.<String>singletonList(SPAN_NAME_HTTP_REQUEST_EXECUTE)); |
| 257 | + } catch (Exception e) { |
| 258 | + logger.log( |
| 259 | + Level.WARNING, "Cannot register default OpenCensus span names for collection.", e); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + private OpenCensusUtils() {} |
| 264 | +} |
0 commit comments