Skip to content

Commit eb367b3

Browse files
committed
allow not published metrics (implement only submetric support)
Signed-off-by: Markus Blaschke <[email protected]>
1 parent af736e4 commit eb367b3

File tree

4 files changed

+143
-9
lines changed

4 files changed

+143
-9
lines changed

example.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ queries:
33
# name of metric
44
- metric: azure_resources
55

6+
# skip metric publishing
7+
# and only publish sub metrics rows (and use configuration only for submetrics)
8+
# publish: false
9+
610
# Azure ResourceGraph query
711
query: |-
812
Resources

kusto/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type (
4545
Fields []ConfigQueryMetricField `yaml:"fields"`
4646
Labels map[string]string `yaml:"labels"`
4747
DefaultField ConfigQueryMetricField `yaml:"defaultField"`
48+
Publish *bool `yaml:"publish"`
4849
}
4950

5051
ConfigQueryMetricField struct {
@@ -111,6 +112,14 @@ func (c *ConfigQueryMetric) Validate() error {
111112
return nil
112113
}
113114

115+
func (c *ConfigQueryMetric) IsPublished() bool {
116+
if c.Publish != nil {
117+
return *c.Publish
118+
}
119+
120+
return true
121+
}
122+
114123
func (c *ConfigQueryMetricField) Validate() error {
115124
if c.Name == "" {
116125
return errors.New("no field name set")

kusto/parse.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func BuildPrometheusMetricList(name string, metricConfig ConfigQueryMetric, row
6464
}
6565
}
6666

67+
// check if metric should be skipped
6768
if isNewMetricRow {
6869
// save as own metric
6970
if _, ok := list[fieldConfig.Metric]; !ok {
@@ -123,11 +124,13 @@ func BuildPrometheusMetricList(name string, metricConfig ConfigQueryMetric, row
123124
}
124125

125126
// add main metrics
126-
for metricName, metricRow := range mainMetrics {
127-
if _, ok := list[metricName]; !ok {
128-
list[metricName] = []MetricRow{}
127+
if metricConfig.IsPublished() {
128+
for metricName, metricRow := range mainMetrics {
129+
if _, ok := list[metricName]; !ok {
130+
list[metricName] = []MetricRow{}
131+
}
132+
list[metricName] = append(list[metricName], *metricRow)
129133
}
130-
list[metricName] = append(list[metricName], *metricRow)
131134
}
132135

133136
// add id labels

kusto/parse_test.go

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package kusto
22

33
import (
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

105223
func parseResourceGraphJsonToResultRow(t *testing.T, data string) map[string]interface{} {

0 commit comments

Comments
 (0)