-
Notifications
You must be signed in to change notification settings - Fork 828
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
1 parent
ac89399
commit 2ea6ffa
Showing
16 changed files
with
214 additions
and
131 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
24 changes: 0 additions & 24 deletions
24
instrumentation/apache-httpclient/apache-httpclient-5.0/library/build.gradle.kts
This file was deleted.
Oops, something went wrong.
68 changes: 0 additions & 68 deletions
68
...elemetry/instrumentation/apachehttpclient/v5_0/ApacheHttpClient5HttpAttributesGetter.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
instrumentation/apache-httpclient/apache-httpclient-5.0/testing/build.gradle.kts
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
instrumentation/apache-httpclient/apache-httpclient-5.2/library/README.md
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,60 @@ | ||
# Library Instrumentation for Apache Http client version 5.2 | ||
|
||
Provides OpenTelemetry instrumentation for [Apache Http Client 5.2](https://hc.apache.org/httpcomponents-client-5.2.x/). | ||
|
||
## Quickstart | ||
|
||
### Add these dependencies to your project | ||
|
||
Replace `OPENTELEMETRY_VERSION` with the [latest | ||
release](https://search.maven.org/search?q=g:io.opentelemetry.instrumentation%20AND%20a:opentelemetry-apache-httpclient-5.2). | ||
|
||
For Maven, add to your `pom.xml` dependencies: | ||
|
||
```xml | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.opentelemetry.instrumentation</groupId> | ||
<artifactId>opentelemetry-apache-httpclient-5.2</artifactId> | ||
<version>OPENTELEMETRY_VERSION</version> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
|
||
For Gradle, add to your dependencies: | ||
|
||
```groovy | ||
implementation("io.opentelemetry.instrumentation:opentelemetry-apache-httpclient-5.2:OPENTELEMETRY_VERSION") | ||
``` | ||
|
||
### Usage | ||
|
||
The instrumentation library provides a builder class `ApacheHttpClient5Telemetry` that wraps | ||
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 org.apache.hc.client5.http.classic.HttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; | ||
|
||
public class ApacheHttpClient5Configuration { | ||
|
||
private OpenTelemetry openTelemetry; | ||
|
||
public ApacheHttpClient5Configuration(OpenTelemetry openTelemetry) { | ||
this.openTelemetry = openTelemetry; | ||
} | ||
|
||
// your configuration of the HttpClient goes here: | ||
protected HttpClientBuilder createBuilder() { | ||
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClientBuilder(); | ||
} | ||
|
||
// creates a new httpClient with openTelemetry instrumentation | ||
public HttpClient newHttpClient() { | ||
return ApacheHttpClient5Telemetry.builder(openTelemetry).build().newHttpClient(); | ||
} | ||
} | ||
``` |
10 changes: 10 additions & 0 deletions
10
instrumentation/apache-httpclient/apache-httpclient-5.2/library/build.gradle.kts
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,10 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
id("otel.nullaway-conventions") | ||
id("otel.animalsniffer-conventions") | ||
} | ||
|
||
dependencies { | ||
library("org.apache.httpcomponents.client5:httpclient5:5.2.1") | ||
library("jakarta.annotation:jakarta.annotation-api:2.1.1") | ||
} |
114 changes: 114 additions & 0 deletions
114
...elemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5HttpAttributesGetter.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 jakarta.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
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 ApacheHttpClient5HttpAttributesGetter | ||
implements HttpClientAttributesGetter<ApacheHttpClient5Request, HttpResponse> { | ||
INSTANCE; | ||
|
||
@Override | ||
public String getHttpRequestMethod(ApacheHttpClient5Request request) { | ||
return request.getMethod(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getUrlFull(ApacheHttpClient5Request request) { | ||
return request.getUrl(); | ||
} | ||
|
||
@Override | ||
public List<String> getHttpRequestHeader(ApacheHttpClient5Request request, String name) { | ||
return getHeader(request, name); | ||
} | ||
|
||
@Override | ||
public Integer getHttpResponseStatusCode( | ||
ApacheHttpClient5Request request, HttpResponse response, @Nullable Throwable error) { | ||
return response.getCode(); | ||
} | ||
|
||
@Override | ||
public List<String> getHttpResponseHeader( | ||
ApacheHttpClient5Request 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(ApacheHttpClient5Request 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( | ||
ApacheHttpClient5Request request, @Nullable HttpResponse response) { | ||
ProtocolVersion protocolVersion = getVersion(request, response); | ||
if (protocolVersion == null) { | ||
return null; | ||
} | ||
return protocolVersion.getProtocol(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getNetworkProtocolVersion( | ||
ApacheHttpClient5Request 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(ApacheHttpClient5Request request) { | ||
return request.getDelegate().getAuthority().getHostName(); | ||
} | ||
|
||
@Override | ||
public Integer getServerPort(ApacheHttpClient5Request request) { | ||
return request.getDelegate().getAuthority().getPort(); | ||
} | ||
|
||
private static ProtocolVersion getVersion( | ||
ApacheHttpClient5Request request, @Nullable HttpResponse response) { | ||
ProtocolVersion protocolVersion = request.getDelegate().getVersion(); | ||
if (protocolVersion == null && response != null) { | ||
protocolVersion = response.getVersion(); | ||
} | ||
return protocolVersion; | ||
} | ||
} |
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
Oops, something went wrong.