From 50533cbd519fa7968bb6370fa0be66778a6bb4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Nicol?= Date: Tue, 14 Jun 2022 14:59:56 +0200 Subject: [PATCH] Mixins templatize clusterlabel fixes (#6383) * fix formating * re-apply https://github.com/grafana/loki/pull/6353 * promtail-mixin: gitignore vendor/ * promtail-mixin: support for config * mixins: improve config files * re-trigger build Co-authored-by: Herve Nicol <12008875+hervenicol@users.noreply.github.com> --- production/loki-mixin/config.libsonnet | 3 +++ .../dashboards/dashboard-utils.libsonnet | 20 +++++++++---------- .../dashboards/loki-chunks.libsonnet | 2 +- .../loki-mixin/dashboards/loki-logs.libsonnet | 10 +++++----- .../dashboards/loki-operational.libsonnet | 8 ++++---- .../dashboards/loki-reads.libsonnet | 2 +- .../dashboards/loki-writes.libsonnet | 2 +- production/promtail-mixin/.gitignore | 1 + production/promtail-mixin/config.libsonnet | 9 +++++++++ .../promtail-mixin/dashboards.libsonnet | 10 ++++++---- 10 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 production/promtail-mixin/.gitignore create mode 100644 production/promtail-mixin/config.libsonnet diff --git a/production/loki-mixin/config.libsonnet b/production/loki-mixin/config.libsonnet index 220325c803..f5bdfe3713 100644 --- a/production/loki-mixin/config.libsonnet +++ b/production/loki-mixin/config.libsonnet @@ -8,5 +8,8 @@ // The label used to differentiate between different nodes (i.e. servers). per_node_label: 'instance', + + // The label used to differentiate between different clusters. + per_cluster_label: 'cluster', }, } diff --git a/production/loki-mixin/dashboards/dashboard-utils.libsonnet b/production/loki-mixin/dashboards/dashboard-utils.libsonnet index 1999c966b0..4b8a9fbf56 100644 --- a/production/loki-mixin/dashboards/dashboard-utils.libsonnet +++ b/production/loki-mixin/dashboards/dashboard-utils.libsonnet @@ -29,15 +29,15 @@ local utils = import 'mixin-utils/utils.libsonnet'; addCluster(multi=false):: if multi then - self.addMultiTemplate('cluster', 'loki_build_info', 'cluster') + self.addMultiTemplate('cluster', 'loki_build_info', $._config.per_cluster_label) else - self.addTemplate('cluster', 'loki_build_info', 'cluster'), + self.addTemplate('cluster', 'loki_build_info', $._config.per_cluster_label), addNamespace(multi=false):: if multi then - self.addMultiTemplate('namespace', 'loki_build_info{cluster=~"$cluster"}', 'namespace') + self.addMultiTemplate('namespace', 'loki_build_info{' + $._config.per_cluster_label + '=~"$cluster"}', 'namespace') else - self.addTemplate('namespace', 'loki_build_info{cluster=~"$cluster"}', 'namespace'), + self.addTemplate('namespace', 'loki_build_info{' + $._config.per_cluster_label + '=~"$cluster"}', 'namespace'), addTag():: self + { @@ -74,18 +74,18 @@ local utils = import 'mixin-utils/utils.libsonnet'; }; if multi then - d.addMultiTemplate('cluster', 'loki_build_info', 'cluster') - .addMultiTemplate('namespace', 'loki_build_info{cluster=~"$cluster"}', 'namespace') + d.addMultiTemplate('cluster', 'loki_build_info', $._config.per_cluster_label) + .addMultiTemplate('namespace', 'loki_build_info{' + $._config.per_cluster_label + '=~"$cluster"}', 'namespace') else - d.addTemplate('cluster', 'loki_build_info', 'cluster') - .addTemplate('namespace', 'loki_build_info{cluster=~"$cluster"}', 'namespace'), + d.addTemplate('cluster', 'loki_build_info', $._config.per_cluster_label) + .addTemplate('namespace', 'loki_build_info{' + $._config.per_cluster_label + '=~"$cluster"}', 'namespace'), }, jobMatcher(job):: - 'cluster=~"$cluster", job=~"($namespace)/%s"' % job, + $._config.per_cluster_label + '=~"$cluster", job=~"($namespace)/%s"' % job, namespaceMatcher():: - 'cluster=~"$cluster", namespace=~"$namespace"', + $._config.per_cluster_label + '=~"$cluster", namespace=~"$namespace"', containerLabelMatcher(containerName):: 'label_name=~"%s.*"' % containerName, diff --git a/production/loki-mixin/dashboards/loki-chunks.libsonnet b/production/loki-mixin/dashboards/loki-chunks.libsonnet index d9e8771840..2056be1405 100644 --- a/production/loki-mixin/dashboards/loki-chunks.libsonnet +++ b/production/loki-mixin/dashboards/loki-chunks.libsonnet @@ -6,7 +6,7 @@ local utils = import 'mixin-utils/utils.libsonnet'; local dashboards = self, 'loki-chunks.json': { local cfg = self, - labelsSelector:: 'cluster="$cluster", job="$namespace/ingester"', + labelsSelector:: $._config.per_cluster_label + '="$cluster", job="$namespace/ingester"', } + $.dashboard('Loki / Chunks', uid='chunks') .addCluster() diff --git a/production/loki-mixin/dashboards/loki-logs.libsonnet b/production/loki-mixin/dashboards/loki-logs.libsonnet index 5f7d2eed43..6e0eb84063 100644 --- a/production/loki-mixin/dashboards/loki-logs.libsonnet +++ b/production/loki-mixin/dashboards/loki-logs.libsonnet @@ -7,7 +7,7 @@ local template = import 'grafonnet/template.libsonnet'; template.new( 'deployment', '$datasource', - 'label_values(kube_deployment_created{cluster="$cluster", namespace="$namespace"}, deployment)', + 'label_values(kube_deployment_created{' + $._config.per_cluster_label + '="$cluster", namespace="$namespace"}, deployment)', sort=1, ), @@ -15,7 +15,7 @@ local template = import 'grafonnet/template.libsonnet'; template.new( 'pod', '$datasource', - 'label_values(kube_pod_container_info{cluster="$cluster", namespace="$namespace", pod=~"$deployment.*"}, pod)', + 'label_values(kube_pod_container_info{' + $._config.per_cluster_label + '="$cluster", namespace="$namespace", pod=~"$deployment.*"}, pod)', sort=1, ), @@ -23,7 +23,7 @@ local template = import 'grafonnet/template.libsonnet'; template.new( 'container', '$datasource', - 'label_values(kube_pod_container_info{cluster="$cluster", namespace="$namespace", pod=~"$pod", pod=~"$deployment.*"}, container)', + 'label_values(kube_pod_container_info{' + $._config.per_cluster_label + '="$cluster", namespace="$namespace", pod=~"$pod", pod=~"$deployment.*"}, container)', sort=1, ), @@ -48,7 +48,7 @@ local template = import 'grafonnet/template.libsonnet'; local cfg = self, showMultiCluster:: true, - clusterLabel:: 'cluster', + clusterLabel:: $._config.per_cluster_label, } + lokiLogs + $.dashboard('Loki / Logs', uid='logs') @@ -62,7 +62,7 @@ local template = import 'grafonnet/template.libsonnet'; targets: [ e { expr: if dashboards['loki-logs.json'].showMultiCluster then super.expr - else std.strReplace(super.expr, 'cluster="$cluster", ', ''), + else std.strReplace(super.expr, $._config.per_cluster_label + '="$cluster", ', ''), } for e in p.targets ], diff --git a/production/loki-mixin/dashboards/loki-operational.libsonnet b/production/loki-mixin/dashboards/loki-operational.libsonnet index 78efdbb96c..75a2d358af 100644 --- a/production/loki-mixin/dashboards/loki-operational.libsonnet +++ b/production/loki-mixin/dashboards/loki-operational.libsonnet @@ -11,7 +11,7 @@ local utils = import 'mixin-utils/utils.libsonnet'; showAnnotations:: true, showLinks:: true, showMultiCluster:: true, - clusterLabel:: 'cluster', + clusterLabel:: $._config.per_cluster_label, matchers:: { cortexgateway: [utils.selector.re('job', '($namespace)/cortex-gw')], @@ -44,13 +44,13 @@ local utils = import 'mixin-utils/utils.libsonnet'; std.strReplace( std.strReplace( expr, - ', cluster="$cluster"', + ', ' + $._config.per_cluster_label + '="$cluster"', '' ), - ', cluster=~"$cluster"', + ', ' + $._config.per_cluster_label + '=~"$cluster"', '' ), - 'cluster="$cluster",', + $._config.per_cluster_label + '="$cluster",', '' ), diff --git a/production/loki-mixin/dashboards/loki-reads.libsonnet b/production/loki-mixin/dashboards/loki-reads.libsonnet index ecb6bf543d..f74d1da1ec 100644 --- a/production/loki-mixin/dashboards/loki-reads.libsonnet +++ b/production/loki-mixin/dashboards/loki-reads.libsonnet @@ -11,7 +11,7 @@ local utils = import 'mixin-utils/utils.libsonnet'; local cfg = self, showMultiCluster:: true, - clusterLabel:: 'cluster', + clusterLabel:: $._config.per_cluster_label, clusterMatchers:: if cfg.showMultiCluster then [utils.selector.re(cfg.clusterLabel, '$cluster')] diff --git a/production/loki-mixin/dashboards/loki-writes.libsonnet b/production/loki-mixin/dashboards/loki-writes.libsonnet index b5133ce2bf..fcdfa71045 100644 --- a/production/loki-mixin/dashboards/loki-writes.libsonnet +++ b/production/loki-mixin/dashboards/loki-writes.libsonnet @@ -8,7 +8,7 @@ local utils = import 'mixin-utils/utils.libsonnet'; local cfg = self, showMultiCluster:: true, - clusterLabel:: 'cluster', + clusterLabel:: $._config.per_cluster_label, clusterMatchers:: if cfg.showMultiCluster then [utils.selector.re(cfg.clusterLabel, '$cluster')] diff --git a/production/promtail-mixin/.gitignore b/production/promtail-mixin/.gitignore new file mode 100644 index 0000000000..a725465aee --- /dev/null +++ b/production/promtail-mixin/.gitignore @@ -0,0 +1 @@ +vendor/ \ No newline at end of file diff --git a/production/promtail-mixin/config.libsonnet b/production/promtail-mixin/config.libsonnet new file mode 100644 index 0000000000..c1e66155bb --- /dev/null +++ b/production/promtail-mixin/config.libsonnet @@ -0,0 +1,9 @@ +{ + _config+:: { + // Tags for dashboards. + tags: ['loki'], + + // The label used to differentiate between different clusters. + per_cluster_label: 'cluster', + }, +} diff --git a/production/promtail-mixin/dashboards.libsonnet b/production/promtail-mixin/dashboards.libsonnet index 1dc5c92abf..d42cb0a50e 100644 --- a/production/promtail-mixin/dashboards.libsonnet +++ b/production/promtail-mixin/dashboards.libsonnet @@ -1,3 +1,4 @@ +local configfile = import 'config.libsonnet'; local g = import 'grafana-builder/grafana.libsonnet'; local loki_mixin_utils = import 'loki-mixin/dashboards/dashboard-utils.libsonnet'; local utils = import 'mixin-utils/utils.libsonnet'; @@ -6,13 +7,14 @@ local utils = import 'mixin-utils/utils.libsonnet'; grafanaDashboards+: { local dashboard = ( loki_mixin_utils { - _config+:: { tags: ['loki'] }, + _config+:: configfile._config, } ), local dashboards = self, - local labelsSelector = 'cluster=~"$cluster", namespace=~"$namespace"', - local quantileLabelSelector = 'cluster=~"$cluster", job=~"$namespace/promtail"', + // local labelsSelector = 'cluster=~"$cluster", namespace=~"$namespace"', + local labelsSelector = dashboard._config.per_cluster_label + '~"$cluster", namespace=~"$namespace"', + local quantileLabelSelector = dashboard._config.per_cluster_label + '=~"$cluster", job=~"$namespace/promtail"', 'promtail.json': { local cfg = self, @@ -20,7 +22,7 @@ local utils = import 'mixin-utils/utils.libsonnet'; dashboard.dashboard('Loki / Promtail', uid='promtail') .addCluster() .addTag() - .addTemplate('namespace', 'promtail_build_info{cluster=~"$cluster"}', 'namespace') + .addTemplate('namespace', 'promtail_build_info{' + dashboard._config.per_cluster_label + '=~"$cluster"}', 'namespace') .addRow( g.row('Targets & Files') .addPanel(