Skip to content

Commit

Permalink
Merge pull request #19625 from roberth-k/f-aws_cloudwatch_log_metric_…
Browse files Browse the repository at this point in the history
…filter-dimensions

r/aws_cloudwatch_log_metric_filter: support dimensions
  • Loading branch information
ewbankkit authored Jun 2, 2021
2 parents deccb39 + 91997a4 commit 15a426b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/19625.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cloudwatch_log_metric_filter: Add `dimensions` argument to `metric_transformation` configuration block
```
5 changes: 5 additions & 0 deletions aws/resource_aws_cloudwatch_log_metric_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func resourceAwsCloudWatchLogMetricFilter() *schema.Resource {
Optional: true,
ValidateFunc: validateTypeStringNullableFloat,
},
"dimensions": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
Expand Down
75 changes: 75 additions & 0 deletions aws/resource_aws_cloudwatch_log_metric_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.name", "EventCount"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.namespace", "YourNamespace"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.value", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.dimensions.%", "0"),
testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
MetricName: aws.String("EventCount"),
MetricNamespace: aws.String("YourNamespace"),
Expand All @@ -60,6 +61,7 @@ func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.namespace", "MyNamespace"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.value", "2"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.default_value", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.dimensions.%", "0"),
testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
MetricName: aws.String("AccessDeniedCount"),
MetricNamespace: aws.String("MyNamespace"),
Expand All @@ -68,6 +70,32 @@ func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
}),
),
},
{
Config: testAccAWSCloudWatchLogMetricFilterConfigModifiedWithDimensions(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchLogMetricFilterExists(resourceName, &mf),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "pattern", "{ $.errorCode = \"AccessDenied\" }"),
testAccCheckCloudWatchLogMetricFilterPattern(&mf, "{ $.errorCode = \"AccessDenied\" }"),
resource.TestCheckResourceAttr(resourceName, "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.name", "AccessDeniedCount"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.namespace", "MyNamespace"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.value", "2"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.dimensions.%", "2"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.dimensions.ErrorCode", "$.errorCode"),
resource.TestCheckResourceAttr(resourceName, "metric_transformation.0.dimensions.Dummy", "$.dummy"),
testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
MetricName: aws.String("AccessDeniedCount"),
MetricNamespace: aws.String("MyNamespace"),
MetricValue: aws.String("2"),
Dimensions: aws.StringMap(map[string]string{
"ErrorCode": "$.errorCode",
"Dummy": "$.dummy",
}),
}),
),
},
{
Config: testAccAWSCloudwatchLogMetricFilterConfigMany(rInt),
Check: testAccCheckCloudwatchLogMetricFilterManyExist("aws_cloudwatch_log_metric_filter.count_dracula", &mf),
Expand Down Expand Up @@ -130,6 +158,24 @@ func testAccCheckCloudWatchLogMetricFilterTransformation(mf *cloudwatchlogs.Metr
*expected.DefaultValue, *given.DefaultValue)
}

if len(expected.Dimensions) > 0 || len(given.Dimensions) > 0 {
e, g := aws.StringValueMap(expected.Dimensions), aws.StringValueMap(given.Dimensions)

if len(e) != len(g) {
return fmt.Errorf("Expected %d dimensions, received %d", len(e), len(g))
}

for ek, ev := range e {
gv, ok := g[ek]
if !ok {
return fmt.Errorf("Expected dimension %s, received nothing", ek)
}
if gv != ev {
return fmt.Errorf("Expected dimension %s to be %s, received %s", ek, ev, gv)
}
}
}

return nil
}
}
Expand Down Expand Up @@ -231,6 +277,35 @@ resource "aws_cloudwatch_log_group" "dada" {
`, rInt, rInt)
}

func testAccAWSCloudWatchLogMetricFilterConfigModifiedWithDimensions(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" {
name = "MyAppAccessCount-%d"
pattern = <<PATTERN
{ $.errorCode = "AccessDenied" }
PATTERN
log_group_name = aws_cloudwatch_log_group.dada.name
metric_transformation {
name = "AccessDeniedCount"
namespace = "MyNamespace"
value = "2"
dimensions = {
ErrorCode = "$.errorCode"
Dummy = "$.dummy"
}
}
}
resource "aws_cloudwatch_log_group" "dada" {
name = "MyApp/access-%d.log"
}
`, rInt, rInt)
}

func testAccAWSCloudwatchLogMetricFilterConfigMany(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "count_dracula" {
Expand Down
10 changes: 10 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,10 @@ func expandCloudWatchLogMetricTransformations(m map[string]interface{}) []*cloud
transformation.DefaultValue = aws.Float64(value)
}

if dims := m["dimensions"].(map[string]interface{}); len(dims) > 0 {
transformation.Dimensions = expandStringMap(dims)
}

return []*cloudwatchlogs.MetricTransformation{&transformation}
}

Expand All @@ -1897,6 +1901,12 @@ func flattenCloudWatchLogMetricTransformations(ts []*cloudwatchlogs.MetricTransf
m["default_value"] = strconv.FormatFloat(aws.Float64Value(ts[0].DefaultValue), 'f', -1, 64)
}

if dims := ts[0].Dimensions; len(dims) > 0 {
m["dimensions"] = pointersMapToStringList(dims)
} else {
m["dimensions"] = nil
}

mts = append(mts, m)

return mts
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/cloudwatch_log_metric_filter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ The `metric_transformation` block supports the following arguments:
* `name` - (Required) The name of the CloudWatch metric to which the monitored log information should be published (e.g. `ErrorCount`)
* `namespace` - (Required) The destination namespace of the CloudWatch metric.
* `value` - (Required) What to publish to the metric. For example, if you're counting the occurrences of a particular term like "Error", the value will be "1" for each occurrence. If you're counting the bytes transferred the published value will be the value in the log event.
* `default_value` - (Optional) The value to emit when a filter pattern does not match a log event.
* `default_value` - (Optional) The value to emit when a filter pattern does not match a log event. Conflicts with `dimensions`.
* `dimensions` - (Optional) Map of fields to use as dimensions for the metric. Up to 3 dimensions are allowed. Conflicts with `default_value`.

## Attributes Reference

Expand Down

0 comments on commit 15a426b

Please sign in to comment.