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

added span to DispatcherServlet.render #409

Merged
merged 10 commits into from Jan 8, 2019
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
@@ -0,0 +1,93 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 - 2019 Elastic and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package co.elastic.apm.agent.spring.webmvc;

import co.elastic.apm.agent.bci.ElasticApmInstrumentation;
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.impl.transaction.Span;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Nullable;

import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import java.util.Arrays;
import java.util.Collection;

public class DispatcherServletRenderInstrumentation extends ElasticApmInstrumentation {

private static final String SPAN_TYPE_DISPATCHER_SERVLET_RENDER = "template.dispatcher-servlet.render";
private static final String RENDER_METHOD = "Render";

@Advice.OnMethodEnter(suppress = Throwable.class)
private static void beforeExecute(@Advice.Argument(0) ModelAndView modelAndView,
This conversation was marked as resolved.
Show resolved Hide resolved
@Advice.Local("span") Span span) {
if (tracer == null || tracer.activeSpan() == null)
This conversation was marked as resolved.
Show resolved Hide resolved
return;
final AbstractSpan<?> parent = tracer.activeSpan();
span = parent
.createSpan()
.withType(SPAN_TYPE_DISPATCHER_SERVLET_RENDER)
.appendToName(RENDER_METHOD).appendToName(" ").appendToName(modelAndView.getViewName())
This conversation was marked as resolved.
Show resolved Hide resolved
.activate();
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
private static void afterExecute(@Advice.Local("span") @Nullable Span span,
@Advice.Thrown @Nullable Throwable t) {
if (span != null) {
span.captureException(t)
.deactivate()
.end();
}
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
return nameStartsWith("org.springframework")
.and(not(isInterface()))
.and(declaresMethod(getMethodMatcher()))
.and(hasSuperType(named("org.springframework.web.servlet.DispatcherServlet")));
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return named("render")
.and(takesArgument(0, named("org.springframework.web.servlet.ModelAndView")))
.and(takesArgument(1, named("javax.servlet.http.HttpServletRequest")))
.and(takesArgument(2, named("javax.servlet.http.HttpServletResponse")));
}

@Override
public Collection<String> getInstrumentationGroupNames() {
return Arrays.asList("dispatcher-servlet", "render");
}
}

Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
co.elastic.apm.agent.spring.webmvc.SpringTransactionNameInstrumentation
co.elastic.apm.agent.spring.webmvc.DispatcherServletRenderInstrumentation
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 - 2019 Elastic and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package co.elastic.apm.agent.spring.webmvc;

import co.elastic.apm.agent.MockReporter;
import co.elastic.apm.agent.bci.ElasticApmAgent;
import co.elastic.apm.agent.configuration.SpyConfiguration;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.ElasticApmTracerBuilder;
import co.elastic.apm.agent.servlet.ServletInstrumentation;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

public class DispatcherServletRenderInstrumentationTest {

private static MockReporter reporter;
private static ElasticApmTracer tracer;
private MockMvc mockMvc;

@BeforeClass
@BeforeAll
public static void setUpAll() {
reporter = new MockReporter();
tracer = new ElasticApmTracerBuilder()
.configurationRegistry(SpyConfiguration.createSpyConfig())
.reporter(reporter)
.build();
ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install(),
Arrays.asList(new ServletInstrumentation(), new DispatcherServletRenderInstrumentation()));
}

@AfterClass
@AfterAll
public static void afterAll() {
ElasticApmAgent.reset();
}

@Before
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders
.standaloneSetup(new MessageController())
.setViewResolvers(jspViewResolver())
.build();
}

private ViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/static/");
bean.setSuffix(".jsp");
return bean;
}

@Controller
public static class MessageController {
@GetMapping("/test")
public String test() {
return "message-view";
}
}

@Test
public void testCallOfDispatcherServletRender() throws Exception {
this.mockMvc.perform(get("/test"));
assertEquals(1, reporter.getTransactions().size());
assertEquals(2, reporter.getSpans().size());
This conversation was marked as resolved.
Show resolved Hide resolved
assertEquals("Render message-view", reporter.getSpans().get(1).getName().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello From Message-view.jsp!</h2>
</body>
</html>
4 changes: 2 additions & 2 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ you should add an additional entry to this list (make sure to also include the d
==== `disable_instrumentations`

A list of instrumentations which should be disabled.
Valid options are `annotations`, `apache-httpclient`, `elasticsearch-restclient`, `http-client`, `incubating`, `jax-rs`, `jax-rs-annotations`, `jdbc`, `opentracing`, `public-api`, `servlet-api`, `servlet-api-async`, `spring-mvc`, `spring-resttemplate`.
Valid options are `annotations`, `apache-httpclient`, `dispatcher-servlet`, `elasticsearch-restclient`, `http-client`, `incubating`, `jax-rs`, `jax-rs-annotations`, `jdbc`, `opentracing`, `public-api`, `render`, `servlet-api`, `servlet-api-async`, `spring-mvc`, `spring-resttemplate`.
If you want to try out incubating features,
set the value to an empty string.

Expand Down Expand Up @@ -979,7 +979,7 @@ The default unit for this option is `ms`
# sanitize_field_names=password,passwd,pwd,secret,*key,*token*,*session*,*credit*,*card*,authorization,set-cookie

# A list of instrumentations which should be disabled.
# Valid options are `annotations`, `apache-httpclient`, `elasticsearch-restclient`, `http-client`, `incubating`, `jax-rs`, `jax-rs-annotations`, `jdbc`, `opentracing`, `public-api`, `servlet-api`, `servlet-api-async`, `spring-mvc`, `spring-resttemplate`.
# Valid options are `annotations`, `apache-httpclient`, `dispatcher-servlet`, `elasticsearch-restclient`, `http-client`, `incubating`, `jax-rs`, `jax-rs-annotations`, `jdbc`, `opentracing`, `public-api`, `render`, `servlet-api`, `servlet-api-async`, `spring-mvc`, `spring-resttemplate`.
# If you want to try out incubating features,
# set the value to an empty string.
#
Expand Down
Empty file added out.txt
Empty file.