提交 fa99652d 编写于 作者: A ascrutae

[Agent] Support hystrix plugin

上级 239579fd
......@@ -32,7 +32,7 @@ public class ServerTypeDefine {
private ServerType[] serverTypes;
private ServerTypeDefine() {
this.serverTypes = new ServerType[29];
this.serverTypes = new ServerType[30];
this.serverTypeNames = new String[11];
addServerType(new ServerType(ComponentsDefine.TOMCAT.getId(), Const.NONE, Const.EMPTY_STRING));
addServerType(new ServerType(ComponentsDefine.HTTPCLIENT.getId(), Const.NONE, Const.EMPTY_STRING));
......@@ -62,6 +62,7 @@ public class ServerTypeDefine {
addServerType(new ServerType(ComponentsDefine.HTTP_ASYNC_CLIENT.getId(), Const.NONE, Const.EMPTY_STRING));
addServerType(new ServerType(ComponentsDefine.KAFKA.getId(), 10, ComponentsDefine.KAFKA.getName()));
addServerType(new ServerType(ComponentsDefine.SERVICECOMB.getId(), Const.NONE, ComponentsDefine.SERVICECOMB.getName()));
addServerType(new ServerType(ComponentsDefine.HYSTRIX.getId(), Const.NONE, ComponentsDefine.HYSTRIX.getName()));
}
public static ServerTypeDefine getInstance() {
......
......@@ -82,6 +82,8 @@ public class ComponentsDefine {
public static final OfficialComponent SERVICECOMB = new OfficialComponent(28, "ServiceComb");
public static final OfficialComponent HYSTRIX = new OfficialComponent(29, "Hystrix");
private static ComponentsDefine INSTANCE = new ComponentsDefine();
private String[] components;
......@@ -91,7 +93,7 @@ public class ComponentsDefine {
}
public ComponentsDefine() {
components = new String[29];
components = new String[30];
addComponent(TOMCAT);
addComponent(HTTPCLIENT);
addComponent(DUBBO);
......@@ -120,6 +122,7 @@ public class ComponentsDefine {
addComponent(HTTP_ASYNC_CLIENT);
addComponent(KAFKA);
addComponent(SERVICECOMB);
addComponent(HYSTRIX);
}
private void addComponent(OfficialComponent component) {
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apm-sdk-plugin</artifactId>
<groupId>org.apache.skywalking</groupId>
<version>5.0.0-alpha</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apm-hystrix-1.x-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.4.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hystrix.v1;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
public class EnhanceRequireObjectCache {
private final String operationNamePrefix;
private ContextSnapshot contextSnapshot;
public EnhanceRequireObjectCache(String prefix) {
operationNamePrefix = prefix;
}
public String getOperationNamePrefix() {
return operationNamePrefix;
}
public ContextSnapshot getContextSnapshot() {
return contextSnapshot;
}
public void setContextSnapshot(ContextSnapshot contextSnapshot) {
this.contextSnapshot = contextSnapshot;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hystrix.v1;
import com.netflix.hystrix.HystrixCollapser;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixObservableCollapser;
import com.netflix.hystrix.HystrixObservableCommand;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
public class HystrixCommandConstructorInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
String identifyKey = "";
if (HystrixCommand.class.isAssignableFrom(objInst.getClass())) {
HystrixCommand hystrixCommand = (HystrixCommand)objInst;
identifyKey = hystrixCommand.getCommandKey().name();
}
if (HystrixCollapser.class.isAssignableFrom(objInst.getClass())) {
HystrixCollapser hystrixCommand = (HystrixCollapser)objInst;
identifyKey = hystrixCommand.getCollapserKey().name();
}
if (HystrixObservableCollapser.class.isAssignableFrom(objInst.getClass())) {
HystrixObservableCollapser hystrixCommand = (HystrixObservableCollapser)objInst;
identifyKey = hystrixCommand.getCollapserKey().name();
}
if (HystrixObservableCommand.class.isAssignableFrom(objInst.getClass())) {
HystrixObservableCommand hystrixCommand = (HystrixObservableCommand)objInst;
identifyKey = hystrixCommand.getCommandKey().name();
}
objInst.setSkyWalkingDynamicField(new EnhanceRequireObjectCache("Hystrix/" + identifyKey));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hystrix.v1;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
public class HystrixPluginsInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
return new SWExecutionHookWrapper((HystrixCommandExecutionHook)ret);
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hystrix.v1;
import com.netflix.hystrix.HystrixInvokable;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
public class SWExecutionHookWrapper extends HystrixCommandExecutionHook {
private final HystrixCommandExecutionHook actual;
public SWExecutionHookWrapper(HystrixCommandExecutionHook actual) {
this.actual = actual;
}
@Override
public <T> void onStart(HystrixInvokable<T> commandInstance) {
EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance;
EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField();
enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture());
actual.onStart(commandInstance);
}
/**
* execution method
*/
@Override public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) {
// create a local span, and continued, The `execution method` running in other thread if the
// hystrix strategy is `THREAD`.
EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance;
EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField();
ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot();
AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Execution");
activeSpan.setComponent(ComponentsDefine.HYSTRIX);
ContextManager.continued(snapshot);
actual.onExecutionStart(commandInstance);
// Because of `fall back` method running in other thread. so we need capture concurrent span for tracing.
enhanceRequireObjectCache.setContextSnapshot(ContextManager.capture());
}
@Override public <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) {
ContextManager.activeSpan().errorOccurred().log(e);
ContextManager.stopSpan();
return actual.onExecutionError(commandInstance, e);
}
@Override public <T> void onExecutionSuccess(HystrixInvokable<T> commandInstance) {
ContextManager.stopSpan();
actual.onExecutionSuccess(commandInstance);
}
/**
* Fallback
*/
@Override public <T> void onFallbackStart(HystrixInvokable<T> commandInstance) {
EnhancedInstance enhancedInstance = (EnhancedInstance)commandInstance;
EnhanceRequireObjectCache enhanceRequireObjectCache = (EnhanceRequireObjectCache)enhancedInstance.getSkyWalkingDynamicField();
ContextSnapshot snapshot = enhanceRequireObjectCache.getContextSnapshot();
AbstractSpan activeSpan = ContextManager.createLocalSpan(enhanceRequireObjectCache.getOperationNamePrefix() + "/Fallback");
activeSpan.setComponent(ComponentsDefine.HYSTRIX);
ContextManager.continued(snapshot);
actual.onFallbackStart(commandInstance);
}
@Override public <T> Exception onFallbackError(HystrixInvokable<T> commandInstance, Exception e) {
ContextManager.activeSpan().errorOccurred().log(e);
ContextManager.stopSpan();
return actual.onFallbackError(commandInstance, e);
}
@Override public <T> void onFallbackSuccess(HystrixInvokable<T> commandInstance) {
ContextManager.stopSpan();
actual.onFallbackSuccess(commandInstance);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hystrix.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch;
public class HystrixCommandInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.hystrix.v1.HystrixCommandConstructorInterceptor";
public static final String ENHANCE_CLASS = "com.netflix.hystrix.HystrixCommand";
@Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getConstructorMatcher() {
return any();
}
@Override public String getConstructorInterceptor() {
return INTERCEPT_CLASS;
}
}
};
}
@Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[0];
}
@Override protected ClassMatch enhanceClass() {
return byHierarchyMatch(new String[] {ENHANCE_CLASS});
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hystrix.v1.define;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
public class HystrixPluginsInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.hystrix.v1.HystrixPluginsInterceptor";
public static final String ENHANCE_METHOD = "getCommandExecutionHook";
public static final String ENHANCE_CLASS = "com.netflix.hystrix.strategy.HystrixPlugins";
@Override
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD);
}
@Override public String getMethodsInterceptor() {
return INTERCEPT_CLASS;
}
@Override public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return byName(ENHANCE_CLASS);
}
}
hystrix-1.x=org.apache.skywalking.apm.plugin.hystrix.v1.define.HystrixCommandInstrumentation
hystrix-1.x=org.apache.skywalking.apm.plugin.hystrix.v1.define.HystrixPluginsInstrumentation
......@@ -55,6 +55,7 @@
<module>httpasyncclient-4.x-plugin</module>
<module>kafka-v1-plugin</module>
<module>servicecomb-plugin</module>
<module>hystrix-1.x-plugin</module>
</modules>
<packaging>pom</packaging>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册