-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request/response body logging to HTTP tracer (#93133)
Adds another logger, `org.elasticsearch.http.HttpBodyTracer`, which logs the body of every HTTP request and response as well as the usual summaries.
- Loading branch information
1 parent
a03a779
commit 421c2d4
Showing
15 changed files
with
380 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 93133 | ||
summary: Add request/response body logging to HTTP tracer | ||
area: Network | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
server/src/main/java/org/elasticsearch/http/HttpBodyTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.http; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.common.ReferenceDocs; | ||
import org.elasticsearch.common.logging.ChunkedLoggingStream; | ||
import org.elasticsearch.transport.NetworkTraceFlag; | ||
|
||
import java.io.OutputStream; | ||
|
||
class HttpBodyTracer { | ||
private static final Logger logger = LogManager.getLogger(HttpBodyTracer.class); | ||
|
||
public static boolean isEnabled() { | ||
return logger.isTraceEnabled(); | ||
} | ||
|
||
enum Type { | ||
REQUEST("request"), | ||
RESPONSE("response"); | ||
|
||
final String text; | ||
|
||
Type(String text) { | ||
this.text = text; | ||
} | ||
} | ||
|
||
static OutputStream getBodyOutputStream(long requestId, Type type) { | ||
try { | ||
if (NetworkTraceFlag.TRACE_ENABLED) { | ||
return ChunkedLoggingStream.create( | ||
logger, | ||
Level.TRACE, | ||
"[" + requestId + "] " + type.text + " body", | ||
ReferenceDocs.HTTP_TRACER | ||
); | ||
} else { | ||
logger.trace("set system property [{}] to [true] to enable HTTP body tracing", NetworkTraceFlag.PROPERTY_NAME); | ||
} | ||
} catch (Exception e) { | ||
assert false : e; // nothing really to go wrong here | ||
} | ||
return OutputStream.nullOutputStream(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
server/src/main/java/org/elasticsearch/rest/LoggingChunkedRestResponseBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.rest; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.bytes.ReleasableBytesReference; | ||
import org.elasticsearch.common.recycler.Recycler; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
public class LoggingChunkedRestResponseBody implements ChunkedRestResponseBody { | ||
|
||
private final ChunkedRestResponseBody inner; | ||
private final OutputStream loggerStream; | ||
|
||
public LoggingChunkedRestResponseBody(ChunkedRestResponseBody inner, OutputStream loggerStream) { | ||
this.inner = inner; | ||
this.loggerStream = loggerStream; | ||
} | ||
|
||
@Override | ||
public boolean isDone() { | ||
return inner.isDone(); | ||
} | ||
|
||
@Override | ||
public ReleasableBytesReference encodeChunk(int sizeHint, Recycler<BytesRef> recycler) throws IOException { | ||
var chunk = inner.encodeChunk(sizeHint, recycler); | ||
try { | ||
chunk.writeTo(loggerStream); | ||
} catch (Exception e) { | ||
assert false : e; // nothing really to go wrong here | ||
} | ||
|
||
return chunk; | ||
} | ||
|
||
@Override | ||
public String getResponseContentTypeString() { | ||
return inner.getResponseContentTypeString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
server/src/main/java/org/elasticsearch/transport/NetworkTraceFlag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.transport; | ||
|
||
import org.elasticsearch.common.Strings; | ||
|
||
public class NetworkTraceFlag { | ||
|
||
private NetworkTraceFlag() { | ||
// no instances; | ||
} | ||
|
||
public static final String PROPERTY_NAME = "es.insecure_network_trace_enabled"; | ||
|
||
public static final boolean TRACE_ENABLED; | ||
|
||
static { | ||
final var propertyValue = System.getProperty(PROPERTY_NAME); | ||
if (propertyValue == null) { | ||
TRACE_ENABLED = false; | ||
} else if ("true".equals(propertyValue)) { | ||
TRACE_ENABLED = true; | ||
} else { | ||
throw new IllegalArgumentException( | ||
Strings.format("system property [%s] may only be set to [true], but was [%s]", PROPERTY_NAME, propertyValue) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.