Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Profiling Java workloads with the Cryostat Agent

June 29, 2023
Andrew Azores
Related topics:
Java
Related products:
Red Hat build of Cryostat

    In Cryostat 2.3, we deliver the first iteration of a long-requested feature: a JDK Instrumentation Agent for profiling Java workloads using JFR.

    In this article we will look at a brief overview or refresher of what an Instrumentation Agent is, what specifically the Cryostat Agent does, reasons you should consider instrumenting your containerized applications with the Cryostat Agent, and finally a brief example of how to include the Cryostat Agent into a Quarkus application.

    What is an Agent?

    The JDK Instrumentation API allows additional software packages, called Agents, to be included with or attached to applications, as optional modular components. Agents can be specified by the application’s startup command line with the -javaagent flag, or they can dynamically attach to a running application process. In either scenario, the Agent can transform the host application’s bytecode - for example, to inject observability hooks or to patch application behaviour - or extend the application with additional functionality.

    This latter mechanism is interesting for Cryostat, since it allows an Agent to act as a sort of plugin to applications, enhancing them with additional capabilities by simply including the Agent rather than needing to directly implement those capabilities. Specifically, the Cryostat Agent:

    1. Implements the Cryostat Discovery Plugin interface. The SmallRye config property cryostat.agent.callback must be set [1]. This value defines the HTTP URL where the Agent instance can be located on the network, and the Agent uses this URL to talk to the Cryostat server instance and inform Cryostat of where it can find this Agent. For example, set the environment variable CRYOSTAT_AGENT_CALLBACK=https://myapp.example.com:8080/.
    2. Provides a readonly HTTP API alternative for Cryostat functionality, removing the requirement for target applications to expose a JMX port. The cryostat.agent.callback URL above is also used as the Agent HTTP API URL if the config property cryostat.agent.registration.prefer-jmx is false (which is the default). If the application JVM does not have JMX enabled then the Agent will always publicize itself to Cryostat using the readonly HTTP API URL. If JMX is enabled then the Agent will still prefer to publicize its HTTP API URL, but this config property can be used to force publication of a read/write JMX Service URL instead.
    3. Can be configured to collect JFR data from the application JVM and periodically push it to the Cryostat backend. This is conceptually similar to Cryostat’s Automated Rules feature, but using an Agent-initiated data push model, rather than Automated Rules’ Cryostat-initiated data pull model. This JFR harvesting also allows the Agent to collect the “tail end” of JFR data as the application shuts down and push that to Cryostat as an on-exit data dump, so cases of abnormal application shutdown can be detected and investigated from the Cryostat console.

    [1] SmallRye config properties are most conveniently set by JVM system property, ex. -Dcryostat.agent.callback="http://myapp.pod.mycluster:9999", or by environment variable transformation ex. CRYOSTAT_AGENT_CALLBACK="http://myapp.pod.mycluster:9999"

    Why should you use the Cryostat Agent?

    So why use the Cryostat Agent with your applications? Let’s start with a brief recap of why to use Cryostat:

    1. To discover and map out your Java applications and their deployment topology
    2. To gather JDK Flight Recorder data from your applications
    3. To store that JFR data in an in-cluster volume
    4. To perform online analysis of that JFR data without the raw data leaving the cluster

    As alluded to previously, Cryostat has historically required target applications to expose a JMX port. Cryostat talks to the application JVM over this JMX port to start and stop JFR recordings and to pull JFR files over the network, enabling the ability to store and analyze this JFR data. But, JMX has a very large surface area, can be difficult to secure, and may be undesirable even behind the curtains of an OpenShift internal network service.

    Since the Cryostat Agent exposes a small, readonly HTTP API, it can be much easier to audit and secure than a JMX port. The agent does not allow for remote dynamic start/stop of JFR recordings, nor does it allow for remote pulling of JFR data. Instead, SmallRye config properties must configure a Cryostat server URL to communicate with and a schedule for pushing collected JFR recordings. The Cryostat Agent knows how to register itself with the Cryostat server as a Discovery Plugin, how to publish a description of itself to the server, how to respond to requests for information about what JFR event types, event templates, and recordings are present in the application JVM, and how to secure its HTTP API so that only the Cryostat server it has registered with is able to request that information. This way, the Cryostat console can still be used to visualize the deployment topology including Cryostat Agent instances. The JFR data pushed by Agents is properly associated with the Agent that pushed it, and that data is stored in the same volume that Cryostat normally uses. Of course, Cryostat’s online analysis tools can be used on this JFR data regardless of whether it was pulled over JMX or pushed over HTTP.

    How do you use the Cryostat Agent?

    So how does one get started using the Cryostat Agent? Let’s use a Quarkus application as an example. Applications built on frameworks other than Quarkus, or with tools other than Maven, should be similar. First, we will modify the application pom.xml to add a dependency on the Cryostat Agent:

     <groupId>org.acme</groupId>
      <artifactId>code-with-quarkus</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <repositories>
        <repository>
        <id>github</id>
        <url>https://maven.pkg.github.com/cryostatio/cryostat-agent</url>
        </repository>
      </repositories>
       …
      <properties>
       …
        <dependency-plugin.version>3.3.0</dependency-plugin.version>
    
        <io.cryostat.agent.version>0.2.0-SNAPSHOT</io.cryostat.agent.version>
      </properties>
       …
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${dependency-plugin.version}</version>
            <executions>
          	  <execution>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy</goal>
                </goals>
                <configuration>
                <artifactItems>
                  <artifactItem>
                  <groupId>io.cryostat</groupId>
                  <artifactId>cryostat-agent</artifactId>
                  <version>${io.cryostat.agent.version}</version>
                  </artifactItem>
                </artifactItems>
                <stripVersion>true</stripVersion>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>

    These three sections define the GitHub Packages repository to pull the upstream Cryostat Agent build from, define the Agent version to use and the version of a build plugin for packaging it with our application, and finally configure the build plugin to actually include the Agent in our application build output as a separate JAR.

    Next, let’s modify the Quarkus Dockerfile.jvm:

    FROM registry.access.redhat.com/ubi8/openjdk-17:1.14
    
    ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'
    
    # We make four distinct layers so if there are application changes the library layers can be re-used
    COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
    COPY --chown=185 target/quarkus-app/*.jar /deployments/
    COPY --chown=185 target/quarkus-app/app/ /deployments/app/
    COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/
    
    # add the Cryostat Agent JAR to the application image
    COPY --chown=185 target/dependency/cryostat-agent.jar /deployments/app/
    
    USER 185
    # Add the -javaagent flag so that the JVM loads the Cryostat Agent on startup
    ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -javaagent:/deployments/app/cryostat-agent.jar"
    ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
    

    The last COPY ensures that the Cryostat Agent JAR is included in the application image, and the JAVA_OPTS sets the default launch command line to include a flag to load the Agent at application startup. When we deploy this application, we must also set the SmallRye config properties cryostat.agent.callback and cryostat.agent.baseuri separately as mentioned above.

    Congratulations, you have successfully bundled your application with the Cryostat Agent and can now enjoy the many benefits of doing so:

    • providing a simple readonly HTTP API for retrieving recording data without exposing a potentially unsafe JMX port
    • allowing Cryostat to discover your application
    • enabling additional features on your application such as pushing collected JFR data to Cryostat on application shutdown
    • pushing collected JFR data to Cryostat on a fixed schedule for monitoring
    Last updated: August 12, 2024
    Disclaimer: Please note the content in this blog post has not been thoroughly reviewed by the Red Hat Developer editorial team. Any opinions expressed in this post are the author's own and do not necessarily reflect the policies or positions of Red Hat.

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.