Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMS (SNS) stored queries from CWI into Terraform / SMS (pinpoint) new queries #1574

Merged
merged 17 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions aws/common/cloudwatch_queries.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
resource "aws_cloudwatch_query_definition" "sms-blocked-as-spam" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Block as spam"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
fields @timestamp as Timestamp, delivery.phoneCarrier as Carrier, delivery.providerResponse as `Provider response`, delivery.destination as `Destination phone number`
| filter delivery.providerResponse like 'spam'
| sort Timestamp desc
| limit 100
}
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-carrier-dwell-times" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Carrier dwell times"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries.name,
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
stats avg(delivery.dwellTimeMsUntilDeviceAck / 1000 / 60) as Avg_carrier_time_minutes,
| count(*) as Number by delivery.phoneCarrier as Carrier
QUERY

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The count(*) as Number should be on a new line for better readability and consistency with other queries.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is on a new line.

}

resource "aws_cloudwatch_query_definition" "sms-get-failures" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Get failures"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
fields @timestamp as Timestamp, status, delivery.phoneCarrier as Carrier, delivery.providerResponse as `Provider response`, delivery.destination as `Destination phone number`, notification.messageId as messageId, @message
| filter status = 'FAILURE'
| sort Timestamp desc
| limit 200
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-get-sms-logs-by-phone-number" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Get SMS logs by phone number"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries.name,
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
fields @timestamp as Timestamp, status as Status, notification.messageId as `Message ID`,
delivery.destination as `Destination phone number`, delivery.providerResponse as `Provider response`,
delivery.smsType as `Message type`
| filter delivery.destination like '1416xxxxxxx'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider parameterizing the phone number filter to avoid hardcoding '1416xxxxxxx'. This will make the query more flexible and reusable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can use parameters in these unfortunately. If that was a query in a dashboard, yeah but that is not the case.

| sort Timestamp desc
| limit 100
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-interntional-sending-status" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / International sending status"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries.name,
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
fields @timestamp, @message
| parse @message /"isoCountryCode":"(?<Country>[^"]+)"/
| parse @message /"eventType":"(?<Event_Type>[^"]+)"/
| parse @message /"isFinal":(?<Is_Final>\w+)/
| filter Is_Final = "true"
| stats count(*) as Event_Count by Country, Event_Type
| display Country, Event_Type, Event_Count
| sort Country asc
| limit 200
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-success-vs-unreachable" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Success vs Unreachable"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries.name,
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
fields @timestamp, delivery.providerResponse
| parse delivery.providerResponse "Phone is currently unreachable/*" as @unavailable
| parse delivery.providerResponse "Message has been * by phone" as @available
| sort @timestamp desc
| stats count(@unavailable), count(@available), count(*) by bin(1h)
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-unreachable-phone-numbers" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Success vs Unreachable"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name 'SMS (SNS) / Success vs Unreachable' is duplicated from the previous query definition. Consider renaming this to avoid confusion.


log_group_names = [
aws_cloudwatch_log_group.sns_deliveries_failures.name
]

query_string = <<QUERY
fields @timestamp, delivery.providerResponse
| filter delivery.providerResponse like "Phone is currently unreachable/unavailable"
| sort @timestamp desc
| limit 20
QUERY
}
37 changes: 36 additions & 1 deletion aws/pinpoint_to_sqs_sms_callbacks/cloudwatch_queries.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
resource "aws_cloudwatch_query_definition" "pinpoint-carrier-dwell-times" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the equivalent of sms-carrier-dwell-times but for pinpoint.

count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (Pinpoint) / Carrier dwell times"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name,
]

query_string = <<QUERY
stats avg((eventTimestamp - messageRequestTimestamp) / 1000 / 60) as Avg_carrier_time_minutes,
count(*) as Number by carrierName as Carrier
| filter isFinal
| sort by Avg_carrier_time_minutes desc
| limit 100
QUERY
}

resource "aws_cloudwatch_query_definition" "pinpoint-failures-by-carrier" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new query over here for failures by carrier.

count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (Pinpoint) / Failures by carrier"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name,
]

query_string = <<QUERY
filter isFinal
| filter messageStatus not like /DELIVERED|SUCCESSFUL/
| stats count(*) as Total by coalesce(carrierName, 'Unknown/VOIP') as Carrier, messageStatus as MessageStatus
| sort by Total desc
QUERY
}

resource "aws_cloudwatch_query_definition" "pinpoint-logs" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS / Pinpoint logs"
name = "SMS (Pinpoint) / Logs"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries.name
Expand Down
1 change: 0 additions & 1 deletion aws/quicksight/dataset_sms_usage_notifications.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ resource "aws_cloudformation_stack" "sms-usage-notifications" {
}
],


LogicalTableMap = {

sms-usage-notifications = {
Expand Down
Loading