Commit ee0f1a0f by bergquist

feat(prometheus): handle more errors

parent 3e73be8d
...@@ -52,18 +52,14 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb ...@@ -52,18 +52,14 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
return resultWithError(result, err) return resultWithError(result, err)
} }
from, _ := queryContext.TimeRange.FromTime() query, err := parseQuery(queries, queryContext)
to, _ := queryContext.TimeRange.ToTime()
query, err := parseQuery(queries)
if err != nil { if err != nil {
return resultWithError(result, err) return resultWithError(result, err)
} }
timeRange := prometheus.Range{ timeRange := prometheus.Range{
Start: from, Start: query.Start,
End: to, End: query.End,
Step: query.Step, Step: query.Step,
} }
...@@ -78,9 +74,9 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb ...@@ -78,9 +74,9 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
} }
func formatLegend(metric pmodel.Metric, query *PrometheusQuery) string { func formatLegend(metric pmodel.Metric, query *PrometheusQuery) string {
r, _ := regexp.Compile(`\{\{\s*(.+?)\s*\}\}`) reg, _ := regexp.Compile(`\{\{\s*(.+?)\s*\}\}`)
result := r.ReplaceAllFunc([]byte(query.LegendFormat), func(in []byte) []byte { result := reg.ReplaceAllFunc([]byte(query.LegendFormat), func(in []byte) []byte {
ind := strings.Replace(strings.Replace(string(in), "{{", "", 1), "}}", "", 1) ind := strings.Replace(strings.Replace(string(in), "{{", "", 1), "}}", "", 1)
if val, exists := metric[pmodel.LabelName(ind)]; exists { if val, exists := metric[pmodel.LabelName(ind)]; exists {
return []byte(val) return []byte(val)
...@@ -92,7 +88,7 @@ func formatLegend(metric pmodel.Metric, query *PrometheusQuery) string { ...@@ -92,7 +88,7 @@ func formatLegend(metric pmodel.Metric, query *PrometheusQuery) string {
return string(result) return string(result)
} }
func parseQuery(queries tsdb.QuerySlice) (*PrometheusQuery, error) { func parseQuery(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) (*PrometheusQuery, error) {
queryModel := queries[0] queryModel := queries[0]
expr, err := queryModel.Model.Get("expr").String() expr, err := queryModel.Model.Get("expr").String()
...@@ -110,10 +106,22 @@ func parseQuery(queries tsdb.QuerySlice) (*PrometheusQuery, error) { ...@@ -110,10 +106,22 @@ func parseQuery(queries tsdb.QuerySlice) (*PrometheusQuery, error) {
return nil, err return nil, err
} }
start, err := queryContext.TimeRange.FromTime()
if err != nil {
return nil, err
}
end, err := queryContext.TimeRange.ToTime()
if err != nil {
return nil, err
}
return &PrometheusQuery{ return &PrometheusQuery{
Expr: expr, Expr: expr,
Step: time.Second * time.Duration(step), Step: time.Second * time.Duration(step),
LegendFormat: format, LegendFormat: format,
Start: start,
End: end,
}, nil }, nil
} }
......
...@@ -6,4 +6,6 @@ type PrometheusQuery struct { ...@@ -6,4 +6,6 @@ type PrometheusQuery struct {
Expr string Expr string
Step time.Duration Step time.Duration
LegendFormat string LegendFormat string
Start time.Time
End time.Time
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment