diff --git a/example/config/alloy/config.alloy b/example/config/alloy/config.alloy index 74ef592c42..79208745b7 100644 --- a/example/config/alloy/config.alloy +++ b/example/config/alloy/config.alloy @@ -8,7 +8,21 @@ logging { level = "debug" // Forward internal logs to the local Loki instance. - write_to = [loki.write.loki.receiver] + write_to = [loki.relabel.alloy_logs.receiver] +} + +loki.relabel "alloy_logs"{ + rule { + target_label = "instance" + replacement = constants.hostname + } + + rule { + target_label = "job" + replacement = "integrations/self" + } + + forward_to = [loki.write.loki.receiver] } tracing { diff --git a/operations/alloy-mixin/alerts.libsonnet b/operations/alloy-mixin/alerts.libsonnet index 548e41febd..ab23492080 100644 --- a/operations/alloy-mixin/alerts.libsonnet +++ b/operations/alloy-mixin/alerts.libsonnet @@ -10,7 +10,7 @@ local openTelemetryAlerts = (import './alerts/opentelemetry.libsonnet'); openTelemetryAlerts.newOpenTelemetryAlertsGroup($._config.enableK8sCluster) ], - prometheusAlerts+: { + prometheusAlerts+:: { groups+: if $._config.enableAlloyCluster then alloyClusterAlerts + otherAlerts diff --git a/operations/alloy-mixin/config.libsonnet b/operations/alloy-mixin/config.libsonnet index 09d7f31e09..221aee45d9 100644 --- a/operations/alloy-mixin/config.libsonnet +++ b/operations/alloy-mixin/config.libsonnet @@ -3,10 +3,12 @@ enableK8sCluster: true, enableAlloyCluster: true, enableLokiLogs: true, - filterSelector: 'job=~"$job"', - groupSelector: if self.enableK8sCluster then self.k8sClusterSelector + ', ' + self.filterSelector else self.filterSelector, - instanceSelector: self.groupSelector + ', instance=~"$instance"', + filterSelector: '', #use it to filter specific metric label values, ie: job=~"integrations/alloy" k8sClusterSelector: 'cluster=~"$cluster", namespace=~"$namespace"', - dashboardTag: 'alloy-mixin' + groupSelector: if self.enableK8sCluster then self.k8sClusterSelector + ', job="$job"' else 'job="$job"', + instanceSelector: self.groupSelector + ', instance=~"$instance"', + logsFilterSelector: '', #use to filter logs originated from alloy, and avoid picking up other platform logs, ie: service_name="alloy" + dashboardTag: 'alloy-mixin', + useSetenceCaseTemplateLabels: false, } } \ No newline at end of file diff --git a/operations/alloy-mixin/dashboards.libsonnet b/operations/alloy-mixin/dashboards.libsonnet index 281c48c765..d701f29ab1 100644 --- a/operations/alloy-mixin/dashboards.libsonnet +++ b/operations/alloy-mixin/dashboards.libsonnet @@ -1,21 +1,21 @@ -local alloyClusterDashboards = - (import './dashboards/cluster-node.libsonnet') + - (import './dashboards/cluster-overview.libsonnet') + - (import './config.libsonnet'); - -local otherDashboards = - (import './dashboards/resources.libsonnet') + - (import './dashboards/controller.libsonnet') + - (import './dashboards/prometheus.libsonnet') + - (import './dashboards/opentelemetry.libsonnet') + - (import './config.libsonnet'); - (import './dashboards/alloy-logs.libsonnet') + -{ - grafanaDashboards+: +{ + local alloyClusterDashboards = + (import './dashboards/cluster-node.libsonnet') + + (import './dashboards/cluster-overview.libsonnet'), + local otherDashboards = + (import './dashboards/resources.libsonnet') + + (import './dashboards/controller.libsonnet') + + (import './dashboards/prometheus.libsonnet') + + (import './dashboards/opentelemetry.libsonnet'), + + grafanaDashboards+:: + // Propagate config down to inner dashboards. + { _config:: $._config } + ( if $._config.enableAlloyCluster then - alloyClusterDashboards + - otherDashboards - else + alloyClusterDashboards + otherDashboards + else + otherDashboards + ) } diff --git a/operations/alloy-mixin/dashboards/alloy-logs.libsonnet b/operations/alloy-mixin/dashboards/alloy-logs.libsonnet index e1d4e894bd..9ce96c8b1e 100644 --- a/operations/alloy-mixin/dashboards/alloy-logs.libsonnet +++ b/operations/alloy-mixin/dashboards/alloy-logs.libsonnet @@ -9,10 +9,10 @@ local logsDashboard = import 'github.com/grafana/jsonnet-libs/logs-lib/logs/main if $._config.enableLokiLogs then { local alloyLogs = logsDashboard.new( - 'Alloy logs overview', + 'Alloy / Logs Overview', datasourceName='loki_datasource', datasourceRegex='', - filterSelector=$._config.filterSelector, + filterSelector=$._config.logsFilterSelector, labels=labels, formatParser=null, showLogsVolume=true @@ -27,7 +27,8 @@ local logsDashboard = import 'github.com/grafana/jsonnet-libs/logs-lib/logs/main dashboards+: { logs+: g.dashboard.withLinksMixin($.grafanaDashboards['alloy-resources.json'].links) - + g.dashboard.withRefresh('10s'), + + g.dashboard.withRefresh('10s') + + g.dashboard.withTagsMixin($._config.dashboardTag), }, }, 'alloy-logs.json': alloyLogs.dashboards.logs, diff --git a/operations/alloy-mixin/dashboards/cluster-node.libsonnet b/operations/alloy-mixin/dashboards/cluster-node.libsonnet index 4d9f417b80..9abac50560 100644 --- a/operations/alloy-mixin/dashboards/cluster-node.libsonnet +++ b/operations/alloy-mixin/dashboards/cluster-node.libsonnet @@ -1,21 +1,15 @@ local dashboard = import './utils/dashboard.jsonnet'; local panel = import './utils/panel.jsonnet'; +local templates = import './utils/templates.libsonnet'; local filename = 'alloy-cluster-node.json'; { local templateVariables = - if $._config.enableK8sCluster then - [ - dashboard.newTemplateVariable('cluster', 'label_values(alloy_component_controller_running_components, cluster)'), - dashboard.newTemplateVariable('namespace', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace)'), - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace", job=~"$job"}, instance)'), - ] - else - [ - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{job=~"$job"}, instance)'), - ], + templates.newTemplateVariablesList( + filterSelector=$._config.filterSelector, + enableK8sCluster=$._config.enableK8sCluster, + includeInstance=true, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), [filename]: dashboard.new(name='Alloy / Cluster Node', tag=$._config.dashboardTag) + diff --git a/operations/alloy-mixin/dashboards/cluster-overview.libsonnet b/operations/alloy-mixin/dashboards/cluster-overview.libsonnet index 361eb6f93e..6364a6c2c8 100644 --- a/operations/alloy-mixin/dashboards/cluster-overview.libsonnet +++ b/operations/alloy-mixin/dashboards/cluster-overview.libsonnet @@ -1,20 +1,16 @@ local dashboard = import './utils/dashboard.jsonnet'; local panel = import './utils/panel.jsonnet'; local filename = 'alloy-cluster-overview.json'; +local templates = import './utils/templates.libsonnet'; local cluster_node_filename = 'alloy-cluster-node.json'; { local templateVariables = - if $._config.enableK8sCluster then - [ - dashboard.newTemplateVariable('cluster', 'label_values(alloy_component_controller_running_components, cluster)'), - dashboard.newTemplateVariable('namespace', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace)'), - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job)'), - ] - else - [ - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components, job)'), - ], + templates.newTemplateVariablesList( + filterSelector=$._config.filterSelector, + enableK8sCluster=$._config.enableK8sCluster, + includeInstance=false, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), [filename]: dashboard.new(name='Alloy / Cluster Overview', tag=$._config.dashboardTag) + @@ -36,7 +32,9 @@ local cluster_node_filename = 'alloy-cluster-node.json'; panel.withPosition({ h: 9, w: 8, x: 0, y: 0 }) + panel.withQueries([ panel.newInstantQuery( - expr='count(cluster_node_info{%(groupSelector)s})' + expr= ||| + 'count(cluster_node_info{%(groupSelector)s})' + ||| % $._config ), ]) ), @@ -49,7 +47,9 @@ local cluster_node_filename = 'alloy-cluster-node.json'; panel.withPosition({ h: 9, w: 16, x: 8, y: 0 }) + panel.withQueries([ panel.newInstantQuery( - expr='cluster_node_info{%(groupSelector)s}', + expr= ||| + 'cluster_node_info{%(groupSelector)s}' + ||| % $._config, format='table', ), ]) + diff --git a/operations/alloy-mixin/dashboards/controller.libsonnet b/operations/alloy-mixin/dashboards/controller.libsonnet index aa5b4ce357..a30b36dc66 100644 --- a/operations/alloy-mixin/dashboards/controller.libsonnet +++ b/operations/alloy-mixin/dashboards/controller.libsonnet @@ -1,20 +1,15 @@ local dashboard = import './utils/dashboard.jsonnet'; local panel = import './utils/panel.jsonnet'; +local templates = import './utils/templates.libsonnet'; local filename = 'alloy-controller.json'; { - local templateVariables = - if $._config.enableK8sCluster then - [ - dashboard.newTemplateVariable('cluster', 'label_values(alloy_component_controller_running_components, cluster)'), - dashboard.newTemplateVariable('namespace', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace)'), - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job)'), - ] - else - [ - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components, job)'), - ], + templates.newTemplateVariablesList( + filterSelector=$._config.filterSelector, + enableK8sCluster=$._config.enableK8sCluster, + includeInstance=false, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), [filename]: dashboard.new(name='Alloy / Controller', tag=$._config.dashboardTag) + @@ -309,7 +304,7 @@ local filename = 'alloy-controller.json'; expr= ||| sum(increase(alloy_component_evaluation_seconds{%(groupSelector)s}[$__rate_interval])) or ignoring (le) - sum by (le) (increase(alloy_component_evaluation_seconds_bucket{%(groupSelector)s}[$__rate_interval]))' + sum by (le) (increase(alloy_component_evaluation_seconds_bucket{%(groupSelector)s}[$__rate_interval])) ||| % $._config, format='heatmap', legendFormat='{{le}}', diff --git a/operations/alloy-mixin/dashboards/opentelemetry.libsonnet b/operations/alloy-mixin/dashboards/opentelemetry.libsonnet index c78d6af468..4046b307e0 100644 --- a/operations/alloy-mixin/dashboards/opentelemetry.libsonnet +++ b/operations/alloy-mixin/dashboards/opentelemetry.libsonnet @@ -1,5 +1,6 @@ local dashboard = import './utils/dashboard.jsonnet'; local panel = import './utils/panel.jsonnet'; +local templates = import './utils/templates.libsonnet'; local filename = 'alloy-opentelemetry.json'; local stackedPanelMixin = { @@ -16,18 +17,11 @@ local stackedPanelMixin = { { local templateVariables = - if $._config.enableK8sCluster then - [ - dashboard.newTemplateVariable('cluster', 'label_values(alloy_component_controller_running_components, cluster)'), - dashboard.newTemplateVariable('namespace', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace)'), - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace", job=~"$job"}, instance)'), - ] - else - [ - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{job=~"$job"}, instance)'), - ], + templates.newTemplateVariablesList( + filterSelector=$._config.filterSelector, + enableK8sCluster=$._config.enableK8sCluster, + includeInstance=true, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), [filename]: dashboard.new(name='Alloy / OpenTelemetry', tag=$._config.dashboardTag) + diff --git a/operations/alloy-mixin/dashboards/prometheus.libsonnet b/operations/alloy-mixin/dashboards/prometheus.libsonnet index b023dcc264..79e26ee3d0 100644 --- a/operations/alloy-mixin/dashboards/prometheus.libsonnet +++ b/operations/alloy-mixin/dashboards/prometheus.libsonnet @@ -1,5 +1,6 @@ local dashboard = import './utils/dashboard.jsonnet'; local panel = import './utils/panel.jsonnet'; +local templates = import './utils/templates.libsonnet'; local filename = 'alloy-prometheus-remote-write.json'; { @@ -397,25 +398,105 @@ local filename = 'alloy-prometheus-remote-write.json'; else remoteWritePanels(y_offset=0), - local templateVariables = + local k8sComponentPathQuery = + if std.isEmpty($._config.filterSelector) then + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{cluster=~"$cluster", namespace=~"$namespace", job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*", component_path=~".*"}, component_path) + ||| + else + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{%(filterSelector)s, cluster=~"$cluster", namespace=~"$namespace", job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*", component_path=~".*"}, component_path) + ||| % $._config, + + local k8sComponentQuery = + if std.isEmpty($._config.filterSelector) then + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{cluster=~"$cluster", namespace=~"$namespace", job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*"}, component_id) + ||| + else + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{%(filterSelector)s, cluster=~"$cluster", namespace=~"$namespace", job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*"}, component_id) + ||| % $._config, + + local k8sUrlQuery = + if std.isEmpty($._config.filterSelector) then + ||| + label_values(prometheus_remote_storage_sent_batch_duration_seconds_sum{cluster=~"$cluster", namespace=~"$namespace", job="$job", instance=~"$instance", component_id=~"$component"}, url) + ||| + else + ||| + label_values(prometheus_remote_storage_sent_batch_duration_seconds_sum{%(filterSelector)s, cluster=~"$cluster", namespace=~"$namespace", job="$job", instance=~"$instance", component_id=~"$component"}, url) + ||| % $._config, + + local componentPathQuery = + if std.isEmpty($._config.filterSelector) then + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*", component_path=~".*"}, component_path) + ||| + else + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{%(filterSelector)s, job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*", component_path=~".*"}, component_path) + ||| % $._config, + + local componentQuery = + if std.isEmpty($._config.filterSelector) then + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*"}, component_id) + ||| + else + ||| + label_values(prometheus_remote_write_wal_samples_appended_total{%(filterSelector)s, job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*"}, component_id) + ||| % $._config, + + local urlQuery = + if std.isEmpty($._config.filterSelector) then + ||| + label_values(prometheus_remote_storage_sent_batch_duration_seconds_sum{job="$job", instance=~"$instance", component_id=~"$component"}, url) + ||| + else + ||| + label_values(prometheus_remote_storage_sent_batch_duration_seconds_sum{%(filterSelector)s, job="$job", instance=~"$instance", component_id=~"$component"}, url) + ||| % $._config, + + local prometheusTemplateVariables = if $._config.enableK8sCluster then - [ - dashboard.newTemplateVariable('cluster', 'label_values(alloy_component_controller_running_components, cluster)'), - dashboard.newTemplateVariable('namespace', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace)'), - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace", job=~"$job"}, instance)'), - dashboard.newMultiTemplateVariable('component_path', 'label_values(prometheus_remote_write_wal_samples_appended_total{cluster=~"$cluster", namespace=~"$namespace", job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*", component_path=~".*"}, component_path)'), - dashboard.newMultiTemplateVariable('component', 'label_values(prometheus_remote_write_wal_samples_appended_total{cluster=~"$cluster", namespace=~"$namespace", job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*"}, component_id)'), - dashboard.newMultiTemplateVariable('url', 'label_values(prometheus_remote_storage_sent_batch_duration_seconds_sum{cluster=~"$cluster", namespace=~"$namespace", job="$job", instance=~"$instance", component_id=~"$component"}, url)'), + [ + dashboard.newMultiTemplateVariable( + name='component_path', + query=k8sComponentPathQuery, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), + dashboard.newMultiTemplateVariable( + name='component', + query=k8sComponentQuery, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), + dashboard.newMultiTemplateVariable( + name='url', + query= k8sUrlQuery, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), ] else - [ - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{job=~"$job"}, instance)'), - dashboard.newMultiTemplateVariable('component_path', 'label_values(prometheus_remote_write_wal_samples_appended_total{job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*", component_path=~".*"}, component_path)'), - dashboard.newMultiTemplateVariable('component', 'label_values(prometheus_remote_write_wal_samples_appended_total{job=~"$job", instance=~"$instance", component_id=~"prometheus.remote_write.*"}, component_id)'), - dashboard.newMultiTemplateVariable('url', 'label_values(prometheus_remote_storage_sent_batch_duration_seconds_sum{job=~"$job", instance=~"$instance", component_id=~"$component"}, url)'), + [ + dashboard.newMultiTemplateVariable( + name='component_path', + query=componentPathQuery, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), + dashboard.newMultiTemplateVariable( + name='component', + query=componentQuery, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), + dashboard.newMultiTemplateVariable( + name='url', + query=urlQuery, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), ], + + local templateVariables = + templates.newTemplateVariablesList( + filterSelector=$._config.filterSelector, + enableK8sCluster=$._config.enableK8sCluster, + includeInstance=true, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels) + + prometheusTemplateVariables, [filename]: dashboard.new(name='Alloy / Prometheus Components', tag=$._config.dashboardTag) + diff --git a/operations/alloy-mixin/dashboards/resources.libsonnet b/operations/alloy-mixin/dashboards/resources.libsonnet index 7aba016d98..47230e171f 100644 --- a/operations/alloy-mixin/dashboards/resources.libsonnet +++ b/operations/alloy-mixin/dashboards/resources.libsonnet @@ -1,5 +1,6 @@ local dashboard = import './utils/dashboard.jsonnet'; local panel = import './utils/panel.jsonnet'; +local templates = import './utils/templates.libsonnet'; local filename = 'alloy-resources.json'; local pointsMixin = { @@ -28,18 +29,11 @@ local stackedPanelMixin = { { local templateVariables = - if $._config.enableK8sCluster then - [ - dashboard.newTemplateVariable('cluster', 'label_values(alloy_component_controller_running_components, cluster)'), - dashboard.newTemplateVariable('namespace', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace)'), - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace", job=~"$job"}, instance)'), - ] - else - [ - dashboard.newMultiTemplateVariable('job', 'label_values(alloy_component_controller_running_components, job)'), - dashboard.newMultiTemplateVariable('instance', 'label_values(alloy_component_controller_running_components{job=~"$job"}, instance)'), - ], + templates.newTemplateVariablesList( + filterSelector=$._config.filterSelector, + enableK8sCluster=$._config.enableK8sCluster, + includeInstance=true, + setenceCaseLabels=$._config.useSetenceCaseTemplateLabels), [filename]: dashboard.new(name='Alloy / Resources', tag=$._config.dashboardTag) + diff --git a/operations/alloy-mixin/dashboards/utils/dashboard.jsonnet b/operations/alloy-mixin/dashboards/utils/dashboard.jsonnet index 09135d023a..15e34abc4a 100644 --- a/operations/alloy-mixin/dashboards/utils/dashboard.jsonnet +++ b/operations/alloy-mixin/dashboards/utils/dashboard.jsonnet @@ -66,9 +66,9 @@ }, }, - newTemplateVariable(name, query):: { + newTemplateVariable(name, query, setenceCaseLabels=false):: { name: name, - label: name, + label: if setenceCaseLabels then $.toSentenceCase(name) else name, type: 'query', query: { query: query, @@ -89,7 +89,7 @@ titleFormat: '{{cluster}}/{{namespace}}', }, - newMultiTemplateVariable(name, query):: $.newTemplateVariable(name, query) { + newMultiTemplateVariable(name, query, setenceCaseLabels=false):: $.newTemplateVariable(name, query, setenceCaseLabels) { multi: true, allValue: '.*', includeAll: true, @@ -126,4 +126,7 @@ targetBlank: false, }], }, + + toSentenceCase(string):: + std.asciiUpper(string[0]) + std.slice(string, 1, std.length(string), 1), } diff --git a/operations/alloy-mixin/dashboards/utils/templates.libsonnet b/operations/alloy-mixin/dashboards/utils/templates.libsonnet new file mode 100644 index 0000000000..050a1bfcaa --- /dev/null +++ b/operations/alloy-mixin/dashboards/utils/templates.libsonnet @@ -0,0 +1,105 @@ +local dashboard = import './dashboard.jsonnet'; + +{ + newTemplateVariablesList(filterSelector='', enableK8sCluster=true, includeInstance=true, setenceCaseLabels=false):: ( + + local clusterTemplateQuery = + if std.isEmpty(filterSelector) then + ||| + label_values(alloy_component_controller_running_components, cluster) + ||| + else + ||| + label_values(alloy_component_controller_running_components{%s}, cluster) + ||| % filterSelector; + + local namespaceTemplateQuery = + if std.isEmpty(filterSelector) then + ||| + label_values(alloy_component_controller_running_components{cluster=~"$cluster"}, namespace) + ||| + else + ||| + label_values(alloy_component_controller_running_components{%s, cluster=~"$cluster"}, namespace) + ||| % filterSelector; + + local k8sJobTemplateQuery = + if std.isEmpty(filterSelector) then + ||| + label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace"}, job) + ||| + else + ||| + label_values(alloy_component_controller_running_components{%s, cluster=~"$cluster", namespace=~"$namespace"}, job) + ||| % filterSelector; + + local k8sInstanceTemplateQuery = + if std.isEmpty(filterSelector) then + ||| + label_values(alloy_component_controller_running_components{cluster=~"$cluster", namespace=~"$namespace", job=~"$job"}, instance) + ||| + else + ||| + label_values(alloy_component_controller_running_components{%s, cluster=~"$cluster", namespace=~"$namespace", job=~"$job"}, instance) + ||| % filterSelector; + + local jobTemplateQuery = + if std.isEmpty(filterSelector) then + ||| + label_values(alloy_component_controller_running_components, job) + ||| + else + ||| + label_values(alloy_component_controller_running_components{%s}, job) + ||| % filterSelector; + + local instanceTemplateQuery = + if std.isEmpty(filterSelector) then + ||| + label_values(alloy_component_controller_running_components{job=~"$job"}, instance) + ||| + else + ||| + label_values(alloy_component_controller_running_components{%s, job=~"$job"}, instance) + ||| % filterSelector; + + if enableK8sCluster then + [ + dashboard.newTemplateVariable( + name='cluster', + query=clusterTemplateQuery, + setenceCaseLabels=setenceCaseLabels), + dashboard.newTemplateVariable( + name='namespace', + query=namespaceTemplateQuery, + setenceCaseLabels=setenceCaseLabels), + dashboard.newMultiTemplateVariable( + name='job', + query=k8sJobTemplateQuery, + setenceCaseLabels=setenceCaseLabels), + ] + + if includeInstance then + [ + dashboard.newMultiTemplateVariable( + name='instance', + query=k8sInstanceTemplateQuery, + setenceCaseLabels=setenceCaseLabels) + ] + else [] + else + [ + dashboard.newMultiTemplateVariable( + name='job', + query=jobTemplateQuery, + setenceCaseLabels=setenceCaseLabels), + ] + + if includeInstance then + [ + dashboard.newMultiTemplateVariable( + name='instance', + query=instanceTemplateQuery, + setenceCaseLabels=setenceCaseLabels) + ] + else [] + ) +} \ No newline at end of file