lib/protoparser: accept timestamp in milliseconds instead of seconds at /api/v1/import/prometheus

This improves consistency with timestamps in Prometheus text exposition format
This commit is contained in:
Aliaksandr Valialkin 2020-09-11 14:03:55 +03:00
parent d3ad0d365e
commit 8bd5aa3516
2 changed files with 4 additions and 6 deletions

View file

@ -552,8 +552,7 @@ Extra labels may be added to all the imported metrics by passing `extra_label=na
For example, `/api/v1/import/prometheus?extra_label=foo=bar` would add `{foo="bar"}` label to all the imported metrics.
If timestamp is missing in `<metric> <value> <timestamp>` Prometheus exposition format line, then the current timestamp is used during data ingestion.
It can be overriden by passing unix timestamp in seconds via `timestamp` query arg. The value may be fractional when millisecond precision is needed.
For example, `/api/v1/import/prometheus?timestamp=1594370496.905`.
It can be overriden by passing unix timestamp in *milliseconds* via `timestamp` query arg. For example, `/api/v1/import/prometheus?timestamp=1594370496905`.
VictoriaMetrics accepts arbitrary number of lines in a single request to `/api/v1/import/prometheus`, i.e. it supports data streaming.

View file

@ -6,7 +6,7 @@ import (
"strconv"
)
// GetTimestamp extracts unix timestamp from `timestamp` query arg.
// GetTimestamp extracts unix timestamp in milliseconds from `timestamp` query arg.
//
// It returns 0 if there is no `timestamp` query arg.
func GetTimestamp(req *http.Request) (int64, error) {
@ -14,10 +14,9 @@ func GetTimestamp(req *http.Request) (int64, error) {
if len(ts) == 0 {
return 0, nil
}
timestamp, err := strconv.ParseFloat(ts, 64)
timestamp, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse `timestamp=%s` query arg: %w", ts, err)
}
// Convert seconds to milliseconds.
return int64(timestamp * 1e3), nil
return timestamp, nil
}