-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
506 additions
and
22 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
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
114 changes: 114 additions & 0 deletions
114
...telemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClientHttpAttributesGetter.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,114 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.apachehttpclient.v5_2; | ||
|
||
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
import org.apache.hc.core5.http.Header; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
import org.apache.hc.core5.http.MessageHeaders; | ||
import org.apache.hc.core5.http.ProtocolVersion; | ||
|
||
enum ApacheHttpClientHttpAttributesGetter | ||
implements HttpClientAttributesGetter<ApacheHttpClientRequest, HttpResponse> { | ||
INSTANCE; | ||
|
||
@Override | ||
public String getHttpRequestMethod(ApacheHttpClientRequest request) { | ||
return request.getMethod(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getUrlFull(ApacheHttpClientRequest request) { | ||
return request.getUrl(); | ||
} | ||
|
||
@Override | ||
public List<String> getHttpRequestHeader(ApacheHttpClientRequest request, String name) { | ||
return getHeader(request, name); | ||
} | ||
|
||
@Override | ||
public Integer getHttpResponseStatusCode( | ||
ApacheHttpClientRequest request, HttpResponse response, @Nullable Throwable error) { | ||
return response.getCode(); | ||
} | ||
|
||
@Override | ||
public List<String> getHttpResponseHeader( | ||
ApacheHttpClientRequest request, HttpResponse response, String name) { | ||
return getHeader(response, name); | ||
} | ||
|
||
private static List<String> getHeader(MessageHeaders messageHeaders, String name) { | ||
return headersToList(messageHeaders.getHeaders(name)); | ||
} | ||
|
||
private static List<String> getHeader(ApacheHttpClientRequest messageHeaders, String name) { | ||
return headersToList(messageHeaders.getDelegate().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 (Header header : headers) { | ||
headersList.add(header.getValue()); | ||
} | ||
return headersList; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getNetworkProtocolName( | ||
ApacheHttpClientRequest request, @Nullable HttpResponse response) { | ||
ProtocolVersion protocolVersion = getVersion(request, response); | ||
if (protocolVersion == null) { | ||
return null; | ||
} | ||
return protocolVersion.getProtocol(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getNetworkProtocolVersion( | ||
ApacheHttpClientRequest request, @Nullable HttpResponse response) { | ||
ProtocolVersion protocolVersion = getVersion(request, response); | ||
if (protocolVersion == null) { | ||
return null; | ||
} | ||
if (protocolVersion.getMinor() == 0) { | ||
return Integer.toString(protocolVersion.getMajor()); | ||
} | ||
return protocolVersion.getMajor() + "." + protocolVersion.getMinor(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getServerAddress(ApacheHttpClientRequest request) { | ||
return request.getDelegate().getAuthority().getHostName(); | ||
} | ||
|
||
@Override | ||
public Integer getServerPort(ApacheHttpClientRequest request) { | ||
return request.getDelegate().getAuthority().getPort(); | ||
} | ||
|
||
private static ProtocolVersion getVersion( | ||
ApacheHttpClientRequest request, @Nullable HttpResponse response) { | ||
ProtocolVersion protocolVersion = request.getDelegate().getVersion(); | ||
if (protocolVersion == null && response != null) { | ||
protocolVersion = response.getVersion(); | ||
} | ||
return protocolVersion; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.../java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClientRequest.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,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.apachehttpclient.v5_2; | ||
|
||
import static java.util.logging.Level.FINE; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nullable; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.apache.hc.core5.http.HttpRequest; | ||
|
||
public final class ApacheHttpClientRequest { | ||
|
||
private static final Logger logger = Logger.getLogger(ApacheHttpClientRequest.class.getName()); | ||
|
||
@Nullable private final URI uri; | ||
private final HttpRequest delegate; | ||
|
||
ApacheHttpClientRequest(@Nullable HttpHost httpHost, HttpRequest httpRequest) { | ||
URI calculatedUri = getUri(httpRequest); | ||
if (calculatedUri != null && httpHost != null) { | ||
uri = getCalculatedUri(httpHost, calculatedUri); | ||
} else { | ||
uri = calculatedUri; | ||
} | ||
delegate = httpRequest; | ||
} | ||
|
||
/** Returns the actual {@link HttpRequest} being executed by the client. */ | ||
public HttpRequest getDelegate() { | ||
return delegate; | ||
} | ||
|
||
String getMethod() { | ||
return delegate.getMethod(); | ||
} | ||
|
||
@Nullable | ||
String getUrl() { | ||
return uri != null ? uri.toString() : null; | ||
} | ||
|
||
@Nullable | ||
private static URI getUri(HttpRequest httpRequest) { | ||
try { | ||
// this can be relative or absolute | ||
return new URI(httpRequest.getUri().toString()); | ||
} catch (URISyntaxException e) { | ||
logger.log(FINE, e.getMessage(), e); | ||
return null; | ||
} | ||
} | ||
|
||
@Nullable | ||
private static URI getCalculatedUri(HttpHost httpHost, URI uri) { | ||
try { | ||
return new URI( | ||
httpHost.getSchemeName(), | ||
uri.getUserInfo(), | ||
httpHost.getHostName(), | ||
httpHost.getPort(), | ||
uri.getPath(), | ||
uri.getQuery(), | ||
uri.getFragment()); | ||
} catch (URISyntaxException e) { | ||
logger.log(FINE, e.getMessage(), e); | ||
return null; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ava/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClientTelemetry.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,58 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.apachehttpclient.v5_2; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import org.apache.hc.client5.http.impl.ChainElement; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
|
||
/** Entrypoint for instrumenting Apache HTTP Client. */ | ||
public final class ApacheHttpClientTelemetry { | ||
|
||
/** | ||
* Returns a new {@link ApacheHttpClientTelemetry} configured with the given {@link | ||
* OpenTelemetry}. | ||
*/ | ||
public static ApacheHttpClientTelemetry create(OpenTelemetry openTelemetry) { | ||
return builder(openTelemetry).build(); | ||
} | ||
|
||
/** | ||
* Returns a new {@link ApacheHttpClientTelemetryBuilder} configured with the given {@link | ||
* OpenTelemetry}. | ||
*/ | ||
public static ApacheHttpClientTelemetryBuilder builder(OpenTelemetry openTelemetry) { | ||
return new ApacheHttpClientTelemetryBuilder(openTelemetry); | ||
} | ||
|
||
private final Instrumenter<ApacheHttpClientRequest, HttpResponse> instrumenter; | ||
private final ContextPropagators propagators; | ||
|
||
ApacheHttpClientTelemetry( | ||
Instrumenter<ApacheHttpClientRequest, HttpResponse> instrumenter, | ||
ContextPropagators propagators) { | ||
this.instrumenter = instrumenter; | ||
this.propagators = propagators; | ||
} | ||
|
||
/** Returns a new {@link CloseableHttpClient} with tracing configured. */ | ||
public CloseableHttpClient newHttpClient() { | ||
return newHttpClientBuilder().build(); | ||
} | ||
|
||
/** Returns a new {@link HttpClientBuilder} to create a client with tracing configured. */ | ||
public HttpClientBuilder newHttpClientBuilder() { | ||
return HttpClientBuilder.create() | ||
.addExecInterceptorAfter( | ||
ChainElement.PROTOCOL.name(), | ||
"OtelExecChainHandler", | ||
new OtelExecChainHandler(instrumenter, propagators)); | ||
} | ||
} |
Oops, something went wrong.