diff --git a/grafana/Untitled111.md b/grafana/Untitled111.md deleted file mode 100644 index 8b13789..0000000 --- a/grafana/Untitled111.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/grafana/datasource/datasource.tf b/grafana/datasource/datasource.tf index b943c87..cb953e1 100644 --- a/grafana/datasource/datasource.tf +++ b/grafana/datasource/datasource.tf @@ -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 + } + }) +} diff --git a/grafana/datasource/variables.tf b/grafana/datasource/variables.tf index 5602fd8..cbf19db 100644 --- a/grafana/datasource/variables.tf +++ b/grafana/datasource/variables.tf @@ -1,17 +1,38 @@ +# variables.tf + # Define datasources (non-sensitive metadata only) variable "datasources" { description = <