STACKITCIN-299 Add support for derivedFields to Grafana TF module
This commit is contained in:
parent
06b2db44bb
commit
77a36b4bb0
3 changed files with 88 additions and 8 deletions
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1,9 +1,13 @@
|
|||
# main.tf
|
||||
|
||||
# Step 1: Create all Grafana data sources with their basic configuration.
|
||||
# This resource establishes the fundamental properties of each data source.
|
||||
resource "grafana_data_source" "this" {
|
||||
for_each = var.datasources
|
||||
|
||||
name = each.key
|
||||
type = each.value.type
|
||||
url = var.datasource_urls[each.value.url_key]
|
||||
name = each.key
|
||||
type = each.value.type
|
||||
url = var.datasource_urls[each.value.url_key]
|
||||
is_default = coalesce(each.value.is_default, false)
|
||||
|
||||
basic_auth_enabled = true
|
||||
|
|
@ -13,5 +17,61 @@ resource "grafana_data_source" "this" {
|
|||
basicAuthPassword = var.datasource_passwords[each.value.pass_key]
|
||||
})
|
||||
|
||||
# Encodes initial, non-dependent JSON data.
|
||||
# Configurations that depend on other datasource UIDs will be handled separately.
|
||||
json_data_encoded = each.value.json_data != null ? jsonencode(each.value.json_data) : null
|
||||
}
|
||||
|
||||
# Step 2: Apply Loki-specific 'derivedFields' configuration.
|
||||
# This resource targets Loki data sources that need to link to another data source (like Tempo).
|
||||
# It runs after the initial data sources are created to resolve the UIDs.
|
||||
resource "grafana_data_source_config" "loki_derived_fields" {
|
||||
# Filter for datasources that are of type 'loki' and have 'derived_fields' defined.
|
||||
for_each = {
|
||||
for k, v in var.datasources : k => v
|
||||
if v.type == "loki" && v.derived_fields != null
|
||||
}
|
||||
|
||||
# The UID of the Loki data source to configure.
|
||||
uid = grafana_data_source.this[each.key].uid
|
||||
|
||||
# Construct the json_data with the derivedFields.
|
||||
json_data_encoded = jsonencode({
|
||||
derivedFields = [
|
||||
for field in each.value.derived_fields : {
|
||||
# The UID of the target data source (e.g., Tempo).
|
||||
datasourceUid = grafana_data_source.this[field.target_datasource_name].uid
|
||||
matcherRegex = field.matcher_regex
|
||||
name = field.name
|
||||
url = field.url
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
# Step 3: Apply Tempo-specific 'tracesToLogsV2' configuration.
|
||||
# This resource targets Tempo data sources that need to link back to a logging data source (like Loki).
|
||||
resource "grafana_data_source_config" "tempo_traces_to_logs" {
|
||||
# Filter for datasources that are of type 'tempo' and have 'traces_to_logs' defined.
|
||||
for_each = {
|
||||
for k, v in var.datasources : k => v
|
||||
if v.type == "tempo" && v.traces_to_logs != null
|
||||
}
|
||||
|
||||
# The UID of the Tempo data source to configure.
|
||||
uid = grafana_data_source.this[each.key].uid
|
||||
|
||||
# Construct the json_data with the tracesToLogsV2 settings.
|
||||
json_data_encoded = jsonencode({
|
||||
tracesToLogsV2 = {
|
||||
# The UID of the target data source (e.g., Loki).
|
||||
datasourceUid = grafana_data_source.this[each.value.traces_to_logs.target_datasource_name].uid
|
||||
query = each.value.traces_to_logs.query
|
||||
customQuery = coalesce(each.value.traces_to_logs.custom_query, true)
|
||||
filterBySpanID = coalesce(each.value.traces_to_logs.filter_by_span_id, false)
|
||||
filterByTraceID = coalesce(each.value.traces_to_logs.filter_by_trace_id, false)
|
||||
spanStartTimeShift = each.value.traces_to_logs.span_start_time_shift
|
||||
spanEndTimeShift = each.value.traces_to_logs.span_end_time_shift
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,38 @@
|
|||
# variables.tf
|
||||
|
||||
# Define datasources (non-sensitive metadata only)
|
||||
variable "datasources" {
|
||||
description = <<EOT
|
||||
Map of datasources to create. Keys are datasource names.
|
||||
Each datasource specifies type (prometheus/loki), keys to lookup URL/user/password,
|
||||
and optional is_default (true/false).
|
||||
Each datasource specifies type (prometheus/loki/tempo), keys to lookup URL/user/password,
|
||||
and optional configurations for linking data sources.
|
||||
EOT
|
||||
type = map(object({
|
||||
type = string # e.g., prometheus, loki
|
||||
type = string # e.g., prometheus, loki, tempo
|
||||
url_key = string # key for URL lookup in datasource_urls map
|
||||
user_key = string # key for username lookup in datasource_users map
|
||||
pass_key = string # key for password lookup in datasource_passwords map
|
||||
is_default = optional(bool) # true if this datasource should be Grafana default
|
||||
json_data = optional(map(any))
|
||||
json_data = optional(map(any)) # Initial, non-dependent JSON data
|
||||
|
||||
# Optional: For Loki, to link to a tracing datasource like Tempo.
|
||||
derived_fields = optional(list(object({
|
||||
target_datasource_name = string # The name of the target datasource (e.g., "tempo-main")
|
||||
matcher_regex = string # Regex to find the trace ID in logs.
|
||||
name = string # Name for the derived field (e.g., "traceID")
|
||||
url = string # URL template, e.g., "$${__value.raw}"
|
||||
})))
|
||||
|
||||
# Optional: For Tempo, to link to a logging datasource like Loki.
|
||||
traces_to_logs = optional(object({
|
||||
target_datasource_name = string # The name of the target datasource (e.g., "loki-main")
|
||||
query = string # The query to run in the target datasource.
|
||||
custom_query = optional(bool)
|
||||
filter_by_span_id = optional(bool)
|
||||
filter_by_trace_id = optional(bool)
|
||||
span_start_time_shift = optional(string)
|
||||
span_end_time_shift = optional(string)
|
||||
}))
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue