diff --git a/go.mod b/go.mod index 65fec3bc2a..726806a7fd 100644 --- a/go.mod +++ b/go.mod @@ -44,7 +44,7 @@ require ( github.com/prometheus/alertmanager v0.27.0 github.com/prometheus/client_golang v1.19.1 github.com/prometheus/client_model v0.6.1 - github.com/prometheus/common v0.54.1-0.20240615204547-04635d2962f9 + github.com/prometheus/common v0.55.0 // Prometheus maps version 2.x.y to tags v0.x.y. github.com/prometheus/prometheus v0.53.1-0.20240625160125-1abeebacb870 github.com/segmentio/fasthash v1.0.3 diff --git a/go.sum b/go.sum index 59a55aa7be..39ffe18f7f 100644 --- a/go.sum +++ b/go.sum @@ -1325,8 +1325,8 @@ github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+ github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA= github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= -github.com/prometheus/common v0.54.1-0.20240615204547-04635d2962f9 h1:WTZ/GBRTImL1HgRTEnJJcM2FuII7PXX1idCIEUJ8/r8= -github.com/prometheus/common v0.54.1-0.20240615204547-04635d2962f9/go.mod h1:1Yn/UzXoahbVLk1sn6wsGiSiemz3XQejcaz9FIA1r+I= +github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc= +github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8= github.com/prometheus/common/sigv4 v0.1.0 h1:qoVebwtwwEhS85Czm2dSROY5fTo2PAPEVdDeppTwGX4= github.com/prometheus/common/sigv4 v0.1.0/go.mod h1:2Jkxxk9yYvCkE5G1sQT7GuEXm57JrvHu9k5YwTjsNtI= github.com/prometheus/exporter-toolkit v0.8.2/go.mod h1:00shzmJL7KxcsabLWcONwpyNEuWhREOnFqZW7vadFS0= diff --git a/vendor/github.com/prometheus/common/helpers/templates/time.go b/vendor/github.com/prometheus/common/helpers/templates/time.go index 266c8c992f..b7dc655f67 100644 --- a/vendor/github.com/prometheus/common/helpers/templates/time.go +++ b/vendor/github.com/prometheus/common/helpers/templates/time.go @@ -14,13 +14,18 @@ package templates import ( + "errors" "fmt" "math" "strconv" "time" + + "github.com/prometheus/common/model" ) -func convertToFloat(i interface{}) (float64, error) { +var errNaNOrInf = errors.New("value is NaN or Inf") + +func ConvertToFloat(i interface{}) (float64, error) { switch v := i.(type) { case float64: return v, nil @@ -41,8 +46,20 @@ func convertToFloat(i interface{}) (float64, error) { } } +func FloatToTime(v float64) (*time.Time, error) { + if math.IsNaN(v) || math.IsInf(v, 0) { + return nil, errNaNOrInf + } + timestamp := v * 1e9 + if timestamp > math.MaxInt64 || timestamp < math.MinInt64 { + return nil, fmt.Errorf("%v cannot be represented as a nanoseconds timestamp since it overflows int64", v) + } + t := model.TimeFromUnixNano(int64(timestamp)).Time().UTC() + return &t, nil +} + func HumanizeDuration(i interface{}) (string, error) { - v, err := convertToFloat(i) + v, err := ConvertToFloat(i) if err != nil { return "", err } @@ -87,3 +104,20 @@ func HumanizeDuration(i interface{}) (string, error) { } return fmt.Sprintf("%.4g%ss", v, prefix), nil } + +func HumanizeTimestamp(i interface{}) (string, error) { + v, err := ConvertToFloat(i) + if err != nil { + return "", err + } + + tm, err := FloatToTime(v) + switch { + case errors.Is(err, errNaNOrInf): + return fmt.Sprintf("%.4g", v), nil + case err != nil: + return "", err + } + + return fmt.Sprint(tm), nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 87c4ba4152..8eeb7659f9 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -802,7 +802,7 @@ github.com/prometheus/client_golang/prometheus/testutil/promlint/validations # github.com/prometheus/client_model v0.6.1 ## explicit; go 1.19 github.com/prometheus/client_model/go -# github.com/prometheus/common v0.54.1-0.20240615204547-04635d2962f9 +# github.com/prometheus/common v0.55.0 ## explicit; go 1.20 github.com/prometheus/common/config github.com/prometheus/common/expfmt