Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OJ-3015: otel #445

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ subprojects {
cri_common_lib
pact_tests
webcompere
otel
}

// The dynamodb enhanced package loads the apache-client as well as the spi-client, so
Expand Down Expand Up @@ -101,7 +102,8 @@ subprojects {

soap "com.sun.xml.messaging.saaj:saaj-impl:1.5.3",
"org.apache.cxf:cxf-rt-frontend-jaxws:3.4.5",
"org.apache.cxf:cxf-rt-transports-http:3.4.5"
"org.apache.cxf:cxf-rt-transports-http:3.4.5",
"org.apache.httpcomponents:httpclient:4.1.4"

powertools "software.amazon.lambda:powertools-logging:${dependencyVersions.powertools_version}",
"software.amazon.lambda:powertools-metrics:${dependencyVersions.powertools_version}",
Expand Down Expand Up @@ -133,6 +135,13 @@ subprojects {

webcompere "uk.org.webcompere:system-stubs-core:${dependencyVersions.webcompere_version}",
"uk.org.webcompere:system-stubs-jupiter:${dependencyVersions.webcompere_version}"

otel "io.opentelemetry.instrumentation:opentelemetry-aws-sdk-2.2-autoconfigure:2.12.0-alpha",
"io.opentelemetry.instrumentation:opentelemetry-java-http-client:2.12.0-alpha",

'io.opentelemetry:opentelemetry-sdk:1.29.0',
'io.opentelemetry:opentelemetry-sdk-logs:1.29.0',
'io.opentelemetry:opentelemetry-exporter-otlp:1.29.0'
}

apply plugin: 'java'
Expand Down
1 change: 1 addition & 0 deletions infrastructure/lambda/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Globals:
- "{{resolve:secretsmanager:${SecretArn}:SecretString:DT_TENANT}}"
- SecretArn: !FindInMap [EnvironmentConfiguration, !Ref Environment, dynatraceSecretArn]
DT_OPEN_TELEMETRY_ENABLE_INTEGRATION: "true"
OTEL_INSTRUMENTATION_AWS_SDK_EXPERIMENTAL_USE_PROPAGATOR_FOR_MESSAGING: "true"
Layers:
- !Sub
- "{{resolve:secretsmanager:${SecretArn}:SecretString:JAVA_LAYER}}"
Expand Down
3 changes: 3 additions & 0 deletions lambdas/abandon/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ plugins {
}

dependencies {

runtimeOnly configurations.otel

implementation project(":lib"),
configurations.cri_common_lib,
configurations.aws,
Expand Down
3 changes: 3 additions & 0 deletions lambdas/answer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ plugins {
}

dependencies {

runtimeOnly configurations.otel

implementation project(":lib"),
configurations.cri_common_lib,
configurations.aws,
Expand Down
3 changes: 3 additions & 0 deletions lambdas/issuecredential/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ plugins {
}

dependencies {

runtimeOnly configurations.otel

implementation project(":lib"),
configurations.cri_common_lib,
configurations.aws,
Expand Down
3 changes: 3 additions & 0 deletions lambdas/question/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ repositories {
}

dependencies {

runtimeOnly configurations.otel

implementation project(":lib"),
configurations.cri_common_lib,
configurations.aws,
Expand Down
2 changes: 2 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repositories {
}

dependencies {
implementation configurations.otel

implementation configurations.cri_common_lib,
configurations.aws,
configurations.dynamodb,
Expand Down
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;
}
}
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;
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.experian.uk.schema.experian.identityiq.services.webservice.IdentityIQWebService;
import com.experian.uk.wasp.TokenService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.http.HTTPConduitFactory;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.lambda.powertools.parameters.SSMProvider;
Expand Down Expand Up @@ -89,6 +92,9 @@ KBVGateway getKbvGateway(KeyStoreLoader keyStoreLoader, KBVClientFactory kbvClie
}

private KBVClientFactory getKbvClientFactory() {
Bus bus = BusFactory.getDefaultBus();
bus.setExtension(new OtelHttpConduitFactory(), HTTPConduitFactory.class);

TokenService tokenService = new TokenService();
SoapToken soapToken = new SoapToken(APPLICATION, true, tokenService, configurationService);
HeaderHandler headerHandler = new HeaderHandler(soapToken);
Expand Down
Loading