Browse Source

promql: expose ParseMetric and ParseMetricSelector

pull/783/head
Fabian Reinartz 10 years ago
parent
commit
0acd44b0e3
  1. 70
      promql/parse.go

70
promql/parse.go

@ -70,6 +70,35 @@ func ParseExpr(input string) (Expr, error) {
return expr, err return expr, err
} }
// ParseMetric parses the input into a metric
func ParseMetric(input string) (m clientmodel.Metric, err error) {
p := newParser(input)
defer p.recover(&err)
m = p.metric()
if p.peek().typ != itemEOF {
p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
}
return m, nil
}
// ParseMetricSelector parses the provided textual metric selector into a list of
// label matchers.
func ParseMetricSelector(input string) (m metric.LabelMatchers, err error) {
p := newParser(input)
defer p.recover(&err)
name := ""
if t := p.peek().typ; t == itemMetricIdentifier || t == itemIdentifier {
name = p.next().val
}
vs := p.vectorSelector(name)
if p.peek().typ != itemEOF {
p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
}
return vs.LabelMatchers, nil
}
// parseSeriesDesc parses the description of a time series. // parseSeriesDesc parses the description of a time series.
func parseSeriesDesc(input string) (clientmodel.Metric, []sequenceValue, error) { func parseSeriesDesc(input string) (clientmodel.Metric, []sequenceValue, error) {
p := newParser(input) p := newParser(input)
@ -137,20 +166,7 @@ func (v sequenceValue) String() string {
func (p *parser) parseSeriesDesc() (m clientmodel.Metric, vals []sequenceValue, err error) { func (p *parser) parseSeriesDesc() (m clientmodel.Metric, vals []sequenceValue, err error) {
defer p.recover(&err) defer p.recover(&err)
name := "" m = p.metric()
m = clientmodel.Metric{}
t := p.peek().typ
if t == itemIdentifier || t == itemMetricIdentifier {
name = p.next().val
t = p.peek().typ
}
if t == itemLeftBrace {
m = clientmodel.Metric(p.labelSet())
}
if name != "" {
m[clientmodel.MetricNameLabel] = clientmodel.LabelValue(name)
}
const ctx = "series values" const ctx = "series values"
for { for {
@ -810,6 +826,32 @@ func (p *parser) labelMatchers(operators ...itemType) metric.LabelMatchers {
return matchers return matchers
} }
// metric parses a metric.
//
// <label_set>
// <metric_identifier> [<label_set>]
//
func (p *parser) metric() clientmodel.Metric {
name := ""
m := clientmodel.Metric{}
t := p.peek().typ
if t == itemIdentifier || t == itemMetricIdentifier {
name = p.next().val
t = p.peek().typ
}
if t != itemLeftBrace && name == "" {
p.errorf("missing metric name or metric selector")
}
if t == itemLeftBrace {
m = clientmodel.Metric(p.labelSet())
}
if name != "" {
m[clientmodel.MetricNameLabel] = clientmodel.LabelValue(name)
}
return m
}
// metricSelector parses a new metric selector. // metricSelector parses a new metric selector.
// //
// <metric_identifier> [<label_matchers>] [ offset <duration> ] // <metric_identifier> [<label_matchers>] [ offset <duration> ]

Loading…
Cancel
Save