static void method1_2() { Profiler.start("method1_2()"); Profiler.addIOCall("select * from user", 50000000); Profiler.addIOCall("select * from address", 50000000); method1_2_1(); final CallStackElement thisCallStackElement = Profiler.getMethodCallParent(); Profiler.stop(); thisCallStackElement.setExecutionTime(500000000); } static void method1_2_1() {
static CallStackElement method0() { final CallStackElement callStackElement = Profiler.activateProfiling("method0()"); try { method1(); return callStackElement; } finally { final CallStackElement thisCallStackElement = Profiler.getMethodCallParent(); Profiler.stop(); thisCallStackElement.setExecutionTime(1000000000); } }
@Test public void testNoProfilingIfNotActive() { assertFalse(Profiler.isProfilingActive()); Profiler.start("dummy"); assertNull(Profiler.getMethodCallParent()); Profiler.stop(); }
@Advice.OnMethodExit(inline = false, onThrowable = Throwable.class) public static void onAfterEvaluate() { final CallStackElement currentFreemarkerCall = Profiler.getMethodCallParent(); Profiler.stop(); removeCurrentNodeIfItHasNoChildren(currentFreemarkerCall); }
static void method1_2_1() { Profiler.start("method1_2_1()"); final CallStackElement thisCallStackElement = Profiler.getMethodCallParent(); Profiler.stop(); thisCallStackElement.setExecutionTime(250000000); } }
@Test public void testInnerPrivateMethod() { class Test { private void test() { } } Test test = new Test(); CallStackElement total = Profiler.activateProfiling("total"); test.test(); Profiler.stop(); Assert.assertFalse(total.toString(), total.getChildren().iterator().next().getSignature().contains("access$")); }
@Test public void testFreemarkerProfilingMethodCall() throws Exception { final CallStackElement callTree = Profiler.activateProfiling("testFreemarkerProfilingMethodCall"); final String renderedTemplate = processTemplate("test.ftl", "${templateModel.getFoo()}", new TemplateModel()); Profiler.stop(); Profiler.deactivateProfiling(); assertThat(renderedTemplate).isEqualTo("foo"); System.out.println(callTree); assertThat(callTree.getChildren()).hasSize(1); final CallStackElement freemarkerNode = callTree.getChildren().get(0); assertThat(freemarkerNode.getSignature()).isEqualTo("test.ftl:1#templateModel.getFoo()"); assertThat(freemarkerNode.getChildren()).hasSize(1); final CallStackElement templateModelNode = freemarkerNode.getChildren().get(0); assertThat(templateModelNode.getSignature()).isEqualTo("String org.stagemonitor.tracing.freemarker.FreemarkerProfilingTransformerTest$TemplateModel.getFoo()"); }
@Advice.OnMethodEnter(inline = false) public static void addIOCall(@Advice.Argument(1) ActionRequest actionRequestBuilder) { if (actionRequestBuilder instanceof SearchRequest) { Profiler.addIOCall(ElasticsearchSearchQueryTransformer.getSearchRequestAsString((SearchRequest) actionRequestBuilder), 0L); } }
@Test public void testProfilerActive() { assertFalse(Profiler.isProfilingActive()); Profiler.activateProfiling(""); assertTrue(Profiler.isProfilingActive()); Profiler.deactivateProfiling(); assertFalse(Profiler.isProfilingActive()); }
@Override public void onStart(SpanWrapper spanWrapper) { final SpanContextInformation contextInfo = SpanContextInformation.forSpan(spanWrapper); if (tracingPlugin.isSampled(spanWrapper) && contextInfo.getPreExecutionInterceptorContext() != null) { determineIfEnableProfiler(spanWrapper, contextInfo); if (!Profiler.isProfilingActive() && contextInfo.getPreExecutionInterceptorContext().isCollectCallTree()) { contextInfo.setCallTree(Profiler.activateProfiling("total")); } } }
@Override public void onFinish(SpanWrapper spanWrapper, String operationName, long durationNanos) { final SpanContextInformation contextInfo = SpanContextInformation.forSpan(spanWrapper); if (contextInfo.getCallTree() != null) { try { Profiler.stop(); if (tracingPlugin.isSampled(spanWrapper)) { determineIfExcludeCallTree(contextInfo); if (isAddCallTreeToSpan(contextInfo, operationName)) { addCallTreeToSpan(contextInfo, spanWrapper, operationName); } } } finally { Profiler.clearMethodCallParent(); } } }
@Advice.OnMethodExit(onThrowable = Throwable.class) public static void exit() { Profiler.stop(); }
@Advice.OnMethodEnter public static void enter(@ProfilerSignature String signature) { Profiler.start(signature); }
public static void addIOCall(String signature, long executionTimeNanos) { addCall(signature + ' ', executionTimeNanos); }
static void method1_1_1() { Profiler.start("method1_1_1()"); final CallStackElement thisCallStackElement = Profiler.getMethodCallParent(); Profiler.stop(); thisCallStackElement.setExecutionTime(200000000); }
@Test public void testDontProfileStagemonitorServlet() throws Exception { Filter filter = new HttpRequestMonitorFilter(); final CallStackElement total = Profiler.activateProfiling("total"); filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain()); Profiler.stop(); assertEquals(0, total.getChildren().size()); }
@Test public void testFreemarkerProfiling() throws Exception { final CallStackElement callTree = Profiler.activateProfiling("testFreemarkerProfiling"); final String renderedTemplate = processTemplate("test.ftl", "${templateModel.foo}", new TemplateModel()); Profiler.stop(); Profiler.deactivateProfiling(); assertThat(renderedTemplate).isEqualTo("foo"); System.out.println(callTree); assertThat(callTree.getChildren()).hasSize(1); final CallStackElement freemarkerNode = callTree.getChildren().get(0); assertThat(freemarkerNode.getSignature()).isEqualTo("test.ftl:1#templateModel.foo"); assertThat(freemarkerNode.getChildren()).hasSize(1); final CallStackElement templateModelNode = freemarkerNode.getChildren().get(0); assertThat(templateModelNode.getSignature()).isEqualTo("String org.stagemonitor.tracing.freemarker.FreemarkerProfilingTransformerTest$TemplateModel.getFoo()"); }
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { final Scope scope = new ExternalHttpRequest(tracingPlugin.getTracer(), request.getMethod().toString(), removeQuery(request.getURI()), request.getURI().getHost(), request.getURI().getPort()).createScope(); try { Profiler.start(request.getMethod().toString() + " " + request.getURI() + " "); tracingPlugin.getTracer().inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new SpringHttpRequestInjectAdapter(request)); return execution.execute(request, body); } finally { Profiler.stop(); scope.close(); } }
@Override public void onAfterAnyExecute(StatementInformation statementInformation, long timeElapsedNanos, SQLException e) { final Scope activeScope = tracingPlugin.getTracer().scopeManager().active(); if (activeScope != null) { final Span span = activeScope.span(); if (statementInformation.getConnectionInformation().getDataSource() instanceof DataSource && jdbcPlugin.isCollectSql()) { MetaData metaData = dataSourceUrlMap.get(statementInformation.getConnectionInformation().getDataSource()); Tags.PEER_SERVICE.set(span, metaData.serviceName); span.setTag("db.type", metaData.productName); span.setTag("db.user", metaData.userName); if (StringUtils.isNotEmpty(statementInformation.getSql())) { String sql = getSql(statementInformation.getSql(), statementInformation.getSqlWithValues()); Profiler.addIOCall(sql, timeElapsedNanos); span.setTag(AbstractExternalRequest.EXTERNAL_REQUEST_METHOD, getMethod(sql)); span.setTag(DB_STATEMENT, sql); } } tracingPlugin.getRequestMonitor().monitorStop(); } }