Skip to content

Commit

Permalink
sling: spotless fix-up
Browse files Browse the repository at this point in the history
  • Loading branch information
rombert committed Oct 27, 2023
1 parent 1b8642a commit ea4be26
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.sling;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.sling.SlingSingletons.REQUEST_ATTR_RESOLVED_SERVLET_NAME;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.servlet.Servlet;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.sling.api.SlingHttpServletRequest;

import javax.servlet.Servlet;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.sling.SlingSingletons.REQUEST_ATTR_RESOLVED_SERVLET_NAME;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

public class ServletResolverInstrumentation implements TypeInstrumentation {
public class ServletResolverInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
Expand All @@ -32,7 +36,7 @@ public void transform(TypeTransformer transformer) {
.and(named("resolveServlet"))
.and(takesArguments(1))
.and(takesArgument(0, named("org.apache.sling.api.SlingHttpServletRequest"))),
this.getClass().getName()+"$ResolveServletAdvice");
this.getClass().getName() + "$ResolveServletAdvice");
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.sling;

import com.google.auto.service.AutoService;
Expand All @@ -15,6 +20,7 @@ public SlingInstrumentationModule() {

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Arrays.asList(new ServletResolverInstrumentation(), new SlingSafeMethodsServletInstrumentation());
return Arrays.asList(
new ServletResolverInstrumentation(), new SlingSafeMethodsServletInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.sling;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
Expand All @@ -15,11 +20,11 @@
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import javax.servlet.ServletRequest;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.sling.api.SlingHttpServletRequest;
import javax.servlet.ServletRequest;

public class SlingSafeMethodsServletInstrumentation implements TypeInstrumentation {
@Override
Expand Down Expand Up @@ -52,7 +57,7 @@ public static void onEnter(
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

if ( !(request instanceof SlingHttpServletRequest) ) {
if (!(request instanceof SlingHttpServletRequest)) {
return;
}

Expand All @@ -66,18 +71,20 @@ public static void onEnter(

// written by ServletResolverInstrumentation
Object servletName = request.getAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME);
if ( !(servletName instanceof String) ) {
if (!(servletName instanceof String)) {
return;
}

// TODO - figure out why don't we have matches for all requests and find a better way to filter
// TODO - figure out why don't we have matches for all requests and find a better way to
// filter
context = helper().start(parentContext, slingRequest);
scope = context.makeCurrent();

// ensure that the top-level route is Sling-specific
HttpServerRoute.update(context, HttpServerRouteSource.CONTROLLER, (String) servletName);

// cleanup and ensure we don't have reuse the resolved Servlet name by accident for other requests
// cleanup and ensure we don't have reuse the resolved Servlet name by accident for other
// requests
request.removeAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
public final class SlingSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.sling-1.0";

static final String REQUEST_ATTR_RESOLVED_SERVLET_NAME = INSTRUMENTATION_NAME + ".resolvedServletName";
static final String REQUEST_ATTR_RESOLVED_SERVLET_NAME =
INSTRUMENTATION_NAME + ".resolvedServletName";

private static final SpanNameExtractor<SlingHttpServletRequest> SPAN_NAME_EXTRACTOR = s -> (String) s.getAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME);
private static final Instrumenter<SlingHttpServletRequest, Void>
INSTRUMENTER = Instrumenter.<SlingHttpServletRequest, Void> builder(GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, SPAN_NAME_EXTRACTOR)
private static final SpanNameExtractor<SlingHttpServletRequest> SPAN_NAME_EXTRACTOR =
s -> (String) s.getAttribute(REQUEST_ATTR_RESOLVED_SERVLET_NAME);
private static final Instrumenter<SlingHttpServletRequest, Void> INSTRUMENTER =
Instrumenter.<SlingHttpServletRequest, Void>builder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, SPAN_NAME_EXTRACTOR)
.buildInstrumenter();

public static Instrumenter<SlingHttpServletRequest, Void> helper() {
Expand Down

0 comments on commit ea4be26

Please sign in to comment.