Tests for ServletHttpServerTracer.getSpanName() (#1990)

This commit is contained in:
jason plumb 2021-01-06 10:59:34 -08:00 committed by GitHub
parent 2b97c42e01
commit 6c30888c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 5 deletions

View File

@ -7,4 +7,8 @@ dependencies {
implementation deps.slf4j
compileOnly group: 'javax.servlet', name: 'servlet-api', version: '2.2'
testImplementation group: 'javax.servlet', name: 'servlet-api', version: '2.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.6.0'
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.18.1'
}

View File

@ -140,15 +140,15 @@ public abstract class ServletHttpServerTracer<RESPONSE>
}
public static String getSpanName(HttpServletRequest request) {
String spanName = request.getServletPath();
if (spanName.isEmpty()) {
String servletPath = request.getServletPath();
if (servletPath.isEmpty()) {
return "HTTP " + request.getMethod();
}
String contextPath = request.getContextPath();
if (contextPath != null && !contextPath.isEmpty() && !contextPath.equals("/")) {
spanName = contextPath + spanName;
if (contextPath == null || contextPath.isEmpty() || contextPath.equals("/")) {
return servletPath;
}
return spanName;
return contextPath + servletPath;
}
/**

View File

@ -0,0 +1,70 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.servlet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import javax.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;
public class ServletHttpServerTracerTest {
@Test
void testGetSpanName_emptySpanName() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletPath()).thenReturn("");
when(request.getMethod()).thenReturn("PUT");
String spanName = ServletHttpServerTracer.getSpanName(request);
assertThat(spanName).isEqualTo("HTTP PUT");
}
@Test
void testGetSpanName_nullSpanName() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletPath()).thenReturn(null);
assertThatThrownBy(() -> ServletHttpServerTracer.getSpanName(request))
.isInstanceOf(NullPointerException.class);
}
@Test
void testGetSpanName_nullContextPath() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletPath()).thenReturn("/swizzler");
when(request.getContextPath()).thenReturn(null);
String spanName = ServletHttpServerTracer.getSpanName(request);
assertThat(spanName).isEqualTo("/swizzler");
}
@Test
void testGetSpanName_emptyContextPath() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletPath()).thenReturn("/swizzler");
when(request.getContextPath()).thenReturn("");
String spanName = ServletHttpServerTracer.getSpanName(request);
assertThat(spanName).isEqualTo("/swizzler");
}
@Test
void testGetSpanName_slashContextPath() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletPath()).thenReturn("/swizzler");
when(request.getContextPath()).thenReturn("/");
String spanName = ServletHttpServerTracer.getSpanName(request);
assertThat(spanName).isEqualTo("/swizzler");
}
@Test
void testGetSpanName_appendsSpanNameToContext() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletPath()).thenReturn("/swizzler");
when(request.getContextPath()).thenReturn("/path/to");
String spanName = ServletHttpServerTracer.getSpanName(request);
assertThat(spanName).isEqualTo("/path/to/swizzler");
}
}