Skip to main content
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 Red Hat 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

    • Red Hat OpenShift AI
    • 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.

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

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

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

How to use Cryostat agent to profile Java workloads

August 15, 2023
Andrew Azores
Related topics:
ContainersJavaQuarkus
Related products:
Red Hat OpenShift

    In Cryostat 2.3, we deliver the first iteration of a long-time requested feature, the JDK instrumentation agent for profiling Java workloads using JFR. This article provides a brief overview of an instrumentation agent, what the Cryostat agent does, reasons to instrument your containerized applications with the Cryostat agent, and finally, an 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 to inject observability hooks, to patch application behavior, or extend the application with additional functionality.

    The latter mechanism is interesting for Cryostat, since it allows an agent to act as a plugin to applications, enhancing them with additional capabilities by including the agent rather than directly implementing those capabilities.

    The Cryostat agent:

    • Implements the Cryostat Discovery Plugin interface. The SmallRye config property cryostat.agent.callback must be set. (SmallRye config properties are most conveniently set by JVM system property -Dcryostat.agent.callback="http://myapp.pod.mycluster:9999" or by environment variable transformation CRYOSTAT_AGENT_CALLBACK="http://myapp.pod.mycluster:9999"). 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 to inform Cryostat of where to find this agent. For example, set the environment variable CRYOSTAT_AGENT_CALLBACK=https://myapp.example.com:8080/.
    • 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.
    • 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.

    Why use Cryostat agent

    Why should you 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 previously mentioned, 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 a Red Hat 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 to use Cryostat agent

    How does one get started with 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, 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 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 previously mentioned.

    The benefits of using Cryostat agent

    Congratulations! You have successfully bundled your application with the Cryostat agent. Now, you can enjoy the many benefits such as:

    • Providing a simple read-only 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: September 19, 2023

    Related Posts

    • What's new in Red Hat build of Cryostat 2.3

    • Managing Java containers with Quarkus and Podman Desktop

    • Introduction to Cryostat: JDK Flight Recorder for containers

    • 10 reasons to develop Quarkus applications on Red Hat OpenShift

    Recent Posts

    • A day in the life of an NVIDIA H100 on Red Hat OpenShift 5

    • Migrate BuildConfigs to builds for Red Hat OpenShift with one plug-in install

    • Run Red Hat Enterprise Linux CoreOS on NVIDIA BlueField DPUs

    • Agent harness engineering: Separating model capabilities from enforcement

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    What’s up next?

    Learn how to optimize Java for today’s compute and runtime demands with Quarkus. Quarkus for Spring Developers introduces Quarkus to Java developers, with a special eye for helping those familiar with Spring’s conventions make a quick and easy transition.

    Get the e-book
    Red Hat Developers logo

    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
    Ask AI