Skip to content

Commit

Permalink
Add relabeling rules to pod logs (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: Pete Wall <[email protected]>
  • Loading branch information
petewall authored Jul 7, 2023
1 parent 0216042 commit 28f9425
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 40 deletions.
4 changes: 2 additions & 2 deletions charts/k8s-monitoring/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: k8s-monitoring
description: A Helm chart for gathering, scraping, and forwarding Kubernetes infrastructure metrics and logs to a Grafana Stack.
type: application

version: 0.0.6
appVersion: 1.1.5
version: 0.0.7
appVersion: 1.1.6
icon: https://raw.githubusercontent.com/grafana/grafana/main/public/img/grafana_icon.svg
maintainers:
- email: [email protected]
Expand Down
2 changes: 1 addition & 1 deletion charts/k8s-monitoring/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# k8s-monitoring

![Version: 0.0.6](https://img.shields.io/badge/Version-0.0.6-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.1.5](https://img.shields.io/badge/AppVersion-1.1.5-informational?style=flat-square)
![Version: 0.0.7](https://img.shields.io/badge/Version-0.0.7-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.1.6](https://img.shields.io/badge/AppVersion-1.1.6-informational?style=flat-square)

A Helm chart for gathering, scraping, and forwarding Kubernetes infrastructure metrics and logs to a Grafana Stack.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
// Cluster Events
loki.source.kubernetes_events "cluster_events" {
job_name = "integrations/kubernetes/eventhandler"
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.process.cluster_events.receiver]
}

loki.process "cluster_events" {
stage.static_labels {
values = {
cluster = local.file.cluster_name.content,
}
}

forward_to = [loki.write.grafana_cloud_loki.receiver]
}
{{ end }}
9 changes: 0 additions & 9 deletions charts/k8s-monitoring/templates/agent_config/_loki.river.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{{ define "agent.config.loki" }}
// Grafana Cloud Loki
loki.process "add_cluster_label" {
stage.static_labels {
values = {
cluster = local.file.cluster_name.content,
}
}
forward_to = [loki.write.grafana_cloud_loki.receiver]
}

local.file "loki_host" {
filename = "/etc/grafana-agent-credentials/loki_host"
}
Expand Down
34 changes: 31 additions & 3 deletions charts/k8s-monitoring/templates/agent_config/_pod_logs.river.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
{{ define "agent.config.logs.pod_logs" }}
// Pod Logs
discovery.relabel "pod_logs" {
targets = discovery.kubernetes.pods.targets

rule {
source_labels = ["__meta_kubernetes_namespace"]
action = "replace"
target_label = "namespace"
}
rule {
source_labels = ["__meta_kubernetes_pod_name"]
action = "replace"
target_label = "pod"
}
rule {
source_labels = ["__meta_kubernetes_pod_container_name"]
action = "replace"
target_label = "container"
}
rule {
source_labels = ["__meta_kubernetes_namespace", "__meta_kubernetes_pod_name"]
separator = "/"
action = "replace"
replacement = "$1"
target_label = "job"
}
}

loki.source.kubernetes "pod_logs" {
targets = discovery.kubernetes.pods.targets
targets = discovery.relabel.pod_logs.output
forward_to = [loki.process.pod_logs.receiver]
}

loki.process "pod_logs" {
stage.static_labels {
values = {
job = "integration/kubernetes/pod_logs",
cluster = local.file.cluster_name.content,
}
}

stage.{{- .Values.logs.pod_logs.loggingFormat }} {}
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.write.grafana_cloud_loki.receiver]
}
{{ end }}
45 changes: 37 additions & 8 deletions tests/default-values/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,37 +373,66 @@ data:
}

// Pod Logs
discovery.relabel "pod_logs" {
targets = discovery.kubernetes.pods.targets

rule {
source_labels = ["__meta_kubernetes_namespace"]
action = "replace"
target_label = "namespace"
}
rule {
source_labels = ["__meta_kubernetes_pod_name"]
action = "replace"
target_label = "pod"
}
rule {
source_labels = ["__meta_kubernetes_pod_container_name"]
action = "replace"
target_label = "container"
}
rule {
source_labels = ["__meta_kubernetes_namespace", "__meta_kubernetes_pod_name"]
separator = "/"
action = "replace"
replacement = "$1"
target_label = "job"
}
}

loki.source.kubernetes "pod_logs" {
targets = discovery.kubernetes.pods.targets
targets = discovery.relabel.pod_logs.output
forward_to = [loki.process.pod_logs.receiver]
}

loki.process "pod_logs" {
stage.static_labels {
values = {
job = "integration/kubernetes/pod_logs",
cluster = local.file.cluster_name.content,
}
}

stage.docker {}
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.write.grafana_cloud_loki.receiver]
}

// Cluster Events
loki.source.kubernetes_events "cluster_events" {
job_name = "integrations/kubernetes/eventhandler"
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.process.cluster_events.receiver]
}

// Grafana Cloud Loki
loki.process "add_cluster_label" {

loki.process "cluster_events" {
stage.static_labels {
values = {
cluster = local.file.cluster_name.content,
}
}

forward_to = [loki.write.grafana_cloud_loki.receiver]
}


// Grafana Cloud Loki
local.file "loki_host" {
filename = "/etc/grafana-agent-credentials/loki_host"
}
Expand Down
45 changes: 37 additions & 8 deletions tests/logs-only/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,66 @@ data:
}
// Pod Logs
discovery.relabel "pod_logs" {
targets = discovery.kubernetes.pods.targets
rule {
source_labels = ["__meta_kubernetes_namespace"]
action = "replace"
target_label = "namespace"
}
rule {
source_labels = ["__meta_kubernetes_pod_name"]
action = "replace"
target_label = "pod"
}
rule {
source_labels = ["__meta_kubernetes_pod_container_name"]
action = "replace"
target_label = "container"
}
rule {
source_labels = ["__meta_kubernetes_namespace", "__meta_kubernetes_pod_name"]
separator = "/"
action = "replace"
replacement = "$1"
target_label = "job"
}
}
loki.source.kubernetes "pod_logs" {
targets = discovery.kubernetes.pods.targets
targets = discovery.relabel.pod_logs.output
forward_to = [loki.process.pod_logs.receiver]
}
loki.process "pod_logs" {
stage.static_labels {
values = {
job = "integration/kubernetes/pod_logs",
cluster = local.file.cluster_name.content,
}
}
stage.docker {}
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.write.grafana_cloud_loki.receiver]
}
// Cluster Events
loki.source.kubernetes_events "cluster_events" {
job_name = "integrations/kubernetes/eventhandler"
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.process.cluster_events.receiver]
}
// Grafana Cloud Loki
loki.process "add_cluster_label" {
loki.process "cluster_events" {
stage.static_labels {
values = {
cluster = local.file.cluster_name.content,
}
}
forward_to = [loki.write.grafana_cloud_loki.receiver]
}
// Grafana Cloud Loki
local.file "loki_host" {
filename = "/etc/grafana-agent-credentials/loki_host"
}
Expand Down
45 changes: 37 additions & 8 deletions tests/openshift-compatible/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,37 +342,66 @@ data:
}

// Pod Logs
discovery.relabel "pod_logs" {
targets = discovery.kubernetes.pods.targets

rule {
source_labels = ["__meta_kubernetes_namespace"]
action = "replace"
target_label = "namespace"
}
rule {
source_labels = ["__meta_kubernetes_pod_name"]
action = "replace"
target_label = "pod"
}
rule {
source_labels = ["__meta_kubernetes_pod_container_name"]
action = "replace"
target_label = "container"
}
rule {
source_labels = ["__meta_kubernetes_namespace", "__meta_kubernetes_pod_name"]
separator = "/"
action = "replace"
replacement = "$1"
target_label = "job"
}
}

loki.source.kubernetes "pod_logs" {
targets = discovery.kubernetes.pods.targets
targets = discovery.relabel.pod_logs.output
forward_to = [loki.process.pod_logs.receiver]
}

loki.process "pod_logs" {
stage.static_labels {
values = {
job = "integration/kubernetes/pod_logs",
cluster = local.file.cluster_name.content,
}
}

stage.docker {}
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.write.grafana_cloud_loki.receiver]
}

// Cluster Events
loki.source.kubernetes_events "cluster_events" {
job_name = "integrations/kubernetes/eventhandler"
forward_to = [loki.process.add_cluster_label.receiver]
forward_to = [loki.process.cluster_events.receiver]
}

// Grafana Cloud Loki
loki.process "add_cluster_label" {

loki.process "cluster_events" {
stage.static_labels {
values = {
cluster = local.file.cluster_name.content,
}
}

forward_to = [loki.write.grafana_cloud_loki.receiver]
}


// Grafana Cloud Loki
local.file "loki_host" {
filename = "/etc/grafana-agent-credentials/loki_host"
}
Expand Down

0 comments on commit 28f9425

Please sign in to comment.