Skip to content

Commit

Permalink
Added Percona's MongoDB exporter to observe database related metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AncientPatata committed Oct 17, 2024
1 parent bea2aea commit 3eec0e1
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 1 deletion.
41 changes: 41 additions & 0 deletions monitoring/onpremise/exporters/mongodb-exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.21.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | >= 2.21.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [kubernetes_deployment.mongodb_exporter](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment) | resource |
| [kubernetes_service.mongodb_exporter_service](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/service) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_certif_mount"></a> [certif\_mount](#input\_certif\_mount) | MongoDB certificate mount secret | <pre>map(object({<br> secret = string<br> path = string<br> mode = string<br> }))</pre> | n/a | yes |
| <a name="input_docker_image"></a> [docker\_image](#input\_docker\_image) | Docker image for partition metrics exporter | <pre>object({<br> image = string<br> tag = string<br> image_pull_secrets = string<br> })</pre> | n/a | yes |
| <a name="input_mongo_url"></a> [mongo\_url](#input\_mongo\_url) | Full MongoDB URI with credentials and tls options included | `string` | n/a | yes |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Namespace of ArmoniK resources | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_namespace"></a> [namespace](#output\_namespace) | Namespace of the MongoDB metrics exporter |
| <a name="output_url"></a> [url](#output\_url) | Url of the MongoDB Metrics exporter |
<!-- END_TF_DOCS -->
96 changes: 96 additions & 0 deletions monitoring/onpremise/exporters/mongodb-exporter/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
resource "kubernetes_deployment" "mongodb_exporter" {
metadata {
name = "mongodb-metrics-exporter"
namespace = var.namespace
labels = {
app = "armonik"
type = "monitoring"
service = "mongodb-metrics-exporter"
}
}

spec {
replicas = 1
selector {
match_labels = {
app = "armonik"
type = "monitoring"
service = "mongodb-metrics-exporter"
}
}

template {
metadata {
name = "mongodb-metrics-exporter"
namespace = var.namespace
labels = {
app = "armonik"
type = "monitoring"
service = "mongodb-metrics-exporter"
}
}
spec {
container {
name = "mongodb-metrics-exporter"
image = var.docker_image.tag != "" ? "${var.docker_image.image}:${var.docker_image.tag}" : var.docker_image.image

env {
name = "MONGODB_URI"
value = var.mongo_url
}

dynamic "volume_mount" {
for_each = var.certif_mount
content {
mount_path = volume_mount.value.path
name = volume_mount.value.secret
read_only = true
}
}

args = ["--log.level=error", "--collector.diagnosticdata", "--collector.replicasetstatus", "--collector.dbstats", "--collector.dbstatsfreestorage", "--collector.topmetrics", "--collector.currentopmetrics", "--collector.indexstats", "--collector.collstats", "--discovering-mode", "--mongodb.uri=$(MONGODB_URI)"]
}

dynamic "volume" {
for_each = var.certif_mount
content {
name = volume.value.secret
secret {
secret_name = volume.value.secret
default_mode = volume.value.mode
}
}
}

}
}
}
}

resource "kubernetes_service" "mongodb_exporter_service" {
metadata {
name = "mongodb-metrics-exporter"
namespace = var.namespace
labels = {
app = "armonik"
type = "monitoring"
service = "mongodb-metrics-exporter"
}
}

spec {
selector = {
app = "armonik"
type = "monitoring"
service = "mongodb-metrics-exporter"
}

port {
port = 9216
target_port = 9216
protocol = "TCP"
}

type = "ClusterIP"
}
}
11 changes: 11 additions & 0 deletions monitoring/onpremise/exporters/mongodb-exporter/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# MongoDB metrics exporter

output "url" {
description = "Url of the MongoDB Metrics exporter"
value = "${kubernetes_service.mongodb_exporter_service.spec[0].cluster_ip}:${kubernetes_service.mongodb_exporter_service.spec[0].port[0].port}"
}

output "namespace" {
description = "Namespace of the MongoDB metrics exporter"
value = var.namespace
}
29 changes: 29 additions & 0 deletions monitoring/onpremise/exporters/mongodb-exporter/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Global variables
variable "namespace" {
description = "Namespace of ArmoniK resources"
type = string
}

# Docker image
variable "docker_image" {
description = "Docker image for partition metrics exporter"
type = object({
image = string
tag = string
image_pull_secrets = string
})
}

variable "certif_mount" {
description = "MongoDB certificate mount secret"
type = map(object({
secret = string
path = string
mode = string
}))
}

variable "mongo_url" {
description = "Full MongoDB URI with credentials and tls options included"
type = string
}
9 changes: 9 additions & 0 deletions monitoring/onpremise/exporters/mongodb-exporter/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.0"
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.21.1"
}
}
}
1 change: 1 addition & 0 deletions monitoring/onpremise/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ No modules.
|------|-------------|------|---------|:--------:|
| <a name="input_docker_image"></a> [docker\_image](#input\_docker\_image) | Docker image for Prometheus | <pre>object({<br> image = string<br> tag = string<br> image_pull_secrets = string<br> })</pre> | n/a | yes |
| <a name="input_metrics_exporter_url"></a> [metrics\_exporter\_url](#input\_metrics\_exporter\_url) | URL of metrics exporter | `string` | n/a | yes |
| <a name="input_mongo_metrics_exporter_url"></a> [mongo\_metrics\_exporter\_url](#input\_mongo\_metrics\_exporter\_url) | URL of the MongoDB metrics exporter | `string` | `null` | no |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Namespace of ArmoniK monitoring | `string` | n/a | yes |
| <a name="input_node_selector"></a> [node\_selector](#input\_node\_selector) | Node selector for Prometheus | `any` | `{}` | no |
| <a name="input_persistent_volume"></a> [persistent\_volume](#input\_persistent\_volume) | Persistent volume info | <pre>object({<br> storage_provisioner = string<br> volume_binding_mode = string<br> parameters = map(string)<br> # Resources for PVC<br> resources = object({<br> limits = object({<br> storage = string<br> })<br> requests = object({<br> storage = string<br> })<br> })<br> })</pre> | `null` | no |
Expand Down
8 changes: 8 additions & 0 deletions monitoring/onpremise/prometheus/prometheus-configmap.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ scrape_configs:
- targets: ["${var.metrics_exporter_url}"]
labels:
namespace: "${var.namespace}"
- job_name: "mongodb-metrics-exporter"
static_configs:
- targets: ["${var.mongo_metrics_exporter_url}"]
labels:
namespace: "${var.namespace}"
metrics_path: /metrics
scheme: http
- job_name: "kubernetes-apiservers"
kubernetes_sd_configs:
Expand Down
7 changes: 7 additions & 0 deletions monitoring/onpremise/prometheus/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ variable "metrics_exporter_url" {
type = string
}

# MongoDB metrics exporter url
variable "mongo_metrics_exporter_url" {
description = "URL of the MongoDB metrics exporter"
type = string
default = null
}

variable "security_context" {
description = "security context for Prometheus pods"
type = object({
Expand Down
9 changes: 9 additions & 0 deletions storage/onpremise/mongodb/createClusterMonitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use admin;
db.createUser({
user: "mongodb_exporter",
pwd: "mongodb_exporter",
roles: [
{ role: "clusterMonitor", db: "admin"},
{ role: "read", db: "local"}
]
});
4 changes: 3 additions & 1 deletion storage/onpremise/mongodb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ resource "helm_release" "mongodb" {
"auth" = {
"databases" = ["database"]
}

"initdbScripts" = {
"createClusterMonitor.js" = file("${path.module}/createClusterMonitor.js")
}
"podSecurityContext" = {
"fsGroup" = var.security_context.fs_group
}
Expand Down

0 comments on commit 3eec0e1

Please sign in to comment.