Scrape Prometheus metrics on Amazon EC2 instances
The CloudWatch agent can scrape applications that expose metrics in the Prometheus format and send the metrics to the CloudWatch OTLP endpoint. You can then query the metrics with PromQL. Both Linux and Windows Server instances are supported.
To scrape Prometheus metrics, you need two files: a standard Prometheus scrape
configuration file, and a CloudWatch agent configuration file whose opentelemetry
section has a prometheus source that points to the scrape configuration
file.
Topics
Prerequisites
-
Install CloudWatch agent version 1.300070.0 or later on the instance. For more information, see Prerequisites.
-
If the agent scrapes other instances, the security groups of those instances must allow inbound traffic on the Prometheus port from the agent's private IP address. The agent's security group must allow outbound traffic to that port.
-
If you use
ec2_sd_configsto discover targets, the IAM role of the instance where the agent runs must allowec2:DescribeInstances.
Creating the Prometheus scrape configuration
The agent supports the standard Prometheus scrape configuration. For more
information about the scrape configuration, see <scrape_config>etc directory, for example
/opt/aws/amazon-cloudwatch-agent/etc/prometheus.yaml on Linux or
C:\ProgramData\Amazon\AmazonCloudWatchAgent\prometheus.yaml on
Windows Server.
The following example scrapes a local application every minute:
global: scrape_interval: 1m scrape_timeout: 10s scrape_configs: - job_name: my-app sample_limit: 10000 static_configs: - targets: ['127.0.0.1:9404'] labels: team: payments
The file contains the following sections:
-
global– Defaults for all scrape jobs.scrape_intervalsets how often to scrape targets, andscrape_timeoutsets how long to wait before a scrape request times out. -
scrape_configs– One entry for each scrape job.job_namenames the job. The agent sets the@resource.service.namelabel on the scraped metrics to this value.sample_limitlimits the number of samples that the agent accepts from each scrape.
To tell the agent which targets to scrape, use one of the following service discovery types in each job:
-
static_configs– A fixed list of targets, and labels to add to all metrics from those targets, as in the previous example. -
file_sd_configs– Reads targets from one or more files. The agent rereads the files when they change, so you can update targets without restarting the agent.scrape_configs: - job_name: my-app file_sd_configs: - files: ['/opt/aws/amazon-cloudwatch-agent/etc/prometheus_sd_1.yaml']Each file lists targets and labels:
- targets: - 127.0.0.1:9404 labels: team: payments -
ec2_sd_configs– Discovers targets from the Amazon EC2 API. The following example scrapes port 9404 on two instances. If you omitregion, the agent uses the Region from the instance metadata.scrape_configs: - job_name: my-app ec2_sd_configs: - region:us-east-1port: 9404 filters: - name: instance-id values: -i-98765432109876543-i-12345678901234567For more information about the filters that you can use, see DescribeInstances in the Amazon EC2 API Reference.
Configuring and starting the agent
-
In the agent configuration file, add a
prometheussource to theopentelemetrysection, and setconfig_pathto the scrape configuration file.{ "opentelemetry": { "collect": { "host_metrics": {}, "prometheus": { "config_path": "/opt/aws/amazon-cloudwatch-agent/etc/prometheus.yaml" } } } }For the other sources that you can add, see Create a CloudWatch agent configuration for OpenTelemetry.
-
Start the agent with the configuration file. On Linux:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/path/to/config.json-sFor other platforms, see Starting the CloudWatch agent.
-
In the CloudWatch console, open Query Studio and run a PromQL query for a metric that your application exposes, filtered by the job name. For example:
{"my_metric", "@resource.service.name"="my-app"}Metrics typically appear within 1–2 minutes. For more information, see Running PromQL queries in Query Studio.
Example: Scrape JVM metrics with the Prometheus JMX exporter
This example runs a Java application with the Prometheus JMX exporter, which exposes JVM metrics such as memory, garbage collection, and threads in the Prometheus format. The agent scrapes the exporter and sends the metrics to CloudWatch.
-
Download the JMX exporter Java agent from the jmx_exporter
project on the GitHub website. -
Create an exporter configuration file, for example
config.yaml. The following configuration exports all MBeans:rules: - pattern: ".*" -
Start the application with the exporter as a Java agent that listens on port 9404:
java -javaagent:/path/to/jmx_prometheus_javaagent.jar=9404:/path/to/config.yaml-jarmy-app.jar -
Confirm that the exporter returns metrics:
curl http://localhost:9404/metricsThe output is plain text in the Prometheus exposition format, with lines that start with
jvm_. -
Create a scrape configuration with a job for the exporter, and then configure and start the agent as described in Configuring and starting the agent.
global: scrape_interval: 1m scrape_configs: - job_name: java-app static_configs: - targets: ['127.0.0.1:9404'] -
In Query Studio, query a JVM metric from the exporter with
"@resource.service.name"="java-app".
For Java applications, you can also use the jmx source in the Classic
configuration. For more information, see Collect Java Management Extensions (JMX) metrics.