Skip to content

Commit

Permalink
Add logic-endpoint and testcase for elasticjob-2.x plugin (apache#5395)
Browse files Browse the repository at this point in the history
  • Loading branch information
hailin0 authored Aug 28, 2020
1 parent f89ed0c commit afc0aef
Show file tree
Hide file tree
Showing 15 changed files with 626 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/plugins-test.3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
- { name: 'spring-kafka-2.2.x-scenario', title: 'Spring-Kafka 2.2.x (7)' }
- { name: 'spring-kafka-2.3.x-scenario', title: 'Spring-Kafka 2.3.x (7)' }
- { name: 'spring-scheduled-scenario', title: 'Spring Scheduled 3.1.x-5.2.x (9)' }
- { name: 'elasticjob-2.x-scenario', title: 'elasticjob-2.x-scenario (2)' }
steps:
- uses: actions/checkout@v2
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

package org.apache.skywalking.apm.plugin.esjob;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.event.type.JobExecutionEvent;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.google.common.base.Strings;
import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag;
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;
Expand All @@ -31,23 +31,31 @@
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;

/**
* {@link JobExecutorInterceptor} enhances {@link com.dangdang.ddframe.job.executor.AbstractElasticJobExecutor#process(ShardingContext)}
* {@link JobExecutorInterceptor} enhances {@link com.dangdang.ddframe.job.executor.AbstractElasticJobExecutor#process(ShardingContexts, int, JobExecutionEvent)}
* ,creating a local span that records job execution.
*/
public class JobExecutorInterceptor implements InstanceMethodsAroundInterceptor {

private static final AbstractTag<String> TAG_ITEM = Tags.ofKey("item");
private static final AbstractTag<String> TAG_TASK_ID = Tags.ofKey("taskId");
private static final AbstractTag<String> TAG_SHARDING_TOTAL_COUNT = Tags.ofKey("shardingTotalCount");
private static final AbstractTag<String> TAG_SHARDING_ITEM_PARAMETERS = Tags.ofKey("shardingItemParameters");

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
ShardingContexts shardingContexts = (ShardingContexts) allArguments[0];
Integer item = (Integer) allArguments[1];
ShardingContext shardingContext = new ShardingContext(shardingContexts, item);
String operateName = shardingContext.getJobName();
if (!Strings.isNullOrEmpty(shardingContext.getShardingParameter())) {
operateName += "-" + shardingContext.getShardingParameter();
}
String operateName = ComponentsDefine.ELASTIC_JOB.getName() + "/" + shardingContexts.getJobName();

AbstractSpan span = ContextManager.createLocalSpan(operateName);
span.setComponent(ComponentsDefine.ELASTIC_JOB);
span.tag(Tags.ofKey("sharding_context"), shardingContext.toString());
Tags.LOGIC_ENDPOINT.set(span, Tags.VAL_LOCAL_SPAN_AS_LOGIC_ENDPOINT);

span.tag(TAG_ITEM, item == null ? "" : String.valueOf(item));
span.tag(TAG_TASK_ID, shardingContexts.getTaskId());
span.tag(TAG_SHARDING_TOTAL_COUNT, Integer.toString(shardingContexts.getShardingTotalCount()));
span.tag(TAG_SHARDING_ITEM_PARAMETERS, shardingContexts.getShardingItemParameters() == null ? "" : shardingContexts.getShardingItemParameters().toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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.network.language.agent.v3.SpanObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -69,13 +70,21 @@ public void assertSuccess() throws Throwable {
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segment);
assertNotNull(spans);
assertThat(spans.size(), is(1));
assertThat(spans.get(0).transform().getOperationName(), is("fooJob-test"));
assertThat(spans.get(0).transform().getComponentId(), is(24));
assertThat(spans.get(0).transform().getTags(0).getKey(), is("sharding_context"));
assertThat(spans.get(0)
.transform()
.getTags(0)
.getValue(), is("ShardingContext(jobName=fooJob, taskId=fooJob1, shardingTotalCount=2, jobParameter=, shardingItem=1, shardingParameter=test)"));

SpanObject.Builder builder = spans.get(0).transform();

assertThat(builder.getOperationName(), is("ElasticJob/fooJob"));
assertThat(builder.getComponentId(), is(24));
assertThat(builder.getTags(0).getKey(), is("x-le"));
assertThat(builder.getTags(0).getValue(), is("{\"logic-span\":true}"));
assertThat(builder.getTags(1).getKey(), is("item"));
assertThat(builder.getTags(1).getValue(), is("1"));
assertThat(builder.getTags(2).getKey(), is("taskId"));
assertThat(builder.getTags(2).getValue(), is("fooJob1"));
assertThat(builder.getTags(3).getKey(), is("shardingTotalCount"));
assertThat(builder.getTags(3).getValue(), is("2"));
assertThat(builder.getTags(4).getKey(), is("shardingItemParameters"));
assertThat(builder.getTags(4).getValue(), is("{1=test}"));
}

@Test
Expand All @@ -89,11 +98,21 @@ public void assertSuccessWithoutSharding() throws Throwable {
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segment);
assertNotNull(spans);
assertThat(spans.size(), is(1));
assertThat(spans.get(0).transform().getOperationName(), is("fooJob"));
assertThat(spans.get(0)
.transform()
.getTags(0)
.getValue(), is("ShardingContext(jobName=fooJob, taskId=fooJob0, shardingTotalCount=1, jobParameter=, shardingItem=0, shardingParameter=null)"));

SpanObject.Builder builder = spans.get(0).transform();

assertThat(builder.getOperationName(), is("ElasticJob/fooJob"));
assertThat(builder.getComponentId(), is(24));
assertThat(builder.getTags(0).getKey(), is("x-le"));
assertThat(builder.getTags(0).getValue(), is("{\"logic-span\":true}"));
assertThat(builder.getTags(1).getKey(), is("item"));
assertThat(builder.getTags(1).getValue(), is("0"));
assertThat(builder.getTags(2).getKey(), is("taskId"));
assertThat(builder.getTags(2).getValue(), is("fooJob0"));
assertThat(builder.getTags(3).getKey(), is("shardingTotalCount"));
assertThat(builder.getTags(3).getValue(), is("1"));
assertThat(builder.getTags(4).getKey(), is("shardingItemParameters"));
assertThat(builder.getTags(4).getValue(), is("{}"));
}

@Test
Expand Down
21 changes: 21 additions & 0 deletions test/plugin/scenarios/elasticjob-2.x-scenario/bin/startup.sh
Original file line number Diff line number Diff line change
@@ -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 -Dzookeeper.host=${ZK_ADDRESS} -jar ${agent_opts} -Dco.paralleluniverse.fibers.detectRunawayFibers=false ${home}/../libs/elasticjob-2.x-scenario.jar &
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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: elasticjob-2.x-scenario
segmentSize: ge 2
segments:
- segmentId: not null
spans:
- operationName: /elasticjob-2.x-scenario/case/call
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 1
isError: false
spanType: Entry
peer: ''
skipAnalysis: false
tags:
- {key: url, value: 'http://localhost:8080/elasticjob-2.x-scenario/case/call'}
- {key: http.method, value: GET}
refs:
- {parentEndpoint: ElasticJob/org.apache.skywalking.apm.testcase.elasticjob.job.DemoSimpleJob, networkAddress: 'localhost:8080', refType: CrossProcess, parentSpanId: 1, parentTraceSegmentId: not null, parentServiceInstance: not null, parentService: not null, traceId: not null}
- segmentId: not null
spans:
- operationName: /elasticjob-2.x-scenario/case/call
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Http
startTime: not null
endTime: not null
componentId: 12
isError: false
spanType: Exit
peer: localhost:8080
skipAnalysis: false
tags:
- {key: http.method, value: GET}
- {key: url, value: 'http://localhost:8080/elasticjob-2.x-scenario/case/call'}
- operationName: ElasticJob/org.apache.skywalking.apm.testcase.elasticjob.job.DemoSimpleJob
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Unknown
startTime: not null
endTime: not null
componentId: 24
isError: false
spanType: Local
peer: ''
skipAnalysis: false
tags:
- {key: x-le, value: '{"logic-span":true}'}
- {key: item, value: '0'}
- {key: taskId, value: not null}
- {key: shardingTotalCount, value: '1'}
- {key: shardingItemParameters, value: '{0=test}'}
28 changes: 28 additions & 0 deletions test/plugin/scenarios/elasticjob-2.x-scenario/configuration.yml
Original file line number Diff line number Diff line change
@@ -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.

type: jvm
entryService: http://localhost:8080/elasticjob-2.x-scenario/case/healthCheck
healthCheck: http://localhost:8080/elasticjob-2.x-scenario/case/healthCheck
startScript: ./bin/startup.sh
environment:
- ZK_ADDRESS=zk-server:2181
depends_on:
- zk-server
dependencies:
zk-server:
image: zookeeper:3.6.0
hostname: zk-server
132 changes: 132 additions & 0 deletions test/plugin/scenarios/elasticjob-2.x-scenario/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?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>elasticjob-2.x-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>
<!-- Cannot be testing elasticjob (2.0.0 ~ 2.0.2) because the job configuration class incompatible -->
<test.framework.version>2.0.3</test.framework.version>
<spring-boot-version>2.1.6.RELEASE</spring-boot-version>
<lombok.version>1.18.10</lombok.version>
<okhttp-version>3.0.0</okhttp-version>
</properties>

<name>skywalking-elasticjob-2.x-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>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>${test.framework.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp-version}</version>
</dependency>
</dependencies>

<build>
<finalName>elasticjob-2.x-scenario</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<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>
Loading

0 comments on commit afc0aef

Please sign in to comment.