Sentry for Java
Learn how to set up Sentry in your Java application and capture your first events.
Sentry's Java SDK enables capturing sessions for Release health as well as reporting messages and errors.
Sentry for Java is a collection of modules provided by Sentry. At its core, Sentry for Java provides a raw client for sending events to Sentry. To begin, we highly recommend you use one of the logging libraries or framework integrations.
The Sentry Java SDK can be used with Kotlin, Scala, and other JVM languages. Code examples are typically provided in both Java and Kotlin.
You need:
- A Sentry account and project
- Your application up and running
- Java version
8(also known as Java1.8) or above
Install the Sentry SDK for your Java logging framework or application:
build.gradleplugins {
id "io.sentry.jvm.gradle" version "6.27.0"
}
dependencies {
implementation 'io.sentry:sentry-async-profiler:8.65.1'
}
plugins {
id "io.sentry.jvm.gradle" version "6.27.0"
}
dependencies {
implementation 'io.sentry:sentry-async-profiler:8.65.1'
}
<plugin>
<groupId>io.sentry</groupId>
<artifactId>sentry-maven-plugin</artifactId>
<version>0.12.0</version>
<!-- Required to allow auto-install of Sentry SDK and Integrations -->
<extensions>true</extensions>
<configuration>
<!-- In case you're self hosting, provide the URL here -->
<!-- <url>http://localhost:8000/</url> -->
<org>___SENTRY_ORG_SLUG___</org>
<project>___SENTRY_PROJECT_SLUG___</project>
<!-- Do not commit your auth token with this file, you should provide it via the SENTRY_AUTH_TOKEN environment variable or similar -->
<!-- <authToken>${env.SENTRY_AUTH_TOKEN}</authToken> -->
<authToken>___SENTRY_AUTH_TOKEN___</authToken>
<!-- Enable debugging to see logs in case something goes wrong when uploading the source bundle -->
<debugSentryCli>true</debugSentryCli>
</configuration>
<executions>
<execution>
<goals>
<!-- Generates a source bundle and uploads it to Sentry -->
<!-- This enables source context, allowing you to see your source code as part of your stack traces in Sentry -->
<!-- Learn more about this feature in its dedicated "Source Context" docs page -->
<goal>uploadSourceBundle</goal>
<!-- Validates Sentry SDK dependency versions. -->
<!-- Mixing SDK dependency versions can result in build or run time errors. -->
<!-- If mixed versions are detected, the build will fail. -->
<goal>validateSdkDependencyVersions</goal>
</goals>
</execution>
</executions>
</plugin>
<dependencies>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-async-profiler</artifactId>
<version>8.65.1</version>
</dependency>
</dependencies>
libraryDependencies += "io.sentry" % "sentry" % "8.65.1"
libraryDependencies += "io.sentry" % "sentry-async-profiler" % "8.65.1"
We recommend using our Gradle plugin as it can add integrations and provide source context for events.
If you are manually adding multiple Sentry dependencies, you can add a bill of materials to avoid specifying the version of each dependency.
Error Monitoring starts once Sentry is configured. Application Metrics need no extra configuration, and you'll learn how to send them in step #4. This guide also configures Tracing, Logs, and Profiling.
Don't need a feature? Remove its configuration from the SDK initialization. To disable Profiling, also remove the sentry-async-profiler dependency. Profiling is not supported on Windows.
Sentry's OpenTelemetry agent attaches when the JVM starts and is configured through a sentry.properties file.
Download the latest version of the sentry-opentelemetry-agent-8.65.1.jar from MavenCentral:
curl https://repo1.maven.org/maven2/io/sentry/sentry-opentelemetry-agent/8.65.1/sentry-opentelemetry-agent-8.65.1.jar -o sentry-opentelemetry-agent-8.65.1.jar
curl https://repo1.maven.org/maven2/io/sentry/sentry-opentelemetry-agent/8.65.1/sentry-opentelemetry-agent-8.65.1.jar -o sentry-opentelemetry-agent-8.65.1.jar
The SDK can be configured using a sentry.properties file.
The snippet also turns on:
- Tracing: via
traces-sample-rate - Profiling: via
profile-session-sample-rateandprofile-lifecycle - Data Collection: via
data-collection - Logs: via
logs.enabled
sentry.propertiesdsn=___PUBLIC_DSN___
# Use Data Collection defaults but don't collect automatic user information.
data-collection.user-info=false
# Configure other data categories here.
# Enable tracing
traces-sample-rate=1.0
# Enable logs
logs.enabled=true
# Enable profiling
profile-session-sample-rate=1.0
profile-lifecycle=TRACE
dsn=___PUBLIC_DSN___
# Use Data Collection defaults but don't collect automatic user information.
data-collection.user-info=false
# Configure other data categories here.
# Enable tracing
traces-sample-rate=1.0
# Enable logs
logs.enabled=true
# Enable profiling
profile-session-sample-rate=1.0
profile-lifecycle=TRACE
Then run your application with:
SENTRY_PROPERTIES_FILE=sentry.properties JAVA_TOOL_OPTIONS="-javaagent:sentry-opentelemetry-agent-8.65.1.jar" java -jar your-application.jar
SENTRY_PROPERTIES_FILE=sentry.properties JAVA_TOOL_OPTIONS="-javaagent:sentry-opentelemetry-agent-8.65.1.jar" java -jar your-application.jar
SENTRY_PROPERTIES_FILE=sentry.properties java -javaagent:sentry-opentelemetry-agent-8.65.1.jar -jar your-application.jar
See the agent setup guide or agentless setup guide for details.
The following setup initializes the Java SDK without the OpenTelemetry agent. Configuration should happen as early as possible in your application's lifecycle.
The snippet also turns on:
- Tracing: via
tracesSampleRate - Profiling: via
profileSessionSampleRateandprofileLifecycle - Data Collection: via
dataCollection - Logs: via
logs.enabled
import io.sentry.ProfileLifecycle;
import io.sentry.Sentry;
Sentry.init(options -> {
options.setDsn("___PUBLIC_DSN___");
// Use Data Collection defaults but don't collect automatic user information.
options.getDataCollection().forceDataCollection();
options.getDataCollection().setUserInfo(false);
// Configure other data categories here.
// Set setTracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production.
options.setTracesSampleRate(1.0);
// Enable profiling
options.setProfileSessionSampleRate(1.0);
options.setProfileLifecycle(ProfileLifecycle.TRACE);
// Enable logs
options.getLogs().setEnabled(true);
});
import io.sentry.ProfileLifecycle;
import io.sentry.Sentry;
Sentry.init(options -> {
options.setDsn("___PUBLIC_DSN___");
// Use Data Collection defaults but don't collect automatic user information.
options.getDataCollection().forceDataCollection();
options.getDataCollection().setUserInfo(false);
// Configure other data categories here.
// Set setTracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production.
options.setTracesSampleRate(1.0);
// Enable profiling
options.setProfileSessionSampleRate(1.0);
options.setProfileLifecycle(ProfileLifecycle.TRACE);
// Enable logs
options.getLogs().setEnabled(true);
});
import io.sentry.ProfileLifecycle
import io.sentry.Sentry
Sentry.init { options ->
options.dsn = "___PUBLIC_DSN___"
// Use Data Collection defaults but don't collect automatic user information.
options.dataCollection.forceDataCollection()
options.dataCollection.userInfo = false
// Configure other data categories here.
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production.
options.tracesSampleRate = 1.0
// Enable profiling
options.profileSessionSampleRate = 1.0
options.profileLifecycle = ProfileLifecycle.TRACE
// Enable logs
options.logs.enabled = true
}
Use Sentry.logger() to send logs at different levels, such as info, warn, or error. You can pass any attributes you want and search or filter by them later. See Set Up Logs for all available logging methods and the syntax for formatting messages.
import io.sentry.Sentry;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
import io.sentry.SentryLogLevel;
import io.sentry.logger.SentryLogParameters;
Sentry.logger().info("A simple log message");
Sentry.logger().error("A %s log message", "formatted");
// attach custom attributes with Sentry.logger().log()
Sentry.logger().log(
SentryLogLevel.FATAL,
SentryLogParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("my.string-attribute", "some-value"),
SentryAttribute.named("my.attribute", new Point(1, 2))
)
),
"log message %s",
"param1"
);
import io.sentry.Sentry;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
import io.sentry.SentryLogLevel;
import io.sentry.logger.SentryLogParameters;
Sentry.logger().info("A simple log message");
Sentry.logger().error("A %s log message", "formatted");
// attach custom attributes with Sentry.logger().log()
Sentry.logger().log(
SentryLogLevel.FATAL,
SentryLogParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("my.string-attribute", "some-value"),
SentryAttribute.named("my.attribute", new Point(1, 2))
)
),
"log message %s",
"param1"
);
import io.sentry.Sentry
import io.sentry.SentryAttribute
import io.sentry.SentryAttributes
import io.sentry.SentryLogLevel
import io.sentry.logger.SentryLogParameters
Sentry.logger().info("A simple log message")
Sentry.logger().error("A %s log message", "formatted")
// attach custom attributes with Sentry.logger().log()
Sentry.logger().log(
SentryLogLevel.FATAL,
SentryLogParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("my.string-attribute", "some-value"),
SentryAttribute.named("my.attribute", Point(1, 2))
)
),
"log message %s",
"param1"
)
We also provide integrations for:
Use Sentry.metrics() to send counters, gauges, and distributions from your app to Sentry. You can pass any attributes you want and search or filter by them later.
See Set Up Metrics for all metric types and configuration options.
import io.sentry.Sentry;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
import io.sentry.metrics.MetricsUnit;
import io.sentry.metrics.SentryMetricsParameters;
Sentry.metrics().count("button_click", 1.0);
Sentry.metrics().gauge("queue_depth", 42.0);
// pass additional attributes via SentryMetricsParameters
Sentry.metrics().distribution(
"response_time",
1.0,
MetricsUnit.Duration.MILLISECOND,
SentryMetricsParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("browser", "Firefox")
)
)
);
import io.sentry.Sentry;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
import io.sentry.metrics.MetricsUnit;
import io.sentry.metrics.SentryMetricsParameters;
Sentry.metrics().count("button_click", 1.0);
Sentry.metrics().gauge("queue_depth", 42.0);
// pass additional attributes via SentryMetricsParameters
Sentry.metrics().distribution(
"response_time",
1.0,
MetricsUnit.Duration.MILLISECOND,
SentryMetricsParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("browser", "Firefox")
)
)
);
import io.sentry.Sentry
import io.sentry.SentryAttribute
import io.sentry.SentryAttributes
import io.sentry.metrics.MetricsUnit
import io.sentry.metrics.SentryMetricsParameters
Sentry.metrics().count("button_click", 1.0)
Sentry.metrics().gauge("queue_depth", 42.0)
// pass additional attributes via SentryMetricsParameters
Sentry.metrics().distribution(
"response_time",
1.0,
MetricsUnit.Duration.MILLISECOND,
SentryMetricsParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("browser", "Firefox")
)
)
)
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
Add this intentional error to your application:
import io.sentry.Sentry;
try {
throw new Exception("This is a test.");
} catch (Exception e) {
Sentry.captureException(e);
}
import io.sentry.Sentry;
try {
throw new Exception("This is a test.");
} catch (Exception e) {
Sentry.captureException(e);
}
import io.sentry.Sentry
try {
throw Exception("This is a test.")
} catch (e: Exception) {
Sentry.captureException(e)
}
Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).
- Explore practical guides on what to monitor, log, track, and investigate after setup
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").