Skip to content

Commit

Permalink
Add static labels to the CloudWatch jobs (#1974)
Browse files Browse the repository at this point in the history
* Add static labels to the CloudWatch jobs

* Fix type

* Update docs

* Update tests

* Remove fmt function

* Rename static_labels -> static_label

* apply comment

* update test

* Fix issue with an inconsistent static labels order

* Fix imports

* Change List to the Map for the static labels

* Fix docs
  • Loading branch information
andriikushch authored Feb 6, 2025
1 parent 484ce41 commit 35f15e2
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/data-sources/cloud_provider_aws_cloudwatch_scrape_job.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
}
scrape_interval_seconds = 300
}
static_labels = {
"label1" = "value1"
"label2" = "value2"
}
}
Expand Down Expand Up @@ -93,6 +98,7 @@ data "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
- `regions_subset_override_used` (Boolean) When true, the `regions` attribute will be the set of regions configured in the override. When false, the `regions` attribute will be the set of regions belonging to the AWS Account resource that is associated with this CloudWatch Scrape Job.
- `role_arn` (String) The AWS ARN of the IAM role associated with the AWS Account resource that is being used by this CloudWatch Scrape Job.
- `service` (Block List) One or more configuration blocks to dictate what this CloudWatch Scrape Job should scrape. Each block must have a distinct `name` attribute. When accessing this as an attribute reference, it is a list of objects. (see [below for nested schema](#nestedblock--service))
- `static_labels` (Map of String) A set of static labels to add to all metrics exported by this scrape job.

<a id="nestedblock--custom_namespace"></a>
### Nested Schema for `custom_namespace`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Read-Only:
- `role_arn` (String) The AWS ARN of the IAM role associated with the AWS Account resource that is being used by this CloudWatch Scrape Job.
- `service` (Block List) One or more configuration blocks to dictate what this CloudWatch Scrape Job should scrape. Each block must have a distinct `name` attribute. When accessing this as an attribute reference, it is a list of objects. (see [below for nested schema](#nestedblock--scrape_job--service))
- `stack_id` (String) The Stack ID of the Grafana Cloud instance. Part of the Terraform Resource ID.
- `static_labels` (Map of String) A set of static labels to add to all metrics exported by this scrape job.

<a id="nestedblock--scrape_job--custom_namespace"></a>
### Nested Schema for `scrape_job.custom_namespace`
Expand Down
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
}
scrape_interval_seconds = 300
}
static_labels = {
"label1" = "value1"
"label2" = "value2"
}
}
```

Expand Down
6 changes: 6 additions & 0 deletions docs/resources/cloud_provider_aws_cloudwatch_scrape_job.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
}
scrape_interval_seconds = 300
}
static_labels = {
"label1" = "value1"
"label2" = "value2"
}
}
```

Expand All @@ -82,6 +87,7 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
- `export_tags` (Boolean) When enabled, AWS resource tags are exported as Prometheus labels to metrics formatted as `aws_<service_name>_info`.
- `regions_subset_override` (Set of String) A subset of the regions that are configured in the associated AWS Account resource to apply to this scrape job. If not set or empty, all of the Account resource's regions are scraped.
- `service` (Block List) One or more configuration blocks to configure AWS services for the CloudWatch Scrape Job to scrape. Each block must have a distinct `name` attribute. When accessing this as an attribute reference, it is a list of objects. (see [below for nested schema](#nestedblock--service))
- `static_labels` (Map of String) A set of static labels to add to all metrics exported by this scrape job.

### Read-Only

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
}
scrape_interval_seconds = 300
}

static_labels = {
"label1" = "value1"
"label2" = "value2"
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
}
scrape_interval_seconds = 300
}

static_labels = {
"label1" = "value1"
"label2" = "value2"
}
}
3 changes: 3 additions & 0 deletions internal/common/cloudproviderapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type AWSCloudWatchScrapeJobRequest struct {
ExportTags bool `json:"exportTags"`
Services []AWSCloudWatchService `json:"services"`
CustomNamespaces []AWSCloudWatchCustomNamespace `json:"customNamespaces"`
StaticLabels map[string]string `json:"staticLabels"`
}
type AWSCloudWatchScrapeJobResponse struct {
Name string `json:"name"`
Expand All @@ -119,6 +120,8 @@ type AWSCloudWatchScrapeJobResponse struct {
ExportTags bool `json:"exportTags"`
Services []AWSCloudWatchService `json:"services"`
CustomNamespaces []AWSCloudWatchCustomNamespace `json:"customNamespaces"`
StaticLabels map[string]string `json:"staticLabels"`

// computed fields beyond the original request
RoleARN string `json:"roleARN"`
Regions []string `json:"regions"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

var (
Expand Down Expand Up @@ -56,6 +57,11 @@ var (
Description: "When the CloudWatch Scrape Job is disabled, this will show the reason that it is in that state.",
Computed: true,
},
"static_labels": schema.MapAttribute{
Description: "A set of static labels to add to all metrics exported by this scrape job.",
Computed: true,
ElementType: basetypes.StringType{},
},
},
Blocks: map[string]schema.Block{
"service": schema.ListNestedBlock{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestAccDataSourceAWSCloudWatchScrapeJob(t *testing.T) {
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.metric.0.statistics.#", fmt.Sprintf("%d", len(testAWSCloudWatchScrapeJobData.CustomNamespaces[0].Metrics[0].Statistics))),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.metric.0.statistics.0", testAWSCloudWatchScrapeJobData.CustomNamespaces[0].Metrics[0].Statistics[0]),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.scrape_interval_seconds", fmt.Sprintf("%d", testAWSCloudWatchScrapeJobData.CustomNamespaces[0].ScrapeIntervalSeconds)),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "static_labels.label1", testAWSCloudWatchScrapeJobData.StaticLabels["label1"]),
resource.TestCheckResourceAttr("data.grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "static_labels.label2", testAWSCloudWatchScrapeJobData.StaticLabels["label2"]),
),
},
// update to remove regions_subset_override so that the account's regions are used instead
Expand Down
28 changes: 28 additions & 0 deletions internal/resources/cloudprovider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type awsCWScrapeJobTFResourceModel struct {
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
Services types.List `tfsdk:"service"`
CustomNamespaces types.List `tfsdk:"custom_namespace"`
StaticLabels types.Map `tfsdk:"static_labels"`
}

type awsCWScrapeJobTFDataSourceModel struct {
Expand All @@ -44,6 +45,7 @@ type awsCWScrapeJobTFDataSourceModel struct {
// See https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes#nested-attribute-types
Services types.List `tfsdk:"service"`
CustomNamespaces types.List `tfsdk:"custom_namespace"`
StaticLabels types.Map `tfsdk:"static_labels"`
}

type awsCWScrapeJobServiceTFModel struct {
Expand Down Expand Up @@ -301,6 +303,12 @@ func (tfData awsCWScrapeJobTFResourceModel) toClientModel(ctx context.Context) (
}
}

diags = tfData.StaticLabels.ElementsAs(ctx, &converted.StaticLabels, false)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return cloudproviderapi.AWSCloudWatchScrapeJobRequest{}, conversionDiags
}

return converted, conversionDiags
}

Expand Down Expand Up @@ -342,6 +350,16 @@ func generateCloudWatchScrapeJobTFResourceModel(ctx context.Context, stackID str
}
converted.CustomNamespaces = customNamespaces

staticLabelsMap := types.MapValueMust(types.StringType, map[string]attr.Value{})
if scrapeJobData.StaticLabels != nil {
staticLabelsMap, diags = types.MapValueFrom(ctx, basetypes.StringType{}, scrapeJobData.StaticLabels)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFResourceModel{}, conversionDiags
}
}
converted.StaticLabels = staticLabelsMap

return converted, conversionDiags
}

Expand Down Expand Up @@ -381,6 +399,16 @@ func generateCloudWatchScrapeJobDataSourceTFModel(ctx context.Context, stackID s
}
converted.CustomNamespaces = customNamespaces

staticLabelsMap := types.MapValueMust(types.StringType, map[string]attr.Value{})
if scrapeJobData.StaticLabels != nil {
staticLabelsMap, diags = types.MapValueFrom(ctx, basetypes.StringType{}, scrapeJobData.StaticLabels)
conversionDiags.Append(diags...)
if conversionDiags.HasError() {
return awsCWScrapeJobTFDataSourceModel{}, conversionDiags
}
}
converted.StaticLabels = staticLabelsMap

return converted, conversionDiags
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
Expand Down Expand Up @@ -112,6 +113,13 @@ func (r resourceAWSCloudWatchScrapeJob) Schema(ctx context.Context, req resource
Description: "When the CloudWatch Scrape Job is disabled, this will show the reason that it is in that state.",
Computed: true,
},
"static_labels": schema.MapAttribute{
Description: "A set of static labels to add to all metrics exported by this scrape job.",
Optional: true,
Computed: true,
ElementType: basetypes.StringType{},
Default: mapdefault.StaticValue(types.MapValueMust(types.StringType, map[string]attr.Value{})),
},
},
Blocks: map[string]schema.Block{
"service": schema.ListNestedBlock{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var testAWSCloudWatchScrapeJobData = cloudproviderapi.AWSCloudWatchScrapeJobResp
},
},
},
StaticLabels: map[string]string{
"label1": "value1",
"label2": "value2",
},
}

func TestAccResourceAWSCloudWatchScrapeJob(t *testing.T) {
Expand Down Expand Up @@ -105,6 +109,8 @@ func TestAccResourceAWSCloudWatchScrapeJob(t *testing.T) {
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.metric.0.statistics.#", fmt.Sprintf("%d", len(testAWSCloudWatchScrapeJobData.CustomNamespaces[0].Metrics[0].Statistics))),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.metric.0.statistics.0", testAWSCloudWatchScrapeJobData.CustomNamespaces[0].Metrics[0].Statistics[0]),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "custom_namespace.0.scrape_interval_seconds", fmt.Sprintf("%d", testAWSCloudWatchScrapeJobData.CustomNamespaces[0].ScrapeIntervalSeconds)),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "static_labels.label1", testAWSCloudWatchScrapeJobData.StaticLabels["label1"]),
resource.TestCheckResourceAttr("grafana_cloud_provider_aws_cloudwatch_scrape_job.test", "static_labels.label2", testAWSCloudWatchScrapeJobData.StaticLabels["label2"]),
),
},
// update to remove regions_subset_override so that the account's regions are used instead
Expand Down Expand Up @@ -306,6 +312,12 @@ resource "grafana_cloud_provider_aws_cloudwatch_scrape_job" "test" {
stack_id = "%[1]s"
name = "%[2]s"
enabled = %[3]t
static_labels = {
"label1" = "value1"
"label2" = "value2"
}
aws_account_resource_id = "%[4]s"
regions_subset_override = [%[5]s]
export_tags = true
Expand Down

0 comments on commit 35f15e2

Please sign in to comment.