|
22 | 22 | import com.google.common.annotations.VisibleForTesting; |
23 | 23 | import io.opentelemetry.api.common.AttributeKey; |
24 | 24 | import io.opentelemetry.api.trace.Span; |
25 | | -import io.opentelemetry.api.trace.StatusCode; |
26 | 25 | import io.opentelemetry.api.trace.Tracer; |
27 | | - |
28 | | -import java.io.BufferedInputStream; |
29 | | -import java.io.BufferedReader; |
30 | 26 | import java.io.IOException; |
31 | | -import java.io.InputStream; |
32 | | -import java.io.InputStreamReader; |
33 | | -import java.net.URI; |
34 | | -import java.net.URISyntaxException; |
35 | | -import java.nio.charset.StandardCharsets; |
36 | | -import java.util.Arrays; |
37 | | -import java.util.Collections; |
38 | | -import java.util.HashSet; |
39 | | -import java.util.Set; |
40 | | -import java.util.concurrent.atomic.AtomicBoolean; |
41 | | -import java.util.concurrent.atomic.AtomicLong; |
42 | | -import java.util.stream.Collectors; |
43 | 27 |
|
44 | 28 | /** |
45 | 29 | * HttpRequestInitializer that wraps a delegate initializer, intercepts all HTTP requests, adds |
|
49 | 33 | @InternalApi |
50 | 34 | public class HttpTracingRequestInitializer implements HttpRequestInitializer { |
51 | 35 |
|
52 | | - // HTTP Specific Telemetry Attributes |
53 | | - public static final AttributeKey<String> HTTP_REQUEST_METHOD = |
54 | | - AttributeKey.stringKey("http.request.method"); |
55 | | - public static final AttributeKey<String> URL_FULL = AttributeKey.stringKey("url.full"); |
56 | | - public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template"); |
57 | | - public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain"); |
58 | | - public static final AttributeKey<Long> HTTP_RESPONSE_STATUS_CODE = |
59 | | - AttributeKey.longKey("http.response.status_code"); |
60 | | - public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT = |
61 | | - AttributeKey.longKey("http.request.resend_count"); |
62 | | - public static final AttributeKey<Long> HTTP_REQUEST_BODY_SIZE = |
63 | | - AttributeKey.longKey("http.request.body.size"); |
64 | | - public static final AttributeKey<Long> HTTP_RESPONSE_BODY_SIZE = |
65 | | - AttributeKey.longKey("http.response.body.size"); |
| 36 | + // HTTP Specific Telemetry Attributes |
| 37 | + public static final AttributeKey<String> HTTP_REQUEST_METHOD = |
| 38 | + AttributeKey.stringKey("http.request.method"); |
| 39 | + public static final AttributeKey<String> URL_FULL = AttributeKey.stringKey("url.full"); |
| 40 | + public static final AttributeKey<String> URL_TEMPLATE = AttributeKey.stringKey("url.template"); |
| 41 | + public static final AttributeKey<String> URL_DOMAIN = AttributeKey.stringKey("url.domain"); |
| 42 | + public static final AttributeKey<Long> HTTP_RESPONSE_STATUS_CODE = |
| 43 | + AttributeKey.longKey("http.response.status_code"); |
| 44 | + public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT = |
| 45 | + AttributeKey.longKey("http.request.resend_count"); |
| 46 | + public static final AttributeKey<Long> HTTP_REQUEST_BODY_SIZE = |
| 47 | + AttributeKey.longKey("http.request.body.size"); |
| 48 | + public static final AttributeKey<Long> HTTP_RESPONSE_BODY_SIZE = |
| 49 | + AttributeKey.longKey("http.response.body.size"); |
66 | 50 |
|
67 | | - @VisibleForTesting static final String HTTP_RPC_SYSTEM_NAME = "http"; |
| 51 | + @VisibleForTesting static final String HTTP_RPC_SYSTEM_NAME = "http"; |
68 | 52 |
|
| 53 | + private final HttpRequestInitializer delegate; |
| 54 | + private final Tracer tracer; |
69 | 55 |
|
70 | | - private final HttpRequestInitializer delegate; |
71 | | - private final Tracer tracer; |
| 56 | + public HttpTracingRequestInitializer(HttpRequestInitializer delegate, Tracer tracer) { |
| 57 | + this.delegate = delegate; |
| 58 | + this.tracer = tracer; |
| 59 | + } |
72 | 60 |
|
73 | | - public HttpTracingRequestInitializer( |
74 | | - HttpRequestInitializer delegate, Tracer tracer) { |
75 | | - this.delegate = delegate; |
76 | | - this.tracer = tracer; |
| 61 | + @Override |
| 62 | + public void initialize(HttpRequest request) throws IOException { |
| 63 | + if (delegate != null) { |
| 64 | + delegate.initialize(request); |
77 | 65 | } |
78 | 66 |
|
79 | | - @Override |
80 | | - public void initialize(HttpRequest request) throws IOException { |
81 | | - if (delegate != null) { |
82 | | - delegate.initialize(request); |
83 | | - } |
84 | | - |
85 | | - if (tracer == null) { |
86 | | - return; |
87 | | - } |
| 67 | + if (tracer == null) { |
| 68 | + return; |
| 69 | + } |
88 | 70 |
|
89 | | - // Get the current active span (created by HttpBigQueryRpc) and add HTTP attributes to it |
90 | | - Span span = Span.current(); |
91 | | - if (!span.getSpanContext().isValid()) { |
92 | | - // No active span to exists, skip instrumentation |
93 | | - return; |
94 | | - } |
| 71 | + // Get the current active span (created by HttpBigQueryRpc) and add HTTP attributes to it |
| 72 | + Span span = Span.current(); |
| 73 | + if (!span.getSpanContext().isValid()) { |
| 74 | + // No active span to exists, skip instrumentation |
| 75 | + return; |
| 76 | + } |
95 | 77 |
|
96 | | - String host = request.getUrl().getHost(); |
97 | | - Integer port = request.getUrl().getPort(); |
| 78 | + String host = request.getUrl().getHost(); |
| 79 | + Integer port = request.getUrl().getPort(); |
98 | 80 |
|
99 | | - // Add initial HTTP attributes to the existing span |
100 | | - addInitialHttpAttributesToSpan(span, host, port); |
101 | | - } |
| 81 | + // Add initial HTTP attributes to the existing span |
| 82 | + addInitialHttpAttributesToSpan(span, host, port); |
| 83 | + } |
102 | 84 |
|
103 | | - /** Add initial HTTP attributes to the existing active span */ |
104 | | - private void addInitialHttpAttributesToSpan( |
105 | | - Span span, String host, Integer port) { |
106 | | - BigQueryTelemetryTracer.addCommonAttributeToSpan(span); |
107 | | - span.setAttribute(BigQueryTelemetryTracer.RPC_SYSTEM_NAME, HTTP_RPC_SYSTEM_NAME); |
108 | | - span.setAttribute(BigQueryTelemetryTracer.SERVER_ADDRESS, host); |
109 | | - if (port != null && port > 0) { |
110 | | - span.setAttribute(BigQueryTelemetryTracer.SERVER_PORT, port.longValue()); |
111 | | - } |
112 | | - // TODO add full sanitized url, url domain, request method |
| 85 | + /** Add initial HTTP attributes to the existing active span */ |
| 86 | + private void addInitialHttpAttributesToSpan(Span span, String host, Integer port) { |
| 87 | + BigQueryTelemetryTracer.addCommonAttributeToSpan(span); |
| 88 | + span.setAttribute(BigQueryTelemetryTracer.RPC_SYSTEM_NAME, HTTP_RPC_SYSTEM_NAME); |
| 89 | + span.setAttribute(BigQueryTelemetryTracer.SERVER_ADDRESS, host); |
| 90 | + if (port != null && port > 0) { |
| 91 | + span.setAttribute(BigQueryTelemetryTracer.SERVER_PORT, port.longValue()); |
113 | 92 | } |
| 93 | + // TODO add full sanitized url, url domain, request method |
| 94 | + } |
114 | 95 | } |
0 commit comments