4

When I query metrics from prometheus, I just get the timestamp when I queried.

For example, If I query data like this,

http://localhost:9090/api/v1/query?query=go_memstats_buck_hash_sys_bytes

Then I got the response like following.

{
    "status": "success",
    "data": {
        "resultType": "vector",
        "result": [
            {
                "metric": {
                    "__name__": "go_memstats_buck_hash_sys_bytes",
                    "instance": "localhost:9090",
                    "job": "prometheus"
                },
                "value": [
                    1557366670.588, <== UNIX time stamp when I queried.
                    "1472884" <== Value
                ]
            }
        ]
    }
}

But in the graph view, I can see the graph like following. It means I can query data with timestamp that prometheus.

enter image description here

I wanna know how to query metrics with timestamp that prometheus has.

Jinho Yoo
  • 1,352
  • 5
  • 17
  • 28
  • The following article may be useful when you need to export time series data from Prometheus - https://valyala.medium.com/analyzing-prometheus-data-with-external-tools-5f3e5e147639 – valyala Mar 22 '22 at 14:55

2 Answers2

4

I found the answer, I need the time range like following.

http://localhost:9090/api/v1/query?query=go_memstats_buck_hash_sys_bytes[5m]

Then the result is,

{
    "status": "success",
    "data": {
        "resultType": "matrix",
        "result": [
            {
                "metric": {
                    "__name__": "go_memstats_buck_hash_sys_bytes",
                    "instance": "localhost:9090",
                    "job": "prometheus"
                },
                "values": [
                    [
                        1557369023.318,
                        "1491644"
                    ],
                    [
                        1557369028.318,
                        "1491644"
                    ],
                    [
                        1557369033.282,
                        "1491644"
                    ],
      .........
                ]
            }
        ]
    }
}
Jinho Yoo
  • 1,352
  • 5
  • 17
  • 28
2

Explanation: You want to get the Creation Metric Timestamp of your specific query. The problem began when you used timestamp function (for example timestamp(your_metric_name{some_label="label_value"})) and unfortunately you realized that the result is the current timestamp of the query. now timestamp. This is not what we were looking for.

Solution: Perform regular query and filter the timestamp value from timestamps array. The response is a JSON output which consists of information about your response status, your metric information (name ,labels, values and results) and values which representing the timestamps on the graph - This is a vector. If we parse the first value in the timestamps array we getting the creation date of this specific metric.

Example of prometheus query response body:

{
"status": "success",
"data": {
    "resultType": "matrix",
    "result": [
        {
            "metric": {
                "__name__": "yourMetricName",
                "some_label_a": "some_value_a",
                "some_label_b": "some_value_b",
                "some_label_c": "some_value_c",
            },
            "values": [
                [
                    1589329002.665,
                    "1"
                ],
                [
                    1589329002.715,
                    "1"
                ],
                [
                    1589329002.765,
                    "1"
                ],
                [
                    1589329002.815,
                    "1"
                ],
                [
                    1589329002.865,
                    "1"
                ]
            ]
        }
    ]
}

}

Implementation:

curl -G --data-urlencode \ 
query="your_metric_name{some_label='some_value_a',some_label_b='some_value_b'}[1d]" \
localhost:9090/api/v1/query | jq .data.result[].values[1][0]

Implementation Explained : I'm performing curl api call (GET method) and sending query param to my prometheus service (which runs locally on localhost on port 9090) afterwards I parse the result (with jq tool) the inner field value array on index 1 which represents the double number of timestamp. The next step is to convert it from epoch time to Date time and you're ready to go. In my example the result would be: 1589329002.665.

avivamg
  • 12,197
  • 3
  • 67
  • 61