Skip to content

Commit

Permalink
feat: Azure Monitor Logs SLOs support [PC-11534] (#143)
Browse files Browse the repository at this point in the history
Co-authored-by: bsski <swedrowskibartosz@gmail.com>
  • Loading branch information
nikodemrafalski and BSski authored Feb 15, 2024
1 parent 4afffd3 commit 8bdf820
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ NAMESPACE=nobl9
NAME=nobl9
BIN_DIR=./bin
BINARY=$(BIN_DIR)/terraform-provider-$(NAME)
VERSION=0.23.0-beta
VERSION=0.24.0
BUILD_FLAGS="-X github.com/nobl9/terraform-provider-nobl9/nobl9.Version=$(VERSION)"
OS_ARCH?=linux_amd64

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ terraform {
required_providers {
nobl9 = {
source = "nobl9/nobl9"
version = "0.23.0-beta"
version = "0.24.0"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ words:
- gopath
- gosec
- govulncheck
- kusto
- ldflags
- logql
- msteams
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ terraform {
required_providers {
nobl9 = {
source = "nobl9/nobl9"
version = "0.23.0-beta"
version = "0.24.0"
}
}
}
Expand Down
88 changes: 68 additions & 20 deletions docs/resources/slo.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,17 @@ Required:

Required:

- `aggregation` (String) Aggregation type
- `data_type` (String) Specifies source: 'metrics' or 'logs'
- `metric_name` (String) Name of the metric
- `resource_id` (String) Name of the added application

Optional:

- `dimensions` (Block Set) Dimensions of the metric (see [below for nested schema](#nestedblock--objective--count_metrics--total--azure_monitor--dimensions))
- `metric_namespace` (String) Namespace of the metric
- `aggregation` (String) Aggregation type [Required for metrics]
- `dimensions` (Block Set) Dimensions of the metric [Optional for metrics] (see [below for nested schema](#nestedblock--objective--count_metrics--total--azure_monitor--dimensions))
- `kql_query` (String) Logs query in Kusto Query Language [Required for logs]
- `metric_name` (String) Name of the metric [Required for metrics]
- `metric_namespace` (String) Namespace of the metric [Optional for metrics]
- `resource_id` (String) Identifier of the Azure Cloud resource [Required for metrics]
- `workspace` (Block Set) Log analytics workspace [Required for logs] (see [below for nested schema](#nestedblock--objective--count_metrics--total--azure_monitor--workspace))

<a id="nestedblock--objective--count_metrics--total--azure_monitor--dimensions"></a>
### Nested Schema for `objective.count_metrics.total.azure_monitor.dimensions`
Expand All @@ -280,6 +282,16 @@ Required:
- `value` (String) Value


<a id="nestedblock--objective--count_metrics--total--azure_monitor--workspace"></a>
### Nested Schema for `objective.count_metrics.total.azure_monitor.workspace`

Required:

- `resource_group` (String) Resource group of the workspace
- `subscription_id` (String) Subscription ID of the workspace
- `workspace_id` (String) ID of the workspace



<a id="nestedblock--objective--count_metrics--total--bigquery"></a>
### Nested Schema for `objective.count_metrics.total.bigquery`
Expand Down Expand Up @@ -598,15 +610,17 @@ Required:

Required:

- `aggregation` (String) Aggregation type
- `data_type` (String) Specifies source: 'metrics' or 'logs'
- `metric_name` (String) Name of the metric
- `resource_id` (String) Name of the added application

Optional:

- `dimensions` (Block Set) Dimensions of the metric (see [below for nested schema](#nestedblock--objective--count_metrics--bad--azure_monitor--dimensions))
- `metric_namespace` (String) Namespace of the metric
- `aggregation` (String) Aggregation type [Required for metrics]
- `dimensions` (Block Set) Dimensions of the metric [Optional for metrics] (see [below for nested schema](#nestedblock--objective--count_metrics--bad--azure_monitor--dimensions))
- `kql_query` (String) Logs query in Kusto Query Language [Required for logs]
- `metric_name` (String) Name of the metric [Required for metrics]
- `metric_namespace` (String) Namespace of the metric [Optional for metrics]
- `resource_id` (String) Identifier of the Azure Cloud resource [Required for metrics]
- `workspace` (Block Set) Log analytics workspace [Required for logs] (see [below for nested schema](#nestedblock--objective--count_metrics--bad--azure_monitor--workspace))

<a id="nestedblock--objective--count_metrics--bad--azure_monitor--dimensions"></a>
### Nested Schema for `objective.count_metrics.bad.azure_monitor.dimensions`
Expand All @@ -617,6 +631,16 @@ Required:
- `value` (String) Value


<a id="nestedblock--objective--count_metrics--bad--azure_monitor--workspace"></a>
### Nested Schema for `objective.count_metrics.bad.azure_monitor.workspace`

Required:

- `resource_group` (String) Resource group of the workspace
- `subscription_id` (String) Subscription ID of the workspace
- `workspace_id` (String) ID of the workspace



<a id="nestedblock--objective--count_metrics--bad--bigquery"></a>
### Nested Schema for `objective.count_metrics.bad.bigquery`
Expand Down Expand Up @@ -935,15 +959,17 @@ Required:

Required:

- `aggregation` (String) Aggregation type
- `data_type` (String) Specifies source: 'metrics' or 'logs'
- `metric_name` (String) Name of the metric
- `resource_id` (String) Name of the added application

Optional:

- `dimensions` (Block Set) Dimensions of the metric (see [below for nested schema](#nestedblock--objective--count_metrics--good--azure_monitor--dimensions))
- `metric_namespace` (String) Namespace of the metric
- `aggregation` (String) Aggregation type [Required for metrics]
- `dimensions` (Block Set) Dimensions of the metric [Optional for metrics] (see [below for nested schema](#nestedblock--objective--count_metrics--good--azure_monitor--dimensions))
- `kql_query` (String) Logs query in Kusto Query Language [Required for logs]
- `metric_name` (String) Name of the metric [Required for metrics]
- `metric_namespace` (String) Namespace of the metric [Optional for metrics]
- `resource_id` (String) Identifier of the Azure Cloud resource [Required for metrics]
- `workspace` (Block Set) Log analytics workspace [Required for logs] (see [below for nested schema](#nestedblock--objective--count_metrics--good--azure_monitor--workspace))

<a id="nestedblock--objective--count_metrics--good--azure_monitor--dimensions"></a>
### Nested Schema for `objective.count_metrics.good.azure_monitor.dimensions`
Expand All @@ -954,6 +980,16 @@ Required:
- `value` (String) Value


<a id="nestedblock--objective--count_metrics--good--azure_monitor--workspace"></a>
### Nested Schema for `objective.count_metrics.good.azure_monitor.workspace`

Required:

- `resource_group` (String) Resource group of the workspace
- `subscription_id` (String) Subscription ID of the workspace
- `workspace_id` (String) ID of the workspace



<a id="nestedblock--objective--count_metrics--good--bigquery"></a>
### Nested Schema for `objective.count_metrics.good.bigquery`
Expand Down Expand Up @@ -1280,15 +1316,17 @@ Required:

Required:

- `aggregation` (String) Aggregation type
- `data_type` (String) Specifies source: 'metrics' or 'logs'
- `metric_name` (String) Name of the metric
- `resource_id` (String) Name of the added application

Optional:

- `dimensions` (Block Set) Dimensions of the metric (see [below for nested schema](#nestedblock--objective--raw_metric--query--azure_monitor--dimensions))
- `metric_namespace` (String) Namespace of the metric
- `aggregation` (String) Aggregation type [Required for metrics]
- `dimensions` (Block Set) Dimensions of the metric [Optional for metrics] (see [below for nested schema](#nestedblock--objective--raw_metric--query--azure_monitor--dimensions))
- `kql_query` (String) Logs query in Kusto Query Language [Required for logs]
- `metric_name` (String) Name of the metric [Required for metrics]
- `metric_namespace` (String) Namespace of the metric [Optional for metrics]
- `resource_id` (String) Identifier of the Azure Cloud resource [Required for metrics]
- `workspace` (Block Set) Log analytics workspace [Required for logs] (see [below for nested schema](#nestedblock--objective--raw_metric--query--azure_monitor--workspace))

<a id="nestedblock--objective--raw_metric--query--azure_monitor--dimensions"></a>
### Nested Schema for `objective.raw_metric.query.azure_monitor.dimensions`
Expand All @@ -1299,6 +1337,16 @@ Required:
- `value` (String) Value


<a id="nestedblock--objective--raw_metric--query--azure_monitor--workspace"></a>
### Nested Schema for `objective.raw_metric.query.azure_monitor.workspace`

Required:

- `resource_group` (String) Resource group of the workspace
- `subscription_id` (String) Subscription ID of the workspace
- `workspace_id` (String) ID of the workspace



<a id="nestedblock--objective--raw_metric--query--bigquery"></a>
### Nested Schema for `objective.raw_metric.query.bigquery`
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
nobl9 = {
source = "nobl9/nobl9"
version = "0.23.0-beta"
version = "0.24.0"
}
}
}
Expand Down
125 changes: 86 additions & 39 deletions nobl9/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
v1alphaSLO "github.com/nobl9/nobl9-go/manifest/v1alpha/slo"
v1Objects "github.com/nobl9/nobl9-go/sdk/endpoints/objects/v1"

Expand Down Expand Up @@ -1078,34 +1079,40 @@ func schemaMetricAzureMonitor() map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data_type": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringInSlice([]string{
v1alphaSLO.AzureMonitorDataTypeMetrics,
v1alphaSLO.AzureMonitorDataTypeLogs,
}, false),
),
Description: "Specifies source: 'metrics' or 'logs'",
},
"resource_id": {
Type: schema.TypeString,
Required: true,
Description: "Name of the added application",
Optional: true,
Description: "Identifier of the Azure Cloud resource [Required for metrics]",
},
"metric_namespace": {
Type: schema.TypeString,
Optional: true,
Description: "Namespace of the metric",
Description: "Namespace of the metric [Optional for metrics]",
},
"metric_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the metric",
Optional: true,
Description: "Name of the metric [Required for metrics]",
},
"aggregation": {
Type: schema.TypeString,
Required: true,
Description: "Aggregation type",
Optional: true,
Description: "Aggregation type [Required for metrics]",
},
"dimensions": {
Type: schema.TypeSet,
Optional: true,
Description: "Dimensions of the metric",
Description: "Dimensions of the metric [Optional for metrics]",
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -1122,6 +1129,35 @@ func schemaMetricAzureMonitor() map[string]*schema.Schema {
},
},
},
"workspace": {
Type: schema.TypeSet,
Optional: true,
Description: "Log analytics workspace [Required for logs]",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subscription_id": {
Type: schema.TypeString,
Required: true,
Description: "Subscription ID of the workspace",
},
"resource_group": {
Type: schema.TypeString,
Required: true,
Description: "Resource group of the workspace",
},
"workspace_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the workspace",
},
},
},
},
"kql_query": {
Type: schema.TypeString,
Optional: true,
Description: "Logs query in Kusto Query Language [Required for logs]",
},
},
},
},
Expand All @@ -1134,38 +1170,41 @@ func marshalAzureMonitorMetric(s *schema.Set) *v1alphaSLO.AzureMonitorMetric {
}

metric := s.List()[0].(map[string]interface{})

dataType := metric["data_type"].(string)
resourceID := metric["resource_id"].(string)
metricNamespace := metric["metric_namespace"].(string)
metricName := metric["metric_name"].(string)
aggregation := metric["aggregation"].(string)

dimensions := metric["dimensions"].(*schema.Set)
var metricDimensions []v1alphaSLO.AzureMonitorMetricDimension

if dimensions.Len() > 0 {
metricDimensions = make([]v1alphaSLO.AzureMonitorMetricDimension, dimensions.Len())
}

for idx, dimension := range dimensions.List() {
n9Dimension := dimension.(map[string]interface{})
name := n9Dimension["name"].(string)
value := n9Dimension["value"].(string)
metricDimensions[idx] = v1alphaSLO.AzureMonitorMetricDimension{
Name: &name,
Value: &value,
result := v1alphaSLO.AzureMonitorMetric{DataType: dataType}
if dataType == v1alphaSLO.AzureMonitorDataTypeMetrics {
result.ResourceID = metric["resource_id"].(string)
result.MetricNamespace = metric["metric_namespace"].(string)
result.MetricName = metric["metric_name"].(string)
result.Aggregation = metric["aggregation"].(string)
dimensions := metric["dimensions"].(*schema.Set)
var metricDimensions []v1alphaSLO.AzureMonitorMetricDimension
if dimensions.Len() > 0 {
metricDimensions = make([]v1alphaSLO.AzureMonitorMetricDimension, dimensions.Len())
}
for idx, dimension := range dimensions.List() {
n9Dimension := dimension.(map[string]interface{})
name := n9Dimension["name"].(string)
value := n9Dimension["value"].(string)
metricDimensions[idx] = v1alphaSLO.AzureMonitorMetricDimension{
Name: &name,
Value: &value,
}
}
result.Dimensions = metricDimensions
} else if dataType == v1alphaSLO.AzureMonitorDataTypeLogs {
result.KQLQuery = metric["kql_query"].(string)
wsList := metric["workspace"].(*schema.Set).List()
if len(wsList) > 0 {
workspace := wsList[0].(map[string]interface{})
result.Workspace = &v1alphaSLO.AzureMonitorMetricLogAnalyticsWorkspace{
SubscriptionID: workspace["subscription_id"].(string),
ResourceGroup: workspace["resource_group"].(string),
WorkspaceID: workspace["workspace_id"].(string),
}
}
}

return &v1alphaSLO.AzureMonitorMetric{
DataType: dataType,
ResourceID: resourceID,
MetricNamespace: metricNamespace,
MetricName: metricName,
Aggregation: aggregation,
Dimensions: metricDimensions,
}
return &result
}

func unmarshalAzureMonitorMetric(metric interface{}) map[string]interface{} {
Expand All @@ -1183,6 +1222,14 @@ func unmarshalAzureMonitorMetric(metric interface{}) map[string]interface{} {
var dimensions any
_ = json.Unmarshal(dim, &dimensions)
res["dimensions"] = dimensions
res["kql_query"] = amMetric.KQLQuery
if amMetric.Workspace != nil {
res["workspace"] = schema.NewSet(oneElementSet, []interface{}{map[string]interface{}{
"subscription_id": amMetric.Workspace.SubscriptionID,
"resource_group": amMetric.Workspace.ResourceGroup,
"workspace_id": amMetric.Workspace.WorkspaceID,
}})
}
return res
}

Expand Down
Loading

0 comments on commit 8bdf820

Please sign in to comment.