Skip to content

Commit

Permalink
Update Grafana integration
Browse files Browse the repository at this point in the history
Signed-off-by: Pete Wall <[email protected]>
  • Loading branch information
petewall committed Jan 6, 2025
1 parent 992907f commit 7b1a0dc
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Key | Type | Default | Description |
|-----|------|---------|-------------|
| fieldSelectors | list | `[]` | Discover Grafana instances based on field selectors. |
| labelSelectors | object | `{}` | Discover Grafana instances based on label selectors. Will automatically set a matcher for `app.kubernetes.io/name: <name>` unless set here. |
| labelSelectors | object | `{}` | Discover Grafana instances based on label selectors. |
| metrics.portName | string | `"grafana"` | Name of the port to scrape metrics from. |
| namespaces | list | `[]` | The namespaces to look for Grafana instances in. Will automatically look for Grafana instances in all namespaces unless specified here |

Expand All @@ -18,7 +18,7 @@
| logs.enabled | bool | `true` | Whether to enable special processing of Grafana pod logs. |
| logs.tuning.dropLogLevels | list | `[]` | The log levels to drop. Will automatically keep all log levels unless specified here. |
| logs.tuning.excludeLines | list | `[]` | Line patterns (valid RE2 regular expression)to exclude from the logs. |
| logs.tuning.scrubTimestamp | bool | `true` | Whether or not the timestamp should be scrubbed from the log line |
| logs.tuning.scrubTimestamp | bool | `true` | Whether the timestamp should be scrubbed from the log line |
| logs.tuning.structuredMetadata | object | `{}` | The structured metadata mappings to set. To not set any structured metadata, set this to an empty object (e.g. `{}`) |
| logs.tuning.timestampFormat | string | `"RFC3339Nano"` | The timestamp format to use for the log line, if not set the default timestamp which is the collection will be used for the log line |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
# @section -- General Settings
name: ""

# -- Discover Grafana instances based on label selectors.
# Will automatically set a matcher for `app.kubernetes.io/name: <name>` unless set here.
# @section -- Discovery Settings
labelSelectors: {}

# -- The namespaces to look for Grafana instances in.
# Will automatically look for Grafana instances in all namespaces unless specified here
# @section -- Discovery Settings
Expand All @@ -17,6 +12,10 @@ namespaces: []
# @section -- Discovery Settings
fieldSelectors: []

# -- Discover Grafana instances based on label selectors.
# @section -- Discovery Settings
labelSelectors: {}

# Settings for metrics collection
metrics:
# -- Whether to enable metrics collection from Grafana.
Expand Down Expand Up @@ -67,7 +66,7 @@ logs:
# @section -- Logs Settings
timestampFormat: "RFC3339Nano"

# -- Whether or not the timestamp should be scrubbed from the log line
# -- Whether the timestamp should be scrubbed from the log line
# @section -- Logs Settings
scrubTimestamp: true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
{{- define "integrations.grafana.defaultSelectorLabel" -}}app.kubernetes.io/name{{- end }}
{{- define "integrations.grafana.defaultSelectorValue" -}}grafana{{- end }}
{{- define "integrations.grafana.validate" }}{{- end }}
{{- define "integrations.grafana.validate" }}
{{- range $instance := $.Values.grafana.instances }}
{{- include "integrations.grafana.instance.validate" (merge $ (dict "instance" $instance)) | nindent 2 }}
{{- end }}
{{- end }}

{{- define "integrations.grafana.instance.validate" }}
{{- if not .instance.labelSelectors }}
{{- $msg := list "" "The Grafana integration requires a label selector" }}
{{- $msg = append $msg "For example, please set:" }}
{{- $msg = append $msg "integrations:" }}
{{- $msg = append $msg " grafana:" }}
{{- $msg = append $msg " instances:" }}
{{- $msg = append $msg (printf " - name: %s" .instance.name) }}
{{- $msg = append $msg " labelSelectors:" }}
{{- $msg = append $msg (printf " app.kubernetes.io/name: %s" .instance.name) }}
{{- $msg = append $msg "OR" }}
{{- $msg = append $msg " labelSelectors:" }}
{{- $msg = append $msg " app.kubernetes.io/name: [grafana-one, grafana-two]" }}
{{- fail (join "\n" $msg) }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
{{- if .logs.enabled }}
{{- $labelList := list }}
{{- $valueList := list }}
{{- $selectors := dict (include "integrations.grafana.defaultSelectorLabel" $) ($instance.name | default (include "integrations.grafana.defaultSelectorValue" $)) }}
{{- if $instance.labelSelectors }}
{{- $selectors = $instance.labelSelectors }}
{{- if .namespaces }}
{{- $labelList = append $labelList "__meta_kubernetes_namespace" -}}
{{- $valueList = append $valueList (printf "(%s)" (join "|" .namespaces)) -}}
{{- end }}

{{- range (keys $selectors) }}
{{- $labelList = append $labelList (include "pod_label" .) -}}
{{- $valueList = append $valueList (index $selectors .) -}}
{{- range $k, $v := .labelSelectors }}
{{- if kindIs "slice" $v }}
{{- $labelList = append $labelList (include "pod_label" $k) -}}
{{- $valueList = append $valueList (printf "(%s)" (join "|" $v)) -}}
{{- else }}
{{- $labelList = append $labelList (include "pod_label" $k) -}}
{{- $valueList = append $valueList $v -}}
{{- end }}
{{- end }}
rule {
source_labels = {{ $labelList | sortAlpha | toJson }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ declare "grafana_integration" {
comment = "Must be a list(MetricsReceiver) where collected metrics should be forwarded to"
}

argument "job_label" {
comment = "The job label to add for all Loki metrics (default: integrations/grafana)"
optional = true
}

argument "keep_metrics" {
comment = "A regular expression of metrics to keep (default: see below)"
optional = true
Expand All @@ -114,7 +119,7 @@ declare "grafana_integration" {
}

prometheus.scrape "grafana" {
job_name = "integrations/grafana"
job_name = coalesce(argument.job_label.value, "integrations/grafana")
forward_to = [prometheus.relabel.grafana.receiver]
targets = argument.targets.value
scrape_interval = coalesce(argument.scrape_interval.value, "60s")
Expand Down Expand Up @@ -148,35 +153,28 @@ declare "grafana_integration" {
{{- define "integrations.grafana.include.metrics" }}
{{- $defaultValues := "integrations/grafana-values.yaml" | .Files.Get | fromYaml }}
{{- with $defaultValues | merge (deepCopy .instance) }}
{{- $metricAllowList := include "integrations.grafana.allowList" (dict "instance" . "Files" $.Files) | fromYamlArray }}
{{- $metricDenyList := .excludeMetrics }}

{{- $nameLabelDefined := false }}
{{- $labelSelectors := list }}
{{- range $k, $v := .labelSelectors }}
{{- if eq $k "app.kubernetes.io/name" }}{{- $nameLabelDefined = true }}{{- end }}
{{- if $v }}
{{- $labelSelectors = append $labelSelectors (printf "%s=%s" $k $v) }}
{{- end }}
{{- end }}
{{- if not $nameLabelDefined }}
{{- $labelSelectors = append $labelSelectors (printf "app.kubernetes.io/name=%s" .name) }}
{{- end }}
{{- $fieldSelectors := list }}
{{- range $k, $v := .fieldSelectors }}
{{- $fieldSelectors = append $fieldSelectors (printf "%s=%s" $k $v) }}
{{- $metricAllowList := include "integrations.grafana.allowList" (dict "instance" . "Files" $.Files) | fromYamlArray }}
{{- $metricDenyList := .excludeMetrics }}
{{- $labelSelectors := list }}
{{- range $k, $v := .labelSelectors }}
{{- if kindIs "slice" $v }}
{{- $labelSelectors = append $labelSelectors (printf "%s in (%s)" $k (join "," $v)) }}
{{- else }}
{{- $labelSelectors = append $labelSelectors (printf "%s=%s" $k $v) }}
{{- end }}
{{- end }}
grafana_integration_discovery {{ include "helper.alloy_name" .name | quote }} {
namespaces = {{ .namespaces | toJson }}
label_selectors = {{ $labelSelectors | toJson }}
{{- if $fieldSelectors }}
field_selectors = {{ $fieldSelectors | toJson }}
{{- if .fieldSelectors }}
field_selectors = {{ .fieldSelectors | toJson }}
{{- end }}
port_name = {{ .metrics.portName | quote }}
}

grafana_integration_scrape {{ include "helper.alloy_name" .name | quote }} {
targets = grafana_integration_discovery.{{ include "helper.alloy_name" .name }}.output
job_label = {{ .jobLabel | quote }}
clustering = true
{{- if $metricAllowList }}
keep_metrics = {{ $metricAllowList | join "|" | quote }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
asserts:
- isKind:
of: ConfigMap
Expand Down Expand Up @@ -90,6 +92,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
logs:
tuning:
timestampFormat: "RFC3339"
Expand All @@ -110,6 +114,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
logs:
tuning:
scrubTimestamp: true
Expand All @@ -135,6 +141,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
logs:
tuning:
structuredMetadata:
Expand All @@ -159,6 +167,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
logs:
tuning:
dropLogLevels:
Expand Down Expand Up @@ -187,6 +197,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
logs:
tuning:
excludeLines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tests:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
asserts:
- isKind:
of: ConfigMap
Expand Down Expand Up @@ -228,36 +230,6 @@ tests:
}
}
- it: should allow you to overwrite the name label
set:
deployAsConfigMap: true
alloy:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: my-grafana-instance
asserts:
- isKind:
of: ConfigMap
- matchRegex:
path: data["metrics.alloy"]
pattern: label_selectors = \["app.kubernetes.io/name=my-grafana-instance"\]

- it: should allow you to dismiss the name label
set:
deployAsConfigMap: true
alloy:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: null
app.kubernetes.io/instance: k8smon
asserts:
- isKind:
of: ConfigMap
- matchRegex:
path: data["metrics.alloy"]
pattern: label_selectors = \["app.kubernetes.io/instance=k8smon"\]
- it: should allow you to restrict the namespaces for metrics
set:
Expand All @@ -267,6 +239,8 @@ tests:
- name: grafana
namespaces:
- k8smon
labelSelectors:
app.kubernetes.io/name: grafana
asserts:
- isKind:
of: ConfigMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
asserts:
- isKind:
of: ConfigMap
Expand Down Expand Up @@ -74,6 +76,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
namespaces:
- k8smon
asserts:
Expand All @@ -89,6 +93,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
logs:
tuning:
timestampFormat: "RFC3339"
Expand All @@ -109,6 +115,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
logs:
tuning:
scrubTimestamp: true
Expand All @@ -134,6 +142,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
logs:
tuning:
structuredMetadata:
Expand All @@ -156,6 +166,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
logs:
tuning:
dropLogLevels:
Expand Down Expand Up @@ -184,6 +196,8 @@ tests:
loki:
instances:
- name: loki
labelSelectors:
app.kubernetes.io/name: loki
logs:
tuning:
excludeLines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ integrations:
grafana:
instances:
- name: grafana
labelSelectors:
app.kubernetes.io/name: grafana
namespaces:
- o11y

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7b1a0dc

Please sign in to comment.