Skip to content

Commit

Permalink
[#2524] add dependency graph
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Feb 24, 2017
1 parent a9fed9d commit f01a711
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 13 deletions.
6 changes: 6 additions & 0 deletions profiler/pom.xml
Expand Up @@ -64,6 +64,12 @@
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-grapher</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>


<dependency>
Expand Down
Expand Up @@ -17,45 +17,46 @@
package com.navercorp.pinpoint.profiler.context;

import com.google.inject.Injector;
import com.google.inject.Key;
import com.navercorp.pinpoint.bootstrap.AgentOption;
import com.navercorp.pinpoint.bootstrap.DefaultAgentOption;
import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService;
import com.navercorp.pinpoint.profiler.AgentInfoSender;
import com.navercorp.pinpoint.profiler.context.module.SpanDataSender;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.sender.DataSender;
import com.navercorp.pinpoint.profiler.util.TestInterceptorRegistryBinder;
import org.junit.Assert;
import org.junit.Test;

import java.net.URL;

import static org.junit.Assert.*;

/**
* @author Woonduk Kang(emeroad)
*/
public class DefaultApplicationContextTest {
@Test
public void test() {
DefaultApplicationContext applicationContext = newApplicationContext();
try {
Injector injector = applicationContext.getInjector();
AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class);
AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class);
Assert.assertSame(instance1, instance2);
} finally {
applicationContext.close();
}

}

private DefaultApplicationContext newApplicationContext() {
ProfilerConfig profilerConfig = new DefaultProfilerConfig();
InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder();
AgentOption agentOption = new DefaultAgentOption(new DummyInstrumentation(),
"mockAgent", "mockApplicationName", profilerConfig, new URL[0],
null, new DefaultServiceTypeRegistryService(), new DefaultAnnotationKeyRegistryService());

DefaultApplicationContext applicationContext = new DefaultApplicationContext(agentOption, binder);

Injector injector = applicationContext.getInjector();
AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class);
AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class);
Assert.assertSame(instance1, instance2);


return new DefaultApplicationContext(agentOption, binder);
}

}
@@ -0,0 +1,101 @@
/*
* 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.profiler.context.graph;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.grapher.graphviz.GraphvizGrapher;
import com.google.inject.grapher.graphviz.GraphvizModule;
import com.navercorp.pinpoint.bootstrap.AgentOption;
import com.navercorp.pinpoint.bootstrap.DefaultAgentOption;
import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.service.DefaultAnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService;
import com.navercorp.pinpoint.profiler.context.DefaultApplicationContext;
import com.navercorp.pinpoint.profiler.context.DummyInstrumentation;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.util.TestInterceptorRegistryBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.security.CodeSource;

/**
* @author Woonduk Kang(emeroad)
*/
public class DependencyGraph {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

public static void main(String[] args) throws IOException {
DependencyGraph graph = new DependencyGraph();
graph.dumpDependencyGraph();
}

public void dumpDependencyGraph() throws IOException {

DefaultApplicationContext applicationContext = newApplicationContext();
try {

Injector injector = applicationContext.getInjector();

String path = currentWorkingDir();
String fileName = path + "../DependencyGraph.dot";
logger.debug("filename:{}", fileName);

Grapher grapher = new Grapher();
grapher.graph(fileName, injector);

} finally {
applicationContext.close();
}
}

private DefaultApplicationContext newApplicationContext() {
ProfilerConfig profilerConfig = new DefaultProfilerConfig();
InterceptorRegistryBinder binder = new TestInterceptorRegistryBinder();
AgentOption agentOption = new DefaultAgentOption(new DummyInstrumentation(),
"mockAgent", "mockApplicationName", profilerConfig, new URL[0],
null, new DefaultServiceTypeRegistryService(), new DefaultAnnotationKeyRegistryService());

return new DefaultApplicationContext(agentOption, binder);
}

private String currentWorkingDir() {
CodeSource codeSource = this.getClass().getProtectionDomain().getCodeSource();
URL location = codeSource.getLocation();
String dir = location.getPath();
return dir;
}

public class Grapher {
public void graph(String filename, Injector demoInjector) throws IOException {
PrintWriter out = new PrintWriter(new File(filename), "UTF-8");

Injector injector = Guice.createInjector(new GraphvizModule());
GraphvizGrapher grapher = injector.getInstance(GraphvizGrapher.class);
grapher.setOut(out);
grapher.setRankdir("TB");
grapher.graph(demoInjector);
}
}
}
@@ -0,0 +1,42 @@
/*
* 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.profiler.context.graph;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.grapher.graphviz.GraphvizGrapher;
import com.google.inject.grapher.graphviz.GraphvizModule;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
* copy https://github.com/google/guice/wiki/Grapher
*/
public class Grapher {

public void graph(String filename, Injector demoInjector) throws IOException {
PrintWriter out = new PrintWriter(new File(filename), "UTF-8");

Injector injector = Guice.createInjector(new GraphvizModule());
GraphvizGrapher grapher = injector.getInstance(GraphvizGrapher.class);
grapher.setOut(out);
grapher.setRankdir("TB");
grapher.graph(demoInjector);
}
}

0 comments on commit f01a711

Please sign in to comment.