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 Java 1.8) or above

Install the Sentry SDK for your Java logging framework or application:

build.gradle
plugins {
  id "io.sentry.jvm.gradle" version "6.27.0"
}
dependencies {
  implementation '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.

Want to learn more about these features?
  • Issues (always enabled): Sentry's core error monitoring product that automatically reports errors, uncaught exceptions, and crashes. If you have something that looks like an exception, Sentry can capture it.
  • Tracing: Track software performance while seeing the impact of errors across multiple systems. For example, distributed tracing allows you to follow a request from the frontend to the backend and back.
  • Profiling: Gain deeper insight than traditional tracing without custom instrumentation, letting you discover slow-to-execute or resource-intensive functions in your app.
  • Logs: Centralize and analyze your application logs to correlate them with errors and performance issues. Search, filter, and visualize log data to understand what's happening in your applications.
  • Application Metrics (always enabled): Track and analyze custom application metrics, such as response times and database query durations, to understand trends and patterns in your application's performance and behavior over time.

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

The SDK can be configured using a sentry.properties file.

The snippet also turns on:

sentry.properties
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

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:

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);
});

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"
);

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")
    )
  )
);

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);
}

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).

Need help locating captured data in your Sentry project?
  • Open the Issues page and select an error from the issues list to view the full details and context of this error. For more details, see the Issue Details documentation.
  • Open the Traces page and select a trace to reveal more information about each span, its duration, and any errors. For an interactive UI walkthrough, click here.
  • Open the Profiles page to see your slowest functions and an aggregate flame graph of your most frequent code paths. If tracing is enabled, select a transaction to see the flame graph for that part of your app. For more information, click here.
  • Open the Logs page and filter by service, environment, or search keywords to view log entries from your application. For an interactive UI walkthrough, click here.
  • Open the Application Metrics page to view and analyze your metrics. For more details, see this interactive walkthrough.

  • Explore practical guides on what to monitor, log, track, and investigate after setup
Are you having problems setting up the SDK or integration?
Was this helpful?
Help improve this content
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").