Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http attributes: only set content_length when it's >0 #8114

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ public void onEnd(
@Nullable RESPONSE response,
@Nullable Throwable error) {

internalSet(
attributes, SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH, requestContentLength(request));
Long requestLength = requestContentLength(request);
if (requestLength != null && requestLength > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't requestLength 0 valid (and different from not being captured)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, true. I've a problem with Reactor Netty instrumentation, which reports content-lenght=0 even for GET & HEAD requests. WDYT about skipping 0 if method is GET/HEAD?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we apply that change just to the reactor netty HttpAttributesGetter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can (well, not in a "nice" way) - since the getter simply returns the headers, which then get parsed in the extractor.
On a second thought, maybe we can live with this: the content-length=0 header is actually set on the outgoing HTTP request, so it's not like the instrumentation is lying. It's just a bit strange to see that in a GET request.

internalSet(attributes, SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH, requestLength);
}

if (response != null) {
Integer statusCode = getter.getStatusCode(request, response, error);
if (statusCode != null && statusCode > 0) {
internalSet(attributes, SemanticAttributes.HTTP_STATUS_CODE, (long) statusCode);
}

internalSet(
attributes,
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
responseContentLength(request, response));
Long responseLength = responseContentLength(request, response);
if (responseLength != null && responseLength > 0) {
internalSet(attributes, SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, responseLength);
}

for (String name : capturedResponseHeaders) {
List<String> values = getter.getResponseHeader(request, response, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public List<String> getRequestHeader(Map<String, String> request, String name) {
@Override
public Integer getStatusCode(
Map<String, String> request, Map<String, String> response, @Nullable Throwable error) {
return Integer.parseInt(response.get("statusCode"));
String statusCode = response.get("statusCode");
return statusCode == null ? null : Integer.parseInt(statusCode);
}

@Override
Expand Down Expand Up @@ -293,4 +294,23 @@ void zeroResends() {
extractor.onEnd(attributes, Context.root(), request, null, null);
assertThat(attributes.build()).isEmpty();
}

@Test
void emptyBodies() {
Map<String, String> request = new HashMap<>();
request.put("header.content-length", "0");

Map<String, String> response = new HashMap<>();
response.put("header.content-length", "-1");

AttributesExtractor<Map<String, String>, Map<String, String>> extractor =
HttpClientAttributesExtractor.builder(
new TestHttpClientAttributesGetter(), new TestNetClientAttributesGetter())
.build();

AttributesBuilder attributes = Attributes.builder();
extractor.onStart(attributes, Context.root(), request);
extractor.onEnd(attributes, Context.root(), request, response, null);
assertThat(attributes.build()).isEmpty();
}
}