Add javadoc to ServletContextPath method (#4484)

* Add javadoc to ServletContextPath method

* Normalise span name and add test

* Update instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/servlet/ServletContextPathTest.java

Co-authored-by: Lauri Tulmin <tulmin@gmail.com>

Co-authored-by: Lauri Tulmin <tulmin@gmail.com>
This commit is contained in:
Nikita Salnikov-Tarnovski 2021-10-25 23:44:02 +03:00 committed by GitHub
parent 25f6864602
commit 3d92cd2337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View File

@ -50,12 +50,21 @@ public final class ServletContextPath {
this.contextPath = contextPath;
}
/**
* Returns a concatenation of a servlet context path stored in the given {@code context} and a
* given {@code spanName}. If there is no servlet path stored in the context, returns {@code
* spanName}.
*/
public static String prepend(Context context, String spanName) {
ServletContextPath servletContextPath = context.get(CONTEXT_KEY);
if (servletContextPath != null) {
String value = servletContextPath.contextPath;
if (value != null) {
return value + spanName;
if (spanName == null || spanName.isEmpty()) {
return value;
} else {
return value + (spanName.startsWith("/") ? spanName : ("/" + spanName));
}
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.api.servlet;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.context.Context;
import org.junit.jupiter.api.Test;
public class ServletContextPathTest {
@Test
public void shouldAddSlashBetweenContextAndSpanName() {
Context contextWithEmptyPath = ServletContextPath.init(Context.root(), p -> p, "");
Context contextWithPath = ServletContextPath.init(Context.root(), p -> p, "/context");
assertThat(ServletContextPath.prepend(contextWithEmptyPath, "spanName")).isEqualTo("spanName");
assertThat(ServletContextPath.prepend(contextWithPath, "spanName"))
.isEqualTo("/context/spanName");
}
@Test
public void shouldNotResultInDuplicateSlash() {
Context contextWithEmptyPath = ServletContextPath.init(Context.root(), p -> p, "");
Context contextWithPath = ServletContextPath.init(Context.root(), p -> p, "/context");
assertThat(ServletContextPath.prepend(contextWithEmptyPath, "/spanName"))
.isEqualTo("/spanName");
assertThat(ServletContextPath.prepend(contextWithPath, "/spanName"))
.isEqualTo("/context/spanName");
}
@Test
public void shouldIgnoreEmptySpanName() {
Context contextWithEmptyPath = ServletContextPath.init(Context.root(), p -> p, "");
Context contextWithPath = ServletContextPath.init(Context.root(), p -> p, "/context");
assertThat(ServletContextPath.prepend(contextWithEmptyPath, "")).isEqualTo("");
assertThat(ServletContextPath.prepend(contextWithPath, "")).isEqualTo("/context");
assertThat(ServletContextPath.prepend(contextWithEmptyPath, null)).isEqualTo(null);
assertThat(ServletContextPath.prepend(contextWithPath, null)).isEqualTo("/context");
}
}