Skip to content

Commit

Permalink
feat: Support promql field in GCM terraform [PC-14302] (#371)
Browse files Browse the repository at this point in the history
## Motivation

Support promql field in GCM terraform.

## Summary

Example:
```
  objective {
    display_name = "obj1"
    name         = "tf-objective-1"
    target       = 0.7
    value        = 1
    op           = "lt"
    raw_metric {
      query {
        gcm {
          project_id = "project1"
		  promql = "sum(rate(http_requests_total{job=\"api-server\"}[5m]))"
		}
      }
    }
  }
```


## Related Changes

nobl9/nobl9-go#564


## Release Notes

Support promql field in GCM terraform
  • Loading branch information
dawidwisn authored Jan 24, 2025
1 parent 46e575b commit eed1aba
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 14 deletions.
40 changes: 30 additions & 10 deletions docs/resources/slo.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Optional:
- `datadog` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/datadog#creating-slos-with-datadog) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--datadog))
- `dynatrace` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/dynatrace#creating-slos-with-dynatrace) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--dynatrace))
- `elasticsearch` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/elasticsearch#creating-slos-with-elasticsearch) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--elasticsearch))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--gcm))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--gcm))
- `grafana_loki` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/grafana-loki#creating-slos-with-grafana-loki) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--grafana_loki))
- `graphite` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/graphite#creating-slos-with-graphite) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--graphite))
- `honeycomb` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/honeycomb#creating-slos-with-honeycomb) (see [below for nested schema](#nestedblock--objective--count_metrics--bad--honeycomb))
Expand Down Expand Up @@ -446,7 +446,11 @@ Required:
Required:

- `project_id` (String) Project ID
- `query` (String) Query for the metrics

Optional:

- `promql` (String) Query for the metrics in PromQL format
- `query` (String) Query for the metrics in MQL format ([deprecated](https://cloud.google.com/stackdriver/docs/deprecations/mql))


<a id="nestedblock--objective--count_metrics--bad--grafana_loki"></a>
Expand Down Expand Up @@ -677,7 +681,7 @@ Optional:
- `datadog` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/datadog#creating-slos-with-datadog) (see [below for nested schema](#nestedblock--objective--count_metrics--good--datadog))
- `dynatrace` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/dynatrace#creating-slos-with-dynatrace) (see [below for nested schema](#nestedblock--objective--count_metrics--good--dynatrace))
- `elasticsearch` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/elasticsearch#creating-slos-with-elasticsearch) (see [below for nested schema](#nestedblock--objective--count_metrics--good--elasticsearch))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--good--gcm))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--good--gcm))
- `grafana_loki` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/grafana-loki#creating-slos-with-grafana-loki) (see [below for nested schema](#nestedblock--objective--count_metrics--good--grafana_loki))
- `graphite` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/graphite#creating-slos-with-graphite) (see [below for nested schema](#nestedblock--objective--count_metrics--good--graphite))
- `honeycomb` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/honeycomb#creating-slos-with-honeycomb) (see [below for nested schema](#nestedblock--objective--count_metrics--good--honeycomb))
Expand Down Expand Up @@ -817,7 +821,11 @@ Required:
Required:

- `project_id` (String) Project ID
- `query` (String) Query for the metrics

Optional:

- `promql` (String) Query for the metrics in PromQL format
- `query` (String) Query for the metrics in MQL format ([deprecated](https://cloud.google.com/stackdriver/docs/deprecations/mql))


<a id="nestedblock--objective--count_metrics--good--grafana_loki"></a>
Expand Down Expand Up @@ -1048,7 +1056,7 @@ Optional:
- `datadog` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/datadog#creating-slos-with-datadog) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--datadog))
- `dynatrace` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/dynatrace#creating-slos-with-dynatrace) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--dynatrace))
- `elasticsearch` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/elasticsearch#creating-slos-with-elasticsearch) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--elasticsearch))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--gcm))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--gcm))
- `grafana_loki` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/grafana-loki#creating-slos-with-grafana-loki) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--grafana_loki))
- `graphite` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/graphite#creating-slos-with-graphite) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--graphite))
- `honeycomb` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/honeycomb#creating-slos-with-honeycomb) (see [below for nested schema](#nestedblock--objective--count_metrics--good_total--honeycomb))
Expand Down Expand Up @@ -1188,7 +1196,11 @@ Required:
Required:

- `project_id` (String) Project ID
- `query` (String) Query for the metrics

Optional:

- `promql` (String) Query for the metrics in PromQL format
- `query` (String) Query for the metrics in MQL format ([deprecated](https://cloud.google.com/stackdriver/docs/deprecations/mql))


<a id="nestedblock--objective--count_metrics--good_total--grafana_loki"></a>
Expand Down Expand Up @@ -1419,7 +1431,7 @@ Optional:
- `datadog` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/datadog#creating-slos-with-datadog) (see [below for nested schema](#nestedblock--objective--count_metrics--total--datadog))
- `dynatrace` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/dynatrace#creating-slos-with-dynatrace) (see [below for nested schema](#nestedblock--objective--count_metrics--total--dynatrace))
- `elasticsearch` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/elasticsearch#creating-slos-with-elasticsearch) (see [below for nested schema](#nestedblock--objective--count_metrics--total--elasticsearch))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--total--gcm))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--count_metrics--total--gcm))
- `grafana_loki` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/grafana-loki#creating-slos-with-grafana-loki) (see [below for nested schema](#nestedblock--objective--count_metrics--total--grafana_loki))
- `graphite` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/graphite#creating-slos-with-graphite) (see [below for nested schema](#nestedblock--objective--count_metrics--total--graphite))
- `honeycomb` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/honeycomb#creating-slos-with-honeycomb) (see [below for nested schema](#nestedblock--objective--count_metrics--total--honeycomb))
Expand Down Expand Up @@ -1559,7 +1571,11 @@ Required:
Required:

- `project_id` (String) Project ID
- `query` (String) Query for the metrics

Optional:

- `promql` (String) Query for the metrics in PromQL format
- `query` (String) Query for the metrics in MQL format ([deprecated](https://cloud.google.com/stackdriver/docs/deprecations/mql))


<a id="nestedblock--objective--count_metrics--total--grafana_loki"></a>
Expand Down Expand Up @@ -1798,7 +1814,7 @@ Optional:
- `datadog` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/datadog#creating-slos-with-datadog) (see [below for nested schema](#nestedblock--objective--raw_metric--query--datadog))
- `dynatrace` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/dynatrace#creating-slos-with-dynatrace) (see [below for nested schema](#nestedblock--objective--raw_metric--query--dynatrace))
- `elasticsearch` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/elasticsearch#creating-slos-with-elasticsearch) (see [below for nested schema](#nestedblock--objective--raw_metric--query--elasticsearch))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--raw_metric--query--gcm))
- `gcm` (Block Set) [Configuration documentation](https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring) (see [below for nested schema](#nestedblock--objective--raw_metric--query--gcm))
- `grafana_loki` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/grafana-loki#creating-slos-with-grafana-loki) (see [below for nested schema](#nestedblock--objective--raw_metric--query--grafana_loki))
- `graphite` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/graphite#creating-slos-with-graphite) (see [below for nested schema](#nestedblock--objective--raw_metric--query--graphite))
- `honeycomb` (Block Set) [Configuration documentation](https://docs.nobl9.com/Sources/honeycomb#creating-slos-with-honeycomb) (see [below for nested schema](#nestedblock--objective--raw_metric--query--honeycomb))
Expand Down Expand Up @@ -1938,7 +1954,11 @@ Required:
Required:

- `project_id` (String) Project ID
- `query` (String) Query for the metrics

Optional:

- `promql` (String) Query for the metrics in PromQL format
- `query` (String) Query for the metrics in MQL format ([deprecated](https://cloud.google.com/stackdriver/docs/deprecations/mql))


<a id="nestedblock--objective--raw_metric--query--grafana_loki"></a>
Expand Down
16 changes: 12 additions & 4 deletions nobl9/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ func unmarshalElasticsearchMetric(metric interface{}) map[string]interface{} {

/**
* Google Cloud Monitoring (GCM) Metric
* https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring
* https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring
*/
const gcmMetric = "gcm"

Expand All @@ -1674,7 +1674,7 @@ func schemaMetricGCM() map[string]*schema.Schema {
Type: schema.TypeSet,
Optional: true,
Description: "[Configuration documentation]" +
"(https://docs.nobl9.com/Sources/google-cloud-monitoring#creating-slos-with-google-cloud-monitoring)",
"(https://docs.nobl9.com/sources/google-cloud-monitoring/#creating-slos-with-google-cloud-monitoring)",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Expand All @@ -1683,9 +1683,15 @@ func schemaMetricGCM() map[string]*schema.Schema {
Description: "Project ID",
},
"query": {
Type: schema.TypeString,
Optional: true,
Description: "Query for the metrics in MQL format" +
" ([deprecated](https://cloud.google.com/stackdriver/docs/deprecations/mql))",
},
"promql": {
Type: schema.TypeString,
Required: true,
Description: "Query for the metrics",
Optional: true,
Description: "Query for the metrics in PromQL format",
},
},
},
Expand All @@ -1703,6 +1709,7 @@ func marshalGCMMetric(s *schema.Set) *v1alphaSLO.GCMMetric {
return &v1alphaSLO.GCMMetric{
ProjectID: metric["project_id"].(string),
Query: metric["query"].(string),
PromQL: metric["promql"].(string),
}
}

Expand All @@ -1714,6 +1721,7 @@ func unmarshalGCMMetric(metric interface{}) map[string]interface{} {
res := make(map[string]interface{})
res["project_id"] = gMetric.ProjectID
res["query"] = gMetric.Query
res["promql"] = gMetric.PromQL

return res
}
Expand Down
62 changes: 62 additions & 0 deletions nobl9/resource_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestAcc_Nobl9SLO(t *testing.T) {
{"test-composite-with-value", testCompositeSLOValueZeroBackwardCompatibility},
{"test-datadog", testDatadogSLO},
{"test-dynatrace", testDynatraceSLO},
{"test-google-cloud-monitoring", testGoogleCloudMonitoringPromQLSLO},
{"test-grafanaloki", testGrafanaLokiSLO},
{"test-graphite", testGraphiteSLO},
{"test-honeycomb", testHoneycombSLO},
Expand Down Expand Up @@ -1325,6 +1326,67 @@ resource "nobl9_slo" ":name" {
return config
}

func testGoogleCloudMonitoringPromQLSLO(name string) string {
var serviceName = name + "-tf-service"
var agentName = name + "-tf-agent"
config :=
testService(serviceName) +
testGoogleCloudMonitoringAgent(agentName) + `
resource "nobl9_slo" ":name" {
name = ":name"
display_name = ":name"
project = ":project"
service = nobl9_service.:serviceName.name
label {
key = "team"
values = ["green","sapphire"]
}
label {
key = "env"
values = ["dev", "staging", "prod"]
}
budgeting_method = "Occurrences"
objective {
display_name = "obj1"
name = "tf-objective-1"
target = 0.7
value = 1
op = "lt"
raw_metric {
query {
gcm {
project_id = "project1"
promql = "sum(rate(http_requests_total{job=\"api-server\"}[5m]))"
}
}
}
}
time_window {
count = 10
is_rolling = true
unit = "Minute"
}
indicator {
name = nobl9_agent.:agentName.name
project = ":project"
kind = "Agent"
}
}
`
config = strings.ReplaceAll(config, ":name", name)
config = strings.ReplaceAll(config, ":serviceName", serviceName)
config = strings.ReplaceAll(config, ":agentName", agentName)
config = strings.ReplaceAll(config, ":project", testProject)

return config
}

func testGrafanaLokiSLO(name string) string {
var serviceName = name + "-tf-service"
var agentName = name + "-tf-agent"
Expand Down

0 comments on commit eed1aba

Please sign in to comment.