-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for Grails * exclude bad version from muzzle * Review fixes * review fixes * rebase * Trigger Build
- Loading branch information
Showing
17 changed files
with
579 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
instrumentation/grails-3.0/javaagent/grails-3.0-javaagent.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
apply from: "$rootDir/gradle/instrumentation.gradle" | ||
|
||
muzzle { | ||
pass { | ||
group = "org.grails" | ||
module = "grails-web-url-mappings" | ||
versions = "[3.0,)" | ||
skipVersions += ['3.1.15'] | ||
assertInverse = true | ||
} | ||
} | ||
|
||
repositories { | ||
maven { | ||
url "https://repo.grails.org/grails/core" | ||
mavenContent { | ||
releasesOnly() | ||
} | ||
} | ||
} | ||
|
||
// first version where our tests work | ||
def grailsVersion = '3.0.6' | ||
def springBootVersion = '1.2.5.RELEASE' | ||
|
||
dependencies { | ||
library ("org.grails:grails-plugin-url-mappings:$grailsVersion") | ||
|
||
testInstrumentation project(':instrumentation:servlet:servlet-3.0:javaagent') | ||
testInstrumentation project(':instrumentation:servlet:servlet-common:javaagent') | ||
testInstrumentation project(':instrumentation:tomcat-7.0:javaagent') | ||
testInstrumentation project(':instrumentation:spring:spring-webmvc-3.1:javaagent') | ||
|
||
testImplementation "org.springframework.boot:spring-boot-autoconfigure:$springBootVersion" | ||
testImplementation "org.springframework.boot:spring-boot-starter-tomcat:$springBootVersion" | ||
|
||
testImplementation(project(':testing-common')) { | ||
exclude group: 'org.eclipse.jetty', module: 'jetty-server' | ||
} | ||
|
||
latestDepTestLibrary ("org.grails:grails-plugin-url-mappings:4.0.+") | ||
latestDepTestLibrary "org.springframework.boot:spring-boot-autoconfigure:2.1.17.RELEASE" | ||
latestDepTestLibrary "org.springframework.boot:spring-boot-starter-tomcat:2.1.17.RELEASE" | ||
} |
72 changes: 72 additions & 0 deletions
72
...lemetry/javaagent/instrumentation/grails/DefaultGrailsControllerClassInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.grails; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.grails.GrailsTracer.tracer; | ||
import static java.util.Collections.singletonMap; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
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.tooling.TypeInstrumentation; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class DefaultGrailsControllerClassInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return named("org.grails.core.DefaultGrailsControllerClass"); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return singletonMap( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("invoke")) | ||
.and(takesArgument(0, named(Object.class.getName()))) | ||
.and(takesArgument(1, named(String.class.getName()))) | ||
.and(takesArguments(2)), | ||
DefaultGrailsControllerClassInstrumentation.class.getName() + "$ControllerAdvice"); | ||
} | ||
|
||
public static class ControllerAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void startSpan( | ||
@Advice.Argument(0) Object controller, | ||
@Advice.Argument(1) String action, | ||
@Advice.FieldValue("defaultActionName") String defaultActionName, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
|
||
context = tracer().startSpan(controller, action != null ? action : defaultActionName); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void stopSpan( | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
if (throwable == null) { | ||
tracer().end(context); | ||
} else { | ||
tracer().endExceptionally(context, throwable); | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...n/java/io/opentelemetry/javaagent/instrumentation/grails/GrailsInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.grails; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.tooling.InstrumentationModule; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class GrailsInstrumentationModule extends InstrumentationModule { | ||
public GrailsInstrumentationModule() { | ||
super("grails", "grails-3.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return asList( | ||
new DefaultGrailsControllerClassInstrumentation(), | ||
new UrlMappingsInfoHandlerAdapterInstrumentation()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...vaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grails/GrailsTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.grails; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath; | ||
import io.opentelemetry.instrumentation.api.tracer.BaseTracer; | ||
import org.grails.web.mapping.mvc.GrailsControllerUrlMappingInfo; | ||
|
||
public class GrailsTracer extends BaseTracer { | ||
|
||
private static final GrailsTracer TRACER = new GrailsTracer(); | ||
|
||
public static GrailsTracer tracer() { | ||
return TRACER; | ||
} | ||
|
||
public Context startSpan(Object controller, String action) { | ||
return startSpan(spanNameForClass(controller.getClass()) + "." + action); | ||
} | ||
|
||
public void nameServerSpan( | ||
Context context, Span serverSpan, GrailsControllerUrlMappingInfo info) { | ||
String action = | ||
info.getActionName() != null | ||
? info.getActionName() | ||
: info.getControllerClass().getDefaultAction(); | ||
serverSpan.updateName( | ||
ServletContextPath.prepend(context, "/" + info.getControllerName() + "/" + action)); | ||
} | ||
|
||
@Override | ||
protected String getInstrumentationName() { | ||
return "io.opentelemetry.javaagent.grails-3.0"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...emetry/javaagent/instrumentation/grails/UrlMappingsInfoHandlerAdapterInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.grails; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.grails.GrailsTracer.tracer; | ||
import static java.util.Collections.singletonMap; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.tracer.ServerSpan; | ||
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.grails.web.mapping.mvc.GrailsControllerUrlMappingInfo; | ||
|
||
public class UrlMappingsInfoHandlerAdapterInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return named("org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter"); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return singletonMap( | ||
isMethod() | ||
.and(isPublic()) | ||
.and(named("handle")) | ||
.and(takesArgument(2, named(Object.class.getName()))) | ||
.and(takesArguments(3)), | ||
UrlMappingsInfoHandlerAdapterInstrumentation.class.getName() + "$ServerSpanNameAdvice"); | ||
} | ||
|
||
public static class ServerSpanNameAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void nameSpan(@Advice.Argument(2) Object handler) { | ||
|
||
if (handler instanceof GrailsControllerUrlMappingInfo) { | ||
Context parentContext = Java8BytecodeBridge.currentContext(); | ||
Span serverSpan = ServerSpan.fromContextOrNull(parentContext); | ||
if (serverSpan != null) { | ||
tracer() | ||
.nameServerSpan(parentContext, serverSpan, (GrailsControllerUrlMappingInfo) handler); | ||
} | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
instrumentation/grails-3.0/javaagent/src/test/groovy/test/ErrorController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package test | ||
|
||
import grails.artefact.Controller | ||
import grails.web.Action | ||
|
||
class ErrorController implements Controller { | ||
|
||
@Action | ||
def index() { | ||
render "Error" | ||
} | ||
} |
Oops, something went wrong.