Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make servlet context path key private #4125

Merged
merged 6 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import java.util.function.Supplier;

/**
* The context key here is used to propagate the servlet context path throughout the request, so
Expand All @@ -22,18 +23,41 @@ public final class ServletContextPath {

// Keeps track of the servlet context path that needs to be prepended to the route when updating
// the span name
public static final ContextKey<String> CONTEXT_KEY =
private static final ContextKey<ServletContextPath> CONTEXT_KEY =
Comment on lines -25 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the ServletContextPath wrapper object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using wrapper helps to distinguish context path not yet set from context path empty. I guess I could use "" to mark empty context path.

ContextKey.named("opentelemetry-servlet-context-path-key");

public static String prepend(Context context, String spanName) {
String value = context.get(CONTEXT_KEY);
// checking isEmpty just to avoid unnecessary string concat / allocation
if (value != null && !value.isEmpty()) {
return value + spanName;
} else {
return spanName;
public static Context init(Context context, Supplier<String> contextPathSupplier) {
ServletContextPath servletContextPath = context.get(CONTEXT_KEY);
if (servletContextPath != null) {
return context;
}
String contextPath = contextPathSupplier.get();
if (contextPath == null) {
// context path isn't know yet
return context;
}
if (contextPath.isEmpty() || contextPath.equals("/")) {
// normalize empty context path to null
contextPath = null;
}
return context.with(CONTEXT_KEY, new ServletContextPath(contextPath));
}

private final String contextPath;

private ServletContextPath(String contextPath) {
this.contextPath = contextPath;
}

private ServletContextPath() {}
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;
}
}

return spanName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public void stopSpan(

instrumenter.end(context, requestContext, responseContext, throwable);
}

public Context updateContext(Context context, HttpServletRequest request) {
return addServletContextPath(context, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Context updateContext(
context,
servlet ? SERVLET : FILTER,
() -> SPAN_NAME_PROVIDER.getSpanNameOrNull(mappingResolver, request));
return updateContext(context, request);
return addServletContextPath(context, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,8 @@ protected Context customizeContext(Context context, REQUEST request) {
return context;
}

private Context addServletContextPath(Context context, REQUEST request) {
String contextPath = accessor.getRequestContextPath(request);
if (contextPath != null && !contextPath.isEmpty() && !contextPath.equals("/")) {
return context.with(ServletContextPath.CONTEXT_KEY, contextPath);
}
return context;
protected Context addServletContextPath(Context context, REQUEST request) {
return ServletContextPath.init(context, () -> accessor.getRequestContextPath(request));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do non-capturing lambda here similar to #4014?

}

public Context getServerContext(REQUEST request) {
Expand All @@ -88,27 +84,13 @@ public void recordException(Context context, Throwable throwable) {
AppServerBridge.recordException(context, throwable);
}

/**
* When server spans are managed by app server instrumentation we need to add context path of
* current request to context if it isn't already added. Servlet instrumentation adds it when it
* starts server span.
*/
public Context updateContext(Context context, REQUEST request) {
String contextPath = context.get(ServletContextPath.CONTEXT_KEY);
if (contextPath == null) {
context = addServletContextPath(context, request);
}

return context;
}

public Context updateContext(
Context context, REQUEST request, MappingResolver mappingResolver, boolean servlet) {
ServerSpanNaming.updateServerSpanName(
context,
servlet ? SERVLET : FILTER,
() -> spanNameProvider.getSpanNameOrNull(mappingResolver, request));
return updateContext(context, request);
return addServletContextPath(context, request);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ protected Context customizeContext(Context context, REQUEST request) {
return addServletContextPath(context, request);
}

private Context addServletContextPath(Context context, REQUEST request) {
String contextPath = accessor.getRequestContextPath(request);
if (contextPath != null && !contextPath.isEmpty() && !contextPath.equals("/")) {
return context.with(ServletContextPath.CONTEXT_KEY, contextPath);
}
return context;
protected Context addServletContextPath(Context context, REQUEST request) {
return ServletContextPath.init(context, () -> accessor.getRequestContextPath(request));
}

@Override
Expand Down Expand Up @@ -224,20 +220,6 @@ public String getSpanName(REQUEST request) {
return contextPath + servletPath;
}

/**
* When server spans are managed by app server instrumentation we need to add context path of
* current request to context if it isn't already added. Servlet instrumentation adds it when it
* starts server span.
*/
public Context updateContext(Context context, REQUEST request) {
String contextPath = context.get(ServletContextPath.CONTEXT_KEY);
if (contextPath == null) {
context = addServletContextPath(context, request);
}

return context;
}

public void onTimeout(Context context, long timeout) {
Span span = Span.fromContext(context);
span.setStatus(StatusCode.ERROR);
Expand Down