Commit 3e73be8d by bergquist

feat(prometheus): improve error handling

parent 4c88db3e
...@@ -49,14 +49,17 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb ...@@ -49,14 +49,17 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
client, err := e.getClient() client, err := e.getClient()
if err != nil { if err != nil {
result.Error = err return resultWithError(result, err)
return result
} }
from, _ := queryContext.TimeRange.FromTime() from, _ := queryContext.TimeRange.FromTime()
to, _ := queryContext.TimeRange.ToTime() to, _ := queryContext.TimeRange.ToTime()
query := parseQuery(queries) query, err := parseQuery(queries)
if err != nil {
return resultWithError(result, err)
}
timeRange := prometheus.Range{ timeRange := prometheus.Range{
Start: from, Start: from,
...@@ -67,15 +70,14 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb ...@@ -67,15 +70,14 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
value, err := client.QueryRange(context.Background(), query.Expr, timeRange) value, err := client.QueryRange(context.Background(), query.Expr, timeRange)
if err != nil { if err != nil {
result.Error = err return resultWithError(result, err)
return result
} }
result.QueryResults = parseResponse(value, query) result.QueryResults = parseResponse(value, query)
return result return result
} }
func formatLegend(metric pmodel.Metric, query PrometheusQuery) string { func formatLegend(metric pmodel.Metric, query *PrometheusQuery) string {
r, _ := regexp.Compile(`\{\{\s*(.+?)\s*\}\}`) r, _ := regexp.Compile(`\{\{\s*(.+?)\s*\}\}`)
result := r.ReplaceAllFunc([]byte(query.LegendFormat), func(in []byte) []byte { result := r.ReplaceAllFunc([]byte(query.LegendFormat), func(in []byte) []byte {
...@@ -90,17 +92,32 @@ func formatLegend(metric pmodel.Metric, query PrometheusQuery) string { ...@@ -90,17 +92,32 @@ func formatLegend(metric pmodel.Metric, query PrometheusQuery) string {
return string(result) return string(result)
} }
func parseQuery(queries tsdb.QuerySlice) PrometheusQuery { func parseQuery(queries tsdb.QuerySlice) (*PrometheusQuery, error) {
queryModel := queries[0] queryModel := queries[0]
return PrometheusQuery{ expr, err := queryModel.Model.Get("expr").String()
Expr: queryModel.Model.Get("expr").MustString(), if err != nil {
Step: time.Second * time.Duration(queryModel.Model.Get("step").MustInt64(1)), return nil, err
LegendFormat: queryModel.Model.Get("legendFormat").MustString(), }
step, err := queryModel.Model.Get("step").Int64()
if err != nil {
return nil, err
} }
format, err := queryModel.Model.Get("legendFormat").String()
if err != nil {
return nil, err
}
return &PrometheusQuery{
Expr: expr,
Step: time.Second * time.Duration(step),
LegendFormat: format,
}, nil
} }
func parseResponse(value pmodel.Value, query PrometheusQuery) map[string]*tsdb.QueryResult { func parseResponse(value pmodel.Value, query *PrometheusQuery) map[string]*tsdb.QueryResult {
queryResults := make(map[string]*tsdb.QueryResult) queryResults := make(map[string]*tsdb.QueryResult)
queryRes := &tsdb.QueryResult{} queryRes := &tsdb.QueryResult{}
...@@ -123,3 +140,8 @@ func parseResponse(value pmodel.Value, query PrometheusQuery) map[string]*tsdb.Q ...@@ -123,3 +140,8 @@ func parseResponse(value pmodel.Value, query PrometheusQuery) map[string]*tsdb.Q
queryResults["A"] = queryRes queryResults["A"] = queryRes
return queryResults return queryResults
} }
func resultWithError(result *tsdb.BatchResult, err error) *tsdb.BatchResult {
result.Error = err
return result
}
...@@ -16,7 +16,7 @@ func TestPrometheus(t *testing.T) { ...@@ -16,7 +16,7 @@ func TestPrometheus(t *testing.T) {
p.LabelName("device"): p.LabelValue("mobile"), p.LabelName("device"): p.LabelValue("mobile"),
} }
query := PrometheusQuery{ query := &PrometheusQuery{
LegendFormat: "legend {{app}} {{device}} {{broken}}", LegendFormat: "legend {{app}} {{device}} {{broken}}",
} }
......
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