Prometheus Literals

In Prometheus, literals refer to the constant values that can be used in queries and alerting rules. Here are some common literals used in Prometheus:

1. Numeric Literals: These include integers and floating-point numbers. For example:

- 42: An integer literal.

- 3.14: A floating-point literal.

2. String Literals: These are enclosed in double quotes ("). For example:

- "hello": A string literal containing the text "hello".

3. Boolean Literals: These represent the truth values of true and false.

4. Duration Literals: These represent time durations. They can be expressed using a combination of numbers and units. For example:

- 5s: A duration of 5 seconds.

- 2h30m: A duration of 2 hours and 30 minutes.

5. Time Literals: These represent specific points in time and are formatted using the ISO 8601 standard. For example:

- 2023-07-02T12:34:56Z: A time literal representing July 2, 2023, at 12:34:56 UTC.

6. Regular Expression Literals: These are enclosed in slashes (`/`). They are used for pattern matching in regular expression-based queries and alerting rules. For example:

- /^http/: A regular expression literal that matches strings starting with "http".

7. Range Vector Selector Literals: These are used to select a range of time series data. They use the square bracket notation. For example:

- my_metric[5m]: A range vector selector literal that selects data for the metric "my_metric" over the past 5 minutes.

These are some of the common literals used in Prometheus. They provide the necessary flexibility to construct queries and alerting rules based on different types of data and time periods.

Examples

Here are examples of Prometheus queries for each type of literal:

1. Numeric Literal:

- Query:

http_requests_total > 100

- Description: This query checks if the metric http_requests_total is greater than 100.

2. String Literal:

- Query:

http_response_code{code="200"}

- Description: This query selects time series where the http_response_code metric has the label code with a value of "200".

3. Boolean Literal:

- Query:

up == 1

- Description: This query selects time series where the up metric has a value of 1, indicating that the target is up and running.

4. Duration Literal:

- Query:

rate(my_metric[5m])

- Description: This query calculates the per-second rate of the metric my_metric over the past 5 minutes.

5. Time Literal:

- Query:

http_requests_total{instance="webserver1"}[1h]

- Description: This query selects the time series for http_requests_total from the instance labeled "webserver1" over the past 1 hour.

6. Regular Expression Literal:

- Query:

count(count by (status) (http_response_code =~ /^5\d{2}$/))

- Description: This query counts the occurrences of HTTP response codes starting with "5" (e.g., 500, 503, etc.).

7. Range Vector Selector Literal:

- Query:

avg_over_time(http_requests_total[5m])

- Description: This query calculates the average value of http_requests_total over the past 5 minutes for each time series.

These examples demonstrate how literals are used within Prometheus queries to filter, aggregate, and perform calculations on metrics data.