mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 16:30:55 +01:00
130e0ea5f0
1. split package rule under /app/vmalert, expose needed objects 2. add vmalert-tool with unittest subcmd https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2945
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package promutils
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/VictoriaMetrics/metricsql"
|
|
)
|
|
|
|
// Duration is duration, which must be used in Prometheus-compatible yaml configs.
|
|
type Duration struct {
|
|
D time.Duration
|
|
}
|
|
|
|
// NewDuration returns Duration for given d.
|
|
func NewDuration(d time.Duration) *Duration {
|
|
return &Duration{
|
|
D: d,
|
|
}
|
|
}
|
|
|
|
// MarshalYAML implements yaml.Marshaler interface.
|
|
func (pd Duration) MarshalYAML() (interface{}, error) {
|
|
return pd.D.String(), nil
|
|
}
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
|
func (pd *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
var s string
|
|
if err := unmarshal(&s); err != nil {
|
|
return err
|
|
}
|
|
ms, err := metricsql.DurationValue(s, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pd.D = time.Duration(ms) * time.Millisecond
|
|
return nil
|
|
}
|
|
|
|
// Duration returns duration for pd.
|
|
func (pd *Duration) Duration() time.Duration {
|
|
if pd == nil {
|
|
return 0
|
|
}
|
|
return pd.D
|
|
}
|
|
|
|
// ParseDuration parses duration string in Prometheus format
|
|
func ParseDuration(s string) (time.Duration, error) {
|
|
ms, err := metricsql.DurationValue(s, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return time.Duration(ms) * time.Millisecond, nil
|
|
}
|
|
|
|
// ParseTime returns time for pd.
|
|
func (pd *Duration) ParseTime() time.Time {
|
|
if pd == nil {
|
|
return time.Time{}
|
|
}
|
|
return time.UnixMilli(pd.Duration().Milliseconds())
|
|
}
|