Skip to content

Commit

Permalink
Introduce x-request-id header support for tracing rest calls with fur…
Browse files Browse the repository at this point in the history
…ther Iris messaging
  • Loading branch information
tomazic89 committed Sep 23, 2024
1 parent da8f38f commit 7a664a4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
3 changes: 0 additions & 3 deletions asyncapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
<module>spec/api</module>
<module>implementation</module>
</modules>
<properties>
<version.jakarta-validation-api>3.1.0</version.jakarta-validation-api>
</properties>

<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static class Message {
public static final String REQUEST_VIA = "x-request-via";
public static final String REQUEST_REFERER = "x-request-referer";
public static final String REQUEST_URI = "x-request-uri";
public static final String REQUEST_ID = "x-request-id";
public static final String SERVER_TIMESTAMP = "x-server-timestamp";
public static final String SUBSCRIPTION_ID = "x-subscription-id";
public static final String CACHE_TTL = "x-cache-ttl";
Expand Down
7 changes: 6 additions & 1 deletion extension/runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.iris-events</groupId>
Expand Down Expand Up @@ -74,6 +75,10 @@
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
import static org.iris_events.common.MessagingHeaders.Message.INSTANCE_ID;
import static org.iris_events.common.MessagingHeaders.Message.JWT;
import static org.iris_events.common.MessagingHeaders.Message.ORIGIN_SERVICE_ID;
import static org.iris_events.common.MessagingHeaders.Message.REQUEST_ID;
import static org.iris_events.common.MessagingHeaders.Message.ROUTER;
import static org.iris_events.common.MessagingHeaders.Message.SERVER_TIMESTAMP;
import static org.iris_events.common.MessagingHeaders.Message.SESSION_ID;
import static org.iris_events.common.MessagingHeaders.Message.SUBSCRIPTION_ID;
import static org.iris_events.common.MessagingHeaders.Message.USER_ID;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.Context;

import org.iris_events.annotations.Scope;
import org.iris_events.common.DeliveryMode;
Expand Down Expand Up @@ -50,6 +52,9 @@ public class BasicPropertiesProvider {
@Inject
TimestampProvider timestampProvider;

@Context
ContainerRequestContext requestContext;

/**
* Gets AmqpBasicProperties from eventContext or creates new, if eventContext returns null. Basic properties headers are
* modified according to provided routingDetails.
Expand All @@ -75,10 +80,17 @@ private AMQP.BasicProperties createAmqpBasicProperties(final String serviceId) {
final var correlationId = correlationIdProvider.getCorrelationId();
log.debug("Creating new AMQP.BasicProperties with correlationId: {}", correlationId);
final var builder = new AMQP.BasicProperties().builder()
.correlationId(correlationId)
.headers(Map.of());
.correlationId(correlationId);
final var headers = new HashMap<String, Object>();
// only set origin_service_id once and never break it as source cannot change
Optional.ofNullable(serviceId).ifPresent(id -> builder.headers(Map.of(ORIGIN_SERVICE_ID, serviceId)));
Optional.ofNullable(serviceId).ifPresent(id -> headers.put(ORIGIN_SERVICE_ID, serviceId));
if (requestContext != null) {
// when thread was started by HTTP request we check for x-request-id header and add it to the AMQP message headers
Optional.ofNullable(requestContext.getHeaderString(REQUEST_ID))
.ifPresent(httpRequestId -> headers.put(REQUEST_ID, httpRequestId));
}
builder.headers(headers);

return builder.build();
}

Expand Down
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<version.impsort.plugin>1.9.0</version.impsort.plugin>
<version.formatter-maven-plugin>2.23.0</version.formatter-maven-plugin>
<version.release.plugin>3.0.1</version.release.plugin>
<version.jakarta-validation-api>3.1.0</version.jakarta-validation-api>
<version.jakarta.ws.rs-api>3.1.0</version.jakarta.ws.rs-api>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -94,6 +96,13 @@
<version>${version.jackson}</version>
</dependency>


<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${version.jakarta.ws.rs-api}</version>
</dependency>

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
Expand Down

0 comments on commit 7a664a4

Please sign in to comment.