Skip to content

Commit 30de4de

Browse files
author
Sergei Malafeev
authored
Merge pull request #606 from Duncan-tree-zhou/duncan/add_rule/quartz
Duncan/add rule/quartz
2 parents 3f2f292 + 63bef0b commit 30de4de

File tree

8 files changed

+257
-0
lines changed

8 files changed

+257
-0
lines changed

opentracing-specialagent/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@
491491
<optional>true</optional>
492492
<scope>provided</scope>
493493
</dependency>
494+
<dependency>
495+
<groupId>io.opentracing.contrib.specialagent.rule</groupId>
496+
<artifactId>quartz</artifactId>
497+
<version>${project.version}</version>
498+
<optional>true</optional>
499+
<scope>provided</scope>
500+
</dependency>
494501
</dependencies>
495502
</profile>
496503
<profile>

rule/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<module>pulsar-functions</module>
9898
<module>dynamic</module>
9999
<module>cxf</module>
100+
<module>quartz</module>
100101
</modules>
101102
<build>
102103
<resources>

rule/quartz/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SpecialAgent Rule for Quartz Scheduler
2+
3+
**Rule Name:** `org.quartz-scheduler:quartz`
4+
5+
## Compatibility
6+
7+
```xml
8+
<groupId>org.quartz-scheduler</groupId>
9+
<artifactId>quartz</artifactId>
10+
<version>[2.0.0,LATEST]</version>
11+
```

rule/quartz/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!--
2+
Copyright 2019 The OpenTracing Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project
17+
xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>io.opentracing.contrib.specialagent.rule</groupId>
23+
<artifactId>rule</artifactId>
24+
<version>1.7.5-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>quartz</artifactId>
27+
<name>SpecialAgent Rule for Quartz</name>
28+
<properties>
29+
<sa.rule.name>quartz</sa.rule.name>
30+
<min.version>2.0.0</min.version>
31+
<passCompatibility>org.quartz-scheduler:quartz:2.0.0</passCompatibility>
32+
</properties>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.quartz-scheduler</groupId>
36+
<artifactId>quartz</artifactId>
37+
<version>${min.version}</version>
38+
<optional>true</optional>
39+
<scope>provided</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-classic</artifactId>
44+
<version>1.2.3</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2019 The OpenTracing Authors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package io.opentracing.contrib.specialagent.rule.quartz;
17+
18+
import static net.bytebuddy.matcher.ElementMatchers.*;
19+
20+
import io.opentracing.contrib.specialagent.AgentRule;
21+
import net.bytebuddy.agent.builder.AgentBuilder;
22+
import net.bytebuddy.agent.builder.AgentBuilder.Transformer;
23+
import net.bytebuddy.asm.Advice;
24+
import net.bytebuddy.description.type.TypeDescription;
25+
import net.bytebuddy.dynamic.DynamicType.Builder;
26+
import net.bytebuddy.utility.JavaModule;
27+
28+
public class QuartzJobBeanAgentRule extends AgentRule {
29+
@Override
30+
public AgentBuilder buildAgentChainedGlobal1(final AgentBuilder builder) {
31+
return builder
32+
.type(hasSuperType(named("org.quartz.Job")).and(not(isInterface())))
33+
.transform(new Transformer() {
34+
@Override
35+
public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
36+
return builder.visit(advice(typeDescription).to(QuartzJobBeanAgentRule.class).on(named("execute")));
37+
}
38+
});
39+
}
40+
41+
@Advice.OnMethodEnter
42+
public static void enter(final @ClassName String className, final @Advice.Origin String origin, final @Advice.This Object thiz, final @Advice.Argument(value = 0) Object arg) {
43+
if (isAllowed(className, origin))
44+
QuartzjobAgentIntercept.enter(thiz, arg);
45+
}
46+
47+
@Advice.OnMethodExit(onThrowable = Throwable.class)
48+
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Thrown Throwable thrown) {
49+
if (isAllowed(className, origin))
50+
QuartzjobAgentIntercept.exit(thrown);
51+
}
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Copyright 2019 The OpenTracing Authors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package io.opentracing.contrib.specialagent.rule.quartz;
17+
18+
import io.opentracing.*;
19+
import io.opentracing.contrib.specialagent.LocalSpanContext;
20+
import io.opentracing.contrib.specialagent.OpenTracingApiUtil;
21+
import io.opentracing.tag.Tags;
22+
import io.opentracing.util.GlobalTracer;
23+
import org.quartz.JobExecutionContext;
24+
25+
public class QuartzjobAgentIntercept {
26+
static final String COMPONENT_NAME = "quartz-job";
27+
28+
public static void enter(final Object thiz, final Object args) {
29+
30+
JobExecutionContext jobExecutionContext = (JobExecutionContext)args;
31+
32+
final Tracer tracer = GlobalTracer.get();
33+
final Span span = tracer
34+
.buildSpan("quartz:" + jobExecutionContext.getJobDetail().getKey().getName())
35+
.withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME)
36+
.withTag("class", thiz.getClass().getName())
37+
.withTag("name", jobExecutionContext.getJobDetail().getKey().getName())
38+
.withTag("group", jobExecutionContext.getJobDetail().getKey().getGroup())
39+
.start();
40+
41+
final Scope scope = tracer.activateSpan(span);
42+
LocalSpanContext.set(COMPONENT_NAME, span, scope);
43+
}
44+
45+
public static void exit(final Throwable thrown) {
46+
final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME);
47+
if (context == null)
48+
return;
49+
50+
if (thrown != null)
51+
OpenTracingApiUtil.setErrorTag(context.getSpan(), thrown);
52+
53+
context.closeAndFinish();
54+
}
55+
56+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2019 The OpenTracing Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
io.opentracing.contrib.specialagent.rule.quartz.QuartzJobBeanAgentRule
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.opentracing.contrib.specialagent.rule.quartz;
2+
3+
4+
import io.opentracing.contrib.specialagent.AgentRunner;
5+
import io.opentracing.contrib.specialagent.TestUtil;
6+
import io.opentracing.mock.MockSpan;
7+
import io.opentracing.mock.MockTracer;
8+
import io.opentracing.tag.Tags;
9+
import org.junit.AfterClass;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.quartz.*;
14+
import org.quartz.impl.StdSchedulerFactory;
15+
16+
import java.util.List;
17+
import java.util.concurrent.TimeUnit;
18+
19+
import static org.awaitility.Awaitility.await;
20+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.quartz.JobBuilder.newJob;
24+
import static org.quartz.TriggerBuilder.newTrigger;
25+
26+
@RunWith(AgentRunner.class)
27+
public class QuartzJobTest {
28+
29+
static Scheduler scheduler = null;
30+
31+
@BeforeClass
32+
public static void beforeClass() throws Exception {
33+
scheduler = StdSchedulerFactory.getDefaultScheduler();
34+
scheduler.start();
35+
}
36+
37+
@Test
38+
public void test(final MockTracer tracer) throws SchedulerException {
39+
JobDetail job = newJob(TestJob.class)
40+
.withIdentity("job1", "group1")
41+
.build();
42+
Trigger trigger = newTrigger()
43+
.withIdentity("trigger1", "group1")
44+
.startNow()
45+
.build();
46+
scheduler.scheduleJob(job, trigger);
47+
await().atMost(10, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), greaterThanOrEqualTo(1));
48+
final List<MockSpan> spans = tracer.finishedSpans();
49+
assertTrue(spans.size() >= 1);
50+
for (final MockSpan span : spans) {
51+
assertEquals("quartz-job", span.tags().get(Tags.COMPONENT.getKey()));
52+
}
53+
}
54+
55+
@AfterClass
56+
public static void afterClass() throws Exception {
57+
scheduler.start();
58+
}
59+
60+
public static class TestJob implements Job {
61+
@Override
62+
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
63+
System.out.println("test job.");
64+
}
65+
}
66+
67+
}

0 commit comments

Comments
 (0)