skywalking 自定义插件

skywalking 自定义插件_第1张图片

环境

基于skywalking-java开发,就是skywalking的java agent,这次在基础上开发一个自定义的插件。

流程演示

  • 首先新建一个model

skywalking 自定义插件_第2张图片

  • 修改pom文件


	4.0.0
	
		apm-sdk-plugin
		org.apache.skywalking
		8.9.0-SNAPSHOT
	
	com.example.custom
	apm-custom-agent-plugin
	custom-agent-plugin
	custom-agent-plugin
	jar

	
		1.8
	
	

	

	
		
			
				maven-deploy-plugin
			
		
	


  • 创建一个plugin,可以让java agent启动的时候可以加载到,这个建议直接复制,应该这里代码要遵循Apace Software Foundation
/*
 * 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.custom;

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.DeclaredInstanceMethodsInterceptPoint;
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 org.apache.skywalking.apm.agent.core.plugin.match.PrefixMatch;

import static net.bytebuddy.matcher.ElementMatchers.isPublic;

/**
 * Intercept  class.
 */
public class KwCoreActivation extends ClassInstanceMethodsEnhancePluginDefine {

    public static final String KW_CORE_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.custom.KwCoreMethodInterceptor";

    @Override
    public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
        return new ConstructorInterceptPoint[0];
    }

    @Override
    public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {
                new DeclaredInstanceMethodsInterceptPoint() {
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return isPublic();
                    }

                    @Override
                    public String getMethodsInterceptor() {
                        return KW_CORE_METHOD_INTERCEPTOR;
                    }

                    @Override
                    public boolean isOverrideArgs() {
                        return false;
                    }
                }
        };
    }

    @Override
    protected ClassMatch enhanceClass() {
        return  PrefixMatch.nameStartsWith("com.example.demo");
    }
}

  • 接下来是创建拦截器,这里面其实用的就是bytebuddy字节码增强技术,后面会慢慢学习。
/*
 * 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.custom;

import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
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.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.util.MethodUtil;

import java.lang.reflect.Method;


/**
 * the abstract method interceptor
 */
public class KwCoreMethodInterceptor implements InstanceMethodsAroundInterceptor {

    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
            MethodInterceptResult result) throws Throwable {
        String operationName = "";
        operationName = MethodUtil.generateOperationName(method);
        AbstractSpan span = ContextManager.createLocalSpan(operationName);
        Tags.DB_TYPE.set(span, "public method");
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
            Object ret) throws Throwable {
        ContextManager.stopSpan();
        return ret;
    }

    @Override
    public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
            Class<?>[] argumentsTypes, Throwable t) {

    }
}

  • 还有最重要的一步,再resources目录下创建文件skywalking-plugin.def,一定要是这个名字,agent只会识别这一个名字。
# 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.

customer-1.0.x=org.apache.skywalking.apm.plugin.custom.KwCoreActivation

  • 最后需要打个包,直接在root工程目录打包即可,可以看到自己打包是否成功。

skywalking 自定义插件_第3张图片

skywalking 自定义插件_第4张图片

  • 启动demo监控工程,加上启动参数,如下所示:
-javaagent:/Users/qinfuxiang/IdeaProjects/skywalking-java/skywalking-agent/skywalking-agent.jar
-Dskywalking.agent.service_name=demo
-Dskywalking.collector.backend_service=127.0.0.1:11800
  • 访问 http://localhost:9999/test,可以看到进入断点,说明成功了。

skywalking 自定义插件_第5张图片

你可能感兴趣的:(中间件,java,maven,intellij-idea)