Skip to content

Commit

Permalink
chore: fixing PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
elbiocaetano committed Jan 3, 2024
1 parent ac89399 commit 2ea6ffa
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 131 deletions.
3 changes: 2 additions & 1 deletion docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ or [contributing](../CONTRIBUTING.md).
These are the supported libraries and frameworks:

| Library/Framework | Auto-instrumented versions | Standalone Library Instrumentation [1] | Semantic Conventions |
|---------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |----------------------------------------------------------------------------------------|
|---------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
| [Akka Actors](https://doc.akka.io/docs/akka/current/typed/index.html) | 2.3+ | N/A | Context propagation |
| [Akka HTTP](https://doc.akka.io/docs/akka-http/current/index.html) | 10.0+ | N/A | [HTTP Client Spans], [HTTP Client Metrics], [HTTP Server Spans], [HTTP Server Metrics] |
| [Alibaba Druid](https://github.com/alibaba/druid) | 1.0+ | [opentelemetry-alibaba-druid-1.0](../instrumentation/alibaba-druid-1.0/library) | [Database Pool Metrics] |
Expand All @@ -30,6 +30,7 @@ These are the supported libraries and frameworks:
| [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] |
| [Apache HttpAsyncClient](https://hc.apache.org/index.html) | 4.1+ | N/A | [HTTP Client Spans], [HTTP Client Metrics] |
| [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] |
| [Apache HttpClient 5](https://hc.apache.org/index.html) | 2.0+ | [opentelemetry-apache-httpclient-5.2](../instrumentation/apache-httpclient/apache-httpclient-5.2/library) | [HTTP Client Spans], [HTTP Client Metrics] |
| [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] |
| [Apache Kafka Streams API](https://kafka.apache.org/documentation/streams/) | 0.11+ | N/A | [Messaging Spans] |
| [Apache MyFaces](https://myfaces.apache.org/) | 1.2+ | N/A | Provides `http.route` [2], Controller Spans [3] |
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

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();
}
}
```
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")
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
Expand Down Expand Up @@ -49,7 +49,7 @@ public CloseableHttpClient newHttpClient() {

/** Returns a new {@link HttpClientBuilder} to create a client with tracing configured. */
public HttpClientBuilder newHttpClientBuilder() {
return org.apache.hc.client5.http.impl.classic.HttpClientBuilder.create()
return HttpClientBuilder.create()
.addExecInterceptorAfter(
ChainElement.PROTOCOL.name(),
"OtelExecChainHandler",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;

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

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

private final OpenTelemetry openTelemetry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;

import io.opentelemetry.context.propagation.TextMapSetter;
import jakarta.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachehttpclient.v5_0;
package io.opentelemetry.instrumentation.apachehttpclient.v5_2;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
Expand Down
Loading

0 comments on commit 2ea6ffa

Please sign in to comment.