Skip to content

Commit

Permalink
Rename ApacheHttpClient5Telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 9, 2024
1 parent 4af0dd5 commit 48d5fac
Show file tree
Hide file tree
Showing 14 changed files with 559 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ implementation("io.opentelemetry.instrumentation:opentelemetry-apache-httpclient

### Usage

The instrumentation library provides the class `ApacheHttpClient5Telemetry` that has a builder
The instrumentation library provides the class `ApacheHttpClientTelemetry` that has a builder
method and allows the creation of an instance of the `HttpClientBuilder` to provide
OpenTelemetry-based spans and context propagation:

```java
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.ApacheHttpClient5Telemetry;
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.ApacheHttpClientTelemetry;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;

public class ApacheHttpClient5Configuration {
public class ApacheHttpClientConfiguration {

private OpenTelemetry openTelemetry;

public ApacheHttpClient5Configuration(OpenTelemetry openTelemetry) {
public ApacheHttpClientConfiguration(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}

// creates a new http client builder for constructing http clients with open telemetry instrumentation
public HttpClientBuilder createBuilder() {
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClientBuilder();
return ApacheHttpClientTelemetry.builder(openTelemetry).build().newHttpClientBuilder();
}

// creates a new http client with open telemetry instrumentation
public HttpClient newHttpClient() {
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClient();
return ApacheHttpClientTelemetry.builder(openTelemetry).build().newHttpClient();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.hc.core5.http.MessageHeaders;
import org.apache.hc.core5.http.ProtocolVersion;

@Deprecated
enum ApacheHttpClient5HttpAttributesGetter
implements HttpClientAttributesGetter<ApacheHttpClient5Request, HttpResponse> {
INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;

/**
* @deprecated Use {@link ApacheHttpClientRequest} instead.
*/
@Deprecated
public final class ApacheHttpClient5Request {

private static final Logger logger = Logger.getLogger(ApacheHttpClient5Request.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.HttpResponse;

/** Entrypoint for instrumenting Apache HTTP Client. */
/**
* Entrypoint for instrumenting Apache HTTP Client.
*
* @deprecated Use {@link ApacheHttpClientTelemetry} instead.
*/
@Deprecated
public final class ApacheHttpClient5Telemetry {

/**
Expand Down Expand Up @@ -53,6 +58,6 @@ public HttpClientBuilder newHttpClientBuilder() {
.addExecInterceptorAfter(
ChainElement.PROTOCOL.name(),
"OtelExecChainHandler",
new OtelExecChainHandler(instrumenter, propagators));
new OtelExecChainHandlerOld(instrumenter, propagators));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.internal.Experimental;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
Expand All @@ -16,7 +17,12 @@
import java.util.function.Function;
import org.apache.hc.core5.http.HttpResponse;

/** A builder for {@link ApacheHttpClient5Telemetry}. */
/**
* A builder for {@link ApacheHttpClient5Telemetry}.
*
* @deprecated Use {@link ApacheHttpClientTelemetryBuilder} instead.
*/
@Deprecated
public final class ApacheHttpClient5TelemetryBuilder {

private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-5.2";
Expand Down Expand Up @@ -90,7 +96,11 @@ public ApacheHttpClient5TelemetryBuilder setKnownMethods(Set<String> knownMethod
*
* @param emitExperimentalHttpClientMetrics {@code true} if the experimental HTTP client metrics
* are to be emitted.
* @deprecated Use {@link
* Experimental#setEmitExperimentalHttpClientMetrics(ApacheHttpClientTelemetryBuilder,
* boolean)} instead.
*/
@Deprecated
@CanIgnoreReturnValue
public ApacheHttpClient5TelemetryBuilder setEmitExperimentalHttpClientMetrics(
boolean emitExperimentalHttpClientMetrics) {
Expand Down
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;
}
}
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;
}
}
}
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));
}
}
Loading

0 comments on commit 48d5fac

Please sign in to comment.