Skip to content

Commit

Permalink
[pinpoint-apm#1729] url stat
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo-jung committed Jan 3, 2018
1 parent 0a56d69 commit b1fd045
Show file tree
Hide file tree
Showing 20 changed files with 460 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2017 NAVER Corp.
*
* 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.
*/
package com.navercorp.pinpoint.bootstrap.plugin.uri;

/**
* @author minwoo.jung
*/
public interface UriStatMetricRegistry {
void incrementUriCount(String uri);
}
11 changes: 11 additions & 0 deletions plugins/spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@
<artifactId>spring-context</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,59 @@ public void setup(ProfilerPluginSetupContext context) {
if (config.hasTarget(SpringBeansTargetScope.POST_PROCESSOR)) {
addAbstractAutowireCapableBeanFactoryTransformer(context);
}

//TODO : (minwoo) set SpringBeansTargetScope??
// addMappingRegistoryTransformer(context);
// doDispatchTransformer(context);
addHandleMatchTransformer(context);
}

private void addHandleMatchTransformer(ProfilerPluginSetupContext context) {
transformTemplate.transform("org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);

final InstrumentMethod createBeanInstance = target.getDeclaredMethod("handleMatch", "org.springframework.web.servlet.mvc.method.RequestMappingInfo", "java.lang.String", "javax.servlet.http.HttpServletRequest");
// createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.RegisterInterceptor", va(urlStatMetricRegistry));
createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.HandleMatchInterceptor");

return target.toBytecode();
}
});
}

private void doDispatchTransformer(ProfilerPluginSetupContext context) {
transformTemplate.transform("org.springframework.web.servlet.DispatcherServlet", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
// UrlStatMetricRegistry urlStatMetricRegistry = new UrlStatMetricRegistry();
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);

final InstrumentMethod createBeanInstance = target.getDeclaredMethod("doDispatch", "javax.servlet.http.HttpServletRequest", "javax.servlet.http.HttpServletResponse");
// createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.RegisterInterceptor", va(urlStatMetricRegistry));
createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.DoDispatchInterceptor");

return target.toBytecode();
}
});
}

private void addMappingRegistoryTransformer(final ProfilerPluginSetupContext context) {

transformTemplate.transform("org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry", new TransformCallback() {
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
// UrlStatMetricRegistry urlStatMetricRegistry = new UrlStatMetricRegistry();
InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);

final InstrumentMethod createBeanInstance = target.getDeclaredMethod("register", "java.lang.Object", "java.lang.Object", "java.lang.reflect.Method");
// createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.RegisterInterceptor", va(urlStatMetricRegistry));
createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.RegisterInterceptor");

return target.toBytecode();
}
});
}

private void addAbstractAutowireCapableBeanFactoryTransformer(final ProfilerPluginSetupContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2017 NAVER Corp.
*
* 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.
*/
package com.navercorp.pinpoint.plugin.spring.beans.interceptor;

import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.plugin.uri.UriStatMetricRegistry;

import javax.servlet.http.HttpServletRequest;

/**
* @author minwoo.jung
*/
public class DoDispatchInterceptor implements AroundInterceptor {

private final UriStatMetricRegistry uriStatMetricRegistry;

public DoDispatchInterceptor(UriStatMetricRegistry uriStatMetricRegistry) {
this.uriStatMetricRegistry = uriStatMetricRegistry;
}

@Override
public void before(Object target, Object[] args) {
System.out.println("=start=");
for (int i = 0 ; i < args.length; i++) {
System.out.println(args[i]);
}
System.out.println("=end=");
if (args != null && args.length > 1) {
if (args[0] instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest)args[0];
// request.getAttribute()
}
}
}

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2017 NAVER Corp.
*
* 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.
*/
package com.navercorp.pinpoint.plugin.spring.beans.interceptor;

import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.plugin.uri.UriStatMetricRegistry;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;

/**
* @author minwoo.jung
*/
public class HandleMatchInterceptor implements AroundInterceptor {
private final UriStatMetricRegistry uriStatMetricRegistry;

public HandleMatchInterceptor(UriStatMetricRegistry uriStatMetricRegistry) {
this.uriStatMetricRegistry = uriStatMetricRegistry;
}

@Override
public void before(Object target, Object[] args) {
}

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
System.out.println("==================start==================");
for (int i = 0 ; i < args.length; i++) {
System.out.println("arg : " + args[i]);
}

if (args != null && args.length == 3) {
if (args[2] instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest)args[2];
Object uri = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

if (uri instanceof String) {
uriStatMetricRegistry.incrementUriCount((String) uri);
System.out.println("url : " + uri);
}
}
}

System.out.println("===================end===================");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2017 NAVER Corp.
*
* 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.
*/
package com.navercorp.pinpoint.plugin.spring.beans.interceptor;

import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

/**
* @author minwoo.jung
*/
public class RegisterInterceptor implements AroundInterceptor {

// private UriStatMetricRegistry urlStatMetricRegistry;

// public RegisterInterceptor(UrlStatMetricRegistry urlStatMetricRegistry) {
// public RegisterInterceptor() {
// this.urlStatMetricRegistry = new UriStatMetricRegistry();
// }

@Override
public void before(Object target, Object[] args) {
System.out.println("=============method call=================");
for (int i =0 ; i < 3 ; i++) {
System.out.println(args[i]);
}
if (args[1] instanceof RequestMappingInfo) {
// urlStatMetricRegistry.addRequestMappingInfo((RequestMappingInfo)args[1]);
}
System.out.println("========================================");
}

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcContext;
import com.navercorp.pinpoint.bootstrap.plugin.uri.UriStatMetricRegistry;
import com.navercorp.pinpoint.bootstrap.sampler.Sampler;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
Expand Down Expand Up @@ -119,6 +120,7 @@
import com.navercorp.pinpoint.profiler.context.provider.stat.response.ResponseTimeMetricProvider;
import com.navercorp.pinpoint.profiler.context.provider.stat.transaction.TransactionMetricCollectorProvider;
import com.navercorp.pinpoint.profiler.context.provider.stat.transaction.TransactionMetricProvider;
import com.navercorp.pinpoint.profiler.context.provider.stat.uri.UriStatMetricRegistryProvider;
import com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory;
import com.navercorp.pinpoint.profiler.context.recorder.RecorderFactory;
import com.navercorp.pinpoint.profiler.context.storage.StorageFactory;
Expand All @@ -129,10 +131,7 @@
import com.navercorp.pinpoint.profiler.metadata.DefaultStringMetaDataService;
import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService;
import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService;
import com.navercorp.pinpoint.profiler.monitor.AgentStatMonitor;
import com.navercorp.pinpoint.profiler.monitor.DeadlockMonitor;
import com.navercorp.pinpoint.profiler.monitor.DeadlockThreadRegistry;
import com.navercorp.pinpoint.profiler.monitor.DefaultAgentStatMonitor;
import com.navercorp.pinpoint.profiler.monitor.*;
import com.navercorp.pinpoint.profiler.monitor.collector.AgentStatCollector;
import com.navercorp.pinpoint.profiler.monitor.collector.AgentStatMetricCollector;
import com.navercorp.pinpoint.profiler.monitor.collector.activethread.ActiveTraceMetricCollector;
Expand Down Expand Up @@ -256,6 +255,7 @@ protected void configure() {
bind(DeadlockMonitor.class).toProvider(DeadlockMonitorProvider.class).in(Scopes.SINGLETON);
bind(AgentInfoSender.class).toProvider(AgentInfoSenderProvider.class).in(Scopes.SINGLETON);
bind(AgentStatMonitor.class).to(DefaultAgentStatMonitor.class).in(Scopes.SINGLETON);
bind(UriStatMonitor.class).to(DefaultUriStatMonitor.class).in(Scopes.SINGLETON);
}

private void bindTraceComponent() {
Expand Down Expand Up @@ -330,6 +330,8 @@ private void bindAgentStatComponent() {
bind(DeadlockMetric.class).toProvider(DeadlockMetricProvider.class).in(Scopes.SINGLETON);
bind(DeadlockMetricCollector.class).toProvider(DeadlockMetricCollectorProvider.class).in(Scopes.SINGLETON);

bind(UriStatMetricRegistry.class).toProvider(UriStatMetricRegistryProvider.class).in(Scopes.SINGLETON);

bind(new TypeLiteral<AgentStatMetricCollector<TAgentStat>>() {})
.annotatedWith(Names.named("AgentStatCollector"))
.to(AgentStatCollector.class).in(Scopes.SINGLETON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.monitor.AgentStatMonitor;
import com.navercorp.pinpoint.profiler.monitor.DeadlockMonitor;
import com.navercorp.pinpoint.profiler.monitor.UriStatMonitor;
import com.navercorp.pinpoint.profiler.sender.DataSender;
import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender;
import com.navercorp.pinpoint.rpc.client.PinpointClientFactory;
Expand Down Expand Up @@ -81,6 +82,7 @@ public class DefaultApplicationContext implements ApplicationContext {
private final DynamicTransformTrigger dynamicTransformTrigger;

private final Injector injector;
private final UriStatMonitor uriStatMonitor;

public DefaultApplicationContext(AgentOption agentOption, final InterceptorRegistryBinder interceptorRegistryBinder) {
this(agentOption, interceptorRegistryBinder, new ApplicationContextModuleFactory());
Expand Down Expand Up @@ -133,6 +135,7 @@ public DefaultApplicationContext(AgentOption agentOption, final InterceptorRegis
this.deadlockMonitor = injector.getInstance(DeadlockMonitor.class);
this.agentInfoSender = injector.getInstance(AgentInfoSender.class);
this.agentStatMonitor = injector.getInstance(AgentStatMonitor.class);
this.uriStatMonitor = injector.getInstance(UriStatMonitor.class);
}

public ClassFileTransformer wrap(ClassFileTransformerDispatcher classFileTransformerDispatcher) {
Expand Down Expand Up @@ -206,6 +209,8 @@ public void start() {
this.deadlockMonitor.start();
this.agentInfoSender.start();
this.agentStatMonitor.start();
// 일단 url 별로 수집은 되고 있고 collector로 보낼수 있도록 batch 형태로 job이 동작되도록 구현부터 해야함.
this.uriStatMonitor.start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.inject.Provider;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.plugin.uri.UriStatMetricRegistry;
import com.navercorp.pinpoint.profiler.context.monitor.DataSourceMonitorRegistryService;
import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService;
import com.navercorp.pinpoint.profiler.objectfactory.ObjectBinderFactory;
Expand All @@ -33,9 +34,10 @@ public class ObjectBinderFactoryProvider implements Provider<ObjectBinderFactory
private final Provider<TraceContext> traceContextProvider;
private final DataSourceMonitorRegistryService dataSourceMonitorRegistryService;
private final Provider<ApiMetaDataService> apiMetaDataServiceProvider;
private final UriStatMetricRegistry uriStatMetricRegistry;

@Inject
public ObjectBinderFactoryProvider(ProfilerConfig profilerConfig, Provider<TraceContext> traceContextProvider, DataSourceMonitorRegistryService dataSourceMonitorRegistryService, Provider<ApiMetaDataService> apiMetaDataServiceProvider) {
public ObjectBinderFactoryProvider(ProfilerConfig profilerConfig, Provider<TraceContext> traceContextProvider, DataSourceMonitorRegistryService dataSourceMonitorRegistryService, Provider<ApiMetaDataService> apiMetaDataServiceProvider, UriStatMetricRegistry uriStatMetricRegistry) {
if (profilerConfig == null) {
throw new NullPointerException("profilerConfig must not be null");
}
Expand All @@ -48,15 +50,19 @@ public ObjectBinderFactoryProvider(ProfilerConfig profilerConfig, Provider<Trace
if (apiMetaDataServiceProvider == null) {
throw new NullPointerException("apiMetaDataServiceProvider must not be null");
}
if (uriStatMetricRegistry == null) {
throw new NullPointerException("uriStatMetricRegistry must not be null");
}
this.profilerConfig = profilerConfig;
this.traceContextProvider = traceContextProvider;
this.dataSourceMonitorRegistryService = dataSourceMonitorRegistryService;
this.apiMetaDataServiceProvider = apiMetaDataServiceProvider;
this.uriStatMetricRegistry = uriStatMetricRegistry;
}

@Override
public ObjectBinderFactory get() {
return new ObjectBinderFactory(profilerConfig, traceContextProvider, dataSourceMonitorRegistryService, apiMetaDataServiceProvider);
return new ObjectBinderFactory(profilerConfig, traceContextProvider, dataSourceMonitorRegistryService, apiMetaDataServiceProvider, uriStatMetricRegistry);
}

}
Loading

0 comments on commit b1fd045

Please # to comment.