-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
162 additions
and
2 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
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
35 changes: 35 additions & 0 deletions
35
lib/src/main/java/uk/gov/di/ipv/cri/kbv/api/service/OtelHttpClientWrapper.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,35 @@ | ||
package uk.gov.di.ipv.cri.kbv.api.service; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.httpclient.JavaHttpClientTelemetry; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.http.HttpClient; | ||
import java.time.Duration; | ||
|
||
public class OtelHttpClientWrapper { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(OtelHttpClientWrapper.class); | ||
private final HttpClient telemetryHttpClient; | ||
|
||
public OtelHttpClientWrapper() { | ||
this.telemetryHttpClient = createTelemetryHttpClient(); | ||
} | ||
|
||
private HttpClient createTelemetryHttpClient() { | ||
LOGGER.info("Creating OpenTelemetry-instrumented HttpClient."); | ||
|
||
return JavaHttpClientTelemetry.builder(GlobalOpenTelemetry.get()) | ||
.build() | ||
.newHttpClient( | ||
HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_2) | ||
.connectTimeout(Duration.ofSeconds(10)) | ||
.followRedirects(HttpClient.Redirect.NORMAL) | ||
.build()); | ||
} | ||
|
||
public HttpClient getTelemetryHttpClient() { | ||
return telemetryHttpClient; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
lib/src/main/java/uk/gov/di/ipv/cri/kbv/api/service/OtelHttpConduit.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,84 @@ | ||
package uk.gov.di.ipv.cri.kbv.api.service; | ||
|
||
import org.apache.cxf.Bus; | ||
import org.apache.cxf.message.Message; | ||
import org.apache.cxf.service.model.EndpointInfo; | ||
import org.apache.cxf.transport.http.Address; | ||
import org.apache.cxf.transport.http.HTTPConduit; | ||
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; | ||
import org.apache.cxf.ws.addressing.EndpointReferenceType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class OtelHttpConduit extends HTTPConduit { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(OtelHttpConduit.class); | ||
|
||
private final OtelHttpClientWrapper otelHttpClientWrapper; | ||
private final HttpClient httpClient; | ||
|
||
public OtelHttpConduit( | ||
Bus bus, | ||
EndpointInfo endpointInfo, | ||
EndpointReferenceType target, | ||
OtelHttpClientWrapper otelHttpClientWrapper) | ||
throws IOException { | ||
super(bus, endpointInfo, target); | ||
this.otelHttpClientWrapper = otelHttpClientWrapper; | ||
this.httpClient = otelHttpClientWrapper.getTelemetryHttpClient(); | ||
} | ||
|
||
@Override | ||
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) | ||
throws IOException { | ||
LOGGER.info("Setting up connection for URL: {}", address.getString()); | ||
if (csPolicy != null) { | ||
LOGGER.info("Applying HTTP client policy settings..."); | ||
} | ||
} | ||
|
||
@Override | ||
protected OutputStream createOutputStream( | ||
Message message, boolean needToCacheRequest, boolean isChunking, int chunkThreshold) | ||
throws IOException { | ||
LOGGER.info("Creating output stream for HTTP request."); | ||
return new ByteArrayOutputStream(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
LOGGER.info("Closing OtelHttpConduit."); | ||
super.close(); | ||
} | ||
|
||
/** Sends an HTTP request asynchronously using the OpenTelemetry instrumented HttpClient. */ | ||
public CompletableFuture<HttpResponse<String>> sendRequestAsync(String url) { | ||
try { | ||
URI uri = URI.create(url); | ||
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); | ||
|
||
LOGGER.info("Sending HTTP request to {}", url); | ||
return httpClient | ||
.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenApply( | ||
response -> { | ||
LOGGER.info( | ||
"Received response with status: {}", response.statusCode()); | ||
return response; | ||
}); | ||
} catch (Exception e) { | ||
LOGGER.error("Error sending HTTP request", e); | ||
CompletableFuture<HttpResponse<String>> failedFuture = new CompletableFuture<>(); | ||
failedFuture.completeExceptionally(e); | ||
return failedFuture; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/src/main/java/uk/gov/di/ipv/cri/kbv/api/service/OtelHttpConduitFactory.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,28 @@ | ||
package uk.gov.di.ipv.cri.kbv.api.service; | ||
|
||
import org.apache.cxf.Bus; | ||
import org.apache.cxf.service.model.EndpointInfo; | ||
import org.apache.cxf.transport.http.HTTPConduit; | ||
import org.apache.cxf.transport.http.HTTPConduitFactory; | ||
import org.apache.cxf.transport.http.HTTPTransportFactory; | ||
import org.apache.cxf.ws.addressing.EndpointReferenceType; | ||
|
||
import java.io.IOException; | ||
|
||
public class OtelHttpConduitFactory implements HTTPConduitFactory { | ||
private final OtelHttpClientWrapper otelHttpClientWrapper; | ||
|
||
public OtelHttpConduitFactory() { | ||
this.otelHttpClientWrapper = new OtelHttpClientWrapper(); | ||
} | ||
|
||
@Override | ||
public HTTPConduit createConduit( | ||
HTTPTransportFactory factory, | ||
Bus bus, | ||
EndpointInfo endpointInfo, | ||
EndpointReferenceType target) | ||
throws IOException { | ||
return new OtelHttpConduit(bus, endpointInfo, target, otelHttpClientWrapper); | ||
} | ||
} |
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