VictoriaMetrics/lib/protoparser/common/timestamp.go
Aliaksandr Valialkin 8bd5aa3516 lib/protoparser: accept timestamp in milliseconds instead of seconds at /api/v1/import/prometheus
This improves consistency with timestamps in Prometheus text exposition format
2020-09-11 14:05:24 +03:00

22 lines
495 B
Go

package common
import (
"fmt"
"net/http"
"strconv"
)
// 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) {
ts := req.FormValue("timestamp")
if len(ts) == 0 {
return 0, nil
}
timestamp, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse `timestamp=%s` query arg: %w", ts, err)
}
return timestamp, nil
}