Skip to content

Commit 0b21b01

Browse files
sarthak77Sarthak Singhal
andauthored
fix : added fallback support for host headers (#279)
* added gethost method * modified * added unit test * resolved PR comments * added unit test * nit * nit Co-authored-by: Sarthak Singhal <[email protected]>
1 parent 82a7a19 commit 0b21b01

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/ApiBoundaryTypeAttributeEnricher.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void enrichEvent(StructuredTrace trace, Event event) {
128128
private void enrichHostHeader(Event event) {
129129
Protocol protocol = EnrichedSpanUtils.getProtocol(event);
130130
if (protocol == Protocol.PROTOCOL_GRPC) {
131-
Optional<String> host = getGrpcAuthority(event);
131+
Optional<String> host = getGrpcHostHeader(event);
132132
if (host.isPresent()) {
133133
addEnrichedAttribute(event, HOST_HEADER, AttributeValueCreator.create(host.get()));
134134
return;
@@ -144,11 +144,18 @@ private void enrichHostHeader(Event event) {
144144
}
145145
}
146146

147-
private Optional<String> getGrpcAuthority(Event event) {
147+
private Optional<String> getGrpcHostHeader(Event event) {
148148
Optional<String> grpcAuthority = RpcSemanticConventionUtils.getGrpcAuthority(event);
149149
if (grpcAuthority.isPresent()) {
150150
return getSanitizedHostValue(grpcAuthority.get());
151151
}
152+
153+
Optional<String> grpcRequestMetadataHost =
154+
RpcSemanticConventionUtils.getGrpcRequestMetadataHost(event);
155+
if (grpcRequestMetadataHost.isPresent()) {
156+
return getSanitizedHostValue(grpcRequestMetadataHost.get());
157+
}
158+
152159
return Optional.empty();
153160
}
154161

hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/test/java/org/hypertrace/traceenricher/enrichment/enrichers/ApiBoundaryTypeAttributeEnricherTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_SYSTEM;
44
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY;
5+
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_HOST;
56
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
67
import static org.mockito.Mockito.doReturn;
78
import static org.mockito.Mockito.mock;
@@ -273,6 +274,10 @@ public void testEnrichEventWithGrpcAuthority() {
273274
innerEntrySpan,
274275
RPC_REQUEST_METADATA_AUTHORITY.getValue(),
275276
AttributeValue.newBuilder().setValue("testHost").build());
277+
addAttributeToEvent(
278+
innerEntrySpan,
279+
RPC_REQUEST_METADATA_HOST.getValue(),
280+
AttributeValue.newBuilder().setValue("testHost2").build());
276281
addAttributeToEvent(
277282
innerEntrySpan,
278283
OTEL_SPAN_TAG_RPC_SYSTEM.getValue(),
@@ -300,6 +305,25 @@ public void testEnrichEventWithGrpcNoAuthority() {
300305
Assertions.assertEquals(EnrichedSpanUtils.getHostHeader(innerEntrySpan), "testHost");
301306
}
302307

308+
@Test
309+
public void testEnrichEventWithGrpcNoAuthorityButRequestMetadataHost() {
310+
mockProtocol(innerEntrySpan, Protocol.PROTOCOL_GRPC);
311+
addEnrichedAttributeToEvent(
312+
innerEntrySpan, X_FORWARDED_HOST_METADATA, AttributeValueCreator.create("testHost"));
313+
314+
addAttributeToEvent(
315+
innerEntrySpan,
316+
RPC_REQUEST_METADATA_HOST.getValue(),
317+
AttributeValue.newBuilder().setValue("testHost2").build());
318+
addAttributeToEvent(
319+
innerEntrySpan,
320+
OTEL_SPAN_TAG_RPC_SYSTEM.getValue(),
321+
AttributeValue.newBuilder().setValue("grpc").build());
322+
323+
target.enrichEvent(trace, innerEntrySpan);
324+
Assertions.assertEquals(EnrichedSpanUtils.getHostHeader(innerEntrySpan), "testHost2");
325+
}
326+
303327
private void mockStructuredGraph() {
304328
when(graph.getParentEvent(innerExitSpan)).thenReturn(innerEntrySpan);
305329
when(graph.getChildrenEvents(innerExitSpan)).thenReturn(null);

semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY_TRUNCATED;
1818
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY;
1919
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH;
20+
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_HOST;
2021
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH;
2122
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT;
2223
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR;
@@ -68,6 +69,7 @@ public class RpcSemanticConventionUtils {
6869
private static final String OTEL_RPC_SYSTEM_GRPC =
6970
OTelRpcSemanticConventions.RPC_SYSTEM_VALUE_GRPC.getValue();
7071
private static final String OTEL_SPAN_TAG_RPC_SYSTEM_ATTR = OTEL_SPAN_TAG_RPC_SYSTEM.getValue();
72+
private static final String RPC_REQUEST_METADATA_HOST_ATTR = RPC_REQUEST_METADATA_HOST.getValue();
7173

7274
private static final String OTHER_GRPC_HOST_PORT = RawSpanConstants.getValue(Grpc.GRPC_HOST_PORT);
7375
private static final String OTHER_GRPC_METHOD = RawSpanConstants.getValue(Grpc.GRPC_METHOD);
@@ -430,6 +432,20 @@ public static Optional<String> getGrpcXForwardedFor(Event event) {
430432
return Optional.empty();
431433
}
432434

435+
public static Optional<String> getGrpcRequestMetadataHost(Event event) {
436+
if (event.getAttributes() == null || event.getAttributes().getAttributeMap() == null) {
437+
return Optional.empty();
438+
}
439+
440+
Map<String, AttributeValue> attributeValueMap = event.getAttributes().getAttributeMap();
441+
442+
if (!isRpcSystemGrpc(attributeValueMap)) {
443+
return Optional.empty();
444+
}
445+
return Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_METADATA_HOST_ATTR))
446+
.map(AttributeValue::getValue);
447+
}
448+
433449
public static Optional<String> getRpcPath(Event event) {
434450
String service = getRpcService(event).orElse("");
435451
String method = getRpcMethod(event).orElse("");

semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY_TRUNCATED;
1818
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY;
1919
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH;
20+
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_HOST;
2021
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH;
2122
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT;
2223
import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR;
@@ -165,6 +166,37 @@ public void testGetGrpcXForwardedFor() {
165166
assertEquals("198.12.34.1", RpcSemanticConventionUtils.getGrpcXForwardedFor(event).get());
166167
}
167168

169+
@Test
170+
public void testGetGrpcRequestMetadataHostForGrpcSystem() {
171+
Event event = mock(Event.class);
172+
when(event.getAttributes())
173+
.thenReturn(
174+
Attributes.newBuilder()
175+
.setAttributeMap(
176+
Map.of(
177+
OTEL_SPAN_TAG_RPC_SYSTEM.getValue(),
178+
AttributeValue.newBuilder().setValue("grpc").build(),
179+
RPC_REQUEST_METADATA_HOST.getValue(),
180+
AttributeValue.newBuilder().setValue("webhost:9011").build()))
181+
.build());
182+
assertEquals(
183+
"webhost:9011", RpcSemanticConventionUtils.getGrpcRequestMetadataHost(event).get());
184+
}
185+
186+
@Test
187+
public void testGetGrpcRequestMetadataHostForNotGrpcSystem() {
188+
Event event = mock(Event.class);
189+
when(event.getAttributes())
190+
.thenReturn(
191+
Attributes.newBuilder()
192+
.setAttributeMap(
193+
Map.of(
194+
RPC_REQUEST_METADATA_HOST.getValue(),
195+
AttributeValue.newBuilder().setValue("webhost:9011").build()))
196+
.build());
197+
assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcRequestMetadataHost(event));
198+
}
199+
168200
@Test
169201
public void testGetGrpcStatusMsg() {
170202
Event event =

span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public enum RpcSpanTag {
55
RPC_ERROR_NAME("rpc.error_name"),
66
RPC_ERROR_MESSAGE("rpc.error_message"),
77
RPC_REQUEST_METADATA("rpc.request.metadata"),
8+
RPC_REQUEST_METADATA_HOST("rpc.request.metadata.host"),
89
RPC_RESPONSE_METADATA("rpc.response.metadata"),
910
RPC_REQUEST_BODY("rpc.request.body"),
1011
RPC_RESPONSE_BODY("rpc.response.body"),

0 commit comments

Comments
 (0)