Skip to content

Commit 9de74b5

Browse files
committed
tech(elastic): replace fmt.print with logging
1 parent f586748 commit 9de74b5

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

pkg/tsdb/elasticsearch/elasticsearch.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package elasticsearch
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"strconv"
87
"time"
98

@@ -36,8 +35,7 @@ func (e *EsExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, conte
3635
//convert dashboard query datastructure to helper objects
3736
esQuerys := e.convertQueries(queries)
3837
for _, esQuery := range esQuerys {
39-
//debug output
40-
fmt.Printf("inspect esquery %+v\n", esQuery)
38+
e.log.Debug("inspect esquery", "query", esQuery)
4139

4240
//build and execute search
4341
searchResult := e.search(esQuery, context)
@@ -73,7 +71,7 @@ func (e *EsExecutor) buildClient(dataSourceInfo *tsdb.DataSourceInfo) *elastic.C
7371
}
7472
client, err := elastic.NewClient(clientOptions...)
7573
if err != nil {
76-
fmt.Printf("\nERROR: creating elastic client \n%#v", err)
74+
e.log.Error("Creating elastic client", "error", err)
7775
}
7876
return client
7977
}
@@ -83,14 +81,12 @@ func (e *EsExecutor) convertQueries(queries tsdb.QuerySlice) []EsQuery {
8381

8482
for _, query := range queries { //TODO allow more then one query
8583
str, _ := query.Model.EncodePretty()
86-
//debug print query
87-
fmt.Printf("\nElastic query json model: \n%s", str)
88-
e.log.Info("Elastic query")
84+
e.log.Info("Elastic query", "query", query.Model)
8985

9086
var esQuery EsQuery
9187
jerr := json.Unmarshal(str, &esQuery)
9288
if jerr != nil {
93-
fmt.Println("json parser error %s", jerr)
89+
e.log.Error("json parser error %s", "error", jerr)
9490
} else {
9591
esQuery.DataSource = query.DataSource
9692
esQuerys = append(esQuerys, esQuery)
@@ -113,11 +109,9 @@ func (e *EsExecutor) search(esQuery EsQuery, context *tsdb.QueryContext) *elasti
113109
searchService = e.buildQuery(searchService, esQuery, context)
114110
searchService = e.buildAggregations(searchService, esQuery, context)
115111

116-
// execute
117112
searchResult, err := searchService.Do()
118113
if err != nil {
119-
// Handle error
120-
fmt.Printf("\nERROR: executing elastic query \n%#v\n%s", err, err)
114+
e.log.Error("Executing elastic query", "error", err)
121115
return nil
122116
}
123117

@@ -137,21 +131,21 @@ func (e *EsExecutor) processAggregation(esQuery EsQuery, index int32, aggregatio
137131
if bAgg.AggType == "date_histogram" {
138132
bucketAgg, found := aggregation.DateHistogram(bAgg.Id)
139133
if found != true {
140-
fmt.Printf("Can not find Aggregation with id %s\n", bAgg.Id)
134+
e.log.Info("Can not find Aggregation with id", "id", bAgg.Id)
141135
}
142136
result := e.processDateHistogram(esQuery, bucketAgg)
143137
results = append(results, result)
144138
} else if bAgg.AggType == "terms" {
145139
bucketAgg, found := aggregation.Terms(bAgg.Id)
146140
if found != true {
147-
fmt.Printf("Can not find Aggregation with id %s\n", bAgg.Id)
141+
e.log.Info("Can not find Aggregation with id", "id", bAgg.Id)
148142
}
149143
aggResults := e.processTerms(esQuery, index, bucketAgg)
150144
for _, result := range aggResults {
151145
results = append(results, result)
152146
}
153147
} else {
154-
fmt.Printf("Aggregation type currently not supported: %s\n", bAgg.AggType)
148+
e.log.Error("Aggregation type currently not supported", "type", bAgg.AggType)
155149
}
156150
return results
157151
}
@@ -191,15 +185,9 @@ func (e *EsExecutor) processDateHistogram(esQuery EsQuery, bucketAgg *elastic.Ag
191185
var values tsdb.TimeSeriesPoints
192186
for _, bucket := range bucketAgg.Buckets {
193187
if mAgg.MetricType == "extended_stats" {
194-
//extended, found := bucket.Aggregations.ExtendedStats(mAgg.Id)
195-
//if mAgg.Meta.Count {
196-
//TODO
197-
//}
198-
fmt.Printf("Aggregation type currently not supported: %s\n", mAgg.MetricType)
188+
e.log.Info("Aggregation type currently not supported", "type", mAgg.MetricType)
199189
} else if mAgg.MetricType == "percentiles" {
200-
//TODO
201-
//Percentiles
202-
fmt.Printf("Aggregation type currently not supported: %s\n", mAgg.MetricType)
190+
e.log.Info("Aggregation type currently not supported", "type", mAgg.MetricType)
203191
} else {
204192
//everything with json key value should work with this
205193
derivative, found := bucket.Aggregations.Derivative(mAgg.Id) //TODO use correct type
@@ -223,15 +211,13 @@ func (e *EsExecutor) processDateHistogram(esQuery EsQuery, bucketAgg *elastic.Ag
223211
}
224212
queryName = queryName + mAgg.Id //TODO we should also append the term
225213

226-
//append time series to results
227214
queryRes.RefId = esQuery.RefId
228215
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
229216
Name: queryName,
230217
Points: values,
231218
})
232219

233-
//debug print
234-
fmt.Printf("query result series %s: %+v\n", queryRes.Series[0].Name, queryRes.Series[0].Points)
220+
e.log.Debug("query result series", "name", queryRes.Series[0].Name, "points", queryRes.Series[0].Points)
235221
}
236222
}
237223

@@ -325,7 +311,7 @@ func (e *EsExecutor) buildMetricAggregation(agg EsMetric, context *tsdb.QueryCon
325311
//TODO change model
326312
aggregation = elastic.NewMovAvgAggregation().BucketsPath(agg.Field).Window(agg.Settings.Window)
327313
} else { //TODO add more
328-
fmt.Printf("Aggregation type currently not supported: %s\n", agg.MetricType)
314+
e.log.Info("Aggregation type currently not supported", "type", agg.MetricType)
329315
}
330316

331317
return aggregation
@@ -347,7 +333,7 @@ func (e *EsExecutor) buildBucketAggregation(agg EsBucketAgg, context *tsdb.Query
347333
}
348334
aggregation = elastic.NewTermsAggregation().Field(agg.Field).Size(int(i))
349335
} else { //TODO add more
350-
fmt.Printf("Aggregation type currently not supported: %s\n", agg.AggType)
336+
e.log.Info("Aggregation type currently not supported", "type", agg.AggType)
351337
}
352338

353339
return aggregation
@@ -362,7 +348,7 @@ func (e *EsExecutor) formatTimeRange(input string) string {
362348

363349
duration, err := time.ParseDuration("-" + input)
364350
if err != nil {
365-
fmt.Printf("Something failed on duration parsing %s\n", err)
351+
e.log.Error("Something failed on duration parsing", "error", err)
366352
}
367353

368354
return strconv.FormatInt(time.Now().Add(duration).UnixNano()/1000/1000, 10)

public/app/plugins/datasource/elasticsearch/plugin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
},
2222

2323
"annotations": true,
24-
"metrics": true
24+
"metrics": true,
25+
"alerting": true
2526
}

0 commit comments

Comments
 (0)