Skip to content

Commit

Permalink
code clean
Browse files Browse the repository at this point in the history
Signed-off-by: Weifeng Wang <[email protected]>
  • Loading branch information
qclaogui committed Mar 15, 2024
1 parent 090a3ba commit 499e292
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 317 deletions.
2 changes: 1 addition & 1 deletion docker-compose/common/config/agent-flow/metrics.river
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ logging {
********************************************/

module.file "docker_compose" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker_compose.river"
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/docker_compose.river"

arguments {
metrics_endpoint = "http://gateway:8080"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ Module: log-all
Description: Wrapper module to include all docker logging modules and use cri parsing
*/
argument "forward_to" {
// comment = "Must be a list(LogsReceiver) where collected logs should be forwarded to"
optional = false
comment = "Must be a list(LogsReceiver) where collected logs should be forwarded to"
}

argument "tenant" {
// comment = "The tenant to filter logs to. This does not have to be the tenantId, this is the value to look for in the logs.agent.grafana.com/tenant annotation, and this can be a regex."
comment = "The tenant to filter logs to. This does not have to be the tenantId, this is the value to look for in the logs.agent.grafana.com/tenant annotation, and this can be a regex."
optional = true
default = ".*"
}

argument "keep_labels" {
// comment = "List of labels to keep before the log message is written to Loki"
comment = "List of labels to keep before the log message is written to Loki"
optional = true
default = [
"app",
Expand All @@ -36,80 +35,80 @@ argument "keep_labels" {
]
}

module.file "log_targets" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/targets/logs-from-docker.river"
module.file "mf_logs_auto_scrape" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/logs-auto-scrape.river"

arguments {
forward_to = [module.file.log_formats_all.exports.process.receiver]
forward_to = [module.file.mf_log_formats_all.exports.process.receiver]
tenant = argument.tenant.value
}
}

module.file "log_formats_all" {
module.file "mf_log_formats_all" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/log-formats/all.river"

arguments {
forward_to = [module.file.log_level_default.exports.process.receiver]
forward_to = [module.file.mf_log_level_default.exports.process.receiver]
}
}

module.file "log_level_default" {
module.file "mf_log_level_default" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/labels/log-level.river"

arguments {
// here we fork, one branch goes to the log level module, the other goes to the metrics module
// this is because we need to reduce the labels on the pre-metrics but they are still necessary in
// downstream modules
forward_to = [
module.file.pre_process_metrics.exports.process.receiver,
module.file.drop_levels.exports.process.receiver,
module.file.mf_pre_process_metrics.exports.process.receiver,
module.file.mf_drop_levels.exports.process.receiver,
]
}
}

module.file "pre_process_metrics" {
module.file "mf_pre_process_metrics" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/metrics/pre-process-bytes-lines.river"

arguments {
forward_to = [module.file.drop_levels.exports.process.receiver]
forward_to = [module.file.mf_drop_levels.exports.process.receiver]
keep_labels = argument.keep_labels.value
}
}

module.file "drop_levels" {
module.file "mf_drop_levels" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/drops/levels.river"

arguments {
forward_to = [module.file.scrub_all.exports.process.receiver]
forward_to = [module.file.mf_scrub_all.exports.process.receiver]
}
}

module.file "scrub_all" {
module.file "mf_scrub_all" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/scrubs/all.river"

arguments {
forward_to = [module.file.mask_all.exports.process.receiver]
forward_to = [module.file.mf_mask_all.exports.process.receiver]
}
}

module.file "mask_all" {
module.file "mf_mask_all" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/masks/all.river"

arguments {
forward_to = [module.file.label_keep.exports.process.receiver]
forward_to = [module.file.mf_label_keep.exports.process.receiver]
}
}

module.file "label_keep" {
module.file "mf_label_keep" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/labels/keep-labels.river"

arguments {
forward_to = [module.file.post_process_metrics.exports.process.receiver]
forward_to = [module.file.mf_post_process_metrics.exports.process.receiver]
keep_labels = argument.keep_labels.value
}
}

module.file "post_process_metrics" {
module.file "mf_post_process_metrics" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/metrics/post-process-bytes-lines.river"

arguments {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ Description: Pre-defined set of labels to keep, this stage should always be in-p
in Loki as that would have extremely high-cardinality.
*/
argument "forward_to" {
// comment = "Must be a list(LogsReceiver) where collected logs should be forwarded to"
optional = false
comment = "Must be a list(LogsReceiver) where collected logs should be forwarded to"
}

argument "keep_labels" {
optional = true
// comment = "List of labels to keep before the log message is written to Loki"
default = [
comment = "List of labels to keep before the log message is written to Loki"
default = [
"app",
"cluster",
"component",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
Module(logs): Docker Containers Logs Auto-Scraping
Description: Scrapes targets for logs based on Docker Containers labels

Note: Every argument except for "forward_to" is optional, and does have a defined default value. However, the values for these
arguments are not defined using the default = " ... " argument syntax, but rather using the coalesce(argument.value, " ... ").
This is because if the argument passed in from another consuming module is set to null, the default = " ... " syntax will
does not override the value passed in, where coalesce() will return the first non-null value.


Following labels are available:

logs.agent.grafana.com/scrape: true

allow containers to declare what tenant their logs should be written to,
the following label is supported:

logs.agent.grafana.com/tenant: "primary"
*/
argument "forward_to" {
comment = "Must be a list(LogsReceiver) where collected logs should be forwarded to"
}

argument "cluster" {
optional = true
}

argument "namespace" {
optional = true
}

argument "tenant" {
comment = "The tenant to write metrics to. This does not have to be the tenantId, this is the value to look for in the logs.agent.grafana.com/tenant label, and this can be a regex. (default: (.*))"
optional = true
}

argument "clustering" {
// Docs: https://grafana.com/docs/agent/latest/flow/concepts/clustering/
comment = "Whether or not clustering should be enabled (default: false)"
optional = true
}

// get the available containers.
discovery.docker "dd_logs" {
host = "unix:///var/run/docker.sock"

filter {
name = "status"
values = ["running"]
}
}

discovery.relabel "dr_docker_logs" {
targets = discovery.docker.dd_logs.targets

/****************************************************************************************************************
* Handle Discovers From Docker Engine Containers Targets to Keep or Drop
* https://grafana.com/docs/agent/latest/flow/reference/components/discovery.docker/#exported-fields
****************************************************************************************************************/
// allow resources to declare their metrics scraped or not
// Example Annotation:
// logs.agent.grafana.com/scrape: false
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_logs_agent_grafana_com_scrape",
]
separator = ";"
regex = "^(?:;*)?(true|false).*$"
replacement = "$1"
target_label = "__tmp_scrape"
}

// drop any targets that have scrape: false
rule {
action = "drop"
source_labels = ["__tmp_scrape"]
regex = "false"
}

// allow resources to declare their metrics the tenant their metrics should be sent to,
// Example Annotation:
// logs.agent.grafana.com/tenant: primary
rule {
action = "keep"
source_labels = [
"__meta_docker_container_label_logs_agent_grafana_com_tenant",
]
regex = "^(" + coalesce(argument.tenant.value, ".*") + ")$"
}

// make all labels on the pod available to the pipeline as labels(for loki process),
// they are omitted before write via labelallow unless explicitly set
rule {
action = "labelmap"
regex = "__meta_docker_container_label_(.+)"
}

/********************************************
* Handle Setting Common Labels
********************************************/

// set the cluster label
rule {
action = "replace"
replacement = coalesce(argument.cluster.value, "docker-compose")
target_label = "cluster"
}

// set the namespace label
rule {
action = "replace"
replacement = coalesce(argument.namespace.value, "monitoring-system")
target_label = "namespace"
}

// set a default job label to be the namespace/service_name
rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_com_docker_compose_service",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = coalesce(argument.namespace.value, "monitoring-system") + "/$1"
target_label = "job"
}

rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_com_docker_compose_service",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "pod"
}

rule {
source_labels = ["__meta_docker_container_name"]
regex = "/(.*)"
target_label = "container"
}

rule {
action = "replace"
source_labels = [
"__meta_docker_container_label_com_docker_compose_service",
"__meta_docker_container_label_app",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "app"
}
}

loki.source.docker "lsd_docker_logs" {
host = "unix:///var/run/docker.sock"
targets = discovery.relabel.dr_docker_logs.output
relabel_rules = discovery.relabel.dr_docker_logs.rules
forward_to = argument.forward_to.value
}

This file was deleted.

Loading

0 comments on commit 499e292

Please sign in to comment.