Skip to content

feat: add support for datadog metrics in third_party_integration #3253

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions docs/data-sources/third_party_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Additional values based on Type
* `DATADOG`
* `api_key` - Your API Key.
* `region` - Two-letter code that indicates which API URL to use. See the `region` response field of [MongoDB API Third-Party Service Integration documentation](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Third-Party-Integrations/operation/getThirdPartyIntegration) for more details. Datadog will use "US" by default.
* `send_collection_latency_metrics` - Toggle sending collection latency metrics that includes database names and collection namesand latency metrics on reads, writes, commands, and transactions.
* `send_database_metrics` - Toggle sending database metrics that includes database names and metrics on the number of collections, storage size, and index size.
* `OPS_GENIE`
* `api_key` - Your API Key.
* `region` - Two-letter code that indicates which API URL to use. See the `region` response field of [MongoDB API Third-Party Service Integration documentation](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Third-Party-Integrations/operation/getThirdPartyIntegration) for more details. Opsgenie will use US by default.
Expand Down
2 changes: 2 additions & 0 deletions docs/data-sources/third_party_integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Additional values based on Type
* `DATADOG`
* `api_key` - Your API Key.
* `region` - Two-letter code that indicates which API URL to use. See the `region` response field of [MongoDB API Third-Party Service Integration documentation](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Third-Party-Integrations/operation/getThirdPartyIntegration) for more details. Datadog will use "US" by default.
* `send_collection_latency_metrics` - Toggle sending collection latency metrics that includes database names and collection namesand latency metrics on reads, writes, commands, and transactions.
* `send_database_metrics` - Toggle sending database metrics that includes database names and metrics on the number of collections, storage size, and index size.
* `OPS_GENIE`
* `api_key` - Your API Key.
* `region` - Two-letter code that indicates which API URL to use. See the `region` response field of [MongoDB API Third-Party Service Integration documentation](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Third-Party-Integrations/operation/getThirdPartyIntegration) for more details. Opsgenie will use US by default.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/third_party_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ resource "mongodbatlas_third_party_integration" "test_datadog" {
* `DATADOG`
* `api_key` - Your API Key.
* `region` (Required) - Two-letter code that indicates which API URL to use. See the `region` request parameter of [MongoDB API Third-Party Service Integration documentation](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Third-Party-Integrations/operation/createThirdPartyIntegration) for more details. Datadog will use "US" by default.
* `send_collection_latency_metrics` - Toggle sending collection latency metrics that includes database names and collection namesand latency metrics on reads, writes, commands, and transactions. Default: `false`.
* `send_database_metrics` - Toggle sending database metrics that includes database names and metrics on the number of collections, storage size, and index size. Default: `false`.
* `OPS_GENIE`
* `api_key` - Your API Key.
* `region` (Required) - Two-letter code that indicates which API URL to use. See the `region` request parameter of [MongoDB API Third-Party Service Integration documentation](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Third-Party-Integrations/operation/createThirdPartyIntegration) for more details. OpsGenie will use "US" by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ resource "mongodbatlas_third_party_integration" "test-datadog" {
type = "DATADOG"
api_key = var.datadog_api_key
region = var.datadog_region

send_collection_latency_metrics = var.send_collection_latency_metrics
send_database_metrics = var.send_database_metrics
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ variable "cluster_name" {
default = "datadog-test-cluster"
type = string
}

variable "send_collection_latency_metrics" {
description = "Send collection latency metrics"
default = false
type = bool
}

variable "send_database_metrics" {
description = "Send database metrics"
default = false
type = bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ func thirdPartyIntegrationSchema() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"send_collection_latency_metrics": {
Type: schema.TypeBool,
Optional: true,
},
"send_database_metrics": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,36 @@ func integrationToSchema(d *schema.ResourceData, integration *admin.ThirdPartyIn
if integrationSchema.Url == nil {
integrationSchema.Url = integration.Url
}
if integrationSchema.SendCollectionLatencyMetrics == nil {
integrationSchema.SendCollectionLatencyMetrics = integration.SendCollectionLatencyMetrics
}
if integrationSchema.SendDatabaseMetrics == nil {
integrationSchema.SendDatabaseMetrics = integration.SendDatabaseMetrics
}

out := map[string]any{
"id": integration.Id,
"type": integration.Type,
"api_key": integrationSchema.ApiKey,
"region": integration.Region,
"service_key": integrationSchema.ServiceKey,
"team_name": integration.TeamName,
"channel_name": integration.ChannelName,
"routing_key": integration.RoutingKey,
"url": integrationSchema.Url,
"secret": integrationSchema.Secret,
"microsoft_teams_webhook_url": integrationSchema.MicrosoftTeamsWebhookUrl,
"user_name": integration.Username,
"password": integrationSchema.Password,
"service_discovery": integration.ServiceDiscovery,
"enabled": integration.Enabled,
"id": integration.Id,
"type": integration.Type,
"api_key": integrationSchema.ApiKey,
"region": integration.Region,
"service_key": integrationSchema.ServiceKey,
"team_name": integration.TeamName,
"channel_name": integration.ChannelName,
"routing_key": integration.RoutingKey,
"url": integrationSchema.Url,
"secret": integrationSchema.Secret,
"microsoft_teams_webhook_url": integrationSchema.MicrosoftTeamsWebhookUrl,
"user_name": integration.Username,
"password": integrationSchema.Password,
"service_discovery": integration.ServiceDiscovery,
"enabled": integration.Enabled,
"send_collection_latency_metrics": integration.SendCollectionLatencyMetrics,
"send_database_metrics": integration.SendDatabaseMetrics,
}

// removing optional empty values, terraform complains about unexpected values even though they're empty
optionals := []string{"api_key", "region", "service_key",
"team_name", "channel_name", "url", "secret", "password"}
"team_name", "channel_name", "url", "secret", "password", "send_collection_latency_metrics", "send_database_metrics"}

for _, attr := range optionals {
if val, ok := out[attr]; ok {
Expand Down Expand Up @@ -183,6 +191,14 @@ func schemaToIntegration(in *schema.ResourceData) (out *admin.ThirdPartyIntegrat
out.Enabled = admin.PtrBool(enabled.(bool))
}

if sendCollectionLatencyMetrics, ok := in.GetOk("send_collection_latency_metrics"); ok {
out.SendCollectionLatencyMetrics = admin.PtrBool(sendCollectionLatencyMetrics.(bool))
}

if sendDatabaseMetrics, ok := in.GetOk("send_database_metrics"); ok {
out.SendDatabaseMetrics = admin.PtrBool(sendDatabaseMetrics.(bool))
}

return out
}

Expand Down Expand Up @@ -236,4 +252,12 @@ func updateIntegrationFromSchema(d *schema.ResourceData, integration *admin.Thir
if d.HasChange("enabled") {
integration.Enabled = admin.PtrBool(d.Get("enabled").(bool))
}

if d.HasChange("send_collection_latency_metrics") {
integration.SendCollectionLatencyMetrics = admin.PtrBool(d.Get("send_collection_latency_metrics").(bool))
}

if d.HasChange("send_database_metrics") {
integration.SendDatabaseMetrics = admin.PtrBool(d.Get("send_database_metrics").(bool))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var integrationTypes = []string{

var requiredPerType = map[string][]string{
"PAGER_DUTY": {"service_key"},
"DATADOG": {"api_key", "region"},
"DATADOG": {"api_key", "region", "send_collection_latency_metrics", "send_database_metrics"},
"OPS_GENIE": {"api_key", "region"},
"VICTOR_OPS": {"api_key"},
"WEBHOOK": {"url"},
Expand Down Expand Up @@ -129,6 +129,16 @@ func Resource() *schema.Resource {
Computed: true,
Optional: true,
},
"send_collection_latency_metrics": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"send_database_metrics": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,42 @@ func datadogTest(tb testing.TB) *resource.TestCase {
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configDatadog(projectID, apiKey, "US"),
Config: configDatadog(projectID, apiKey, "US", false, false),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", apiKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "false"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "false"),
resource.TestCheckResourceAttr(dataSourceName, "type", intType),
resource.TestCheckResourceAttr(dataSourceName, "region", region),
resource.TestCheckResourceAttr(dataSourceName, "send_collection_latency_metrics", "false"),
resource.TestCheckResourceAttr(dataSourceName, "send_database_metrics", "false"),
),
},
{
Config: configDatadog(projectID, updatedAPIKey, "US"),
Config: configDatadog(projectID, updatedAPIKey, "US", false, false),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", updatedAPIKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "false"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "false"),
),
},
{
Config: configDatadog(projectID, updatedAPIKey, "US", true, true),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "type", intType),
resource.TestCheckResourceAttr(resourceName, "api_key", updatedAPIKey),
resource.TestCheckResourceAttr(resourceName, "region", region),
resource.TestCheckResourceAttr(resourceName, "send_collection_latency_metrics", "true"),
resource.TestCheckResourceAttr(resourceName, "send_database_metrics", "true"),
resource.TestCheckResourceAttr(dataSourceName, "send_collection_latency_metrics", "true"),
resource.TestCheckResourceAttr(dataSourceName, "send_database_metrics", "true"),
),
},
importStep(resourceName),
Expand Down Expand Up @@ -424,19 +443,23 @@ func configVictorOps(projectID, apiKey string) string {
) + singularDataStr
}

func configDatadog(projectID, apiKey, region string) string {
func configDatadog(projectID, apiKey, region string, sendCollectionLatencyMetrics, sendDatabaseMetrics bool) string {
return fmt.Sprintf(`
resource "mongodbatlas_third_party_integration" "test" {
project_id = "%[1]s"
type = "%[2]s"
api_key = "%[3]s"
region ="%[4]s"
send_collection_latency_metrics = %[5]t
send_database_metrics = %[6]t
}
`,
projectID,
"DATADOG",
apiKey,
region,
sendCollectionLatencyMetrics,
sendDatabaseMetrics,
) + singularDataStr
}

Expand Down