Skip to content

Commit 83c4635

Browse files
committed
Fix metrics filter with colon : in query value
Example of a metric filter affected by this: ``` cloudsql.googleapis.com/database:resource.labels.database_id="project-id:database-name" ``` + Added/updated tests + Updated README with this example and a note about quoting special characters Signed-off-by: Daniel Kontsek <[email protected]>
1 parent 11d3a0a commit 83c4635

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,18 @@ Example: \
165165

166166
The `filter_query` will be applied to a final metrics API query when querying for metric data. You can read more about the metric API filter options in GCPs documentation https://cloud.google.com/monitoring/api/v3/filters
167167

168-
The final query sent to the metrics API already includes filters for project and metric type. Each applicable `filter_query` will be appended to the query with an AND
168+
The final query sent to the metrics API already includes filters for project and metric type. Each applicable `filter_query` will be appended to the query with an `AND`. String filter values that contain special characters (e.g. `:` colon) must be quoted with quotation marks `"`. Please always check logs for potential syntax errors from GCP.
169169

170170
Full example
171171
```
172172
stackdriver_exporter \
173173
--google.project-ids=my-test-project \
174174
--monitoring.metrics-prefixes='pubsub.googleapis.com/subscription' \
175175
--monitoring.metrics-prefixes='compute.googleapis.com/instance/cpu' \
176+
--monitoring.metrics-prefixes='cloudsql.googleapis.com/database' \
176177
--monitoring.filters='pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match("us-west4.*my-team-subs.*")' \
177-
--monitoring.filters='compute.googleapis.com/instance/cpu:resource.labels.instance=monitoring.regex.full_match("us-west4.*my-team-subs.*")'
178+
--monitoring.filters='compute.googleapis.com/instance/cpu:resource.labels.instance=monitoring.regex.full_match("us-west4.*my-team-subs.*")' \
179+
--monitoring.filters='cloudsql.googleapis.com/database:resource.labels.database_id="my-test-project:my-database-name"'
178180
```
179181

180182
Using projects filter:

utils/utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func NormalizeMetricName(metricName string) string {
4242
}
4343

4444
func SplitExtraFilter(extraFilter string, separator string) (string, string) {
45-
mPrefix := strings.Split(extraFilter, separator)
46-
if mPrefix[0] == extraFilter {
45+
mPrefix := strings.SplitN(extraFilter, separator, 2)
46+
if len(mPrefix) != 2 {
4747
return "", ""
4848
}
49-
return mPrefix[0], strings.Join(mPrefix[1:], "")
49+
return mPrefix[0], mPrefix[1]
5050
}
5151

5252
func ProjectResource(projectID string) string {

utils/utils_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,23 @@ var _ = Describe("ProjectResource", func() {
3131
Expect(ProjectResource("fake-project-1")).To(Equal("projects/fake-project-1"))
3232
})
3333
})
34+
35+
var _ = Describe("SplitExtraFilter", func() {
36+
It("returns an empty string from incomplete filter", func() {
37+
metricPrefix, filterQuery := SplitExtraFilter("This_is__a-MetricName.Example/with/no/filter", ":")
38+
Expect(metricPrefix).To(Equal(""))
39+
Expect(filterQuery).To(Equal(""))
40+
})
41+
42+
It("returns a metric prefix and filter query from basic filter", func() {
43+
metricPrefix, filterQuery := SplitExtraFilter("This_is__a-MetricName.Example/with:filter.name=filter_value", ":")
44+
Expect(metricPrefix).To(Equal("This_is__a-MetricName.Example/with"))
45+
Expect(filterQuery).To(Equal("filter.name=filter_value"))
46+
})
47+
48+
It("returns a metric prefix and filter query filter with colon", func() {
49+
metricPrefix, filterQuery := SplitExtraFilter(`This_is__a-MetricName.Example/with:filter.name="filter:value"`, ":")
50+
Expect(metricPrefix).To(Equal("This_is__a-MetricName.Example/with"))
51+
Expect(filterQuery).To(Equal("filter.name=\"filter:value\""))
52+
})
53+
})

0 commit comments

Comments
 (0)