View a markdown version of this page

Profile Java workloads on Kubernetes - Amazon CloudWatch

Profile Java workloads on Kubernetes

Turn on Java profiling with the CloudWatch Observability EKS add-on or the amazon-cloudwatch-observability Helm chart, and send continuous profiles from your Java workloads on Kubernetes to Amazon CloudWatch Profiles.

The add-on and the Helm chart use the same profiling setting. Only how you install the setting differs. Choose the variant that matches your cluster:

  • Amazon EKS add-on – For Amazon EKS clusters that use the CloudWatch Observability EKS add-on. You set the profiling value in the advanced configuration of the add-on.

  • Helm chart – For other Kubernetes clusters, and for Amazon EKS clusters that use the Helm chart instead of the add-on. You set the profiling value in the Helm chart.

Where a step differs between the two variants, this tutorial labels each variant.

When you finish, your Java pods collect their own profiles. The AWS Distro for OpenTelemetry (ADOT) Java agent in each pod sends profiles in OpenTelemetry Protocol (OTLP) format to the CloudWatch agent on its node. The CloudWatch agent signs each request to the CloudWatch profiles endpoint with AWS Signature Version 4 (SigV4), using the credentials of its IAM role, such as the node role or the instance role of your nodes. CloudWatch stores the profiles under the service.name of each workload, with cluster, namespace, and pod labels. You don't rebuild images or change entry points.

Time to complete: About 15 minutes, plus the time to restart your workloads

Cost to complete: There is no charge for Amazon CloudWatch Profiles during the preview. Standard Amazon EC2 charges apply for the cluster that you run, and Amazon EKS charges apply if the cluster runs on Amazon EKS.

Prerequisites

Before you begin, make sure that you have the following:

  • Java workloads that run on Linux nodes in a Kubernetes cluster: an Amazon EKS cluster if you use the add-on, or any Kubernetes cluster if you use the Helm chart.

  • The ADOT Java agent version 2.32.0 or later.

  • The CloudWatch agent version 1.300075.0 or later.

  • The CloudWatchAgentServerPolicy managed policy attached to the IAM role that the CloudWatch agent uses, such as the node role or the instance role of your nodes. This policy includes the cloudwatch:PutProfileRecords permission that the signing identity needs. For more information, see IAM permissions required.

  • Credentials with the cloudwatch:GetProfileRecords permission to view the profile data. For more information, see IAM permissions required.

Amazon EKS add-on variant – If you use the CloudWatch Observability EKS add-on, you also need the following:

  • An Amazon EKS cluster with Linux nodes.

  • The CloudWatch Observability EKS add-on (amazon-cloudwatch-observability) version v8.0.1-eksbuild.1 or later, installed on the cluster. Earlier versions reject the profiling setting. To see the installed version, run aws eks describe-addon --cluster-name cluster-name --addon-name amazon-cloudwatch-observability --query addon.addonVersion. For instructions, see Install the CloudWatch agent with the Amazon CloudWatch Observability EKS add-on or the Helm chart. For more information about the add-on, see OTel Container Insights (Recommended).

  • The add-on version v8.0.1-eksbuild.1 installs the CloudWatch agent version 1.300075.0 or later by default.

Helm chart variant – If you use the Helm chart, you also need the following:

  • A Kubernetes cluster with Linux nodes, and the amazon-cloudwatch-observability Helm chart version 8.0.0 or later installed. For instructions, see Enable your applications on Kubernetes.

  • The OTLP endpoint of the Helm chart turned on. This endpoint is on by default. Profiling requires otlp.enabled, otlp.http.enabled, and agent.enabled set to true, and an entry in agents for the node agent.

Step 1: Turn on Java profiling

Set manager.autoInstrumentationConfiguration.java.profiling.enabled to the string "true". Follow the variant that matches your cluster: Amazon EKS add-on variant: Turn on Java profiling with the CloudWatch Observability EKS add-on or Helm chart variant: Turn on Java profiling with the Helm chart.

Profiles go to the OTLP endpoint that the CloudWatch agent serves on every node, on HTTP port 4318 by default. The add-on and the Helm chart turn on this endpoint by default, whether or not you turn on profiling. Profiling doesn't change the receivers of the agent. Before you continue, read Restrict access to the OTLP ports.

By default, the add-on and the chart turn on wall-clock and memory profiling. The profiler collects wall-clock profiles, which sample threads both on and off the CPU, and memory allocation profiles. The ADOT Java agent leaves memory profiling off by default, so the operator sets OTEL_AWS_PROFILER_MEMORY_ENABLED to true in each profiled container. A value that the container sets takes precedence. To turn off memory profiling for a container, set OTEL_AWS_PROFILER_MEMORY_ENABLED to false on the container. To change other settings, see Opt out, turn off, and tune profiling.

Amazon EKS add-on variant: Turn on Java profiling with the CloudWatch Observability EKS add-on

Important

The value must be the string "true", in quotation marks, not a boolean.

The configuration values that you pass to the add-on replace its earlier configuration values. If the add-on already has configuration values, merge them with the profiling setting.

Amazon EKS console
To turn on Java profiling with the Amazon EKS console
  1. Open the Amazon EKS console at https://console.aws.amazon.com/eks/home#/clusters.

  2. Choose the name of your cluster.

  3. Choose the Add-ons tab.

  4. Select the Amazon CloudWatch Observability add-on, and then choose Edit.

  5. For Version, choose v8.0.1-eksbuild.1 or later.

  6. Expand Optional configuration settings.

  7. For Configuration values, enter the following JSON. If the field already contains configuration values, add the manager key to them instead of replacing them.

    {"manager":{"autoInstrumentationConfiguration":{"java":{"profiling":{"enabled":"true"}}}}}
  8. Choose Save changes.

AWS CLI

To turn on Java profiling with the AWS CLI, run the following command. Replace cluster-name with the name of your Amazon EKS cluster. If the add-on already has configuration values, merge them into the JSON.

aws eks update-addon \ --cluster-name cluster-name \ --addon-name amazon-cloudwatch-observability \ --addon-version addon-version \ --configuration-values '{"manager":{"autoInstrumentationConfiguration":{"java":{"profiling":{"enabled":"true"}}}}}'

Replace addon-version with v8.0.1-eksbuild.1 or a later version. If your add-on is earlier, this command also updates it, which updates the CloudWatch agent and operator images.

If the command fails because of a conflict with existing settings, you can add --resolve-conflicts OVERWRITE to the command. Before you use this option, make sure that the Amazon EKS add-on doesn't manage settings that you need to self-manage.

Helm chart variant: Turn on Java profiling with the Helm chart

Important

If you pass a boolean, the install fails with an error that tells you to use --set-string.

The only setting under java.profiling is enabled. Any other key also makes the install fail.

To turn on Java profiling with the Helm chart
  • Run the following command. Replace cluster-name with the name of your cluster, and the example Region (us-east-1) with your own Region. If you installed the chart under a different release name, use that name. If you installed the chart with other values, include them in this command too.

    helm upgrade --install amazon-cloudwatch-observability \ aws-observability/amazon-cloudwatch-observability \ --namespace amazon-cloudwatch \ --set clusterName=cluster-name \ --set region=us-east-1 \ --set-string manager.autoInstrumentationConfiguration.java.profiling.enabled=true

    The --set-string option passes the value as a string.

Step 2: Verify that profiles arrive in CloudWatch

Before you verify, restart your Java workloads so that new pods pick up the setting. Profiling applies only to new pods.

To restart a Java workload
  1. Restart the workload. Replace deployment-name and namespace with the values for your workload.

    kubectl rollout restart deployment/deployment-name -n namespace
  2. Wait for the rollout to finish.

    kubectl rollout status deployment/deployment-name -n namespace

First confirm that the new pods have profiling turned on. Then confirm that CloudWatch receives the profile data. The first profiles arrive a few minutes after the pods start.

To verify that profiles arrive in CloudWatch
  1. Check that a new pod has the OTEL_AWS_PROFILER_ENABLED environment variable. Replace pod-name and namespace with the values for one of the restarted pods.

    kubectl get pod pod-name -n namespace -o yaml | grep -A 1 OTEL_AWS_PROFILER

    The output shows the following environment variables:

    • OTEL_AWS_PROFILER_ENABLED, with the value true

    • OTEL_AWS_PROFILER_MEMORY_ENABLED, with the value true unless the container sets it

    • OTEL_AWS_PROFILER_ENDPOINT, which points to the CloudWatch agent

    • OTEL_AWS_PROFILER_DATA_DIR

    • OTEL_AWS_PROFILER_MAX_PAYLOAD_BYTES

    If the container sets its own OTEL_AWS_PROFILER_ENDPOINT, the output doesn't show OTEL_AWS_PROFILER_MAX_PAYLOAD_BYTES. You can also run kubectl describe pod and check the environment and volumes of the application container.

  2. Find your workload and its flame graph in CloudWatch. For instructions, see Verify that profile data is arriving. Look for the service.name of your workload, and the following profile types:

    • wall:wall:nanoseconds:wall:nanoseconds

    • memory:alloc_objects:count:alloc_objects:count

    • memory:alloc_space:bytes:alloc_space:bytes

    The wall-clock flame graph includes threads that wait or sleep, not only threads that run on the CPU.

Opt out, turn off, and tune profiling

After you turn on profiling, you can exclude individual containers, turn profiling off for the cluster, or change how the ADOT Java agent profiles.

Opt out a container – To exclude a container, set OTEL_AWS_PROFILER_ENABLED to a value other than "true", such as "false". Set it in the env of the container, or in a ConfigMap that the container loads with envFrom. The operator then doesn't add the profiling variables or the volume mount to that container. A value from a Secret doesn't opt the container out.

The following command sets the variable on a container of a deployment. Changing the pod template starts a rollout, so the new pods start without profiling.

kubectl set env deployment/deployment-name -n namespace \ -c container-name OTEL_AWS_PROFILER_ENABLED=false

Turn off profiling – To turn off profiling for the cluster, set manager.autoInstrumentationConfiguration.java.profiling.enabled to the string "false" in the same way as in Step 1, in the configuration values of the add-on or with the Helm command. Then restart your workloads.

Tune profiling – To change how the ADOT Java agent profiles, such as the profiling mode, memory profiling, or the profile length, set OTEL_AWS_PROFILER_* environment variables on the application container. The add-on and the chart don't have settings for these options. For the variables and their defaults, see the ADOT Java agent in Profile your application.

The following example sets the profiling mode in a container spec.

env: - name: OTEL_AWS_PROFILER_MODE value: "cpu"

Longer windows and memory profiling use more disk space. If a container needs more than 160 MiB, mount your own volume at a path other than /otel-profiler-data-java. Then set OTEL_AWS_PROFILER_DATA_DIR to a directory on that volume.

Clean up

To stop profiling, turn it off as described in Opt out, turn off, and tune profiling. Then restart your Java workloads. Replace deployment-name and namespace with the values for your workload.

kubectl rollout restart deployment/deployment-name -n namespace

If you created a cluster only for this tutorial, delete the cluster to stop its charges. Standard Amazon EC2 charges apply for the cluster, and Amazon EKS charges apply if the cluster runs on Amazon EKS.

Considerations

Keep the following in mind when you turn on Java profiling:

  • Profiling is experimental. Profile attributes and service names might change.

  • Profiling supports HotSpot Java virtual machines (JVMs) on Linux x64 and arm64.

  • The operator adds a disk-backed emptyDir volume named opentelemetry-profiler-data-java to each profiled pod. It mounts the volume at /otel-profiler-data-java in each profiled container, and sets OTEL_AWS_PROFILER_DATA_DIR to /otel-profiler-data-java/container-name. Because of this volume, profiling works in containers that use readOnlyRootFilesystem.

  • The size limit of the volume is 160 MiB for each profiled container. If the profiler data exceeds the limit, Kubernetes evicts the pod.

  • Only new pods are profiled. Restart a workload to start or stop profiling.

  • If a container already sets a profiling variable in env, with valueFrom, or in a ConfigMap that it loads with envFrom, the operator keeps the value of the container. The injected value overrides a value that comes from a Secret in envFrom.

  • The operator doesn't profile the following containers. The pod still starts, and the operator log shows Skipping Java profiling injection with the reason.

    • Containers in Windows pods.

    • Containers in pods on AWS Fargate. These pods don't have a CloudWatch agent on their node.

    • Containers that might send OTLP data over gRPC and don't set their own OTEL_AWS_PROFILER_ENDPOINT. This applies when OTEL_EXPORTER_OTLP_PROTOCOL is set to grpc in any letter case in env or envFrom, or comes from valueFrom. It also applies when JAVA_TOOL_OPTIONS contains -Dotel.exporter.otlp.protocol=grpc.

    • Containers in pods that already have a volume named opentelemetry-profiler-data-java, and containers that already mount a volume with that name or at /otel-profiler-data-java.

  • If you turn on autoMonitor.monitorAllServices, the operator profiles every Java workload behind a Service.

  • Profiles travel unencrypted inside the cluster, from the pod to the CloudWatch agent on its node.

  • Many busy profiled JVMs on one node increase the memory use of the CloudWatch agent. If needed, raise the memory limit in agent.resources.

Restrict access to the OTLP ports

Important

Profiling uses the OTLP endpoint of the CloudWatch agent on each node. The add-on and the Helm chart turn on this endpoint by default, whether or not you turn on profiling. The endpoint listens on ports 4317 (gRPC) and 4318 (HTTP) without Transport Layer Security (TLS) or authentication. The agent uses the host network, so it listens on every node IP address. It accepts OTLP metrics, logs, traces, and profiles.

Kubernetes network policies usually don't apply to pods that use the host network, such as the CloudWatch agent. Restrict access to these ports with node security groups or a host firewall. Never expose the ports outside your cluster's network.

With the Helm chart, if you don't need the endpoint, set otlp.enabled to false. This also turns off Java profiling, which requires the endpoint. If manager.autoInstrumentationConfiguration.java.profiling.enabled is "true", set it to "false" too, or the install fails.

Troubleshoot profiling on Kubernetes

The following issues can occur when you turn on Java profiling with the CloudWatch Observability EKS add-on or the Helm chart.

  • The add-on update fails (Amazon EKS add-on variant)

    Check that the configuration values are valid JSON, that enabled is the string "true", and that java.profiling contains no other keys. If the update fails because of a conflict, see the --resolve-conflicts option in Step 1.

  • The Helm install fails (Helm chart variant)

    The chart checks the profiling setting and fails the install with an error. Check the error message:

    • The message tells you to use --set-string. You passed a boolean instead of a string. Pass the value with --set-string.

    • The message says that a key is not a setting. java.profiling accepts only enabled. Remove the other key. To tune the profiler, set OTEL_AWS_PROFILER_* variables in the env of the container.

    • The message says that profiling requires the node agent's OTLP HTTP endpoint. Set otlp.enabled, otlp.http.enabled, and agent.enabled to true. Keep the entry in agents whose name matches agent.name.

  • No profile data in CloudWatch

    Check the following causes:

    • The pod started before you turned on profiling. Restart the workload, and check the pod for OTEL_AWS_PROFILER_ENABLED.

    • The operator skipped the container. Check the operator log for Skipping Java profiling injection and the reason. For the conditions, see Considerations.

    • The container sets OTEL_AWS_PROFILER_ENABLED to a value other than true. Remove the variable from the container, or set it to true.

    • The container sets its own OTEL_AWS_PROFILER_DATA_DIR to a directory that isn't writable. Point it to a writable volume, or remove it.

    • The CloudWatch agent version is earlier than 1.300075.0, so the agent doesn't accept profiles. Your application keeps running. Update the add-on, or upgrade the agent.

    • The node role doesn't have the cloudwatch:PutProfileRecords permission. Attach the CloudWatchAgentServerPolicy managed policy to the role that the CloudWatch agent uses.

  • CloudWatch agent pods are Pending or don't start

    Check the following causes:

    • Another process on the node uses the OTLP host ports, so the agent pod stays Pending. Change the ports with otlp.grpc.port and otlp.http.port, and then restart your Java workloads.

    • A custom agent configuration opens its own OTLP receiver on otlp.grpc.port or otlp.http.port. The agent can't bind the port, and it doesn't start. The chart doesn't check for this conflict. Move the custom receiver to other ports, or remove it.

  • Kubernetes evicts profiled pods

    The profiler data exceeds the size limit of the opentelemetry-profiler-data-java volume, which is 160 MiB for each profiled container. Longer profile windows and memory profiling use more space. Mount your own volume at a path other than /otel-profiler-data-java, and set OTEL_AWS_PROFILER_DATA_DIR to a directory on it.

Next steps

Now that profile data arrives in CloudWatch, you can do the following:

For more information, see the following topics: