Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for ignoring certain monitor slugs #2943

Merged
merged 25 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3b2debe
checkin
adinauer Sep 11, 2023
1de0ba7
Add trace context, release and env; factory methods; fix serializatio…
adinauer Sep 12, 2023
3dd4fce
add tests
adinauer Sep 13, 2023
41cd16d
remove serial version uid
adinauer Sep 13, 2023
97b8e63
Merge branch 'main' into feat/crons
adinauer Sep 13, 2023
083e021
add back serial version uid
adinauer Sep 13, 2023
4828fa3
quartz support for spring jakarta
adinauer Sep 15, 2023
ef54e76
changelog
adinauer Sep 15, 2023
cc23d3c
code review changes; add sample code to spring scheduled job
adinauer Sep 15, 2023
6038dc5
Merge branch 'feat/crons' into feat/crons-quartz
adinauer Sep 15, 2023
44a3c75
add checkin example to scheduled job
adinauer Sep 15, 2023
b558570
Merge branch 'feat/crons' into feat/crons-quartz
adinauer Sep 15, 2023
0c29f12
Move quartz job listener into a separate module
adinauer Sep 18, 2023
24215e5
add quartz auto config to spring boot 2
adinauer Sep 19, 2023
68931af
Merge branch 'main' into feat/crons-quartz
adinauer Sep 19, 2023
a709a40
fix comment
adinauer Sep 19, 2023
372a2f0
Add enableAutomaticCheckIns and ignoreCheckIns options
adinauer Sep 20, 2023
66cb754
move __ slug logic
adinauer Sep 20, 2023
f4fb1dd
fix sample by adding quartz lib
adinauer Sep 20, 2023
2e7fb74
Merge branch 'feat/crons-quartz' into feat/crons-options
adinauer Sep 20, 2023
86e5ded
changelog
adinauer Sep 20, 2023
c420272
code review changes
adinauer Sep 22, 2023
fc2a884
ignore unused monitors
adinauer Sep 22, 2023
b2290f5
changelog
adinauer Sep 25, 2023
15d9388
Merge branch 'feat/crons-lib-support' into feat/crons-options
adinauer Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .craft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ targets:
maven:io.sentry:sentry-apollo:
maven:io.sentry:sentry-jdbc:
maven:io.sentry:sentry-graphql:
# maven:io.sentry:sentry-quartz:
maven:io.sentry:sentry-android-navigation:
maven:io.sentry:sentry-compose:
maven:io.sentry:sentry-compose-android:
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report_java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ body:
- sentry-logback
- sentry-log4j2
- sentry-graphql
- sentry-quartz
- sentry-openfeign
- sentry-apache-http-client-5
- other
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Add `sendModules` option for disable sending modules ([#2926](https://github.com/getsentry/sentry-java/pull/2926))
- Send `db.system` and `db.name` in span data for androidx.sqlite spans ([#2928](https://github.com/getsentry/sentry-java/pull/2928))
- Add API for sending checkins (CRONS) manually ([#2935](https://github.com/getsentry/sentry-java/pull/2935))
- Automatic CRON checkins for Quartz ([#2940](https://github.com/getsentry/sentry-java/pull/2940))

### Fixes

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Sentry SDK for Java and Android
| sentry-log4j2 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-log4j2/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-log4j2) |
| sentry-bom | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-bom/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-bom) |
| sentry-graphql | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-graphql/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-graphql) |
| sentry-quartz | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-quartz/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-quartz) |
| sentry-openfeign | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-openfeign/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-openfeign) |
| sentry-opentelemetry-agent | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-opentelemetry-agent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-opentelemetry-agent) |
| sentry-opentelemetry-agentcustomization | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-opentelemetry-agentcustomization/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-opentelemetry-agentcustomization) |
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/java/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ object Config {

val springBootStarter = "org.springframework.boot:spring-boot-starter:$springBootVersion"
val springBootStarterGraphql = "org.springframework.boot:spring-boot-starter-graphql:$springBootVersion"
val springBootStarterQuartz = "org.springframework.boot:spring-boot-starter-quartz:$springBootVersion"
val springBootStarterTest = "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
val springBootStarterWeb = "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
val springBootStarterWebsocket = "org.springframework.boot:spring-boot-starter-websocket:$springBootVersion"
Expand All @@ -85,6 +86,7 @@ object Config {

val springBoot3Starter = "org.springframework.boot:spring-boot-starter:$springBoot3Version"
val springBoot3StarterGraphql = "org.springframework.boot:spring-boot-starter-graphql:$springBoot3Version"
val springBoot3StarterQuartz = "org.springframework.boot:spring-boot-starter-quartz:$springBoot3Version"
val springBoot3StarterTest = "org.springframework.boot:spring-boot-starter-test:$springBoot3Version"
val springBoot3StarterWeb = "org.springframework.boot:spring-boot-starter-web:$springBoot3Version"
val springBoot3StarterWebsocket = "org.springframework.boot:spring-boot-starter-websocket:$springBoot3Version"
Expand Down Expand Up @@ -128,6 +130,8 @@ object Config {

val graphQlJava = "com.graphql-java:graphql-java:17.3"

val quartz = "org.quartz-scheduler:quartz:2.3.0"

val kotlinReflect = "org.jetbrains.kotlin:kotlin-reflect"
val kotlinStdLib = "org.jetbrains.kotlin:kotlin-stdlib"

Expand Down Expand Up @@ -227,6 +231,7 @@ object Config {
val SENTRY_APOLLO3_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.apollo3"
val SENTRY_APOLLO_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.apollo"
val SENTRY_GRAPHQL_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.graphql"
val SENTRY_QUARTZ_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.quartz"
val SENTRY_JDBC_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.jdbc"
val SENTRY_SERVLET_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.servlet"
val SENTRY_SERVLET_JAKARTA_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.servlet.jakarta"
Expand Down
16 changes: 16 additions & 0 deletions sentry-quartz/api/sentry-quartz.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public final class io/sentry/quartz/BuildConfig {
public static final field SENTRY_QUARTZ_SDK_NAME Ljava/lang/String;
public static final field VERSION_NAME Ljava/lang/String;
}

public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener {
public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String;
public static final field SENTRY_CHECK_IN_SLUG_KEY Ljava/lang/String;
public fun <init> ()V
public fun <init> (Lio/sentry/IHub;)V
public fun getName ()Ljava/lang/String;
public fun jobExecutionVetoed (Lorg/quartz/JobExecutionContext;)V
public fun jobToBeExecuted (Lorg/quartz/JobExecutionContext;)V
public fun jobWasExecuted (Lorg/quartz/JobExecutionContext;Lorg/quartz/JobExecutionException;)V
}

83 changes: 83 additions & 0 deletions sentry-quartz/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import net.ltgt.gradle.errorprone.errorprone
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`java-library`
kotlin("jvm")
jacoco
id(Config.QualityPlugins.errorProne)
id(Config.QualityPlugins.gradleVersions)
id(Config.BuildPlugins.buildConfig) version Config.BuildPlugins.buildConfigVersion
}

configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
kotlinOptions.languageVersion = Config.kotlinCompatibleLanguageVersion
}

dependencies {
api(projects.sentry)
compileOnly(Config.Libs.quartz)

compileOnly(Config.CompileOnly.nopen)
errorprone(Config.CompileOnly.nopenChecker)
errorprone(Config.CompileOnly.errorprone)
errorprone(Config.CompileOnly.errorProneNullAway)
compileOnly(Config.CompileOnly.jetbrainsAnnotations)

// tests
testImplementation(projects.sentry)
testImplementation(projects.sentryTestSupport)
testImplementation(kotlin(Config.kotlinStdLib))
testImplementation(Config.TestLibs.kotlinTestJunit)
testImplementation(Config.TestLibs.mockitoKotlin)
testImplementation(Config.TestLibs.mockitoInline)
}

configure<SourceSetContainer> {
test {
java.srcDir("src/test/java")
}
}

jacoco {
toolVersion = Config.QualityPlugins.Jacoco.version
}

tasks.jacocoTestReport {
reports {
xml.required.set(true)
html.required.set(false)
}
}

tasks {
jacocoTestCoverageVerification {
violationRules {
rule { limit { minimum = Config.QualityPlugins.Jacoco.minimumCoverage } }
}
}
check {
dependsOn(jacocoTestCoverageVerification)
dependsOn(jacocoTestReport)
}
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone {
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "io.sentry")
}
}

buildConfig {
useJavaOutput()
packageName("io.sentry.quartz")
buildConfigField("String", "SENTRY_QUARTZ_SDK_NAME", "\"${Config.Sentry.SENTRY_QUARTZ_SDK_NAME}\"")
buildConfigField("String", "VERSION_NAME", "\"${project.version}\"")
}
227 changes: 227 additions & 0 deletions sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package io.sentry.quartz;

import io.sentry.BuildConfig;
import io.sentry.CheckIn;
import io.sentry.CheckInStatus;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.MonitorConfig;
import io.sentry.MonitorSchedule;
import io.sentry.MonitorScheduleUnit;
import io.sentry.Sentry;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.SentryLevel;
import io.sentry.protocol.SentryId;
import io.sentry.util.Objects;
import java.util.List;
import java.util.TimeZone;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quartz.CalendarIntervalTrigger;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.JobListener;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

public final class SentryJobListener implements JobListener {

public static final String SENTRY_CHECK_IN_ID_KEY = "sentry-checkin-id";
public static final String SENTRY_CHECK_IN_SLUG_KEY = "sentry-checkin-slug";

private final @NotNull IHub hub;

public SentryJobListener() {
this(HubAdapter.getInstance());
}

public SentryJobListener(final @NotNull IHub hub) {
this.hub = Objects.requireNonNull(hub, "hub is required");
SentryIntegrationPackageStorage.getInstance().addIntegration("Quartz");
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-quartz", BuildConfig.VERSION_NAME);
}

@Override
public String getName() {
return "sentry-job-listener";
}

@Override
public void jobToBeExecuted(JobExecutionContext context) {
try {
if (isDisabled()) {
return;
}
final @NotNull String slug = getSlug(context.getJobDetail());
final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS);

final @Nullable MonitorConfig monitorConfig = extractMonitorConfig(context);
if (monitorConfig != null) {
checkIn.setMonitorConfig(monitorConfig);
}

final @NotNull SentryId checkInId = Sentry.captureCheckIn(checkIn);
context.put(SENTRY_CHECK_IN_ID_KEY, checkInId);
context.put(SENTRY_CHECK_IN_SLUG_KEY, slug);
} catch (Throwable t) {
Sentry.getCurrentHub()
.getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Unable to capture check-in in jobToBeExecuted.", t);
}
}

private @NotNull String getSlug(final @Nullable JobDetail jobDetail) {
if (jobDetail == null) {
return "fallback";
}
final @NotNull StringBuilder slugBuilder = new StringBuilder();

final @Nullable JobKey key = jobDetail.getKey();
if (key != null) {
slugBuilder.append(key.getName());
slugBuilder.append("__");
}

final @Nullable Class<? extends Job> jobClass = jobDetail.getJobClass();
if (jobClass != null) {
slugBuilder.append(jobClass.getCanonicalName());
}

return slugBuilder.toString();
}

private @Nullable MonitorConfig extractMonitorConfig(final @NotNull JobExecutionContext context) {
@Nullable MonitorSchedule schedule = null;
@Nullable String cronExpression = null;
@Nullable TimeZone timeZone = TimeZone.getDefault();
@Nullable Integer repeatInterval = null;
@Nullable MonitorScheduleUnit timeUnit = null;

try {
List<? extends Trigger> triggersOfJob =
context.getScheduler().getTriggersOfJob(context.getTrigger().getJobKey());
for (Trigger trigger : triggersOfJob) {
if (trigger instanceof CronTrigger) {
final CronTrigger cronTrigger = (CronTrigger) trigger;
cronExpression = cronTrigger.getCronExpression();
timeZone = cronTrigger.getTimeZone();
} else if (trigger instanceof SimpleTrigger) {
final SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
long tmpRepeatInterval = simpleTrigger.getRepeatInterval();
repeatInterval = millisToMinutes(Double.valueOf(tmpRepeatInterval));
timeUnit = MonitorScheduleUnit.MINUTE;
} else if (trigger instanceof CalendarIntervalTrigger) {
final CalendarIntervalTrigger calendarIntervalTrigger = (CalendarIntervalTrigger) trigger;
DateBuilder.IntervalUnit repeatIntervalUnit =
calendarIntervalTrigger.getRepeatIntervalUnit();
int tmpRepeatInterval = calendarIntervalTrigger.getRepeatInterval();
if (DateBuilder.IntervalUnit.SECOND.equals(repeatIntervalUnit)) {
repeatInterval = secondsToMinutes(Double.valueOf(tmpRepeatInterval));
timeUnit = MonitorScheduleUnit.MINUTE;
} else if (DateBuilder.IntervalUnit.MILLISECOND.equals(repeatIntervalUnit)) {
repeatInterval = millisToMinutes(Double.valueOf(tmpRepeatInterval));
timeUnit = MonitorScheduleUnit.MINUTE;
} else {
repeatInterval = tmpRepeatInterval;
timeUnit = convertUnit(repeatIntervalUnit);
}
}
}
} catch (Throwable t) {
Sentry.getCurrentHub()
.getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Unable to extract monitor config for check-in.", t);
}
if (cronExpression != null) {
schedule = MonitorSchedule.crontab(cronExpression);
} else if (repeatInterval != null && timeUnit != null) {
schedule = MonitorSchedule.interval(repeatInterval.intValue(), timeUnit);
}

if (schedule != null) {
final @Nullable MonitorConfig monitorConfig = new MonitorConfig(schedule);
if (timeZone != null) {
monitorConfig.setTimezone(timeZone.getID());
}
return monitorConfig;
} else {
return null;
}
}

private @Nullable Integer millisToMinutes(final @NotNull Double milis) {
return Double.valueOf((milis / 1000.0) / 60.0).intValue();
}

private @Nullable Integer secondsToMinutes(final @NotNull Double seconds) {
return Double.valueOf(seconds / 60.0).intValue();
}

private @Nullable MonitorScheduleUnit convertUnit(
final @Nullable DateBuilder.IntervalUnit intervalUnit) {
if (intervalUnit == null) {
return null;
}

if (DateBuilder.IntervalUnit.MINUTE.equals(intervalUnit)) {
return MonitorScheduleUnit.MINUTE;
} else if (DateBuilder.IntervalUnit.HOUR.equals(intervalUnit)) {
return MonitorScheduleUnit.HOUR;
} else if (DateBuilder.IntervalUnit.DAY.equals(intervalUnit)) {
return MonitorScheduleUnit.DAY;
} else if (DateBuilder.IntervalUnit.WEEK.equals(intervalUnit)) {
return MonitorScheduleUnit.WEEK;
} else if (DateBuilder.IntervalUnit.MONTH.equals(intervalUnit)) {
return MonitorScheduleUnit.MONTH;
} else if (DateBuilder.IntervalUnit.YEAR.equals(intervalUnit)) {
return MonitorScheduleUnit.YEAR;
}

return null;
}

@Override
public void jobExecutionVetoed(JobExecutionContext context) {
// do nothing
}

@Override
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
try {
if (isDisabled()) {
return;
}

final @Nullable Object checkInIdObjectFromContext = context.get(SENTRY_CHECK_IN_ID_KEY);
final @Nullable Object slugObjectFromContext = context.get(SENTRY_CHECK_IN_SLUG_KEY);
final @NotNull SentryId checkInId =
checkInIdObjectFromContext == null
? new SentryId()
: (SentryId) checkInIdObjectFromContext;
final @Nullable String slug =
slugObjectFromContext == null ? null : (String) slugObjectFromContext;
if (slug != null) {
final boolean isFailed = jobException != null;
final @NotNull CheckInStatus status = isFailed ? CheckInStatus.ERROR : CheckInStatus.OK;
Sentry.captureCheckIn(new CheckIn(checkInId, slug, status));
}
} catch (Throwable t) {
Sentry.getCurrentHub()
.getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t);
}
}

private boolean isDisabled() {
return !hub.getOptions().isEnableAutomaticCheckIns();
}
}