Skip to content

Commit

Permalink
implemented application type detector - pinpoint-apm#195
Browse files Browse the repository at this point in the history
  • Loading branch information
Xylus committed Mar 9, 2015
1 parent baa2528 commit 05d5423
Show file tree
Hide file tree
Showing 28 changed files with 1,646 additions and 304 deletions.
6 changes: 5 additions & 1 deletion agent/src/main/resources/pinpoint.config
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ profiler.tcpdatasender.command.accept.enable=true
#profiler.applicationservertype=TOMCAT
#profiler.applicationservertype=BLOC

###########################################################
# application type detect order #
###########################################################
profiler.type.detect.order=

###########################################################
# user defined classes #
Expand Down Expand Up @@ -216,4 +220,4 @@ profiler.log4j.logging.transactioninfo=false
###########################################################
# logback
###########################################################
profiler.logback.logging.transactioninfo=false
profiler.logback.logging.transactioninfo=false
6 changes: 5 additions & 1 deletion agent/src/test/resources/pinpoint-spring-bean-test.config
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ profiler.tcpdatasender.command.accept.enable=true
#profiler.applicationservertype=TOMCAT
#profiler.applicationservertype=BLOC

###########################################################
# application type detect order #
###########################################################
profiler.type.detect.order=

###########################################################
# user defined classes #
Expand Down Expand Up @@ -194,4 +198,4 @@ profiler.log4j.logging.transactioninfo=false
###########################################################
# logback
###########################################################
profiler.logback.logging.transactioninfo=false
profiler.logback.logging.transactioninfo=false
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import com.navercorp.pinpoint.bootstrap.util.NumberUtils;
import com.navercorp.pinpoint.bootstrap.util.spring.PropertyPlaceholderHelper;
import com.navercorp.pinpoint.common.ServiceType;
import com.navercorp.pinpoint.common.util.PropertyUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -210,6 +212,7 @@ public static ProfilerConfig load(String pinpointConfigFileName) throws IOExcept
private long agentInfoSendRetryInterval = DEFAULT_AGENT_INFO_SEND_RETRY_INTERVAL;

private String applicationServerType;
private List<String> applicationTypeDetectOrder;
private boolean log4jLoggingTransactionInfo;
private boolean logbackLoggingTransactionInfo;

Expand Down Expand Up @@ -589,6 +592,10 @@ public boolean isRedisPipelineEnabled() {
public Filter<String> getProfilableClassFilter() {
return profilableClassFilter;
}

public List<String> getApplicationTypeDetectOrder() {
return applicationTypeDetectOrder;
}

public String getApplicationServerType() {
return applicationServerType;
Expand Down Expand Up @@ -770,6 +777,8 @@ void readPropertyValues() {
// service type
this.applicationServerType = readString("profiler.applicationservertype", null);

// application type detector order
this.applicationTypeDetectOrder = readTypeDetectOrder("profiler.type.detect.order");

// TODO have to remove
// profile package included in order to test "call stack view".
Expand Down Expand Up @@ -836,6 +845,15 @@ public long readLong(String propertyName, long defaultValue) {
return result;
}

public List<String> readTypeDetectOrder(String propertyName) {
String value = properties.getProperty(propertyName);
if (value == null) {
return Collections.emptyList();
}
String[] orders = value.trim().split(",");
return Arrays.asList(orders);
}

public boolean readBoolean(String propertyName, boolean defaultValue) {
String value = properties.getProperty(propertyName, Boolean.toString(defaultValue));
boolean result = Boolean.parseBoolean(value);
Expand All @@ -845,7 +863,6 @@ public boolean readBoolean(String propertyName, boolean defaultValue) {
return result;
}


@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ProfilerConfig{");
Expand Down Expand Up @@ -929,6 +946,7 @@ public String toString() {
sb.append(", profilableClassFilter=").append(profilableClassFilter);
sb.append(", DEFAULT_AGENT_INFO_SEND_RETRY_INTERVAL=").append(DEFAULT_AGENT_INFO_SEND_RETRY_INTERVAL);
sb.append(", agentInfoSendRetryInterval=").append(agentInfoSendRetryInterval);
sb.append(", applicationTypeDetectOrder='").append(applicationTypeDetectOrder).append('\'');
sb.append(", applicationServerType=").append(applicationServerType);
sb.append('}');
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
*/
package com.navercorp.pinpoint.bootstrap.plugin;

import com.navercorp.pinpoint.common.ServiceType;

/**
* @author Jongho Moon
* @author HyunGil Jeong
*
*/
public interface ServerTypeDetector {
public String getServerTypeName();
public ServiceType getServerType();
public boolean detect();
public boolean canOverride(String serverType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2015 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.resolver;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.navercorp.pinpoint.bootstrap.plugin.ServerTypeDetector;
import com.navercorp.pinpoint.common.ServiceType;

/**
* This class attempts to resolve the current application type through {@link ServerTypeDetector}.
* The application type is resolved by checking the conditions defined in each of the loaded detector's {@code detect} method.
* <p>
* If no match is found, the application type defaults to {@code ServiceType.STAND_ALONE}
* </li>
* </ul>
*
* @author HyunGil Jeong
*/
public class ServerTypeResolver {

private final Logger logger = Logger.getLogger(ServerTypeResolver.class.getName());

private final List<ServerTypeDetector> serverTypeDetectors;

private static final ServiceType DEFAULT_SERVER_TYPE = ServiceType.STAND_ALONE;

public ServerTypeResolver(List<ServerTypeDetector> serverTypeDetectors) {
if (serverTypeDetectors == null) {
throw new IllegalArgumentException("serverTypeDetectors should not be null");
}
this.serverTypeDetectors = serverTypeDetectors;
}

public ServiceType resolve() {
for (ServerTypeDetector currentDetector : this.serverTypeDetectors) {
logger.log(Level.INFO, "Attempting to resolve using " + currentDetector.getClass());
if (currentDetector.detect()) {
logger.log(Level.INFO, "Match found using " + currentDetector.getClass().getSimpleName());
return currentDetector.getServerType();
} else {
logger.log(Level.INFO, "No match found using " + currentDetector.getClass());
}
}
logger.log(Level.INFO, "Server type not resolved. Defaulting to " + DEFAULT_SERVER_TYPE.getName());
return DEFAULT_SERVER_TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2015 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.resolver.condition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author HyunGil Jeong
*/
public abstract class BaseCondition<T> implements Condition<T> {

private final ConditionChecker<Condition<T>> conditionChecker;

protected final List<T> requiredConditions;

@SuppressWarnings("unchecked")
protected BaseCondition(List<T> requiredConditions, ConditionChecker<? extends Condition<T>> conditionMatcher) {
this.requiredConditions = Collections.unmodifiableList(requiredConditions);
this.conditionChecker = (ConditionChecker<Condition<T>>)conditionMatcher;
}

@Override
public boolean matches() {
return this.conditionChecker.check(this);
}

public abstract static class ConditionBuilder<C extends Condition<T>, T> {

private final Set<T> requiredConditions = new HashSet<T>();

protected ConditionBuilder(T requiredCondition) {
if (requiredCondition == null) {
throw new IllegalArgumentException("requiredCondition may not be null");
}
this.or(requiredCondition);
}

public ConditionBuilder<C, T> or(T requiredCondition) {
if (requiredCondition != null) {
this.requiredConditions.add(requiredCondition);
}
return this;
}

public final C done() {
if (this.requiredConditions.isEmpty()) {
return build(Collections.<T>emptyList());
}
return build(new ArrayList<T>(this.requiredConditions));
}

protected abstract C build(List<T> requiredConditions);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015 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.resolver.condition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author HyunGil Jeong
*/
public abstract class BaseConditionSet<T> implements ConditionSet<T> {

private final ConditionChecker<ConditionSet<T>> conditionSetMatcher;

protected final List<Set<T>> requiredConditionSets;

@SuppressWarnings("unchecked")
protected BaseConditionSet(List<Set<T>> requiredConditionSets, ConditionChecker<? extends ConditionSet<T>> conditionSetMatcher) {
this.requiredConditionSets = Collections.unmodifiableList(requiredConditionSets);
this.conditionSetMatcher = (ConditionChecker<ConditionSet<T>>)conditionSetMatcher;
}

@Override
public boolean matches() {
return this.conditionSetMatcher.check(this);
}

public abstract static class ConditionSetBuilder<C extends ConditionSet<T>, T> {

private final Set<Set<T>> requiredConditionSets = new HashSet<Set<T>>();

protected ConditionSetBuilder(T ... requiredConditions) {
if (requiredConditions == null) {
throw new IllegalArgumentException("requiredConditions may not be null");
}
this.or(requiredConditions);
}

public ConditionSetBuilder<C, T> or(T ... requiredConditions) {
if (requiredConditions != null) {
Set<T> requiredConditionSet = new HashSet<T>(requiredConditions.length);
for (T requiredCondition : requiredConditions) {
if (requiredCondition != null) {
requiredConditionSet.add(requiredCondition);
}
}
if (!requiredConditionSet.isEmpty()) {
this.requiredConditionSets.add(requiredConditionSet);
}
}
return this;
}

public final C done() {
if (this.requiredConditionSets.isEmpty()) {
return build(Collections.<Set<T>>emptyList());
}
return build(new ArrayList<Set<T>>(this.requiredConditionSets));
}

protected abstract C build(List<Set<T>> requiredConditionSets);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2015 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.resolver.condition;

/**
* @author HyunGil Jeong
*/
public interface Condition<T> {

public boolean matches();

}
Loading

0 comments on commit 05d5423

Please # to comment.