forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHttpTracer.java
169 lines (153 loc) · 6.38 KB
/
HttpTracer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.http;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.opensearch.common.Nullable;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;
import java.util.List;
/**
* Http request trace logger. See {@link #maybeTraceRequest(RestRequest, Exception)} for details.
*
* @opensearch.internal
*/
class HttpTracer {
private final Logger logger = LogManager.getLogger(HttpTracer.class);
private volatile String[] tracerLogInclude;
private volatile String[] tracerLogExclude;
HttpTracer(Settings settings, ClusterSettings clusterSettings) {
setTracerLogInclude(HttpTransportSettings.SETTING_HTTP_TRACE_LOG_INCLUDE.get(settings));
setTracerLogExclude(HttpTransportSettings.SETTING_HTTP_TRACE_LOG_EXCLUDE.get(settings));
clusterSettings.addSettingsUpdateConsumer(HttpTransportSettings.SETTING_HTTP_TRACE_LOG_INCLUDE, this::setTracerLogInclude);
clusterSettings.addSettingsUpdateConsumer(HttpTransportSettings.SETTING_HTTP_TRACE_LOG_EXCLUDE, this::setTracerLogExclude);
}
/**
* Logs the given request if request tracing is enabled and the request uri matches the current include and exclude patterns defined
* in {@link HttpTransportSettings#SETTING_HTTP_TRACE_LOG_INCLUDE} and {@link HttpTransportSettings#SETTING_HTTP_TRACE_LOG_EXCLUDE}.
* If the request was logged returns a logger to log sending the response with or {@code null} otherwise.
*
* @param restRequest Rest request to trace
* @param e Exception when handling the request or {@code null} if none
* @return This instance to use for logging the response via {@link #traceResponse} to this request if it was logged or
* {@code null} if the request wasn't logged
*/
@Nullable
HttpTracer maybeTraceRequest(RestRequest restRequest, @Nullable Exception e) {
if (logger.isTraceEnabled() && TransportService.shouldTraceAction(restRequest.uri(), tracerLogInclude, tracerLogExclude)) {
logger.trace(
new ParameterizedMessage(
"[{}][{}][{}][{}] received request from [{}]",
restRequest.getRequestId(),
restRequest.header(Task.X_OPAQUE_ID),
restRequest.method(),
restRequest.uri(),
restRequest.getHttpChannel()
),
e
);
return this;
}
return null;
}
/**
* Logs the response to a request that was logged by {@link #maybeTraceRequest(RestRequest, Exception)}.
*
* @param restResponse RestResponse
* @param httpChannel HttpChannel the response was sent on
* @param contentLength Value of the response content length header
* @param opaqueHeader Value of HTTP header {@link Task#X_OPAQUE_ID}
* @param requestId Request id as returned by {@link RestRequest#getRequestId()}
* @param success Whether the response was successfully sent
*/
void traceResponse(
RestResponse restResponse,
HttpChannel httpChannel,
String contentLength,
String opaqueHeader,
long requestId,
boolean success
) {
logger.trace(
new ParameterizedMessage(
"[{}][{}][{}][{}][{}][{}] sent response to [{}] success [{}]",
requestId,
opaqueHeader,
restResponse.status(),
restResponse.status().getStatus(),
restResponse.contentType(),
contentLength,
httpChannel,
success
)
);
}
/**
* Logs the response chunk to a request that was logged by {@link #maybeTraceRequest(RestRequest, Exception)}.
*
* @param chunk response chunk
* @param httpChannel HttpChannel the response was sent on
* @param contentLength Value of the response content length header
* @param opaqueHeader Value of HTTP header {@link Task#X_OPAQUE_ID}
* @param requestId Request id as returned by {@link RestRequest#getRequestId()}
* @param success Whether the response was successfully sent
*/
void traceChunk(
HttpChunk chunk,
StreamingHttpChannel httpChannel,
String contentLength,
String opaqueHeader,
long requestId,
boolean success
) {
logger.trace(
new ParameterizedMessage(
"[{}][{}][{}] sent next chunk to [{}] success [{}]",
requestId,
opaqueHeader,
contentLength,
httpChannel,
success
)
);
}
private void setTracerLogInclude(List<String> tracerLogInclude) {
this.tracerLogInclude = tracerLogInclude.toArray(Strings.EMPTY_ARRAY);
}
private void setTracerLogExclude(List<String> tracerLogExclude) {
this.tracerLogExclude = tracerLogExclude.toArray(Strings.EMPTY_ARRAY);
}
}