Skip to content

Commit

Permalink
avoid using streams API when transforming the headers list
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Oct 4, 2021
1 parent 1914320 commit 1e81792
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

package io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient;

import static io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient.ApacheHttpClientRequest.headersToList;

import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.config.HttpHeadersConfig;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -80,8 +79,6 @@ protected Long responseContentLengthUncompressed(
@Override
protected List<String> responseHeader(
ApacheHttpClientRequest request, HttpResponse response, String name) {
return Arrays.stream(response.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(response.getHeaders(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
Expand Down Expand Up @@ -38,9 +38,19 @@ public ApacheHttpClientRequest(HttpHost httpHost, HttpRequest httpRequest) {
}

public List<String> getHeader(String name) {
return Arrays.stream(delegate.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(delegate.getHeaders(name));
}

// minimize memory overhead by not using streams
static List<String> headersToList(Header[] headers) {
if (headers.length == 0) {
return Collections.emptyList();
}
List<String> headersList = new ArrayList<>(headers.length);
for (int i = 0; i < headers.length; ++i) {
headersList.set(i, headers[i].getValue());
}
return headersList;
}

public void setHeader(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0;

import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.ApacheHttpClientRequest.headersToList;

import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.config.HttpHeadersConfig;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -77,8 +76,6 @@ protected Long responseContentLengthUncompressed(
@Override
protected List<String> responseHeader(
ApacheHttpClientRequest request, HttpResponse response, String name) {
return Arrays.stream(response.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(response.getHeaders(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
Expand Down Expand Up @@ -44,9 +44,19 @@ public ApacheHttpClientRequest(HttpUriRequest httpRequest) {
}

public List<String> getHeader(String name) {
return Arrays.stream(delegate.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(delegate.getHeaders(name));
}

// minimize memory overhead by not using streams
static List<String> headersToList(Header[] headers) {
if (headers.length == 0) {
return Collections.emptyList();
}
List<String> headersList = new ArrayList<>(headers.length);
for (int i = 0; i < headers.length; ++i) {
headersList.set(i, headers[i].getValue());
}
return headersList;
}

public void setHeader(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

package io.opentelemetry.instrumentation.apachehttpclient.v4_3;

import static io.opentelemetry.instrumentation.apachehttpclient.v4_3.ApacheHttpClientRequest.headersToList;

import io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeaders;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -79,8 +78,6 @@ protected Long responseContentLengthUncompressed(
@Override
protected List<String> responseHeader(
ApacheHttpClientRequest request, HttpResponse response, String name) {
return Arrays.stream(response.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(response.getHeaders(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
Expand Down Expand Up @@ -43,9 +43,19 @@ public HttpRequest getDelegate() {
}

List<String> getHeader(String name) {
return Arrays.stream(delegate.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(delegate.getHeaders(name));
}

// minimize memory overhead by not using streams
static List<String> headersToList(Header[] headers) {
if (headers.length == 0) {
return Collections.emptyList();
}
List<String> headersList = new ArrayList<>(headers.length);
for (int i = 0; i < headers.length; ++i) {
headersList.set(i, headers[i].getValue());
}
return headersList;
}

void setHeader(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.config.HttpHeadersConfig;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
Expand Down Expand Up @@ -77,9 +77,7 @@ protected String userAgent(ClassicHttpRequest request) {

@Override
protected List<String> requestHeader(ClassicHttpRequest request, String name) {
return Arrays.stream(request.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(request.getHeaders(name));
}

@Override
Expand Down Expand Up @@ -142,8 +140,18 @@ protected Long responseContentLengthUncompressed(
@Override
protected List<String> responseHeader(
ClassicHttpRequest request, HttpResponse response, String name) {
return Arrays.stream(response.getHeaders(name))
.map(Header::getValue)
.collect(Collectors.toList());
return headersToList(response.getHeaders(name));
}

// minimize memory overhead by not using streams
private static List<String> headersToList(Header[] headers) {
if (headers.length == 0) {
return Collections.emptyList();
}
List<String> headersList = new ArrayList<>(headers.length);
for (int i = 0; i < headers.length; ++i) {
headersList.set(i, headers[i].getValue());
}
return headersList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.config.HttpHeadersConfig;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable;

final class JaxRsClientHttpAttributesExtractor
Expand Down Expand Up @@ -41,9 +41,16 @@ protected String url(ClientRequest httpRequest) {

@Override
protected List<String> requestHeader(ClientRequest httpRequest, String name) {
return httpRequest.getHeaders().getOrDefault(name, emptyList()).stream()
.map(String::valueOf)
.collect(Collectors.toList());
List<Object> rawHeaders = httpRequest.getHeaders().getOrDefault(name, emptyList());
if (rawHeaders.isEmpty()) {
return emptyList();
}
List<String> stringHeaders = new ArrayList<>(rawHeaders.size());
int i = 0;
for (Object headerValue : rawHeaders) {
stringHeaders.set(i++, String.valueOf(headerValue));
}
return stringHeaders;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.config.HttpHeadersConfig;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.ws.rs.core.Response;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocation;
Expand Down Expand Up @@ -40,9 +40,16 @@ protected String url(ClientInvocation httpRequest) {

@Override
protected List<String> requestHeader(ClientInvocation httpRequest, String name) {
return httpRequest.getHeaders().getHeaders().getOrDefault(name, emptyList()).stream()
.map(String::valueOf)
.collect(Collectors.toList());
List<Object> rawHeaders = httpRequest.getHeaders().getHeaders().getOrDefault(name, emptyList());
if (rawHeaders.isEmpty()) {
return emptyList();
}
List<String> stringHeaders = new ArrayList<>(rawHeaders.size());
int i = 0;
for (Object headerValue : rawHeaders) {
stringHeaders.set(i++, String.valueOf(headerValue));
}
return stringHeaders;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeaders;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.restlet.data.Parameter;
import org.restlet.data.Reference;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.util.Series;

final class RestletHttpAttributesExtractor
extends HttpServerAttributesExtractor<Request, Response> {
Expand Down Expand Up @@ -61,9 +63,7 @@ protected String host(Request request) {

@Override
protected List<String> requestHeader(Request request, String name) {
return getHeaders(request).subList(name, /* ignoreCase = */ true).stream()
.map(Parameter::getValue)
.collect(Collectors.toList());
return parametersToList(getHeaders(request).subList(name, /* ignoreCase = */ true));
}

@Override
Expand Down Expand Up @@ -115,8 +115,19 @@ protected Integer statusCode(Request request, Response response) {

@Override
protected List<String> responseHeader(Request request, Response response, String name) {
return getHeaders(response).subList(name, /* ignoreCase = */ true).stream()
.map(Parameter::getValue)
.collect(Collectors.toList());
return parametersToList(getHeaders(response).subList(name, /* ignoreCase = */ true));
}

// minimize memory overhead by not using streams
private static List<String> parametersToList(Series<Parameter> headers) {
if (headers.isEmpty()) {
return Collections.emptyList();
}
List<String> stringHeaders = new ArrayList<>(headers.size());
int i = 0;
for (Parameter header : headers) {
stringHeaders.set(i++, header.getValue());
}
return stringHeaders;
}
}

0 comments on commit 1e81792

Please sign in to comment.