Commit 5763d3ca by Erik Sundell Committed by Daniel Lee

stackdriver: make sure distinct labels are returned. also added test

parent 37fe488b
...@@ -212,7 +212,7 @@ func (e *StackdriverExecutor) unmarshalResponse(res *http.Response) (StackDriver ...@@ -212,7 +212,7 @@ func (e *StackdriverExecutor) unmarshalResponse(res *http.Response) (StackDriver
func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data StackDriverResponse) error { func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data StackDriverResponse) error {
metricLabels := make(map[string][]string) metricLabels := make(map[string][]string)
// resourceLabels := make(map[string][]string) resourceLabels := make(map[string][]string)
for _, series := range data.TimeSeries { for _, series := range data.TimeSeries {
points := make([]tsdb.TimePoint, 0) points := make([]tsdb.TimePoint, 0)
...@@ -225,11 +225,17 @@ func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data Sta ...@@ -225,11 +225,17 @@ func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data Sta
metricName := series.Metric.Type metricName := series.Metric.Type
for key, value := range series.Metric.Labels { for key, value := range series.Metric.Labels {
metricLabels[key] = append(metricLabels[key], value) if !containsLabel(metricLabels[key], value) {
metricLabels[key] = append(metricLabels[key], value)
}
metricName += " " + value metricName += " " + value
} }
// queryRes.Meta.Set("resourceLabels", series.Resource.Labels) for key, value := range series.Resource.Labels {
if !containsLabel(resourceLabels[key], value) {
resourceLabels[key] = append(resourceLabels[key], value)
}
}
queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{
Name: metricName, Name: metricName,
...@@ -237,11 +243,21 @@ func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data Sta ...@@ -237,11 +243,21 @@ func (e *StackdriverExecutor) parseResponse(queryRes *tsdb.QueryResult, data Sta
}) })
} }
queryRes.Meta.Set("resourceLabels", resourceLabels)
queryRes.Meta.Set("metricLabels", metricLabels) queryRes.Meta.Set("metricLabels", metricLabels)
return nil return nil
} }
func containsLabel(labels []string, newLabel string) bool {
for _, val := range labels {
if val == newLabel {
return true
}
}
return false
}
func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.DataSource) (*http.Request, error) { func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.DataSource) (*http.Request, error) {
u, _ := url.Parse(dsInfo.Url) u, _ := url.Parse(dsInfo.Url)
u.Path = path.Join(u.Path, "render") u.Path = path.Join(u.Path, "render")
......
...@@ -152,8 +152,22 @@ func TestStackdriver(t *testing.T) { ...@@ -152,8 +152,22 @@ func TestStackdriver(t *testing.T) {
}) })
Convey("Should add meta for labels to the response", func() { Convey("Should add meta for labels to the response", func() {
instanceName := res.Meta.Get("metricLabels").MustMap()["instance_name"] metricLabels := res.Meta.Get("metricLabels").Interface().(map[string][]string)
So(instanceName, ShouldNotBeNil) So(metricLabels, ShouldNotBeNil)
So(len(metricLabels["instance_name"]), ShouldEqual, 3)
So(metricLabels["instance_name"][0], ShouldEqual, "collector-asia-east-1")
So(metricLabels["instance_name"][1], ShouldEqual, "collector-europe-west-1")
So(metricLabels["instance_name"][2], ShouldEqual, "collector-us-east-1")
resourceLabels := res.Meta.Get("resourceLabels").Interface().(map[string][]string)
So(resourceLabels, ShouldNotBeNil)
So(len(resourceLabels["zone"]), ShouldEqual, 3)
So(resourceLabels["zone"][0], ShouldEqual, "asia-east1-a")
So(resourceLabels["zone"][1], ShouldEqual, "europe-west1-b")
So(resourceLabels["zone"][2], ShouldEqual, "us-east1-b")
So(len(resourceLabels["project_id"]), ShouldEqual, 1)
So(resourceLabels["project_id"][0], ShouldEqual, "grafana-prod")
}) })
}) })
}) })
......
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