mirror of
https://github.com/prometheus/prometheus
synced 2025-04-10 03:22:19 +00:00
Move string functionality to pkg/strutil
This commit is contained in:
parent
f45a5cab60
commit
dbc0d30e3e
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/utility"
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -479,7 +479,7 @@ func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
if err := unmarshal(&s); err != nil {
|
if err := unmarshal(&s); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dur, err := utility.StringToDuration(s)
|
dur, err := strutil.StringToDuration(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -489,5 +489,5 @@ func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
|
|
||||||
// MarshalYAML implements the yaml.Marshaler interface.
|
// MarshalYAML implements the yaml.Marshaler interface.
|
||||||
func (d Duration) MarshalYAML() (interface{}, error) {
|
func (d Duration) MarshalYAML() (interface{}, error) {
|
||||||
return utility.DurationToString(time.Duration(d)), nil
|
return strutil.DurationToString(time.Duration(d)), nil
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package utility
|
package strutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
@ -21,8 +21,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
"github.com/prometheus/prometheus/storage/metric"
|
"github.com/prometheus/prometheus/storage/metric"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type parser struct {
|
type parser struct {
|
||||||
@ -982,7 +982,7 @@ func (p *parser) checkType(node Node) (typ ExprType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseDuration(ds string) (time.Duration, error) {
|
func parseDuration(ds string) (time.Duration, error) {
|
||||||
dur, err := utility.StringToDuration(ds)
|
dur, err := strutil.StringToDuration(ds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ import (
|
|||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
"github.com/prometheus/prometheus/storage/metric"
|
"github.com/prometheus/prometheus/storage/metric"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (matrix Matrix) String() string {
|
func (matrix Matrix) String() string {
|
||||||
@ -145,7 +145,7 @@ func (node *AlertStmt) String() string {
|
|||||||
s := fmt.Sprintf("ALERT %s", node.Name)
|
s := fmt.Sprintf("ALERT %s", node.Name)
|
||||||
s += fmt.Sprintf("\n\tIF %s", node.Expr)
|
s += fmt.Sprintf("\n\tIF %s", node.Expr)
|
||||||
if node.Duration > 0 {
|
if node.Duration > 0 {
|
||||||
s += fmt.Sprintf("\n\tFOR %s", utility.DurationToString(node.Duration))
|
s += fmt.Sprintf("\n\tFOR %s", strutil.DurationToString(node.Duration))
|
||||||
}
|
}
|
||||||
if len(node.Labels) > 0 {
|
if len(node.Labels) > 0 {
|
||||||
s += fmt.Sprintf("\n\tWITH %s", node.Labels)
|
s += fmt.Sprintf("\n\tWITH %s", node.Labels)
|
||||||
@ -207,7 +207,7 @@ func (node *MatrixSelector) String() string {
|
|||||||
Name: node.Name,
|
Name: node.Name,
|
||||||
LabelMatchers: node.LabelMatchers,
|
LabelMatchers: node.LabelMatchers,
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s[%s]", vecSelector.String(), utility.DurationToString(node.Range))
|
return fmt.Sprintf("%s[%s]", vecSelector.String(), strutil.DurationToString(node.Range))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *NumberLiteral) String() string {
|
func (node *NumberLiteral) String() string {
|
||||||
@ -260,7 +260,7 @@ func (node *AlertStmt) DotGraph() string {
|
|||||||
%#p -> %x;
|
%#p -> %x;
|
||||||
%s
|
%s
|
||||||
}`,
|
}`,
|
||||||
node, node.Name, utility.DurationToString(node.Duration),
|
node, node.Name, strutil.DurationToString(node.Duration),
|
||||||
node, reflect.ValueOf(node.Expr).Pointer(),
|
node, reflect.ValueOf(node.Expr).Pointer(),
|
||||||
node.Expr.DotGraph(),
|
node.Expr.DotGraph(),
|
||||||
)
|
)
|
||||||
|
@ -24,12 +24,11 @@ import (
|
|||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
|
"github.com/prometheus/prometheus/pkg/testutil"
|
||||||
"github.com/prometheus/prometheus/storage"
|
"github.com/prometheus/prometheus/storage"
|
||||||
"github.com/prometheus/prometheus/storage/local"
|
"github.com/prometheus/prometheus/storage/local"
|
||||||
"github.com/prometheus/prometheus/storage/metric"
|
"github.com/prometheus/prometheus/storage/metric"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/pkg/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -91,7 +90,7 @@ func (t *Test) parseLoad(lines []string, i int) (int, *loadCmd, error) {
|
|||||||
}
|
}
|
||||||
parts := patLoad.FindStringSubmatch(lines[i])
|
parts := patLoad.FindStringSubmatch(lines[i])
|
||||||
|
|
||||||
gap, err := utility.StringToDuration(parts[1])
|
gap, err := strutil.StringToDuration(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
||||||
}
|
}
|
||||||
@ -132,7 +131,7 @@ func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
|
|||||||
return i, nil, perr
|
return i, nil, perr
|
||||||
}
|
}
|
||||||
|
|
||||||
offset, err := utility.StringToDuration(at)
|
offset, err := strutil.StringToDuration(at)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
"github.com/prometheus/prometheus/utility"
|
"github.com/prometheus/prometheus/utility"
|
||||||
)
|
)
|
||||||
@ -201,7 +202,7 @@ func (rule *AlertingRule) DotGraph() string {
|
|||||||
%#p -> %x;
|
%#p -> %x;
|
||||||
%s
|
%s
|
||||||
}`,
|
}`,
|
||||||
&rule, rule.name, utility.DurationToString(rule.holdDuration),
|
&rule, rule.name, strutil.DurationToString(rule.holdDuration),
|
||||||
&rule, reflect.ValueOf(rule.Vector).Pointer(),
|
&rule, reflect.ValueOf(rule.Vector).Pointer(),
|
||||||
rule.Vector.DotGraph(),
|
rule.Vector.DotGraph(),
|
||||||
)
|
)
|
||||||
@ -209,7 +210,7 @@ func (rule *AlertingRule) DotGraph() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rule *AlertingRule) String() string {
|
func (rule *AlertingRule) String() string {
|
||||||
return fmt.Sprintf("ALERT %s IF %s FOR %s WITH %s", rule.name, rule.Vector, utility.DurationToString(rule.holdDuration), rule.Labels)
|
return fmt.Sprintf("ALERT %s IF %s FOR %s WITH %s", rule.name, rule.Vector, strutil.DurationToString(rule.holdDuration), rule.Labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLSnippet returns an HTML snippet representing this alerting rule.
|
// HTMLSnippet returns an HTML snippet representing this alerting rule.
|
||||||
@ -220,11 +221,11 @@ func (rule *AlertingRule) HTMLSnippet(pathPrefix string) template.HTML {
|
|||||||
}
|
}
|
||||||
return template.HTML(fmt.Sprintf(
|
return template.HTML(fmt.Sprintf(
|
||||||
`ALERT <a href="%s">%s</a> IF <a href="%s">%s</a> FOR %s WITH %s`,
|
`ALERT <a href="%s">%s</a> IF <a href="%s">%s</a> FOR %s WITH %s`,
|
||||||
pathPrefix+utility.GraphLinkForExpression(alertMetric.String()),
|
pathPrefix+strutil.GraphLinkForExpression(alertMetric.String()),
|
||||||
rule.name,
|
rule.name,
|
||||||
pathPrefix+utility.GraphLinkForExpression(rule.Vector.String()),
|
pathPrefix+strutil.GraphLinkForExpression(rule.Vector.String()),
|
||||||
rule.Vector,
|
rule.Vector,
|
||||||
utility.DurationToString(rule.holdDuration),
|
strutil.DurationToString(rule.holdDuration),
|
||||||
rule.Labels))
|
rule.Labels))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,10 +28,10 @@ import (
|
|||||||
|
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
"github.com/prometheus/prometheus/notification"
|
"github.com/prometheus/prometheus/notification"
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
"github.com/prometheus/prometheus/storage"
|
"github.com/prometheus/prometheus/storage"
|
||||||
"github.com/prometheus/prometheus/template"
|
"github.com/prometheus/prometheus/template"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Constants for instrumentation.
|
// Constants for instrumentation.
|
||||||
@ -213,7 +213,7 @@ func (m *Manager) queueAlertNotifications(rule *AlertingRule, timestamp clientmo
|
|||||||
Value: aa.Value,
|
Value: aa.Value,
|
||||||
ActiveSince: aa.ActiveSince.Time(),
|
ActiveSince: aa.ActiveSince.Time(),
|
||||||
RuleString: rule.String(),
|
RuleString: rule.String(),
|
||||||
GeneratorURL: m.prometheusURL + strings.TrimLeft(utility.GraphLinkForExpression(rule.Vector.String()), "/"),
|
GeneratorURL: m.prometheusURL + strings.TrimLeft(strutil.GraphLinkForExpression(rule.Vector.String()), "/"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
m.notificationHandler.SubmitReqs(notifications)
|
m.notificationHandler.SubmitReqs(notifications)
|
||||||
|
@ -20,8 +20,8 @@ import (
|
|||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A RecordingRule records its vector expression into new timeseries.
|
// A RecordingRule records its vector expression into new timeseries.
|
||||||
@ -89,9 +89,9 @@ func (rule RecordingRule) HTMLSnippet(pathPrefix string) template.HTML {
|
|||||||
ruleExpr := rule.vector.String()
|
ruleExpr := rule.vector.String()
|
||||||
return template.HTML(fmt.Sprintf(
|
return template.HTML(fmt.Sprintf(
|
||||||
`<a href="%s">%s</a>%s = <a href="%s">%s</a>`,
|
`<a href="%s">%s</a>%s = <a href="%s">%s</a>`,
|
||||||
pathPrefix+utility.GraphLinkForExpression(rule.name),
|
pathPrefix+strutil.GraphLinkForExpression(rule.name),
|
||||||
rule.name,
|
rule.name,
|
||||||
rule.labels,
|
rule.labels,
|
||||||
pathPrefix+utility.GraphLinkForExpression(ruleExpr),
|
pathPrefix+strutil.GraphLinkForExpression(ruleExpr),
|
||||||
ruleExpr))
|
ruleExpr))
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,8 @@ import (
|
|||||||
|
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/strutil"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A version of vector that's easier to use from templates.
|
// A version of vector that's easier to use from templates.
|
||||||
@ -152,8 +152,8 @@ func NewTemplateExpander(text string, name string, data interface{}, timestamp c
|
|||||||
},
|
},
|
||||||
"match": regexp.MatchString,
|
"match": regexp.MatchString,
|
||||||
"title": strings.Title,
|
"title": strings.Title,
|
||||||
"graphLink": utility.GraphLinkForExpression,
|
"graphLink": strutil.GraphLinkForExpression,
|
||||||
"tableLink": utility.TableLinkForExpression,
|
"tableLink": strutil.TableLinkForExpression,
|
||||||
"sortByLabel": func(label string, v queryResult) queryResult {
|
"sortByLabel": func(label string, v queryResult) queryResult {
|
||||||
sorter := queryResultByLabelSorter{v[:], label}
|
sorter := queryResultByLabelSorter{v[:], label}
|
||||||
sort.Stable(sorter)
|
sort.Stable(sorter)
|
||||||
|
Loading…
Reference in New Issue
Block a user