Skip to content

Commit 3dd4b3c

Browse files
committed
feat: introduce url.template
1 parent a4a85b3 commit 3dd4b3c

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerContext.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,13 @@ public Map<String, Object> getAttemptAttributes() {
182182
if (transport() == Transport.GRPC && !Strings.isNullOrEmpty(fullMethodName())) {
183183
attributes.put(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE, fullMethodName());
184184
}
185-
if (transport() == Transport.HTTP && !Strings.isNullOrEmpty(httpMethod())) {
186-
attributes.put(ObservabilityAttributes.HTTP_METHOD_ATTRIBUTE, httpMethod());
185+
if (transport() == Transport.HTTP) {
186+
if (!Strings.isNullOrEmpty(httpMethod())) {
187+
attributes.put(ObservabilityAttributes.HTTP_METHOD_ATTRIBUTE, httpMethod());
188+
}
189+
if (!Strings.isNullOrEmpty(httpPathTemplate())) {
190+
attributes.put(ObservabilityAttributes.HTTP_URL_TEMPLATE_ATTRIBUTE, httpPathTemplate());
191+
}
187192
}
188193
return attributes;
189194
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityAttributes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class ObservabilityAttributes {
6262
/** The HTTP method of the request (e.g., "GET"). Only used in HTTP transport. */
6363
public static final String HTTP_METHOD_ATTRIBUTE = "http.request.method";
6464

65+
/** The HTTP method of the request (e.g., "GET"). Only used in HTTP transport. */
66+
public static final String HTTP_URL_TEMPLATE_ATTRIBUTE = "url.template";
67+
6568
/**
6669
* The error codes of the request. The value will be the string representation of the canonical
6770
* gRPC status code (e.g., "OK", "INTERNAL").

gax-java/gax/src/test/java/com/google/api/gax/tracing/ApiTracerContextTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ void testGetAttemptAttributes_rpcSystemName() {
140140
assertThat(attributes).containsEntry(ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE, "grpc");
141141
}
142142

143+
@Test
144+
void testGetAttemptAttributes_httpPathTemplate() {
145+
ApiTracerContext context =
146+
ApiTracerContext.newBuilder()
147+
.setLibraryMetadata(LibraryMetadata.empty())
148+
.setTransport(ApiTracerContext.Transport.HTTP)
149+
.setHttpPathTemplate("the-template")
150+
.build();
151+
Map<String, Object> attributes = context.getAttemptAttributes();
152+
153+
assertThat(attributes)
154+
.containsEntry(ObservabilityAttributes.HTTP_URL_TEMPLATE_ATTRIBUTE, "the-template");
155+
}
156+
143157
@Test
144158
void testGetAttemptAttributes_empty() {
145159
ApiTracerContext context = ApiTracerContext.empty();

gax-java/gax/src/test/java/com/google/api/gax/tracing/ObservabilityUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ void testToOtelAttributes_correctConversion() {
8484
.isEqualTo(attribute2Value);
8585
}
8686

87+
@Test
88+
void testToOtelAttributes_urlTemplateAttribute() {
89+
String urlTemplateValue = "/blogs/{blog}/posts/{post}";
90+
Map<String, String> attributes =
91+
ImmutableMap.of(ObservabilityAttributes.HTTP_URL_TEMPLATE_ATTRIBUTE, urlTemplateValue);
92+
93+
Attributes otelAttributes = ObservabilityUtils.toOtelAttributes(attributes);
94+
95+
Truth.assertThat(
96+
otelAttributes.get(
97+
AttributeKey.stringKey(ObservabilityAttributes.HTTP_URL_TEMPLATE_ATTRIBUTE)))
98+
.isEqualTo(urlTemplateValue);
99+
}
100+
87101
@Test
88102
void testToOtelAttributes_nullInput() {
89103
Throwable thrown =

java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelTracing.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ void testTracing_successfulEcho_httpjson() throws Exception {
189189
.getAttributes()
190190
.get(AttributeKey.stringKey(ObservabilityAttributes.HTTP_METHOD_ATTRIBUTE)))
191191
.isEqualTo("POST");
192+
assertThat(
193+
attemptSpan
194+
.getAttributes()
195+
.get(AttributeKey.stringKey(ObservabilityAttributes.HTTP_URL_TEMPLATE_ATTRIBUTE)))
196+
.isEqualTo("v1beta1/echo:echo");
192197
}
193198
}
194199
}

0 commit comments

Comments
 (0)