@@ -2,7 +2,6 @@ package kusto
22
33import (
44 "encoding/json"
5- "fmt"
65 "github.com/prometheus/client_golang/prometheus"
76 "gopkg.in/yaml.v2"
87 "testing"
@@ -66,10 +65,67 @@ defaultField:
6665
6766 metricList := BuildPrometheusMetricList (queryConfig .Metric , queryConfig .MetricConfig , resultRow )
6867
69- fmt .Println (metricList )
70- if len (metricList ) != 2 {
71- t .Fatalf (`metric count not valid, expected: %v, found: %v` , 2 , len (metricList ))
72- }
68+ metricTestSuite := testingMetricResult {t : t , list : metricList }
69+ metricTestSuite .assertMetricNames (2 )
70+
71+ metricTestSuite .assertMetric ("azure_testing" )
72+ metricTestSuite .metric ("azure_testing" ).assertRowCount (1 )
73+ metricTestSuite .metric ("azure_testing" ).row (0 ).assertLabels ("id" , "example" )
74+ metricTestSuite .metric ("azure_testing" ).row (0 ).assertLabel ("id" , "foobar" )
75+ metricTestSuite .metric ("azure_testing" ).row (0 ).assertLabel ("example" , "barfoo" )
76+ metricTestSuite .metric ("azure_testing" ).row (0 ).assertValue (20 )
77+
78+ metricTestSuite .assertMetric ("azure_testing_value" )
79+ metricTestSuite .metric ("azure_testing_value" ).assertRowCount (2 )
80+ metricTestSuite .metric ("azure_testing_value" ).row (0 ).assertLabels ("id" , "scope" )
81+ metricTestSuite .metric ("azure_testing_value" ).row (0 ).assertLabel ("id" , "foobar" )
82+ metricTestSuite .metric ("azure_testing_value" ).row (0 ).assertLabel ("scope" , "one" )
83+ metricTestSuite .metric ("azure_testing_value" ).row (0 ).assertValue (13 )
84+
85+ metricTestSuite .metric ("azure_testing_value" ).row (1 ).assertLabels ("id" , "scope" )
86+ metricTestSuite .metric ("azure_testing_value" ).row (1 ).assertLabel ("id" , "foobar" )
87+ metricTestSuite .metric ("azure_testing_value" ).row (1 ).assertLabel ("scope" , "two" )
88+ metricTestSuite .metric ("azure_testing_value" ).row (1 ).assertValue (12 )
89+ }
90+
91+ func TestMetricRowParsingWithSubMetrics (t * testing.T ) {
92+ resultRow := parseResourceGraphJsonToResultRow (t , `{
93+ "name": "foobar",
94+ "count_": 20,
95+ "valueA": 13,
96+ "valueB": 12,
97+ "should-not-exists": "testing"
98+ }` )
99+
100+ queryConfig := parseMetricConfig (t , `
101+ metric: azure_testing
102+ labels:
103+ example: barfoo
104+ fields:
105+ - name: name
106+ target: id
107+ type: id
108+
109+ - name: count_
110+ type: value
111+
112+ - name: valueA
113+ metric: azure_testing_value
114+ type: value
115+ labels:
116+ scope: one
117+
118+ - name: valueB
119+ metric: azure_testing_value
120+ type: value
121+ labels:
122+ scope: two
123+
124+ defaultField:
125+ type: ignore
126+ ` )
127+
128+ metricList := BuildPrometheusMetricList (queryConfig .Metric , queryConfig .MetricConfig , resultRow )
73129
74130 metricTestSuite := testingMetricResult {t : t , list : metricList }
75131 metricTestSuite .assertMetricNames (2 )
@@ -99,7 +155,69 @@ defaultField:
99155 secondRow .assertLabel ("scope" , "two" )
100156 secondRow .assertValue (12 )
101157 }
158+ }
159+
160+ func TestMetricRowParsingWithSubMetricsWithDisabledMainMetric (t * testing.T ) {
161+ resultRow := parseResourceGraphJsonToResultRow (t , `{
162+ "name": "foobar",
163+ "count_": 20,
164+ "valueA": 13,
165+ "valueB": 12,
166+ "should-not-exists": "testing"
167+ }` )
168+
169+ queryConfig := parseMetricConfig (t , `
170+ metric: "azure_testing"
171+ publish: false
172+ labels:
173+ example: barfoo
174+ fields:
175+ - name: name
176+ target: id
177+ type: id
178+
179+ - name: count_
180+ type: value
181+
182+ - name: valueA
183+ metric: azure_testing_value
184+ type: value
185+ labels:
186+ scope: one
187+
188+ - name: valueB
189+ metric: azure_testing_value
190+ type: value
191+ labels:
192+ scope: two
193+
194+ defaultField:
195+ type: ignore
196+ ` )
197+
198+ metricList := BuildPrometheusMetricList (queryConfig .Metric , queryConfig .MetricConfig , resultRow )
199+
200+ metricTestSuite := testingMetricResult {t : t , list : metricList }
201+ metricTestSuite .assertMetricNames (1 )
202+
203+ metricTestSuite .assertMetric ("azure_testing_value" )
204+ metricTestSuite .metric ("azure_testing_value" ).assertRowCount (2 )
205+
206+ firstRow := metricTestSuite .metric ("azure_testing_value" ).findRowByLabels (prometheus.Labels {"scope" : "one" })
207+ {
208+ firstRow .assertLabels ("id" , "scope" )
209+ firstRow .assertLabel ("id" , "foobar" )
210+ firstRow .assertLabel ("scope" , "one" )
211+ firstRow .assertValue (13 )
212+ }
102213
214+ secondRow := metricTestSuite .metric ("azure_testing_value" ).findRowByLabels (prometheus.Labels {"scope" : "two" })
215+ {
216+ secondRow .assertLabels ("id" , "scope" )
217+ secondRow .assertLabel ("id" , "foobar" )
218+ secondRow .assertLabel ("scope" , "two" )
219+ secondRow .assertValue (12 )
220+ }
103221}
104222
105223func parseResourceGraphJsonToResultRow (t * testing.T , data string ) map [string ]interface {} {
0 commit comments