Skip to content

Commit bd0e78f

Browse files
committed
chore: fixing PR suggestions
1 parent ac89399 commit bd0e78f

File tree

16 files changed

+213
-130
lines changed

16 files changed

+213
-130
lines changed

docs/supported-libraries.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ These are the supported libraries and frameworks:
3030
| [Apache Dubbo](https://github.com/apache/dubbo/) | 2.7+ | [opentelemetry-apache-dubbo-2.7](../instrumentation/apache-dubbo-2.7/library-autoconfigure) | [RPC Client Spans], [RPC Server Spans] |
3131
| [Apache HttpAsyncClient](https://hc.apache.org/index.html) | 4.1+ | N/A | [HTTP Client Spans], [HTTP Client Metrics] |
3232
| [Apache HttpClient](https://hc.apache.org/index.html) | 2.0+ | [opentelemetry-apache-httpclient-4.3](../instrumentation/apache-httpclient/apache-httpclient-4.3/library) | [HTTP Client Spans], [HTTP Client Metrics] |
33+
| [Apache HttpClient 5](https://hc.apache.org/index.html) | 2.0+ | [opentelemetry-apache-httpclient-5.0](../instrumentation/apache-httpclient/apache-httpclient-5.0/library) | [HTTP Client Spans], [HTTP Client Metrics] |
3334
| [Apache Kafka Producer/Consumer API](https://kafka.apache.org/documentation/#producerapi) | 0.11+ | [opentelemetry-kafka-clients-2.6](../instrumentation/kafka/kafka-clients/kafka-clients-2.6/library) | [Messaging Spans] |
3435
| [Apache Kafka Streams API](https://kafka.apache.org/documentation/streams/) | 0.11+ | N/A | [Messaging Spans] |
3536
| [Apache MyFaces](https://myfaces.apache.org/) | 1.2+ | N/A | Provides `http.route` [2], Controller Spans [3] |

instrumentation/apache-httpclient/apache-httpclient-5.0/library/build.gradle.kts

-24
This file was deleted.

instrumentation/apache-httpclient/apache-httpclient-5.0/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_0/ApacheHttpClient5HttpAttributesGetter.java

-68
This file was deleted.

instrumentation/apache-httpclient/apache-httpclient-5.0/testing/build.gradle.kts

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Library Instrumentation for Apache Http client version 5.2
2+
3+
Provides OpenTelemetry instrumentation for [Apache Http Client 5.2](https://hc.apache.org/httpcomponents-client-5.2.x/).
4+
5+
## Quickstart
6+
7+
### Add these dependencies to your project
8+
9+
Replace `OPENTELEMETRY_VERSION` with the [latest
10+
release](https://search.maven.org/search?q=g:io.opentelemetry.instrumentation%20AND%20a:opentelemetry-apache-httpclient-5.2).
11+
12+
For Maven, add to your `pom.xml` dependencies:
13+
14+
```xml
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.opentelemetry.instrumentation</groupId>
18+
<artifactId>opentelemetry-apache-httpclient-5.2</artifactId>
19+
<version>OPENTELEMETRY_VERSION</version>
20+
</dependency>
21+
</dependencies>
22+
```
23+
24+
For Gradle, add to your dependencies:
25+
26+
```groovy
27+
implementation("io.opentelemetry.instrumentation:opentelemetry-apache-httpclient-5.2:OPENTELEMETRY_VERSION")
28+
```
29+
30+
### Usage
31+
32+
The instrumentation library provides a builder class `ApacheHttpClient5Telemetry` that wraps
33+
an instance of the `HttpClientBuilder` to provide OpenTelemetry-based spans and context
34+
propagation.
35+
36+
```java
37+
import io.opentelemetry.api.OpenTelemetry;
38+
import io.opentelemetry.instrumentation.apachehttpclient.v5_2.ApacheHttpClient5Telemetry;
39+
import org.apache.hc.client5.http.classic.HttpClient;
40+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
41+
42+
public class ApacheHttpClient5Configuration {
43+
44+
private OpenTelemetry openTelemetry;
45+
46+
public ApacheHttpClient5Configuration(OpenTelemetry openTelemetry) {
47+
this.openTelemetry = openTelemetry;
48+
}
49+
50+
// your configuration of the HttpClient goes here:
51+
protected HttpClientBuilder createBuilder() {
52+
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClientBuilder();
53+
}
54+
55+
// creates a new httpClient with openTelemetry instrumentation
56+
public HttpClient newHttpClient() {
57+
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClient();
58+
}
59+
}
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
id("otel.library-instrumentation")
3+
id("otel.nullaway-conventions")
4+
id("otel.animalsniffer-conventions")
5+
}
6+
7+
dependencies {
8+
library("org.apache.httpcomponents.client5:httpclient5:5.2.1")
9+
library("jakarta.annotation:jakarta.annotation-api:2.1.1")
10+
}
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 jakarta.annotation.Nullable;
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
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 ApacheHttpClient5HttpAttributesGetter
19+
implements HttpClientAttributesGetter<ApacheHttpClient5Request, HttpResponse> {
20+
INSTANCE;
21+
22+
@Override
23+
public String getHttpRequestMethod(ApacheHttpClient5Request request) {
24+
return request.getMethod();
25+
}
26+
27+
@Override
28+
@Nullable
29+
public String getUrlFull(ApacheHttpClient5Request request) {
30+
return request.getUrl();
31+
}
32+
33+
@Override
34+
public List<String> getHttpRequestHeader(ApacheHttpClient5Request request, String name) {
35+
return getHeader(request, name);
36+
}
37+
38+
@Override
39+
public Integer getHttpResponseStatusCode(
40+
ApacheHttpClient5Request request, HttpResponse response, @Nullable Throwable error) {
41+
return response.getCode();
42+
}
43+
44+
@Override
45+
public List<String> getHttpResponseHeader(
46+
ApacheHttpClient5Request 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(ApacheHttpClient5Request 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+
ApacheHttpClient5Request 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+
ApacheHttpClient5Request 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(ApacheHttpClient5Request request) {
98+
return request.getDelegate().getAuthority().getHostName();
99+
}
100+
101+
@Override
102+
public Integer getServerPort(ApacheHttpClient5Request request) {
103+
return request.getDelegate().getAuthority().getPort();
104+
}
105+
106+
private static ProtocolVersion getVersion(
107+
ApacheHttpClient5Request 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
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
77

88
import static java.util.logging.Level.FINE;
99

Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
77

88
import io.opentelemetry.api.OpenTelemetry;
99
import io.opentelemetry.context.propagation.ContextPropagators;
@@ -49,7 +49,7 @@ public CloseableHttpClient newHttpClient() {
4949

5050
/** Returns a new {@link HttpClientBuilder} to create a client with tracing configured. */
5151
public HttpClientBuilder newHttpClientBuilder() {
52-
return org.apache.hc.client5.http.impl.classic.HttpClientBuilder.create()
52+
return HttpClientBuilder.create()
5353
.addExecInterceptorAfter(
5454
ChainElement.PROTOCOL.name(),
5555
"OtelExecChainHandler",
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
77

88
import com.google.errorprone.annotations.CanIgnoreReturnValue;
99
import io.opentelemetry.api.OpenTelemetry;
@@ -27,7 +27,7 @@
2727
/** A builder for {@link ApacheHttpClient5Telemetry}. */
2828
public final class ApacheHttpClient5TelemetryBuilder {
2929

30-
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-5.0";
30+
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-5.2";
3131

3232
private final OpenTelemetry openTelemetry;
3333

Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
77

88
import io.opentelemetry.context.propagation.TextMapSetter;
99
import jakarta.annotation.Nullable;
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
6+
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;
77

88
import io.opentelemetry.context.Context;
99
import io.opentelemetry.context.propagation.ContextPropagators;

0 commit comments

Comments
 (0)