Skip to content

Commit 54a3376

Browse files
committed
feat: #74 Add OpenTelemetry traces propagation
Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com>
1 parent 2a06d38 commit 54a3376

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

proxy/src/main/java/io/reshapr/proxy/proxy/GrpcProxyService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@
4949
import jakarta.enterprise.context.ApplicationScoped;
5050
import org.jboss.logging.Logger;
5151

52-
import javax.net.ssl.X509TrustManager;
5352
import java.io.ByteArrayInputStream;
5453
import java.io.IOException;
5554
import java.net.URI;
5655
import java.net.URL;
5756
import java.nio.charset.StandardCharsets;
58-
import java.security.cert.X509Certificate;
5957
import java.util.List;
6058
import java.util.Map;
6159
import java.util.concurrent.TimeUnit;
@@ -288,6 +286,9 @@ private static Metadata convertHeadersToMetadata(Map<String, List<String>> heade
288286
// Manage the Forwarded and X-Forwarded-For headers.
289287
HeadersUtil.addForwardingHeaders(headers);
290288

289+
// Also inject OpenTelemetry tracing headers.
290+
HeadersUtil.injectTracingHeaders(headers);
291+
291292
// Some specific headers must not be included in the gRPC metadata.
292293
headers.entrySet().stream()
293294
.filter(entry -> !RESTRICTED_HEADERS.contains(entry.getKey().toLowerCase()))

proxy/src/main/java/io/reshapr/proxy/proxy/HeadersUtil.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
import io.reshapr.proxy.context.MethodHandlingContext;
1919

20+
import io.opentelemetry.context.Context;
21+
import io.opentelemetry.context.propagation.TextMapPropagator;
22+
import io.opentelemetry.context.propagation.TextMapSetter;
23+
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
24+
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
25+
2026
import java.util.List;
2127
import java.util.Map;
2228

@@ -30,6 +36,10 @@ public class HeadersUtil {
3036
public static final String X_FORWARDED_FOR = "X-Forwarded-For";
3137
private static final String FORWARDED_FOR = "for=";
3238

39+
private static final TextMapPropagator OTEL_PROPAGATOR =
40+
TextMapPropagator.composite(W3CTraceContextPropagator.getInstance(),
41+
W3CBaggagePropagator.getInstance());
42+
3343
private HeadersUtil () {
3444
// Utility class
3545
}
@@ -57,6 +67,14 @@ public static void addForwardingHeaders(Map<String, List<String>> headers) {
5767
}
5868
}
5969

70+
/**
71+
* Inject tracing headers into the given map.
72+
* @param headers The headers map to inject into.
73+
*/
74+
public static void injectTracingHeaders(Map<String, List<String>> headers) {
75+
OTEL_PROPAGATOR.inject(Context.current(), headers, new MapTextMapSetter());
76+
}
77+
6078
private static String getForAddress(String remoteAddress) {
6179
if (!remoteAddress.contains(":")) {
6280
return remoteAddress;
@@ -65,4 +83,13 @@ private static String getForAddress(String remoteAddress) {
6583
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Forwarded#transitioning_from_x-forwarded-for_to_forwarded
6684
return "\"[" + remoteAddress + "]\"";
6785
}
86+
87+
private static class MapTextMapSetter implements TextMapSetter<Map<String, List<String>>> {
88+
@Override
89+
public void set(Map<String, List<String>> carrier, String key, String value) {
90+
if (carrier != null) {
91+
carrier.put(key, List.of(value));
92+
}
93+
}
94+
}
6895
}

proxy/src/main/java/io/reshapr/proxy/proxy/ProxyService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public BackendResponse callBackend(ConfigurationEntry configuration, URI externa
7878
// Manage the Forwarded and X-Forwarded-For headers.
7979
HeadersUtil.addForwardingHeaders(requestHeaders);
8080

81+
// Also inject OpenTelemetry tracing headers.
82+
HeadersUtil.injectTracingHeaders(headers);
83+
8184
// If the configuration has a backend secret, manage security headers.
8285
if (configuration.backendSecret() != null) {
8386
manageSecurityHeaders(configuration.backendSecret(), requestHeaders);

proxy/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ quarkus.otel.traces.enabled=true
3333
quarkus.otel.metrics.enabled=true
3434
quarkus.otel.logs.enabled=true
3535
#quarkus.otel.exporter.otlp.endpoint=http://localhost:4317
36-
#quarkus.otel.exporter.otlp.protocol=http/protobuf
36+
#quarkus.otel.exporter.otlp.protocol=http|grpc
3737
#quarkus.otel.exporter.otlp.headers=authorization=Bearer my_secret
3838

3939
# Stores configuration.

0 commit comments

Comments
 (0)