Expose complete Tracing APIs in the tracing toolkit (#442)

This commit is contained in:
叶梦飞 2023-01-24 08:01:18 +08:00 committed by GitHub
parent f366503d58
commit ac84d57023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
62 changed files with 3879 additions and 14 deletions

View File

@ -57,6 +57,7 @@ jobs:
case:
- activemq-scenario
- apm-toolkit-trace-scenario
- apm-toolkit-tracer-scenario
- armeria-0.96minus-scenario
- armeria-0.96plus-scenario
- avro-scenario

View File

@ -6,9 +6,10 @@ Release Notes.
------------------
* Enhance lettuce plugin to adopt uniform tags.
* Expose complete Tracing APIs in the tracing toolkit
#### Documentation
* Update docs of Tracing APIs, reorganize the API docs into six parts
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/168?closed=1)

View File

@ -0,0 +1,41 @@
/*
* 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.toolkit.trace;
public class CarrierItemRef {
public boolean hasNext() {
return false;
}
public CarrierItemRef next() {
return new CarrierItemRef();
}
public String getHeadKey() {
return "";
}
public String getHeadValue() {
return "";
}
public void setHeadValue(String headValue) {
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.toolkit.trace;
public class ContextCarrierRef {
public CarrierItemRef items() {
return new CarrierItemRef();
}
}

View File

@ -0,0 +1,22 @@
/*
* 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.toolkit.trace;
public class ContextSnapshotRef {
}

View File

@ -0,0 +1,41 @@
/*
* 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.toolkit.trace;
import java.util.Map;
public class SpanRef {
public SpanRef prepareForAsync() {
return new SpanRef();
}
public SpanRef asyncFinish() {
return new SpanRef();
}
public void log(Throwable t) {
}
public void log(Map<String, ?> event) {
}
public void tag(String key, String value) {
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.toolkit.trace;
public class Tracer {
public static SpanRef createEntrySpan(String operationName, ContextCarrierRef carrierRef) {
return new SpanRef();
}
public static SpanRef createLocalSpan(String operationName) {
return new SpanRef();
}
public static SpanRef createExitSpan(String operationName, String remotePeer) {
return new SpanRef();
}
public static SpanRef createExitSpan(String operationName, ContextCarrierRef carrierRef, String remotePeer) {
return new SpanRef();
}
public static void stopSpan() {
}
public static void inject(ContextCarrierRef carrierRef) {
}
public static void extract(ContextCarrierRef carrierRef) {
}
public static ContextSnapshotRef capture() {
return new ContextSnapshotRef();
}
public static void continued(ContextSnapshotRef snapshot) {
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
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 java.lang.reflect.Method;
public class CarrierItemGetHeadKeyInterceptor 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 {
CarrierItem carrierItem = (CarrierItem) objInst.getSkyWalkingDynamicField();
return carrierItem.getHeadKey();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
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 java.lang.reflect.Method;
public class CarrierItemGetHeadValueInterceptor 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 {
CarrierItem carrierItem = (CarrierItem) objInst.getSkyWalkingDynamicField();
return carrierItem.getHeadValue();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,150 @@
/*
* 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.toolkit.activation.trace;
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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
public class CarrierItemRefActivation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.trace.CarrierItemRef";
private static final String HAS_NEXT_METHOD_NAME = "hasNext";
private static final String HAS_NEXT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.CarrierItemRefHasNextInterceptor";
private static final String NEXT_METHOD_NAME = "next";
private static final String NEXT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.CarrierItemRefNextInterceptor";
private static final String GET_HEADKEY_METHOD_NAME = "getHeadKey";
private static final String GET_HEADKEY_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.CarrierItemGetHeadKeyInterceptor";
private static final String GET_HEADVALUE_METHOD_NAME = "getHeadValue";
private static final String GET_HEADVALUE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.CarrierItemGetHeadValueInterceptor";
private static final String SET_HEADVALUE_METHOD_NAME = "setHeadValue";
private static final String SET_HEADVALUE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.CarrierItemSetHeadValueInterceptor";
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[]{
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(HAS_NEXT_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return HAS_NEXT_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(NEXT_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return NEXT_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(GET_HEADKEY_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return GET_HEADKEY_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(GET_HEADVALUE_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return GET_HEADVALUE_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(SET_HEADVALUE_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return SET_HEADVALUE_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
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 java.lang.reflect.Method;
public class CarrierItemRefHasNextInterceptor 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 {
CarrierItem carrierItem = (CarrierItem) objInst.getSkyWalkingDynamicField();
return carrierItem.hasNext();
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
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 java.lang.reflect.Method;
public class CarrierItemRefNextInterceptor 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 {
CarrierItem carrierItem = (CarrierItem) objInst.getSkyWalkingDynamicField();
((EnhancedInstance) ret).setSkyWalkingDynamicField(carrierItem.next());
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
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 java.lang.reflect.Method;
public class CarrierItemSetHeadValueInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
CarrierItem carrierItem = (CarrierItem) objInst.getSkyWalkingDynamicField();
carrierItem.setHeadValue(String.valueOf(allArguments[0]));
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
return null;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,83 @@
/*
* 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.toolkit.activation.trace;
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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.named;
/* maybe we can reduce the ConstructInterceptor of ContextCarrierRef */
public class ContextCarrierRefActivation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.trace.ContextCarrierRef";
private static final String ITEMS_METHOD_NAME = "items";
private static final String ITEMS_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.ContextCarrierRefItemsInterceptor";
private static final String INIT_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.ContextCarrierRefConstructInterceptor";
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return any();
}
@Override
public String getConstructorInterceptor() {
return INIT_METHOD_INTERCEPTOR;
}
}
};
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ITEMS_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return ITEMS_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
}

View File

@ -0,0 +1,30 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
public class ContextCarrierRefConstructInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
objInst.setSkyWalkingDynamicField(new ContextCarrier());
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
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 java.lang.reflect.Method;
public class ContextCarrierRefItemsInterceptor 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 {
ContextCarrier contextCarrier = (ContextCarrier) objInst.getSkyWalkingDynamicField();
CarrierItem carrierItem = contextCarrier.items();
((EnhancedInstance) ret).setSkyWalkingDynamicField(carrierItem);
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,63 @@
/*
* 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.toolkit.activation.trace;
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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.any;
public class ContextSnapshotRefActivation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.trace.ContextSnapshotRef";
private static final String INIT_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.ContextSnapshotRefConstructInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[]{
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return any();
}
@Override
public String getConstructorInterceptor() {
return INIT_METHOD_INTERCEPTOR;
}
}
};
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[0];
}
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
}

View File

@ -0,0 +1,28 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
public class ContextSnapshotRefConstructInterceptor implements InstanceConstructorInterceptor {
@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable {
}
}

View File

@ -0,0 +1,149 @@
/*
* 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.toolkit.activation.trace;
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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;
public class SpanRefActivation extends ClassInstanceMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.trace.SpanRef";
private static final String PREPARE_FOR_ASYNC_METHOD_NAME = "prepareForAsync";
private static final String PREPARE_FOR_ASYNC_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.SpanRefPrepareForAsyncInterceptor";
private static final String ASYNC_FINISH_METHOD_NAME = "asyncFinish";
private static final String ASYNC_FINISH_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.SpanRefAsyncFinishInterceptor";
private static final String LOG_METHOD_NAME = "log";
private static final String LOG_THROWABLE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.SpanRefLogThrowableInterceptor";
private static final String LOG_MAP_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.SpanRefLogMapInterceptor";
private static final String TAG_METHOD_NAME = "tag";
private static final String TAG_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.SpanRefTagInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(PREPARE_FOR_ASYNC_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return PREPARE_FOR_ASYNC_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ASYNC_FINISH_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return ASYNC_FINISH_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(LOG_METHOD_NAME).and(takesArgumentWithType(0, "java.lang.Throwable"));
}
@Override
public String getMethodsInterceptor() {
return LOG_THROWABLE_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(LOG_METHOD_NAME).and(takesArgumentWithType(0, "java.util.Map"));
}
@Override
public String getMethodsInterceptor() {
return LOG_MAP_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new InstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(TAG_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return TAG_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.toolkit.activation.trace;
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 java.lang.reflect.Method;
public class SpanRefAsyncFinishInterceptor 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 {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.asyncFinish();
((EnhancedInstance) ret).setSkyWalkingDynamicField(span);
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.toolkit.activation.trace;
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 java.lang.reflect.Method;
import java.util.Map;
public class SpanRefLogMapInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.log(System.currentTimeMillis(), (Map<String, ?>) allArguments[0]);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.toolkit.activation.trace;
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 java.lang.reflect.Method;
public class SpanRefLogThrowableInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.log((Throwable) allArguments[0]);
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
return null;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.toolkit.activation.trace;
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 java.lang.reflect.Method;
public class SpanRefPrepareForAsyncInterceptor 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 {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.prepareForAsync();
((EnhancedInstance) ret).setSkyWalkingDynamicField(span);
return ret;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.toolkit.activation.trace;
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 java.lang.reflect.Method;
public class SpanRefTagInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
span.tag(Tags.ofKey(String.valueOf(allArguments[0])), String.valueOf(allArguments[1]));
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
return null;
}
@Override
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}

View File

@ -0,0 +1,229 @@
/*
* 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.toolkit.activation.trace;
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.StaticMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
public class TracerActivation extends ClassStaticMethodsEnhancePluginDefine {
private static final String ENHANCE_CLASS = "org.apache.skywalking.apm.toolkit.trace.Tracer";
private static final String CREATE_ENTRY_SPAN_METHOD_NAME = "createEntrySpan";
private static final String CREATE_ENTRY_SPAN_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerCreateEntrySpanInterceptor";
private static final String CREATE_LOCAL_SPAN_METHOD_NAME = "createLocalSpan";
private static final String CREATE_LOCAL_SPAN_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerCreateLocalSpanInterceptor";
private static final String CREATE_EXIT_SPAN_METHOD_NAME = "createExitSpan";
private static final String CREATE_EXIT_SPAN_WITH_CONTEXT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerCreateExitSpanWithContextInterceptor";
private static final String CREATE_EXIT_SPAN_NO_CONTEXT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerCreateExitSpanNoContextInterceptor";
private static final String STOP_SPAN_METHOD_NAME = "stopSpan";
private static final String STOP_SPAN_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerStopSpanInterceptor";
private static final String INJECT_METHOD_NAME = "inject";
private static final String INJECT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerInjectInterceptor";
private static final String EXTRACT_METHOD_NAME = "extract";
private static final String EXTRACT_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerExtractInterceptor";
private static final String CAPTURE_METHOD_NAME = "capture";
private static final String CAPTURE_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerCaptureInterceptor";
private static final String CONTINUED_METHOD_NAME = "continued";
private static final String CONTINUED_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.toolkit.activation.trace.TracerContinuedInterceptor";
@Override
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[0];
}
@Override
public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() {
return new StaticMethodsInterceptPoint[] {
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(CREATE_ENTRY_SPAN_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return CREATE_ENTRY_SPAN_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(CREATE_LOCAL_SPAN_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return CREATE_LOCAL_SPAN_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(CREATE_EXIT_SPAN_METHOD_NAME).and(takesArguments(3));
}
@Override
public String getMethodsInterceptor() {
return CREATE_EXIT_SPAN_WITH_CONTEXT_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(CREATE_EXIT_SPAN_METHOD_NAME).and(takesArguments(2));
}
@Override
public String getMethodsInterceptor() {
return CREATE_EXIT_SPAN_NO_CONTEXT_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(STOP_SPAN_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return STOP_SPAN_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(INJECT_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return INJECT_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(EXTRACT_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return EXTRACT_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(CAPTURE_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return CAPTURE_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new StaticMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(CONTINUED_METHOD_NAME);
}
@Override
public String getMethodsInterceptor() {
return CONTINUED_INTERCEPTOR_CLASS;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return NameMatch.byName(ENHANCE_CLASS);
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.toolkit.activation.trace;
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.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerCaptureInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
ContextSnapshot contextSnapshot = ContextManager.capture();
((EnhancedInstance) ret).setSkyWalkingDynamicField(contextSnapshot);
return ret;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.toolkit.activation.trace;
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.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerContinuedInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
EnhancedInstance contextSnapshotRef = (EnhancedInstance) allArguments[0];
ContextSnapshot contextSnapshot = (ContextSnapshot) contextSnapshotRef.getSkyWalkingDynamicField();
ContextManager.continued(contextSnapshot);
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
return null;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerCreateEntrySpanInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
ContextCarrier contextCarrier = null;
if (allArguments[1] != null) {
EnhancedInstance contextCarrierRef = (EnhancedInstance) allArguments[1];
if (contextCarrierRef.getSkyWalkingDynamicField() != null)
contextCarrier = (ContextCarrier) contextCarrierRef.getSkyWalkingDynamicField();
}
AbstractSpan entrySpan = ContextManager.createEntrySpan(String.valueOf(allArguments[0]), contextCarrier);
((EnhancedInstance) ret).setSkyWalkingDynamicField(entrySpan);
return ret;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerCreateExitSpanNoContextInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
AbstractSpan exitSpan = ContextManager.createExitSpan(String.valueOf(allArguments[0]), String.valueOf(allArguments[1]));
((EnhancedInstance) ret).setSkyWalkingDynamicField(exitSpan);
return ret;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerCreateExitSpanWithContextInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
ContextCarrier contextCarrier = new ContextCarrier();
AbstractSpan exitSpan = ContextManager.createExitSpan(String.valueOf(allArguments[0]), contextCarrier, String.valueOf(allArguments[2]));
((EnhancedInstance) allArguments[1]).setSkyWalkingDynamicField(contextCarrier);
((EnhancedInstance) ret).setSkyWalkingDynamicField(exitSpan);
return ret;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
// import org.apache.skywalking.apm.toolkit.trace.SpanRef;
import java.lang.reflect.Method;
// Some problems about EnhancedInstance
public class TracerCreateLocalSpanInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
AbstractSpan localSpan = ContextManager.createLocalSpan(String.valueOf(allArguments[0]));
((EnhancedInstance) ret).setSkyWalkingDynamicField(localSpan);
return ret;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,47 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerExtractInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
// we can focus on these statements when debugging, such object changes will influence original object?
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
EnhancedInstance contextCarrierRef = (EnhancedInstance) allArguments[0];
ContextCarrier contextCarrier = (ContextCarrier) contextCarrierRef.getSkyWalkingDynamicField();
ContextManager.extract(contextCarrier);
return null;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerInjectInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
ContextCarrier contextCarrier = new ContextCarrier();
ContextManager.inject(contextCarrier);
((EnhancedInstance) allArguments[0]).setSkyWalkingDynamicField(contextCarrier);
return null;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -0,0 +1,41 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
import java.lang.reflect.Method;
public class TracerStopSpanInterceptor implements StaticMethodsAroundInterceptor {
@Override
public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, MethodInterceptResult result) {
ContextManager.stopSpan();
}
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
return null;
}
@Override
public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
}
}

View File

@ -19,4 +19,10 @@ toolkit-trace=org.apache.skywalking.apm.toolkit.activation.trace.TraceAnnotation
toolkit-trace=org.apache.skywalking.apm.toolkit.activation.trace.TraceContextActivation
toolkit-trace=org.apache.skywalking.apm.toolkit.activation.trace.CallableOrRunnableActivation
toolkit-tag=org.apache.skywalking.apm.toolkit.activation.trace.TagAnnotationActivation
toolkit-exception=org.apache.skywalking.apm.toolkit.activation.trace.IgnoredExceptionAnnotationActivation
toolkit-exception=org.apache.skywalking.apm.toolkit.activation.trace.IgnoredExceptionAnnotationActivation
toolkit-tracer=org.apache.skywalking.apm.toolkit.activation.trace.CarrierItemRefActivation
toolkit-tracer=org.apache.skywalking.apm.toolkit.activation.trace.ContextCarrierRefActivation
toolkit-tracer=org.apache.skywalking.apm.toolkit.activation.trace.ContextSnapshotRefActivation
toolkit-tracer=org.apache.skywalking.apm.toolkit.activation.trace.SpanRefActivation
toolkit-tracer=org.apache.skywalking.apm.toolkit.activation.trace.TracerActivation

View File

@ -0,0 +1,191 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.toolkit.trace.CarrierItemRef;
import org.apache.skywalking.apm.toolkit.trace.ContextCarrierRef;
import org.apache.skywalking.apm.toolkit.trace.Tracer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import java.lang.reflect.Method;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class ContextCarrierRefTest {
@SegmentStoragePoint
private SegmentStorage storage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedContextCarrierRef;
@Mock
private EnhancedInstance enhancedCarrierItemRef;
@Mock
private EnhancedInstance enhancedSpanRef;
private ContextCarrierRefItemsInterceptor contextCarrierRefItemsInterceptor;
private CarrierItemRefNextInterceptor carrierItemRefNextInterceptor;
private CarrierItemRefHasNextInterceptor carrierItemRefHasNextInterceptor;
private CarrierItemGetHeadKeyInterceptor carrierItemGetHeadKeyInterceptor;
private CarrierItemGetHeadValueInterceptor carrierItemGetHeadValueInterceptor;
private CarrierItemSetHeadValueInterceptor carrierItemSetHeadValueInterceptor;
private TracerCreateExitSpanWithContextInterceptor tracerCreateExitSpanWithContextInterceptor;
private TracerStopSpanInterceptor tracerStopSpanInterceptor;
@Before
public void setup() throws Exception {
contextCarrierRefItemsInterceptor = new ContextCarrierRefItemsInterceptor();
carrierItemRefNextInterceptor = new CarrierItemRefNextInterceptor();
carrierItemRefHasNextInterceptor = new CarrierItemRefHasNextInterceptor();
carrierItemGetHeadKeyInterceptor = new CarrierItemGetHeadKeyInterceptor();
carrierItemGetHeadValueInterceptor = new CarrierItemGetHeadValueInterceptor();
carrierItemSetHeadValueInterceptor = new CarrierItemSetHeadValueInterceptor();
tracerCreateExitSpanWithContextInterceptor = new TracerCreateExitSpanWithContextInterceptor();
tracerStopSpanInterceptor = new TracerStopSpanInterceptor();
enhancedContextCarrierRef = new EnhancedInstance() {
ContextCarrier contextCarrier = new ContextCarrier();
@Override
public Object getSkyWalkingDynamicField() {
return this.contextCarrier;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.contextCarrier = (ContextCarrier) value;
}
};
enhancedCarrierItemRef = new EnhancedInstance() {
CarrierItem carrierItem;
@Override
public Object getSkyWalkingDynamicField() {
return this.carrierItem;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.carrierItem = (CarrierItem) value;
}
};
enhancedSpanRef = new EnhancedInstance() {
AbstractSpan span;
@Override
public Object getSkyWalkingDynamicField() {
return this.span;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.span = (AbstractSpan) value;
}
};
}
@Test
public void testCarrierMethods() throws Throwable {
Method items = ContextCarrierRef.class.getDeclaredMethod("items");
Method createExitSpan = Tracer.class.getDeclaredMethod("createExitSpan", String.class, ContextCarrierRef.class, String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
Method hasNext = CarrierItemRef.class.getDeclaredMethod("hasNext");
Method next = CarrierItemRef.class.getDeclaredMethod("next");
Method getHeadKey = CarrierItemRef.class.getDeclaredMethod("getHeadKey");
Method getHeadValue = CarrierItemRef.class.getDeclaredMethod("getHeadValue");
Method setHeadValue = CarrierItemRef.class.getDeclaredMethod("setHeadValue", String.class);
String headKey;
String headValue;
Boolean ifHasNext = true;
tracerCreateExitSpanWithContextInterceptor.beforeMethod(Tracer.class, createExitSpan, null, null, null);
tracerCreateExitSpanWithContextInterceptor.afterMethod(Tracer.class, createExitSpan, new Object[]{"testExitSpanWithContext", enhancedContextCarrierRef, "127.0.0.1:8888"}, null, enhancedSpanRef);
contextCarrierRefItemsInterceptor.beforeMethod(enhancedContextCarrierRef, items, null, null, null);
enhancedCarrierItemRef = (EnhancedInstance) contextCarrierRefItemsInterceptor.afterMethod(enhancedContextCarrierRef, items, null, null, enhancedCarrierItemRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
ContextCarrier contextCarrierFromRef = (ContextCarrier) enhancedContextCarrierRef.getSkyWalkingDynamicField();
CarrierItem carrierItem = contextCarrierFromRef.items();
carrierItemSetHeadValueInterceptor.beforeMethod(enhancedCarrierItemRef, setHeadValue, new Object[]{"hello"}, null, null);
carrierItemSetHeadValueInterceptor.afterMethod(enhancedCarrierItemRef, setHeadValue, null, null, null);
carrierItemGetHeadValueInterceptor.beforeMethod(enhancedCarrierItemRef, getHeadValue, null, null, null);
headValue = (String) carrierItemGetHeadValueInterceptor.afterMethod(enhancedCarrierItemRef, getHeadValue, null, null, null);
assertThat(headValue, is("hello"));
while (ifHasNext) {
carrierItemRefHasNextInterceptor.beforeMethod(enhancedCarrierItemRef, hasNext, null, null, null);
ifHasNext = (Boolean) carrierItemRefHasNextInterceptor.afterMethod(enhancedCarrierItemRef, hasNext, null, null, null);
assertThat(carrierItem.hasNext(), is(ifHasNext));
if (!ifHasNext) break;
carrierItemRefNextInterceptor.beforeMethod(enhancedCarrierItemRef, next, null, null, null);
enhancedCarrierItemRef = (EnhancedInstance) carrierItemRefNextInterceptor.afterMethod(enhancedCarrierItemRef, next, null, null, enhancedCarrierItemRef);
carrierItemGetHeadKeyInterceptor.beforeMethod(enhancedCarrierItemRef, getHeadKey, null, null, null);
headKey = (String) carrierItemGetHeadKeyInterceptor.afterMethod(enhancedCarrierItemRef, getHeadKey, null, null, null);
carrierItemGetHeadValueInterceptor.beforeMethod(enhancedCarrierItemRef, getHeadValue, null, null, null);
headValue = (String) carrierItemGetHeadValueInterceptor.afterMethod(enhancedCarrierItemRef, getHeadValue, null, null, null);
carrierItem = carrierItem.next();
assertThat(headKey, is(carrierItem.getHeadKey()));
assertThat(headValue, is(carrierItem.getHeadValue()));
}
}
}

View File

@ -0,0 +1,215 @@
/*
* 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.toolkit.activation.trace;
/*
* build an EnhancedInstance to store the created span
* and then use this span to do
* 1. prepareForAsync
* 2. asyncFinish
* 3. log(Throwable)
* 4. log(Map)
*/
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.LocalSpan;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.SpanAssert;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.toolkit.trace.SpanRef;
import org.apache.skywalking.apm.toolkit.trace.Tracer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class SpanRefTest {
@SegmentStoragePoint
private SegmentStorage storage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedSpanRef;
private SpanRefPrepareForAsyncInterceptor spanRefPrepareForAsyncInterceptor;
private SpanRefAsyncFinishInterceptor spanRefAsyncFinishInterceptor;
private SpanRefLogMapInterceptor spanRefLogMapInterceptor;
private SpanRefLogThrowableInterceptor spanRefLogThrowableInterceptor;
private SpanRefTagInterceptor spanRefTagInterceptor;
private TracerCreateLocalSpanInterceptor tracerCreateLocalSpanInterceptor;
private TracerStopSpanInterceptor tracerStopSpanInterceptor;
@Before
public void setUp() throws Exception {
spanRefPrepareForAsyncInterceptor = new SpanRefPrepareForAsyncInterceptor();
spanRefAsyncFinishInterceptor = new SpanRefAsyncFinishInterceptor();
spanRefLogMapInterceptor = new SpanRefLogMapInterceptor();
spanRefLogThrowableInterceptor = new SpanRefLogThrowableInterceptor();
spanRefTagInterceptor = new SpanRefTagInterceptor();
tracerCreateLocalSpanInterceptor = new TracerCreateLocalSpanInterceptor();
tracerStopSpanInterceptor = new TracerStopSpanInterceptor();
enhancedSpanRef = new EnhancedInstance() {
AbstractSpan span;
@Override
public Object getSkyWalkingDynamicField() {
return this.span;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.span = (AbstractSpan) value;
}
};
}
@Test
public void testSpanRefPrepareAndFinishForAsync() throws Throwable {
Method prepareForAsync = SpanRef.class.getDeclaredMethod("prepareForAsync");
Method asyncFinish = SpanRef.class.getDeclaredMethod("asyncFinish");
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
enhancedSpanRef = (EnhancedInstance) tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[]{"localSpanForAsync"}, null, enhancedSpanRef);
spanRefPrepareForAsyncInterceptor.beforeMethod(enhancedSpanRef, prepareForAsync, null, null, null);
enhancedSpanRef = (EnhancedInstance) spanRefPrepareForAsyncInterceptor.afterMethod(enhancedSpanRef, prepareForAsync, null, null, enhancedSpanRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(0));
spanRefAsyncFinishInterceptor.beforeMethod(enhancedSpanRef, asyncFinish, null, null, null);
enhancedSpanRef = (EnhancedInstance) spanRefAsyncFinishInterceptor.afterMethod(enhancedSpanRef, asyncFinish, null, null, enhancedSpanRef);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
AbstractTracingSpan tracingSpan = spans.get(0);
assertThat(tracingSpan.getOperationName(), is("localSpanForAsync"));
}
@Test
public void testSpanRefLogMap() throws Throwable {
Method logForMap = SpanRef.class.getDeclaredMethod("log", Map.class);
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
String eventInfo = "this log is for testing spanRef log function with Map parameter";
Map<String, String> testEvents = new HashMap<>();
testEvents.put("event", "info");
testEvents.put("message", eventInfo);
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
enhancedSpanRef = (EnhancedInstance) tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[]{"localSpanForLogMap"}, null, enhancedSpanRef);
spanRefLogMapInterceptor.beforeMethod(enhancedSpanRef, logForMap, new Object[]{testEvents}, null, null);
spanRefLogMapInterceptor.afterMethod(enhancedSpanRef, logForMap, null, null, null);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
LocalSpan localSpan = (LocalSpan) enhancedSpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(localSpan, 1);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
AbstractTracingSpan span = spans.get(0);
SpanAssert.assertLogSize(span, 1);
}
@Test
public void testSpanRefLogThrowable() throws Throwable {
Method logForThrowable = SpanRef.class.getDeclaredMethod("log", Throwable.class);
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
enhancedSpanRef = (EnhancedInstance) tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[]{"localSpanForLogThrowable"}, null, enhancedSpanRef);
spanRefLogThrowableInterceptor.beforeMethod(enhancedSpanRef, logForThrowable, new Object[]{new RuntimeException("test-Throwable")}, null, null);
spanRefLogThrowableInterceptor.afterMethod(enhancedSpanRef, logForThrowable, null, null, null);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
LocalSpan localSpan = (LocalSpan) enhancedSpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(localSpan, 1);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
AbstractTracingSpan span = spans.get(0);
SpanAssert.assertLogSize(span, 1);
}
@Test
public void testSpanRefTag() throws Throwable {
Method tag = SpanRef.class.getDeclaredMethod("tag", String.class, String.class);
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
enhancedSpanRef = (EnhancedInstance) tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[]{"localSpanForTag"}, null, enhancedSpanRef);
spanRefTagInterceptor.beforeMethod(enhancedSpanRef, tag, new Object[] {"skywalking_tag_key", "skywalking_tag_value"}, null, null);
spanRefTagInterceptor.afterMethod(enhancedSpanRef, tag, null, null, null);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
LocalSpan localSpan = (LocalSpan) enhancedSpanRef.getSkyWalkingDynamicField();
SpanAssert.assertTagSize(localSpan, 1);
SpanAssert.assertTag(localSpan, 0, "skywalking_tag_value");
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
AbstractTracingSpan span = spans.get(0);
SpanAssert.assertTagSize(span, 1);
SpanAssert.assertTag(span, 0, "skywalking_tag_value");
}
}

View File

@ -0,0 +1,377 @@
/*
* 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.toolkit.activation.trace;
import org.apache.skywalking.apm.agent.core.conf.Config;
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.apache.skywalking.apm.agent.core.context.trace.EntrySpan;
import org.apache.skywalking.apm.agent.core.context.trace.ExitSpan;
import org.apache.skywalking.apm.agent.core.context.trace.LocalSpan;
import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.test.helper.SegmentHelper;
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.apache.skywalking.apm.agent.test.tools.SpanAssert;
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.apache.skywalking.apm.toolkit.trace.ContextCarrierRef;
import org.apache.skywalking.apm.toolkit.trace.ContextSnapshotRef;
import org.apache.skywalking.apm.toolkit.trace.Tracer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import java.lang.reflect.Method;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class TracerTest {
@SegmentStoragePoint
private SegmentStorage storage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
@Mock
private EnhancedInstance enhancedSpanRef;
@Mock
private EnhancedInstance enhancedContextCarrierRef;
@Mock
private EnhancedInstance enhancedContextSnapshotRef;
private TracerCaptureInterceptor tracerCaptureInterceptor;
private TracerContinuedInterceptor tracerContinuedInterceptor;
private TracerCreateEntrySpanInterceptor tracerCreateEntrySpanInterceptor;
private TracerCreateLocalSpanInterceptor tracerCreateLocalSpanInterceptor;
private TracerCreateExitSpanNoContextInterceptor tracerCreateExitSpanNoContextInterceptor;
private TracerCreateExitSpanWithContextInterceptor tracerCreateExitSpanWithContextInterceptor;
private TracerExtractInterceptor tracerExtractInterceptor;
private TracerInjectInterceptor tracerInjectInterceptor;
private TracerStopSpanInterceptor tracerStopSpanInterceptor;
@Before
public void setUp() throws Exception {
tracerCaptureInterceptor = new TracerCaptureInterceptor();
tracerContinuedInterceptor = new TracerContinuedInterceptor();
tracerCreateEntrySpanInterceptor = new TracerCreateEntrySpanInterceptor();
tracerCreateLocalSpanInterceptor = new TracerCreateLocalSpanInterceptor();
tracerCreateExitSpanNoContextInterceptor = new TracerCreateExitSpanNoContextInterceptor();
tracerCreateExitSpanWithContextInterceptor = new TracerCreateExitSpanWithContextInterceptor();
tracerExtractInterceptor = new TracerExtractInterceptor();
tracerInjectInterceptor = new TracerInjectInterceptor();
tracerStopSpanInterceptor = new TracerStopSpanInterceptor();
enhancedSpanRef = new EnhancedInstance() {
AbstractTracingSpan abstractTracingSpan;
@Override
public Object getSkyWalkingDynamicField() {
return this.abstractTracingSpan;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.abstractTracingSpan = (AbstractTracingSpan) value;
}
};
enhancedContextCarrierRef = new EnhancedInstance() {
ContextCarrier contextCarrier = new ContextCarrier();
@Override
public Object getSkyWalkingDynamicField() {
return this.contextCarrier;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.contextCarrier = (ContextCarrier) value;
}
};
enhancedContextSnapshotRef = new EnhancedInstance() {
ContextSnapshot contextSnapshot;
@Override
public Object getSkyWalkingDynamicField() {
return this.contextSnapshot;
}
@Override
public void setSkyWalkingDynamicField(Object value) {
this.contextSnapshot = (ContextSnapshot) value;
}
};
}
@Test
public void testTracerCreateEntrySpan() throws Throwable {
Method createEntrySpan = Tracer.class.getDeclaredMethod("createEntrySpan", String.class, ContextCarrierRef.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
tracerCreateEntrySpanInterceptor.beforeMethod(Tracer.class, createEntrySpan, null, null, null);
EnhancedInstance entrySpanRef = (EnhancedInstance) tracerCreateEntrySpanInterceptor.afterMethod(Tracer.class, createEntrySpan, new Object[]{"createEntrySpanMethod", null}, null, enhancedSpanRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
AbstractTracingSpan tracingSpan = spans.get(0);
SpanAssert.assertLogSize(tracingSpan, 0);
SpanAssert.assertTagSize(tracingSpan, 0);
assertThat(tracingSpan.getOperationName(), is("createEntrySpanMethod"));
EntrySpan entrySpanFromRef = (EntrySpan) entrySpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(entrySpanFromRef, 0);
SpanAssert.assertTagSize(entrySpanFromRef, 0);
assertThat(entrySpanFromRef.getOperationName(), is("createEntrySpanMethod"));
}
@Test
public void testTracerCreateLocalSpan() throws Throwable {
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
EnhancedInstance localSpanRef = (EnhancedInstance) tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[]{"testCreateLocalSpanMethod"}, null, enhancedSpanRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
AbstractTracingSpan tracingSpan = spans.get(0);
SpanAssert.assertLogSize(tracingSpan, 0);
SpanAssert.assertTagSize(tracingSpan, 0);
assertThat(tracingSpan.getOperationName(), is("testCreateLocalSpanMethod"));
AbstractTracingSpan localSpanFromRef = (AbstractTracingSpan) localSpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(localSpanFromRef, 0);
SpanAssert.assertTagSize(localSpanFromRef, 0);
assertThat(localSpanFromRef.getOperationName(), is("testCreateLocalSpanMethod"));
}
@Test
public void testTracerInjectAndExtract() throws Throwable {
Method createExitSpan = Tracer.class.getDeclaredMethod("createExitSpan", String.class, String.class);
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
Method inject = Tracer.class.getDeclaredMethod("inject", ContextCarrierRef.class);
Method extract = Tracer.class.getDeclaredMethod("extract", ContextCarrierRef.class);
Config.Agent.SERVICE_NAME = "testTracerCreateExitSpanNoContextService";
Config.Agent.INSTANCE_NAME = "testTracerCreateExitSpanNoContextIns";
tracerCreateExitSpanNoContextInterceptor.beforeMethod(Tracer.class, createExitSpan, null, null, null);
EnhancedInstance exitSpanRef = (EnhancedInstance) tracerCreateExitSpanNoContextInterceptor.afterMethod(Tracer.class, createExitSpan, new Object[]{"testCreateExitSpanMethod", "127.0.0.1:6666"}, null, enhancedSpanRef);
tracerInjectInterceptor.beforeMethod(Tracer.class, inject, null, null, null);
tracerInjectInterceptor.afterMethod(Tracer.class, inject, new Object[] {enhancedContextCarrierRef}, null, null);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
ExitSpan exitSpan = (ExitSpan) spans.get(0);
SpanAssert.assertLogSize(exitSpan, 0);
SpanAssert.assertTagSize(exitSpan, 0);
assertThat(exitSpan.getOperationName(), is("testCreateExitSpanMethod"));
assertThat(exitSpan.getPeer(), is("127.0.0.1:6666"));
ExitSpan exitSpanFromRef = (ExitSpan) exitSpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(exitSpanFromRef, 0);
SpanAssert.assertTagSize(exitSpanFromRef, 0);
assertThat(exitSpanFromRef.getOperationName(), is("testCreateExitSpanMethod"));
assertThat(exitSpanFromRef.getPeer(), is("127.0.0.1:6666"));
ContextCarrier contextCarrierFromInject = (ContextCarrier) (enhancedContextCarrierRef.getSkyWalkingDynamicField());
Assert.assertNotNull(contextCarrierFromInject);
assertThat(contextCarrierFromInject.getSpanId(), is(exitSpan.getSpanId()));
assertThat(contextCarrierFromInject.getTraceSegmentId(), is(traceSegment.getTraceSegmentId()));
assertThat(contextCarrierFromInject.getTraceId(), is(traceSegment.getRelatedGlobalTrace().getId()));
assertThat(contextCarrierFromInject.getAddressUsedAtClient(), is(exitSpan.getPeer()));
Thread thread = new Thread(() -> {
try {
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[] {"newSegLocalSpan"}, null, enhancedSpanRef);
tracerExtractInterceptor.beforeMethod(Tracer.class, extract, null, null, null);
tracerExtractInterceptor.afterMethod(Tracer.class, extract, new Object[] {enhancedContextCarrierRef}, null, null);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(2));
TraceSegment newTraceSegment = storage.getTraceSegments().get(1);
assertThat(newTraceSegment.getRef().getTraceId(), is(traceSegment.getRelatedGlobalTrace().getId()));
assertThat(newTraceSegment.getRef().getTraceSegmentId(), is(traceSegment.getTraceSegmentId()));
assertThat(newTraceSegment.getRelatedGlobalTrace().getId(), is(traceSegment.getRelatedGlobalTrace().getId()));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
});
thread.start();
thread.join();
}
@Test
public void testTracerCreateExitSpanWithContext() throws Throwable {
Method createExitSpan = Tracer.class.getDeclaredMethod("createExitSpan", String.class, ContextCarrierRef.class, String.class);
Method createEntrySpan = Tracer.class.getDeclaredMethod("createEntrySpan", String.class, ContextCarrierRef.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
Config.Agent.SERVICE_NAME = "testTracerCreateExitSpanNoContextService";
Config.Agent.INSTANCE_NAME = "testTracerCreateExitSpanNoContextIns";
tracerCreateExitSpanWithContextInterceptor.beforeMethod(Tracer.class, createExitSpan, null, null, null);
EnhancedInstance exitSpanRef = (EnhancedInstance) tracerCreateExitSpanWithContextInterceptor.afterMethod(Tracer.class, createExitSpan, new Object[]{"testExitSpanWithContext", enhancedContextCarrierRef, "127.0.0.1:5555"}, null, enhancedSpanRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
ExitSpan exitSpan = (ExitSpan) spans.get(0);
SpanAssert.assertLogSize(exitSpan, 0);
SpanAssert.assertTagSize(exitSpan, 0);
assertThat(exitSpan.getOperationName(), is("testExitSpanWithContext"));
assertThat(exitSpan.getPeer(), is("127.0.0.1:5555"));
ExitSpan exitSpanFromRef = (ExitSpan) exitSpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(exitSpanFromRef, 0);
SpanAssert.assertTagSize(exitSpanFromRef, 0);
assertThat(exitSpanFromRef.getOperationName(), is("testExitSpanWithContext"));
assertThat(exitSpanFromRef.getPeer(), is("127.0.0.1:5555"));
ContextCarrier contextCarrierFromInject = (ContextCarrier) (((EnhancedInstance) enhancedContextCarrierRef).getSkyWalkingDynamicField());
Assert.assertNotNull(contextCarrierFromInject);
assertThat(contextCarrierFromInject.getSpanId(), is(exitSpan.getSpanId()));
assertThat(contextCarrierFromInject.getTraceSegmentId(), is(traceSegment.getTraceSegmentId()));
assertThat(contextCarrierFromInject.getTraceId(), is(traceSegment.getRelatedGlobalTrace().getId()));
assertThat(contextCarrierFromInject.getAddressUsedAtClient(), is(exitSpan.getPeer()));
Thread thread = new Thread(() -> {
try {
enhancedSpanRef.setSkyWalkingDynamicField(null);
tracerCreateEntrySpanInterceptor.beforeMethod(Tracer.class, createEntrySpan, null, null, null);
EnhancedInstance entrySpanRef = (EnhancedInstance) tracerCreateEntrySpanInterceptor.afterMethod(Tracer.class, createEntrySpan, new Object[] {"entrySpanFromExtract", enhancedContextCarrierRef}, null, enhancedSpanRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(2));
TraceSegment newTraceSegment = storage.getTraceSegments().get(1);
assertThat(newTraceSegment.getRef().getTraceId(), is(traceSegment.getRelatedGlobalTrace().getId()));
assertThat(newTraceSegment.getRef().getTraceSegmentId(), is(traceSegment.getTraceSegmentId()));
assertThat(newTraceSegment.getRelatedGlobalTrace().getId(), is(traceSegment.getRelatedGlobalTrace().getId()));
List<AbstractTracingSpan> spans1 = SegmentHelper.getSpans(newTraceSegment);
assertThat(spans1.size(), is(1));
EntrySpan entrySpan = (EntrySpan) spans1.get(0);
assertThat(entrySpan.isEntry(), is(true));
assertThat(entrySpan.getOperationName(), is("entrySpanFromExtract"));
EntrySpan entrySpanFromRef = (EntrySpan) entrySpanRef.getSkyWalkingDynamicField();
SpanAssert.assertLogSize(entrySpanFromRef, 0);
SpanAssert.assertTagSize(entrySpanFromRef, 0);
assertThat(entrySpanFromRef.isEntry(), is(true));
assertThat(entrySpanFromRef.getOperationName(), is("entrySpanFromExtract"));
assertThat(entrySpanFromRef.getSpanId(), is(entrySpan.getSpanId()));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
});
thread.start();
thread.join();
}
@Test
public void testTracerCaptureAndContinued() throws Throwable {
Method capture = Tracer.class.getDeclaredMethod("capture");
Method continued = Tracer.class.getDeclaredMethod("continued", ContextSnapshotRef.class);
Method createLocalSpan = Tracer.class.getDeclaredMethod("createLocalSpan", String.class);
Method stopSpan = Tracer.class.getDeclaredMethod("stopSpan");
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[] {"localSpanForCapture"}, null, enhancedSpanRef);
tracerCaptureInterceptor.beforeMethod(Tracer.class, capture, null, null, null);
EnhancedInstance enhancedInsSnapshot = (EnhancedInstance) tracerCaptureInterceptor.afterMethod(Tracer.class, capture, null, null, enhancedContextSnapshotRef);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = storage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
Thread thread = new Thread(() -> {
try {
tracerCreateLocalSpanInterceptor.beforeMethod(Tracer.class, createLocalSpan, null, null, null);
tracerCreateLocalSpanInterceptor.afterMethod(Tracer.class, createLocalSpan, new Object[] {"localSpanForContinued"}, null, enhancedSpanRef);
tracerContinuedInterceptor.beforeMethod(Tracer.class, continued, new Object[] {enhancedInsSnapshot}, null, null);
tracerContinuedInterceptor.afterMethod(Tracer.class, continued, null, null, null);
tracerStopSpanInterceptor.beforeMethod(Tracer.class, stopSpan, null, null, null);
tracerStopSpanInterceptor.afterMethod(Tracer.class, stopSpan, null, null, null);
assertThat(storage.getTraceSegments().size(), is(2));
TraceSegment newTraceSegment = storage.getTraceSegments().get(1);
assertThat(newTraceSegment.getRef().getTraceId(), is(traceSegment.getRelatedGlobalTrace().getId()));
assertThat(newTraceSegment.getRef().getTraceSegmentId(), is(traceSegment.getTraceSegmentId()));
assertThat(newTraceSegment.getRelatedGlobalTrace().getId(), is(traceSegment.getRelatedGlobalTrace().getId()));
List<AbstractTracingSpan> newSpans = SegmentHelper.getSpans(newTraceSegment);
LocalSpan newSpan = (LocalSpan) newSpans.get(0);
assertThat(newSpan.getOperationName(), is("localSpanForContinued"));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
});
thread.start();
thread.join();
}
}

View File

@ -0,0 +1,9 @@
Dependency the toolkit, such as using maven or gradle
```xml
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>${skywalking.version}</version>
</dependency>
```

View File

@ -0,0 +1,25 @@
# Use annotation to mark the method you want to trace.
* Add `@Trace` to any method you want to trace. After that, you can see the span in the Stack.
* Methods annotated with `@Tag` will try to tag the **current active span** with the given key (`Tag#key()`) and (`Tag#value()`),
if there is no active span at all, this annotation takes no effect. `@Tag` can be repeated, and can be used in companion with `@Trace`, see examples below.
The `value` of `Tag` is the same as what are supported in [Customize Enhance Trace](Customize-enhance-trace.md).
```java
/**
* The codes below will generate a span,
* and two types of tags,
one type tag: keys are `tag1` and `tag2`, values are the passed-in parameters, respectively,
the other type tag: keys are `username` and `age`, values are the return value in User, respectively
*/
@Trace
@Tag(key = "tag1", value = "arg[0]")
@Tag(key = "tag2", value = "arg[1]")
@Tag(key = "username", value = "returnedObj.username")
@Tag(key = "age", value = "returnedObj.age")
public User methodYouWantToTrace(String param1, String param2) {
// ...
}
```
_Sample codes only_

View File

@ -0,0 +1,12 @@
* Use `TraceContext.putCorrelation()` API to put custom data in tracing context.
```java
Optional<String> previous = TraceContext.putCorrelation("customKey", "customValue");
```
CorrelationContext will remove the item when the value is `null` or empty.
* Use `TraceContext.getCorrelation()` API to get custom data.
```java
Optional<String> value = TraceContext.getCorrelation("customKey");
```
CorrelationContext configuration descriptions could be found in [the agent configuration](README.md#table-of-agent-configuration-properties) documentation, with `correlation.` as the prefix.
_Sample codes only_

View File

@ -1,13 +1,5 @@
# trace cross thread
* Dependency the toolkit, such as using maven or gradle
```xml
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>${skywalking.version}</version>
</dependency>
```
There are some other ways to trace cross thread except the methods in `Tracer`.
* usage 1.
```java
@TraceCrossThread

View File

@ -0,0 +1,23 @@
* Use `TraceContext.traceId()` API to obtain traceId.
```java
import TraceContext;
...
modelAndView.addObject("traceId", TraceContext.traceId());
```
* Use `TraceContext.segmentId()` API to obtain segmentId.
```java
import TraceContext;
...
modelAndView.addObject("segmentId", TraceContext.segmentId());
```
* Use `TraceContext.spanId()` API to obtain spanId.
```java
import TraceContext;
...
modelAndView.addObject("spanId", TraceContext.spanId());
```
_Sample codes only_

View File

@ -0,0 +1,213 @@
# Create Span
* Use `Tracer.createEntrySpan()` API to create entry span, and then use `SpanRef` to contain the reference of created span in agent kernel. The first parameter is operation name of span and the second parameter is the `ContextCarrierRef` instance which is the reference of contextcarrier in agent kernel. If the second parameter is not null, the process of creating entry span will do the extract operation which will be introduced in **inject/extract** scenario.
```java
import Tracer;
...
SpanRef spanRef = Tracer.createEnteySpan("${operationName}", null);
```
* Use `Tracer.createLocalSpan()` API to create local span, the only parameter is the operation name of span.
```java
import Tracer;
...
SpanRef spanRef = Tracer.createLocalSpan("${operationName}");
```
* Use `Tracer.createExitSpan()` API to create exit span
* **two parameters case**: the first parameter is the operation name of span, the second parameter is the remote peer which means the peer address of exit operation.
```java
import Tracer;
...
SpanRef spanRef = Tracer.createExitSpan("${operationName}", "${remotePeer}");
```
* **three parameters case**: the first parameter is the operation name of span, the second parameter is the `ContextCarrierRef` instance and the third parameter is the remote peer. This case will be introduced in **inject/extract** scenario.
* Use `Tracer.stopSpan()` API to stop current span
```java
import Tracer;
...
Tracer.stopSpan();
```
# Inject/Extract Context Carrier
The Inject/extract is to pass context information between different process. The `ContextCarrierRef` contains the reference of `ContextCarrier` and the `CarrierItemRef` contains the reference of CarrierItem. The `CarrierItem` instances compose a linked list.
* Use `Tracer.inject()` to inject information of current context into carrier
* Use `Tracer.extract()` to extract info from contextCarrier.
* Use `items()` of `ContextCarrierRef` instance to get head `CarrierItemRef` instance.
* Use `hasNext()` of `CarrierItemRef` instance to judge if the `CarrierItemRef` has next item.
* Use `next()` of `CarrierItemRef` instance to get next item
* Use `getHeadKey` of `CarrierItemRef` instance to get key of current item
* Use `getHeadValue` of `CarrierItemRef` instance to get value of current item
* Use `setHeadValue` of `CarrierItemRef` instance to set value of current item
```java
/*
You can consider map as the message's header/metadata, such as Http, MQ and RPC.
Do the inject operation in one process and then pass the map in header/metadata.
*/
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
Tracer.inject(contextCarrierRef);
Map<String, String> map = new HashMap<>();
CarrierItemRef next = contextCarrierRef.items();
while (next.hasNext()) {
next = next.next();
map.put(next.getHeadKey(), next.getHeadValue());
}
...
```
```java
// Receive the map representing a header/metadata and do the extract operation in another process.
...
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
CarrierItemRef next = contextCarrierRef.items();
for (Map.Entry<String, String> entry : map.entrySet()) {
if (next.hasNext()) {
next = next.next();
if (entry.getKey().equals(next.getHeadKey()))
next.setHeadValue(entry.getValue());
}
}
Tracer.extract(contextCarrierRef);
```
Also, you can do the inject/extract operation when creating exit/entry span.
```java
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
SpanRef spanRef = Tracer.createExitSpan("operationName", contextCarrierRef, "remotePeer");
Map<String, String> map = new HashMap<>();
CarrierItemRef next = contextCarrierRef.items();
while (next.hasNext()) {
next = next.next();
map.put(next.getHeadKey(), next.getHeadValue());
}
...
```
```java
...
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
CarrierItemRef next = contextCarrierRef.items();
for (Map.Entry<String, String> entry : map.entrySet()) {
if (next.hasNext()) {
next = next.next();
if (entry.getKey().equals(next.getHeadKey()))
next.setHeadValue(entry.getValue());
}
}
SpanRef spanRef = Tracer.createEntrySpan("${operationName}", contextCarrierRef);
```
# Capture/Continue Context Snapshot
* Use `Tracer.capture()` to capture the segment info and store it in `ContextSnapshotRef`, and then use `Tracer.continued()` to load the snapshot as the ref segment info. The capture/continue is used for tracing context in the x-thread tracing.
```java
import Tracer;
...
ContextSnapshotRef contextSnapshotRef = Tracer.capture();
Thread thread = new Thread(() -> {
SpanRef spanRef = Tracer.createLocalSpan("${operationName}");
Tracer.continued(contextSnapshotRef);
...
});
thread.start();
thread.join();
```
# Add Span's Tag and Log
* Use `log` of `SpanRef` instance to record log in span
```java
import SpanRef;
...
SpanRef spanRef = Tracer.createLocalSpan("${operationName}");
// Throwable parameter
spanRef.log(new RuntimeException("${exception_message}"));
// Map parameter
Map<String, String> logMap = new HashMap<>();
logMap.put("event", "${event_type}");
logMap.put("message", "${message_value}");
spanRef.log(logMap);
```
* Use `tag` of `SpanRef` instance to add tag to span, the parameters of tag are two String which are key and value respectively.
```java
import SpanRef;
...
SpanRef spanRef = Tracer.createLocalSpan(operationName);
spanRef.tag("${key}", "${value}");
```
# Async Prepare/Finish
* Use `prepareForAsync` of `SpanRef` instance to make the span still alive until `asyncFinish` called, and then in specific time use `asyncFinish` of this `SpanRef` instance to notify this span that it could be finished.
```java
import SpanRef;
...
SpanRef spanRef = Tracer.createLocalSpan("${operationName}");
spanRef.prepareForAsync();
// the span does not finish because of the prepareForAsync() operation
Tracer.stopSpan();
Thread thread = new Thread(() -> {
...
spanRef.asyncFinish();
});
thread.start();
thread.join();
```
# ActiveSpan
You can use the `ActiveSpan` to get the current span and do some operations.
* Add custom tag in the context of traced method, `ActiveSpan.tag("key", "val")`.
* `ActiveSpan.error()` Mark the current span as error status.
* `ActiveSpan.error(String errorMsg)` Mark the current span as error status with a message.
* `ActiveSpan.error(Throwable throwable)` Mark the current span as error status with a Throwable.
* `ActiveSpan.debug(String debugMsg)` Add a debug level log message in the current span.
* `ActiveSpan.info(String infoMsg)` Add an info level log message in the current span.
* `ActiveSpan.setOperationName(String operationName)` Customize an operation name.
```java
ActiveSpan.tag("my_tag", "my_value");
ActiveSpan.error();
ActiveSpan.error("Test-Error-Reason");
ActiveSpan.error(new RuntimeException("Test-Error-Throwable"));
ActiveSpan.info("Test-Info-Msg");
ActiveSpan.debug("Test-debug-Msg");
ActiveSpan.setOperationName("${opetationName}");
```
_Sample codes only_

View File

@ -125,6 +125,7 @@
- toolkit-tag
- toolkit-trace
- toolkit-exception
- toolkit-tracer
- toolkit-webflux
- undertow-2.x-plugin
- vertx-core-3.x

View File

@ -37,13 +37,23 @@ catalog:
- name: "Manual APIs"
catalog:
- name: "Tracing APIs"
path: "/en/setup/service-agent/java-agent/application-toolkit-trace"
catalog:
- name: "Add Trace Toolkit Dependencies"
path: "/en/setup/service-agent/java-agent/application-toolkit-dependency"
- name: "Use Native Tracer"
path: "/en/setup/service-agent/java-agent/application-toolkit-tracer"
- name: "Read Tracing Context"
path: "/en/setup/service-agent/java-agent/application-toolkit-trace-read-context"
- name: "Annotations"
path: "/en/setup/service-agent/java-agent/application-toolkit-trace-annotation"
- name: "Use Correlation Context"
path: "/en/setup/service-agent/java-agent/application-toolkit-trace-correlation-context"
- name: "Across Thread Solution"
path: "/en/setup/service-agent/java-agent/application-toolkit-trace-cross-thread"
- name: "OpenTracing Tracer"
path: "/en/setup/service-agent/java-agent/opentracing"
- name: "Meter APIs"
path: "/en/setup/service-agent/java-agent/application-toolkit-meter"
- name: "Across Thread Solution"
path: "/en/setup/service-agent/java-agent/application-toolkit-trace-cross-thread"
- name: "Micrometer Registry"
path: "/en/setup/service-agent/java-agent/application-toolkit-micrometer"
- name: "Micrometer 1.10 Observation"

View File

@ -0,0 +1,21 @@
#!/bin/bash
#
# 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.
home="$(cd "$(dirname $0)"; pwd)"
java -jar ${agent_opts} ${home}/../libs/apm-toolkit-tracer-scenario.jar &

View File

@ -0,0 +1,190 @@
# 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.
segmentItems:
- serviceName: apm-toolkit-tracer-scenario
segmentSize: eq 5
segments:
- segmentId: not null
spans:
- operationName: HEAD:/case/healthCheck
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 14
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: url, value: 'http://localhost:8080/apm-toolkit-tracer-scenario/case/healthCheck'}
- {key: http.method, value: HEAD}
- {key: http.status_code, value: '200'}
- segmentId: not null
spans:
- operationName: call-new-thread
parentSpanId: -1
spanId: 0
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: true
spanType: Local
peer: ''
skipAnalysis: false
logs:
- logEvent:
- {key: event, value: error}
- {key: error.kind, value: java.lang.RuntimeException}
- {key: message, value: test-Throwable-log}
- key: stack
value: not null
refs:
- {parentEndpoint: trace-scenario-entry, networkAddress: '', refType: CrossThread,
parentSpanId: 0, parentTraceSegmentId: not null,
parentServiceInstance: not null, parentService: apm-toolkit-tracer-scenario,
traceId: not null}
- segmentId: not null
spans:
- {operationName: local-in-another-new-process, parentSpanId: 0, spanId: 1, spanLayer: Unknown,
startTime: nq 0, endTime: nq 0, componentId: 0, isError: false,
spanType: Local, peer: '', skipAnalysis: false}
- {operationName: exit-another-new-process, parentSpanId: 0, spanId: 2, spanLayer: Unknown,
startTime: nq 0, endTime: nq 0, componentId: 0, isError: false,
spanType: Exit, peer: '127.0.0.1:8888', skipAnalysis: false}
- operationName: start-another-new-process
parentSpanId: -1
spanId: 0
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: enter_position, value: startServiceWithExtract}
- {key: http.status_code, value: '200'}
refs:
- {parentEndpoint: start-new-process, networkAddress: 'localhost:8080', refType: CrossProcess,
parentSpanId: 3, parentTraceSegmentId: not null,
parentServiceInstance: not null, parentService: apm-toolkit-tracer-scenario,
traceId: not null}
- segmentId: not null
spans:
- {operationName: local-in-new-process, parentSpanId: 0, spanId: 1, spanLayer: Unknown,
startTime: nq 0, endTime: nq 0, componentId: 0, isError: false,
spanType: Local, peer: '', skipAnalysis: false}
- {operationName: exit-new-process, parentSpanId: 0, spanId: 2, spanLayer: Unknown,
startTime: nq 0, endTime: nq 0, componentId: 0, isError: false,
spanType: Exit, peer: '127.0.0.1:6666', skipAnalysis: false}
- operationName: /apm-toolkit-tracer-scenario/case/startAnotherNewProcess
parentSpanId: 0
spanId: 3
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 2
isError: false
spanType: Exit
peer: localhost:8080
skipAnalysis: false
tags:
- {key: url, value: 'http://localhost:8080/apm-toolkit-tracer-scenario/case/startAnotherNewProcess'}
- {key: http.method, value: POST}
- {key: http.status_code, value: '200'}
- operationName: start-new-process
parentSpanId: -1
spanId: 0
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: http.status_code, value: '200'}
refs:
- {parentEndpoint: trace-scenario-entry, networkAddress: 'localhost:8080', refType: CrossProcess,
parentSpanId: 4, parentTraceSegmentId: not null,
parentServiceInstance: not null, parentService: apm-toolkit-tracer-scenario,
traceId: not null}
- segmentId: not null
spans:
- {operationName: trace-scenario-local, parentSpanId: 0, spanId: 1, spanLayer: Unknown,
startTime: nq 0, endTime: nq 0, componentId: 0, isError: false,
spanType: Local, peer: '', skipAnalysis: false}
- operationName: prepare and finish async
parentSpanId: 0
spanId: 2
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: false
spanType: Local
peer: ''
skipAnalysis: false
logs:
- logEvent:
- {key: event, value: info}
- {key: message, value: in async thread}
- operationName: trace-scenario-exit
parentSpanId: 0
spanId: 3
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: false
spanType: Exit
peer: 127.0.0.1:5555
skipAnalysis: false
tags:
- {key: exit_position, value: endServiceWithCarrier}
- operationName: /apm-toolkit-tracer-scenario/case/startNewProcess
parentSpanId: 0
spanId: 4
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 2
isError: false
spanType: Exit
peer: localhost:8080
skipAnalysis: false
tags:
- {key: url, value: 'http://localhost:8080/apm-toolkit-tracer-scenario/case/startNewProcess'}
- {key: http.method, value: POST}
- {key: http.status_code, value: '200'}
- operationName: trace-scenario-entry
parentSpanId: -1
spanId: 0
spanLayer: Unknown
startTime: nq 0
endTime: nq 0
componentId: 0
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: http.status_code, value: '200'}

View File

@ -0,0 +1,20 @@
# 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.
type: jvm
entryService: http://localhost:8080/apm-toolkit-tracer-scenario/case/apm-toolkit-tracer-scenario
healthCheck: http://localhost:8080/apm-toolkit-tracer-scenario/case/healthCheck
startScript: ./bin/startup.sh

View File

@ -0,0 +1,126 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->
<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">
<groupId>org.apache.skywalking.apm.testcase</groupId>
<artifactId>apm-toolkit-tracer-scenario</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.version>1.8</compiler.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<test.framework.version>YOUR VERSION</test.framework.version>
<spring.boot.version>2.1.6.RELEASE</spring.boot.version>
<lombok.version>1.18.20</lombok.version>
</properties>
<name>skywalking-apm-toolkit-tracer-scenario</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
<build>
<finalName>apm-toolkit-tracer-scenario</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${compiler.version}</source>
<target>${compiler.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<outputDirectory>./target/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>./bin</directory>
<fileMode>0775</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>${project.build.directory}/apm-toolkit-tracer-scenario.jar</source>
<outputDirectory>./libs</outputDirectory>
<fileMode>0775</fileMode>
</file>
</files>
</assembly>

View File

@ -0,0 +1,41 @@
/*
* 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.toolkit.trace;
public class CarrierItemRef {
public boolean hasNext() {
return false;
}
public CarrierItemRef next() {
return new CarrierItemRef();
}
public String getHeadKey() {
return "";
}
public String getHeadValue() {
return "";
}
public void setHeadValue(String headValue) {
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.toolkit.trace;
public class ContextCarrierRef {
public CarrierItemRef items() {
return new CarrierItemRef();
}
}

View File

@ -0,0 +1,22 @@
/*
* 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.toolkit.trace;
public class ContextSnapshotRef {
}

View File

@ -0,0 +1,41 @@
/*
* 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.toolkit.trace;
import java.util.Map;
public class SpanRef {
public SpanRef prepareForAsync() {
return new SpanRef();
}
public SpanRef asyncFinish() {
return new SpanRef();
}
public void log(Throwable t) {
}
public void log(Map<String, ?> event) {
}
public void tag(String key, String value) {
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.toolkit.trace;
public class Tracer {
public static SpanRef createEntrySpan(String operationName, ContextCarrierRef carrierRef) {
return new SpanRef();
}
public static SpanRef createLocalSpan(String operationName) {
return new SpanRef();
}
public static SpanRef createExitSpan(String operationName, String remotePeer) {
return new SpanRef();
}
public static SpanRef createExitSpan(String operationName, ContextCarrierRef carrierRef, String remotePeer) {
return new SpanRef();
}
public static void stopSpan() {
}
public static void inject(ContextCarrierRef carrierRef) {
}
public static void extract(ContextCarrierRef carrierRef) {
}
public static ContextSnapshotRef capture() {
return new ContextSnapshotRef();
}
public static void continued(ContextSnapshotRef snapshot) {
}
}

View File

@ -0,0 +1,34 @@
/*
* 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 test.apache.skywalking.apm.testcase.toolkit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
try {
SpringApplication.run(Application.class, args);
} catch (Exception e) {
// Never do this
}
}
}

View File

@ -0,0 +1,129 @@
/*
* 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 test.apache.skywalking.apm.testcase.toolkit.controller;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/case")
@Log4j2
public class CaseController {
private static final String SUCCESS = "Success";
@Autowired
private TestService testService;
@RequestMapping("/apm-toolkit-tracer-scenario")
@ResponseBody
public String testcase() {
testService.startService("trace-scenario-entry");
testService.doSomething("trace-scenario-local");
try {
testService.callNewThread("call-new-thread");
} catch (Exception e) {
// ignore
}
try {
testService.asyncPrepareAndFinish();
} catch (Exception e) {
}
Map<String, String> map = testService.endServiceWithCarrier("trace-scenario-exit", "127.0.0.1:5555");
try {
doPost("http://localhost:8080/apm-toolkit-tracer-scenario/case/startNewProcess", map);
} catch (IOException e) {
// ignore
}
return SUCCESS;
}
@PostMapping("/startNewProcess")
@ResponseBody
public String startNewProcess(@RequestParam Map<String, String> params) {
testService.startServiceWithCarrier("start-new-process", params);
testService.doSomething("local-in-new-process");
Map<String, String> map = testService.endServiceWithInject("exit-new-process", "127.0.0.1:6666");
try {
doPost("http://localhost:8080/apm-toolkit-tracer-scenario/case/startAnotherNewProcess", map);
} catch (IOException e) {
// ignore
}
return SUCCESS;
}
@PostMapping("/startAnotherNewProcess")
@ResponseBody
public String startAnotherNewProcess(@RequestParam Map<String, String> params) {
testService.startServiceWithExtract("start-another-new-process", params);
testService.doSomething("local-in-another-new-process");
testService.endService("exit-another-new-process", "127.0.0.1:8888");
return SUCCESS;
}
@RequestMapping("/healthCheck")
@ResponseBody
public String healthCheck() {
return SUCCESS;
}
private static void doPost(String url, Map<String, String> map) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> parameters = new ArrayList<>(0);
for (Map.Entry<String, String> entry : map.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(formEntity);
ResponseHandler<String> responseHandler = response -> {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
};
httpClient.execute(httpPost, responseHandler);
} finally {
httpClient.close();
}
}
}

View File

@ -0,0 +1,141 @@
/*
* 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 test.apache.skywalking.apm.testcase.toolkit.controller;
import org.apache.skywalking.apm.toolkit.trace.CarrierItemRef;
import org.apache.skywalking.apm.toolkit.trace.Tracer;
import org.apache.skywalking.apm.toolkit.trace.ContextCarrierRef;
import org.apache.skywalking.apm.toolkit.trace.ContextSnapshotRef;
import org.apache.skywalking.apm.toolkit.trace.SpanRef;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class TestService {
public void startService(String operationName) {
SpanRef entrySpanRef = Tracer.createEntrySpan(operationName, null);
Tracer.stopSpan();
}
public void startServiceWithCarrier(String operationName, Map<String, String> map) {
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
CarrierItemRef next = contextCarrierRef.items();
for (Map.Entry<String, String> entry : map.entrySet()) {
if (next.hasNext()) {
next = next.next();
if (entry.getKey().equals(next.getHeadKey()))
next.setHeadValue(entry.getValue());
}
}
SpanRef entrySpanRef = Tracer.createEntrySpan(operationName, contextCarrierRef);
Tracer.stopSpan();
}
public void startServiceWithExtract(String operationName, Map<String, String> map) {
SpanRef entrySpanRef = Tracer.createEntrySpan(operationName, null);
entrySpanRef.tag("enter_position", "startServiceWithExtract");
doExtract(map);
Tracer.stopSpan();
}
public void doSomething(String operationName) {
SpanRef localSpanRef = Tracer.createLocalSpan(operationName);
Tracer.stopSpan();
}
public void endService(String operationName, String remotePeer) {
SpanRef exitSpanRef = Tracer.createExitSpan(operationName, remotePeer);
Tracer.stopSpan();
}
public Map<String, String> endServiceWithCarrier(String operationName, String remotePeer) {
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
SpanRef exitSpanRef = Tracer.createExitSpan(operationName, contextCarrierRef, remotePeer);
exitSpanRef.tag("exit_position", "endServiceWithCarrier");
Map<String, String> map = new HashMap<>();
CarrierItemRef next = contextCarrierRef.items();
while (next.hasNext()) {
next = next.next();
map.put(next.getHeadKey(), next.getHeadValue());
}
Tracer.stopSpan();
return map;
}
public Map<String, String> endServiceWithInject(String operationName, String remotePeer) {
SpanRef exitSpanRef = Tracer.createExitSpan(operationName, remotePeer);
Map<String, String> map = this.doInject();
Tracer.stopSpan();
return map;
}
public void callNewThread(String operationName) throws InterruptedException {
ContextSnapshotRef contextSnapshotRef = Tracer.capture();
Thread thread = new Thread(() -> {
SpanRef spanRef = Tracer.createLocalSpan(operationName);
spanRef.log(new RuntimeException("test-Throwable-log"));
Tracer.continued(contextSnapshotRef);
Tracer.stopSpan();
});
thread.start();
thread.join();
}
private Map<String, String> doInject() {
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
Tracer.inject(contextCarrierRef);
Map<String, String> map = new HashMap<>();
CarrierItemRef next = contextCarrierRef.items();
while (next.hasNext()) {
next = next.next();
map.put(next.getHeadKey(), next.getHeadValue());
}
return map;
}
private void doExtract(Map<String, String> map) {
ContextCarrierRef contextCarrierRef = new ContextCarrierRef();
CarrierItemRef next = contextCarrierRef.items();
for (Map.Entry<String, String> entry : map.entrySet()) {
if (next.hasNext()) {
next = next.next();
if (entry.getKey().equals(next.getHeadKey()))
next.setHeadValue(entry.getValue());
}
}
Tracer.extract(contextCarrierRef);
}
public void asyncPrepareAndFinish() throws InterruptedException {
SpanRef spanRef = Tracer.createLocalSpan("prepare and finish async");
spanRef.prepareForAsync();
Tracer.stopSpan();
Thread thread = new Thread(() -> {
Map<String, String> logMap = new HashMap<>();
logMap.put("event", "info");
logMap.put("message", "in async thread");
spanRef.log(logMap);
spanRef.asyncFinish();
});
thread.start();
thread.join();
}
}

View File

@ -0,0 +1,23 @@
#
# 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.
#
#
server:
port: 8080
servlet:
context-path: /apm-toolkit-tracer-scenario
logging:
config: classpath:log4j2.xml

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_ERR">
<PatternLayout charset="UTF-8" pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,19 @@
# 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.
# lists your version here (Contains only the last version number of each minor version.)
1.0.0