Skip to content

Commit 30f0a7b

Browse files
authored
feat: add forming grpc reqeuest url if rpc attributes are present (#280)
* feat: add forming grpc reqeust url if rpc attributes are present * nit: minor fix on comment
1 parent ece2263 commit 30f0a7b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

hypertrace-view-generator/hypertrace-view-generator/src/main/java/org/hypertrace/viewgenerator/generators/SpanEventViewGenerator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.hypertrace.core.datamodel.StructuredTrace;
1616
import org.hypertrace.core.datamodel.shared.SpanAttributeUtils;
1717
import org.hypertrace.semantic.convention.utils.http.HttpSemanticConventionUtils;
18+
import org.hypertrace.semantic.convention.utils.rpc.RpcSemanticConventionUtils;
1819
import org.hypertrace.traceenricher.enrichedspan.constants.EnrichedSpanConstants;
1920
import org.hypertrace.traceenricher.enrichedspan.constants.utils.EnrichedSpanUtils;
2021
import org.hypertrace.traceenricher.enrichedspan.constants.v1.Api;
@@ -315,7 +316,15 @@ String getRequestUrl(Event event, Protocol protocol) {
315316
.orElse(HttpSemanticConventionUtils.getHttpPath(event).orElse(null));
316317

317318
case PROTOCOL_GRPC:
318-
return event.getEventName();
319+
/**
320+
* For RPC methods, we show URL/URI as a combination of rpc.service and rpc.method. The same
321+
* information is also available as Span Name -
322+
* https://github.com/open-telemetry/opentelemetry-specification/blob/3e380e249f60c3a5f68746f5e84d10195ba41a79/specification/trace/semantic_conventions/rpc.md#span-name
323+
* So, as part of this method, we will form Url using rpc.service and rpc.method, and
324+
* fallback to spanName. While setting GRPC protocol from rpc attributes, it already checks
325+
* for rpc.system.
326+
*/
327+
return RpcSemanticConventionUtils.getRpcPath(event).orElse(event.getEventName());
319328
}
320329
return null;
321330
}

hypertrace-view-generator/hypertrace-view-generator/src/test/java/org/hypertrace/viewgenerator/generators/SpanEventViewGeneratorTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,57 @@ public void test_getRequestUrl_grpcProctol_shouldReturnEventName() {
7979
spanEventViewGenerator.getRequestUrl(event, Protocol.PROTOCOL_GRPC));
8080
}
8181

82+
@Test
83+
public void test_getRequestUrl_grpcProctol_shouldReturnRpcServiceAndMethod() {
84+
Event event = mock(Event.class);
85+
when(event.getAttributes())
86+
.thenReturn(
87+
Attributes.newBuilder()
88+
.setAttributeMap(
89+
Map.of(
90+
"rpc.service",
91+
AttributeValue.newBuilder().setValue("hipstershop.AdService").build(),
92+
"rpc.method",
93+
AttributeValue.newBuilder().setValue("GetEcho").build()))
94+
.build());
95+
when(event.getEventName()).thenReturn("Sent.hipstershop.AdService.GetAds");
96+
assertEquals(
97+
"hipstershop.AdService.GetEcho",
98+
spanEventViewGenerator.getRequestUrl(event, Protocol.PROTOCOL_GRPC));
99+
}
100+
101+
@Test
102+
public void test_getRequestUrl_grpcProctol_shouldReturnEventNameIfOnlyRpcService() {
103+
Event event = mock(Event.class);
104+
when(event.getAttributes())
105+
.thenReturn(
106+
Attributes.newBuilder()
107+
.setAttributeMap(
108+
Map.of(
109+
"rpc.service",
110+
AttributeValue.newBuilder().setValue("hipstershop.AdService").build()))
111+
.build());
112+
when(event.getEventName()).thenReturn("Sent.hipstershop.AdService.GetAds");
113+
assertEquals(
114+
"Sent.hipstershop.AdService.GetAds",
115+
spanEventViewGenerator.getRequestUrl(event, Protocol.PROTOCOL_GRPC));
116+
}
117+
118+
@Test
119+
public void test_getRequestUrl_grpcProctol_shouldReturnEventNameIfOnlyRpcMethod() {
120+
Event event = mock(Event.class);
121+
when(event.getAttributes())
122+
.thenReturn(
123+
Attributes.newBuilder()
124+
.setAttributeMap(
125+
Map.of("rpc.method", AttributeValue.newBuilder().setValue("GetEcho").build()))
126+
.build());
127+
when(event.getEventName()).thenReturn("Sent.hipstershop.AdService.GetAds");
128+
assertEquals(
129+
"Sent.hipstershop.AdService.GetAds",
130+
spanEventViewGenerator.getRequestUrl(event, Protocol.PROTOCOL_GRPC));
131+
}
132+
82133
@Test
83134
public void testGetRequestUrl_fullUrlIsAbsent() {
84135
Event event =

0 commit comments

Comments
 (0)