Skip to content

Commit de9c891

Browse files
authored
Rename ApacheHttpClient5Telemetry to ApacheHttpClientTelemetry (since already under v5 package) (#12854)
1 parent 8b55b5a commit de9c891

14 files changed

+531
-18
lines changed

instrumentation/apache-httpclient/apache-httpclient-5.2/library/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,32 @@ implementation("io.opentelemetry.instrumentation:opentelemetry-apache-httpclient
2929

3030
### Usage
3131

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

3636
```java
3737
import io.opentelemetry.api.OpenTelemetry;
38-
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.ApacheHttpClient5Telemetry;
38+
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.ApacheHttpClientTelemetry;
3939
import org.apache.hc.client5.http.classic.HttpClient;
4040
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
4141

42-
public class ApacheHttpClient5Configuration {
42+
public class ApacheHttpClientConfiguration {
4343

4444
private OpenTelemetry openTelemetry;
4545

46-
public ApacheHttpClient5Configuration(OpenTelemetry openTelemetry) {
46+
public ApacheHttpClientConfiguration(OpenTelemetry openTelemetry) {
4747
this.openTelemetry = openTelemetry;
4848
}
4949

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

5555
// creates a new http client with open telemetry instrumentation
5656
public HttpClient newHttpClient() {
57-
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClient();
57+
return ApacheHttpClientTelemetry.builder(openTelemetry).build().newHttpClient();
5858
}
5959
}
6060
```

instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5HttpAttributesGetter.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.hc.core5.http.MessageHeaders;
1616
import org.apache.hc.core5.http.ProtocolVersion;
1717

18+
@Deprecated
1819
enum ApacheHttpClient5HttpAttributesGetter
1920
implements HttpClientAttributesGetter<ApacheHttpClient5Request, HttpResponse> {
2021
INSTANCE;

instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5Request.java

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import org.apache.hc.core5.http.HttpHost;
1515
import org.apache.hc.core5.http.HttpRequest;
1616

17+
/**
18+
* @deprecated Use {@link ApacheHttpClientRequest} instead.
19+
*/
20+
@Deprecated
1721
public final class ApacheHttpClient5Request {
1822

1923
private static final Logger logger = Logger.getLogger(ApacheHttpClient5Request.class.getName());

instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5Telemetry.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
1414
import org.apache.hc.core5.http.HttpResponse;
1515

16-
/** Entrypoint for instrumenting Apache HTTP Client. */
16+
/**
17+
* Entrypoint for instrumenting Apache HTTP Client.
18+
*
19+
* @deprecated Use {@link ApacheHttpClientTelemetry} instead.
20+
*/
21+
@Deprecated
1722
public final class ApacheHttpClient5Telemetry {
1823

1924
/**
@@ -53,6 +58,6 @@ public HttpClientBuilder newHttpClientBuilder() {
5358
.addExecInterceptorAfter(
5459
ChainElement.PROTOCOL.name(),
5560
"OtelExecChainHandler",
56-
new OtelExecChainHandler(instrumenter, propagators));
61+
new OtelExecChainHandlerOld(instrumenter, propagators));
5762
}
5863
}

instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5TelemetryBuilder.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.google.errorprone.annotations.CanIgnoreReturnValue;
99
import io.opentelemetry.api.OpenTelemetry;
10+
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.internal.Experimental;
1011
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
1112
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1213
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
@@ -16,7 +17,12 @@
1617
import java.util.function.Function;
1718
import org.apache.hc.core5.http.HttpResponse;
1819

19-
/** A builder for {@link ApacheHttpClient5Telemetry}. */
20+
/**
21+
* A builder for {@link ApacheHttpClient5Telemetry}.
22+
*
23+
* @deprecated Use {@link ApacheHttpClientTelemetryBuilder} instead.
24+
*/
25+
@Deprecated
2026
public final class ApacheHttpClient5TelemetryBuilder {
2127

2228
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-5.2";
@@ -90,7 +96,11 @@ public ApacheHttpClient5TelemetryBuilder setKnownMethods(Set<String> knownMethod
9096
*
9197
* @param emitExperimentalHttpClientMetrics {@code true} if the experimental HTTP client metrics
9298
* are to be emitted.
99+
* @deprecated Use {@link
100+
* Experimental#setEmitExperimentalTelemetry(ApacheHttpClientTelemetryBuilder, boolean)}
101+
* instead.
93102
*/
103+
@Deprecated
94104
@CanIgnoreReturnValue
95105
public ApacheHttpClient5TelemetryBuilder setEmitExperimentalHttpClientMetrics(
96106
boolean emitExperimentalHttpClientMetrics) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
7+
8+
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import javax.annotation.Nullable;
13+
import org.apache.hc.core5.http.Header;
14+
import org.apache.hc.core5.http.HttpResponse;
15+
import org.apache.hc.core5.http.MessageHeaders;
16+
import org.apache.hc.core5.http.ProtocolVersion;
17+
18+
enum ApacheHttpClientHttpAttributesGetter
19+
implements HttpClientAttributesGetter<ApacheHttpClientRequest, HttpResponse> {
20+
INSTANCE;
21+
22+
@Override
23+
public String getHttpRequestMethod(ApacheHttpClientRequest request) {
24+
return request.getMethod();
25+
}
26+
27+
@Override
28+
@Nullable
29+
public String getUrlFull(ApacheHttpClientRequest request) {
30+
return request.getUrl();
31+
}
32+
33+
@Override
34+
public List<String> getHttpRequestHeader(ApacheHttpClientRequest request, String name) {
35+
return getHeader(request, name);
36+
}
37+
38+
@Override
39+
public Integer getHttpResponseStatusCode(
40+
ApacheHttpClientRequest request, HttpResponse response, @Nullable Throwable error) {
41+
return response.getCode();
42+
}
43+
44+
@Override
45+
public List<String> getHttpResponseHeader(
46+
ApacheHttpClientRequest request, HttpResponse response, String name) {
47+
return getHeader(response, name);
48+
}
49+
50+
private static List<String> getHeader(MessageHeaders messageHeaders, String name) {
51+
return headersToList(messageHeaders.getHeaders(name));
52+
}
53+
54+
private static List<String> getHeader(ApacheHttpClientRequest messageHeaders, String name) {
55+
return headersToList(messageHeaders.getDelegate().getHeaders(name));
56+
}
57+
58+
// minimize memory overhead by not using streams
59+
private static List<String> headersToList(Header[] headers) {
60+
if (headers.length == 0) {
61+
return Collections.emptyList();
62+
}
63+
List<String> headersList = new ArrayList<>(headers.length);
64+
for (Header header : headers) {
65+
headersList.add(header.getValue());
66+
}
67+
return headersList;
68+
}
69+
70+
@Nullable
71+
@Override
72+
public String getNetworkProtocolName(
73+
ApacheHttpClientRequest request, @Nullable HttpResponse response) {
74+
ProtocolVersion protocolVersion = getVersion(request, response);
75+
if (protocolVersion == null) {
76+
return null;
77+
}
78+
return protocolVersion.getProtocol();
79+
}
80+
81+
@Nullable
82+
@Override
83+
public String getNetworkProtocolVersion(
84+
ApacheHttpClientRequest request, @Nullable HttpResponse response) {
85+
ProtocolVersion protocolVersion = getVersion(request, response);
86+
if (protocolVersion == null) {
87+
return null;
88+
}
89+
if (protocolVersion.getMinor() == 0) {
90+
return Integer.toString(protocolVersion.getMajor());
91+
}
92+
return protocolVersion.getMajor() + "." + protocolVersion.getMinor();
93+
}
94+
95+
@Override
96+
@Nullable
97+
public String getServerAddress(ApacheHttpClientRequest request) {
98+
return request.getDelegate().getAuthority().getHostName();
99+
}
100+
101+
@Override
102+
public Integer getServerPort(ApacheHttpClientRequest request) {
103+
return request.getDelegate().getAuthority().getPort();
104+
}
105+
106+
private static ProtocolVersion getVersion(
107+
ApacheHttpClientRequest request, @Nullable HttpResponse response) {
108+
ProtocolVersion protocolVersion = request.getDelegate().getVersion();
109+
if (protocolVersion == null && response != null) {
110+
protocolVersion = response.getVersion();
111+
}
112+
return protocolVersion;
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
7+
8+
import static java.util.logging.Level.FINE;
9+
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import java.util.logging.Logger;
13+
import javax.annotation.Nullable;
14+
import org.apache.hc.core5.http.HttpHost;
15+
import org.apache.hc.core5.http.HttpRequest;
16+
17+
public final class ApacheHttpClientRequest {
18+
19+
private static final Logger logger = Logger.getLogger(ApacheHttpClientRequest.class.getName());
20+
21+
@Nullable private final URI uri;
22+
private final HttpRequest delegate;
23+
24+
ApacheHttpClientRequest(@Nullable HttpHost httpHost, HttpRequest httpRequest) {
25+
URI calculatedUri = getUri(httpRequest);
26+
if (calculatedUri != null && httpHost != null) {
27+
uri = getCalculatedUri(httpHost, calculatedUri);
28+
} else {
29+
uri = calculatedUri;
30+
}
31+
delegate = httpRequest;
32+
}
33+
34+
/** Returns the actual {@link HttpRequest} being executed by the client. */
35+
public HttpRequest getDelegate() {
36+
return delegate;
37+
}
38+
39+
String getMethod() {
40+
return delegate.getMethod();
41+
}
42+
43+
@Nullable
44+
String getUrl() {
45+
return uri != null ? uri.toString() : null;
46+
}
47+
48+
@Nullable
49+
private static URI getUri(HttpRequest httpRequest) {
50+
try {
51+
// this can be relative or absolute
52+
return new URI(httpRequest.getUri().toString());
53+
} catch (URISyntaxException e) {
54+
logger.log(FINE, e.getMessage(), e);
55+
return null;
56+
}
57+
}
58+
59+
@Nullable
60+
private static URI getCalculatedUri(HttpHost httpHost, URI uri) {
61+
try {
62+
return new URI(
63+
httpHost.getSchemeName(),
64+
uri.getUserInfo(),
65+
httpHost.getHostName(),
66+
httpHost.getPort(),
67+
uri.getPath(),
68+
uri.getQuery(),
69+
uri.getFragment());
70+
} catch (URISyntaxException e) {
71+
logger.log(FINE, e.getMessage(), e);
72+
return null;
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
7+
8+
import io.opentelemetry.api.OpenTelemetry;
9+
import io.opentelemetry.context.propagation.ContextPropagators;
10+
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
11+
import org.apache.hc.client5.http.impl.ChainElement;
12+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
13+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
14+
import org.apache.hc.core5.http.HttpResponse;
15+
16+
/** Entrypoint for instrumenting Apache HTTP Client. */
17+
public final class ApacheHttpClientTelemetry {
18+
19+
/**
20+
* Returns a new {@link ApacheHttpClientTelemetry} configured with the given {@link
21+
* OpenTelemetry}.
22+
*/
23+
public static ApacheHttpClientTelemetry create(OpenTelemetry openTelemetry) {
24+
return builder(openTelemetry).build();
25+
}
26+
27+
/**
28+
* Returns a new {@link ApacheHttpClientTelemetryBuilder} configured with the given {@link
29+
* OpenTelemetry}.
30+
*/
31+
public static ApacheHttpClientTelemetryBuilder builder(OpenTelemetry openTelemetry) {
32+
return new ApacheHttpClientTelemetryBuilder(openTelemetry);
33+
}
34+
35+
private final Instrumenter<ApacheHttpClientRequest, HttpResponse> instrumenter;
36+
private final ContextPropagators propagators;
37+
38+
ApacheHttpClientTelemetry(
39+
Instrumenter<ApacheHttpClientRequest, HttpResponse> instrumenter,
40+
ContextPropagators propagators) {
41+
this.instrumenter = instrumenter;
42+
this.propagators = propagators;
43+
}
44+
45+
/** Returns a new {@link CloseableHttpClient} with tracing configured. */
46+
public CloseableHttpClient newHttpClient() {
47+
return newHttpClientBuilder().build();
48+
}
49+
50+
/** Returns a new {@link HttpClientBuilder} to create a client with tracing configured. */
51+
public HttpClientBuilder newHttpClientBuilder() {
52+
return HttpClientBuilder.create()
53+
.addExecInterceptorAfter(
54+
ChainElement.PROTOCOL.name(),
55+
"OtelExecChainHandler",
56+
new OtelExecChainHandler(instrumenter, propagators));
57+
}
58+
}

0 commit comments

Comments
 (0)