-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DMVP-4666): grafana dashboard create ability
- Loading branch information
Showing
88 changed files
with
2,791 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,5 @@ override.tf.json | |
# Ignore CLI configuration files | ||
.terraformrc | ||
terraform.rc | ||
|
||
.terraform.lock.hcl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module "application_dashboard" { | ||
source = "./modules/dashboard/" | ||
|
||
count = length(var.application_dashboard) > 0 ? 1 : 0 | ||
|
||
name = var.name | ||
rows = var.application_dashboard.rows | ||
data_source = var.application_dashboard.data_source | ||
variables = var.application_dashboard.variables | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.tfstate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Module to create Grafana dashboard from json/hcl | ||
## Yaml example | ||
``` | ||
source: dasmeta/grafana/onpremise//modules/dashboard | ||
version: x.y.z | ||
variables: | ||
name: test-dashboard | ||
data_source: | ||
uid: "0000" | ||
rows: | ||
- type : block/sla | ||
- type : "block/ingress" | ||
- type : "block/service" | ||
name : "service-name-1" | ||
host : "example.com" | ||
- type : "block/service", | ||
name : "service-name-2" | ||
- | ||
- type : "text/title", | ||
text : "End" | ||
``` | ||
|
||
## HCL example | ||
``` | ||
module "this" { | ||
source = "dasmeta/grafana/onpremise//modules/dashboard" | ||
version = "x.y.z" | ||
name = "test-dashboard-with-blocks" | ||
data_source = { | ||
uid: "0000" | ||
} | ||
rows = [ | ||
{ "type" : "block/sla" }, | ||
{ type : "block/ingress" }, | ||
{ type : "block/service", name : "service-name-1", namespace: "dev", host : "example.com" }, | ||
{ type : "block/service", name : "service-name-2", namespace: "dev" }, | ||
{ type : text/title, text: "End"} | ||
] | ||
} | ||
``` | ||
|
||
## How add new widget | ||
1. create module in modules/widgets (copy from one) | ||
2. implement data loading as required | ||
3. add new widget tf module in widget-{widget-group-name | single}.tf file | ||
4. add new widget line in widget_result local | ||
|
||
## How add new block | ||
1. create module in modules/blocks (copy from one) | ||
2. implement data loading as required | ||
3. add new block tf module in widget-blocks.tf | ||
4. add new block line in blocks_results local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
locals { | ||
dashboard_title = var.name | ||
|
||
# fill dashboard variable options and current required fields | ||
grafana_templating_list_variables_options_fill = [for variable in var.variables : merge(variable, { | ||
options = [for option in variable.options : merge(option, { text = coalesce(option.text, option.value) })] | ||
})] | ||
grafana_templating_list_variables = [for variable in local.grafana_templating_list_variables_options_fill : merge(variable, { | ||
current = try(variable.options[index(variable.options.*.selected, true)], null) | ||
})] | ||
|
||
|
||
## Blocks | ||
|
||
# get all blocks and annotate | ||
initial_blocks = [ | ||
for index1, block in var.rows : { | ||
block : block, | ||
index1 : index1, | ||
type : replace(block.type, "block/", "") | ||
} if strcontains(try(block.type, ""), "block/") | ||
] | ||
|
||
# annotate each block type with subIndex | ||
blocks_by_type = { for block_type in distinct([for block in local.initial_blocks : block.type]) : | ||
block_type => [for index2, block in local.initial_blocks : merge(block, { index2 : index2 }) if strcontains(block.type, block_type)] | ||
} | ||
|
||
# bring all module results together | ||
blocks_results = { | ||
ingress = values(module.block_ingress).*.result | ||
service = values(module.block_service).*.result | ||
sla = values(module.block_sla).*.result | ||
} | ||
|
||
blocks_by_type_results = concat([], [ | ||
for block_type, type_blocks in local.blocks_by_type : [ | ||
for index3, block in type_blocks : merge(block, { results : local.blocks_results[block.type][index3] }) if contains(keys(local.blocks_results), block.type) | ||
] if contains(keys(local.blocks_results), block_type) | ||
]...) | ||
|
||
# inject block widgets into rows/panels listing in place of block/* definitions | ||
rows = concat([], [ | ||
for index1, row in var.rows : | ||
concat(strcontains(try(row.type, ""), "block/") ? [] : [row], | ||
[ | ||
for item in local.blocks_by_type_results : item.results if item.index1 == index1 | ||
]...) | ||
]...) | ||
|
||
|
||
## Widgets | ||
# default values from module and provided from outside | ||
widget_default_values = merge( | ||
{ | ||
period = 3 # in minutes | ||
stat = "Sum" | ||
width = 6 | ||
height = 8 | ||
expressions = [] | ||
yAxis = { left = { min = 0 } } | ||
data_source = var.data_source | ||
container = "$container" | ||
namespace = "$namespace" | ||
cluster = "$cluster" | ||
account_id = null | ||
region = null | ||
anomaly_detection = false | ||
anomaly_deviation = 6 | ||
}, | ||
var.defaults | ||
) | ||
|
||
# this will walk through every widget and add row/column + merge with default values | ||
widget_config_with_raw_column_data_and_defaults = [ | ||
for row_number, row in local.rows : [ | ||
for column_number, column in row : merge( | ||
local.widget_default_values, | ||
column, | ||
{ | ||
row = row_number, | ||
column = column_number, | ||
row_count = length(local.rows), | ||
column_count = length(row) | ||
} | ||
) | ||
] | ||
] | ||
|
||
# groups rows by widget type | ||
widget_config = { for key, item in flatten(local.widget_config_with_raw_column_data_and_defaults) : | ||
item.type => merge( | ||
item, | ||
# calculate coordinates based on defaults and row/column details | ||
{ | ||
coordinates = { | ||
x = item.column * item.width | ||
y = item.row | ||
width = item.width | ||
height = item.height | ||
} | ||
} | ||
)... } | ||
|
||
# combine results (last step) | ||
widget_result = concat( | ||
# Container widgets | ||
values(module.container_cpu_widget).*.data, | ||
values(module.container_memory_widget).*.data, | ||
values(module.container_network_widget).*.data, | ||
values(module.container_restarts_widget).*.data, | ||
values(module.container_replicas_widget).*.data, | ||
values(module.container_request_count_widget).*.data, | ||
values(module.container_response_time_widget).*.data, | ||
|
||
# Ingress widgets | ||
values(module.ingress_connections_widget).*.data, | ||
values(module.ingress_request_rate_widget).*.data, | ||
values(module.ingress_request_count_widget).*.data, | ||
values(module.ingress_response_time_widget).*.data, | ||
|
||
# Text widgets | ||
values(module.text_title).*.data, | ||
values(module.text_title_with_link).*.data, | ||
values(module.text_title_with_collapse).*.data, | ||
|
||
# sla/slo/sli widgets | ||
values(module.widget_sla_slo_sli_main).*.data, | ||
values(module.widget_sla_slo_sli_latency).*.data, | ||
|
||
# single widgets | ||
values(module.widget_custom).*.data, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
resource "grafana_dashboard" "metrics" { | ||
config_json = jsonencode({ | ||
uid = random_string.grafana_dashboard_id.result | ||
title = local.dashboard_title | ||
style = "dark" | ||
timezone = "browser" | ||
editable = true | ||
schemaVersion = 35 | ||
fiscalYearStartMonth = 0 | ||
graphTooltip = 0 | ||
links = [] | ||
liveNow = false | ||
annotations = {} | ||
refresh = "1m" | ||
tags = [] | ||
templating = { | ||
list = local.grafana_templating_list_variables | ||
} | ||
time = { | ||
from = "now-6h" | ||
to = "now" | ||
} | ||
timepicker = {} | ||
weekStart = "" | ||
panels = local.widget_result | ||
}) | ||
} | ||
|
||
resource "random_string" "grafana_dashboard_id" { | ||
length = 16 | ||
special = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
output "result" { | ||
description = "description" | ||
value = [ | ||
[ | ||
{ type : "text/title-with-collapse", text : "Nginx Ingress Controller" } | ||
], | ||
[ | ||
{ type : "ingress/request-rate" }, | ||
{ type : "ingress/connections" }, | ||
{ type : "ingress/response-time" }, | ||
{ type : "ingress/request-count" } | ||
], | ||
[ | ||
{ type : "ingress/request-rate", by_host : true }, | ||
{ type : "ingress/response-time", by_host : true }, | ||
{ type : "ingress/request-count", by_host : true }, | ||
{ type : "ingress/request-count", by_host : true, only_5xx: true } | ||
], | ||
[ | ||
{ type : "container/cpu", container : "controller", namespace : "ingress-nginx", width : 12 }, | ||
{ type : "container/memory", container : "controller", namespace : "ingress-nginx", width : 12 } | ||
], | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
variable "account_id" { | ||
type = string | ||
description = "AWS account ID" | ||
} | ||
|
||
variable "balancer_name" { | ||
type = string | ||
description = "ALB name" | ||
} | ||
|
||
variable "region" { | ||
type = string | ||
default = "" | ||
} |
Oops, something went wrong.