Marcio Cunha

How to export Prometheus metrics to JSON in terminal

Learn the objective technique to extract monitoring data directly from the Prometheus API using curl and jq for quick audits.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The Prometheus HTTP API exposes structured endpoints that bypass heavy browsers for rapid diagnostics.
  • The curl command retrieves raw monitoring payloads automatically right from the command line.
  • The jq tool processes complex filters and formats raw responses into a readable JSON tree.
  • Targeted spot audits eliminate the need to open sluggish dashboards during high-criticality moments.
  • Well-directed Instant Query requests reduce network consumption and isolate exact system metrics.

Why extract Prometheus metrics through the terminal

Prometheus is a popular monitoring tool that stores system usage data, such as server processor and memory consumption. During slowdowns or severe failures, opening complex graphical interfaces can be slow and inefficient. Knowing alternative pathways helps with troubleshooting, especially when we need to cross infrastructure data with other tools, much like when we convert Nginx logs to JSON via terminal for unified analysis.

In practice, this means we can check the exact state of any application in a fraction of a second using only native command-line tools. This direct approach prevents operational bottlenecks and ensures the engineer has immediate access to raw numbers, without depending on dashboards that might be offline precisely when a system goes down.

Understanding the Prometheus HTTP API

Behind every visual interface of Prometheus lies a robust HTTP API, a communication channel that allows other software to talk to the metrics database. Whenever a dashboard refreshes, it sends requests to this very API. Understanding this mechanism opens doors for automations and extremely agile manual checks during infrastructure audits.

There are two main types of queries we can make via the API: the Instant Query, which returns the current value of a metric at a given millisecond, and the Range Query, which pulls history over time. For quick audits and current status checks, the instant query is the ideal choice because it is lighter and gets straight to the point.

Making the first request with the curl command

The curl command is a universal utility present in almost all modern operating systems, used to transfer data across web addresses. It serves as our initial bridge to fetch content generated by the monitoring server. If you have ever needed to investigate a container in CrashLoopBackOff error that needs debugging on Linux, you know the terminal is the best place for quick answers.

To fetch a specific metric, we point curl to the default Prometheus port, usually 9090. Here is how to structure the basic command in your terminal:

curl -G 'http://localhost:9090/api/v1/query' --data-urlencode 'query=up'

This command performs a search for the up indicator, which tells us whether the monitored services are responding or down. The --data-urlencode parameter ensures that special characters in our search do not break the request when transmitted over the network.

Formatting raw output with the jq tool

The standard return from the Prometheus API comes in JSON format, but in a single, densely packed block that makes direct human reading difficult. This is where the jq utility comes in, a structured file processor that acts as a Swiss Army knife to slice and colorize data directly on the black screen of the terminal.

By combining curl with jq, we transform an unreadable response into an organized, colorful structure. Here is a practical example of how to apply this combination in your day-to-day systems administration:

curl -sG 'http://localhost:9090/api/v1/query' --data-urlencode 'query=up' | jq '.'

The vertical bar character, known as a pipe, takes the output generated by the previous command and immediately hands it over for jq processing. The dot inside single quotes instructs the tool to display all content applying indentation and visual color highlighting.

We can go further and extract only the useful part of the response, ignoring unnecessary metadata that clutters the screen during an urgent audit. The jq filter allows us to navigate through JSON document keys in a surgical manner:

curl -sG 'http://localhost:9090/api/v1/query' --data-urlencode 'query=node_memory_Active_bytes' | jq '.data.result[] | {metric: .metric.instance, value: .value[1]}'

This refined command isolates the monitored machine's address and the exact active memory value, discarding repetitive information and delivering a clean report to the operator.

Automating quick infrastructure audits

When we need to audit dozens of servers, creating small command-line scripts saves hours of repetitive work. We can save extracted data to local files or pass them through additional filters to identify which network nodes are exceeding resource usage limits.

This flexibility turns the terminal into a personal, customizable control panel, perfectly adapted to each engineer's workflow. Instead of navigating extensive menus, a single command saved in history resolves environmental health checks.

Final considerations

Directly extracting Prometheus metrics using simple system commands represents a valuable skill for any technology professional. Mastering the combined use of web requests and structured text processors guarantees total autonomy in the face of complex infrastructure failures. With this lean approach, diagnostics that once required heavy tools are resolved instantly right on the terminal screen.