Skip to content

Commit bc983fc

Browse files
committed
fix: SpanTracer to not set attribute if url sanitization failed
1 parent aa4822c commit bc983fc

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/SpanTracer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,13 @@ private void endAttempt() {
207207

208208
@Override
209209
public void requestUrlResolved(String url) {
210-
if (attemptSpan != null && url != null) {
211-
attemptSpan.setAttribute(
212-
ObservabilityAttributes.HTTP_URL_FULL_ATTRIBUTE, ObservabilityUtils.sanitizeUrlFull(url));
210+
if (attemptSpan == null) {
211+
return;
212+
}
213+
String sanitizedUrlString = ObservabilityUtils.sanitizeUrlFull(url);
214+
if (sanitizedUrlString.isEmpty()) {
215+
return;
213216
}
217+
attemptSpan.setAttribute(ObservabilityAttributes.HTTP_URL_FULL_ATTRIBUTE, sanitizedUrlString);
214218
}
215219
}

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/SpanTracerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import static com.google.common.truth.Truth.assertThat;
3333
import static org.mockito.ArgumentMatchers.any;
3434
import static org.mockito.ArgumentMatchers.anyString;
35+
import static org.mockito.ArgumentMatchers.eq;
36+
import static org.mockito.Mockito.never;
3537
import static org.mockito.Mockito.verify;
3638
import static org.mockito.Mockito.when;
3739

@@ -259,4 +261,15 @@ void testRequestUrlResolved_setsAttribute() {
259261
ObservabilityAttributes.HTTP_URL_FULL_ATTRIBUTE,
260262
"https://example.com?api_key=REDACTED");
261263
}
264+
265+
@Test
266+
void testRequestUrlResolved_badUrl_notSet() {
267+
spanTracer.attemptStarted(new Object(), 1);
268+
269+
String rawUrl = "htps:::://the-example";
270+
spanTracer.requestUrlResolved(rawUrl);
271+
272+
verify(span, never())
273+
.setAttribute(eq(ObservabilityAttributes.HTTP_URL_FULL_ATTRIBUTE), anyString());
274+
}
262275
}

0 commit comments

Comments
 (0)