Skip to content

Commit

Permalink
added tests for scheduler/quartz OTel integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik committed Nov 28, 2023
1 parent f0b9cd7 commit f4167af
Show file tree
Hide file tree
Showing 23 changed files with 1,012 additions and 0 deletions.
144 changes: 144 additions & 0 deletions integration-tests/opentelemetry-quartz/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-integration-test-opentelemetry-quartz</artifactId>
<name>Quarkus - Integration Tests - OpenTelemetry Quartz</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-quartz</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>

<!-- Needed for InMemorySpanExporter to verify captured traces -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-quartz-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.it.opentelemetry.quartz;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/scheduler/count")
public class CountResource {

@Inject
Counter counter;

@Inject
ManualScheduledCounter manualScheduledCounter;

@Inject
JobDefinitionCounter jobDefinitionCounter;

@GET
@Produces(MediaType.TEXT_PLAIN)
public Integer getCount() {
return counter.get();
}

@GET
@Path("manual")
@Produces(MediaType.TEXT_PLAIN)
public Integer getManualCount() {
return manualScheduledCounter.get();
}

@GET
@Path("job-definition")
@Produces(MediaType.TEXT_PLAIN)
public Integer getJobDefinitionCount() {
return jobDefinitionCounter.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.it.opentelemetry.quartz;

import java.util.concurrent.atomic.AtomicInteger;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;

import io.quarkus.scheduler.Scheduled;

@ApplicationScoped
public class Counter {

AtomicInteger counter;

@PostConstruct
void init() {
counter = new AtomicInteger();
}

public int get() {
return counter.get();
}

@Scheduled(cron = "*/1 * * * * ?", identity = "myCounter")
void increment() throws InterruptedException {
Thread.sleep(100l);
counter.incrementAndGet();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.it.opentelemetry.quartz;

import java.util.List;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.data.SpanData;

@Path("")
public class ExporterResource {
@Inject
InMemorySpanExporter inMemorySpanExporter;

@GET
@Path("/export")
public List<SpanData> export() { // only export scheduled spans
return inMemorySpanExporter.getFinishedSpanItems()
.stream()
.filter(sd -> !sd.getName().contains("export") && !sd.getName().contains("GET"))
.collect(Collectors.toList());
}

@ApplicationScoped
static class InMemorySpanExporterProducer {
@Produces
@Singleton
InMemorySpanExporter inMemorySpanExporter() {
return InMemorySpanExporter.create();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.it.opentelemetry.quartz;

import jakarta.enterprise.context.ApplicationScoped;

import io.quarkus.scheduler.Scheduled;

@ApplicationScoped
public class FailedBasicScheduler {

@Scheduled(cron = "*/1 * * * * ?", identity = "myFailedBasicScheduler")
void init() throws InterruptedException {
Thread.sleep(100l);
throw new RuntimeException("error occurred in myFailedBasicScheduler.");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.it.opentelemetry.quartz;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import io.quarkus.quartz.QuartzScheduler;
import io.quarkus.runtime.Startup;

@ApplicationScoped
@Startup
public class FailedJobDefinitionScheduler {

@Inject
QuartzScheduler scheduler;

@PostConstruct
void init() {
scheduler.newJob("myFailedJobDefinition").setCron("*/1 * * * * ?").setTask(ex -> {
try {
Thread.sleep(100l);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
throw new RuntimeException("error occurred in myFailedJobDefinition.");
}).schedule();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.it.opentelemetry.quartz;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import io.quarkus.runtime.Startup;
import io.quarkus.runtime.annotations.RegisterForReflection;

@Startup
@ApplicationScoped
public class FailedManualScheduler {
@Inject
org.quartz.Scheduler quartz;

@PostConstruct
void init() throws SchedulerException {
JobDetail job = JobBuilder.newJob(CountingJob.class).withIdentity("myFailedManualJob", "myFailedGroup").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("myFailedTrigger", "myFailedGroup")
.startNow()
.withSchedule(SimpleScheduleBuilder
.simpleSchedule()
.repeatForever()
.withIntervalInSeconds(1))
.build();
quartz.scheduleJob(job, trigger);
}

@RegisterForReflection
public static class CountingJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
try {
Thread.sleep(100l);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
throw new RuntimeException("error occurred in myFailedManualJob.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.it.opentelemetry.quartz;

import java.util.concurrent.atomic.AtomicInteger;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import io.quarkus.quartz.QuartzScheduler;
import io.quarkus.runtime.Startup;

@ApplicationScoped
@Startup
public class JobDefinitionCounter {

@Inject
QuartzScheduler scheduler;

AtomicInteger counter;

@PostConstruct
void init() {
counter = new AtomicInteger();
scheduler.newJob("myJobDefinition").setCron("*/1 * * * * ?").setTask(ex -> {
try {
Thread.sleep(100l);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
counter.incrementAndGet();
}).schedule();
}

public int get() {
return counter.get();
}
}
Loading

0 comments on commit f4167af

Please sign in to comment.