Skip to content

Commit

Permalink
New module for Grafana LGTMP Stack receiver provider
Browse files Browse the repository at this point in the history
Signed-off-by: Weifeng Wang <[email protected]>

Update provider.river
  • Loading branch information
qclaogui committed Apr 8, 2024
1 parent ef276c8 commit 039f9d2
Show file tree
Hide file tree
Showing 21 changed files with 1,552 additions and 1,513 deletions.
2 changes: 1 addition & 1 deletion docker-compose/common/config/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AGENT_IMAGE=grafana/agent:v0.40.3
LOKI_IMAGE=grafana/loki:2.9.6
GRAFANA_IMAGE=grafana/grafana:10.4.1
GRAFANA_IMAGE=grafana/grafana:11.0.0-preview
TEMPO_IMAGE=grafana/tempo:2.4.1
MIMIR_IMAGE=grafana/mimir:2.12.0
MIMIRTOOL_IMAGE=grafana/mimirtool:2.12.0
Expand Down
8 changes: 0 additions & 8 deletions docker-compose/common/config/agent-flow/.gitignore

This file was deleted.

15 changes: 8 additions & 7 deletions docker-compose/common/config/agent-flow/logs.river
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ logging {
/********************************************
* Grafana LGTMP Stack Receiver Provider
********************************************/
module.file "lgtmp_provider_local" {
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/local.river"
import.file "provider" {
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/provider.river"
}

arguments {
logs_endpoint = "http://gateway:3100"
}
provider.lgtmp_stack "docker_compose" {
logs_endpoint = "http://gateway:3100"
metrics_endpoint = "http://gateway:8080"
}

/********************************************
Expand All @@ -23,7 +24,7 @@ module.file "logs_primary" {
filename = env("AGENT_CONFIG_FOLDER") + "/modules/docker/logs/all.river"

arguments {
forward_to = [module.file.lgtmp_provider_local.exports.logs_receiver]
forward_to = [provider.lgtmp_stack.docker_compose.logs_receiver]
}
}

Expand All @@ -34,6 +35,6 @@ module.file "metrics_primary" {
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/docker/metrics/all.river"

arguments {
forward_to = [module.file.lgtmp_provider_local.exports.metrics_receiver]
forward_to = [provider.lgtmp_stack.docker_compose.metrics_receiver]
}
}
12 changes: 6 additions & 6 deletions docker-compose/common/config/agent-flow/metrics.river
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ logging {
/********************************************
* Grafana LGTMP Stack Receiver Provider
********************************************/
module.file "lgtmp_provider_local" {
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/local.river"
import.file "provider" {
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/provider.river"
}

arguments {
metrics_endpoint = "http://gateway:8080"
}
provider.lgtmp_stack "docker_compose" {
metrics_endpoint = "http://gateway:8080"
}

/********************************************
Expand All @@ -23,6 +23,6 @@ module.file "metrics_primary" {
filename = coalesce(env("AGENT_CONFIG_FOLDER"), "/etc/agent-config") + "/modules/docker/metrics/all.river"

arguments {
forward_to = [module.file.lgtmp_provider_local.exports.metrics_receiver]
forward_to = [provider.lgtmp_stack.docker_compose.metrics_receiver]
}
}
52 changes: 52 additions & 0 deletions docker-compose/common/config/agent-flow/modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# LGTMP agent moudles

<https://grafana.com/docs/agent/latest/flow/concepts/modules/>

```river
import.git "provider" {
repository = "https://github.com/qclaogui/codelab-monitoring.git"
revision = "main"
path = "docker-compose/common/config/agent-flow/modules/provider.river"
pull_frequency = "15m"
}
// get the receivers provider
provider.lgtmp_stack "docker_compose" {
metrics_endpoint = "http://mimir:8080"
}
// scrape metrics and write to grafana cloud
prometheus.scrape "default" {
targets = [
{"__address__" = "127.0.0.1:12345"},
]
forward_to = [
provider.lgtmp_stack.docker_compose.metrics_receiver,
]
}
```

```river
import.git "provider" {
repository = "https://github.com/qclaogui/codelab-monitoring.git"
revision = "main"
path = "docker-compose/common/config/agent-flow/modules/provider.river"
pull_frequency = "15m"
}
// get the receivers provider
provider.grafana_cloud "stack_name" {
stack_name = env("GRAFANA_CLOUD_STACK_NAME")
token = env("GRAFANA_CLOUD_STACK_TOKEN")
}
// scrape metrics and write to grafana cloud
prometheus.scrape "default" {
targets = [
{"__address__" = "127.0.0.1:12345"},
]
forward_to = [
provider.grafana_cloud.stack_name.metrics_receiver,
]
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
Module(logs_auto_scrape): 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"
*/

declare "logs_auto_scrape" {

/********************************************
* ARGUMENTS
********************************************/
argument "forward_to" {
comment = "Must be a list(LogsReceiver) where collected logs should be forwarded to"
}

argument "targets" {
comment = "The running containers."
}

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
}

// get logs from discovery relabel dr_docker_logs below
loki.source.docker "lsd_docker_logs" {
forward_to = argument.forward_to.value

host = "unix:///var/run/docker.sock"
targets = discovery.relabel.dr_docker_logs.output
relabel_rules = discovery.relabel.dr_docker_logs.rules
refresh_interval = "15s"
}

discovery.relabel "dr_docker_logs" {
targets = argument.targets.value

/****************************************************************************************************************
* 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 {
action = "replace"
source_labels = [
"__meta_docker_container_label_com_docker_compose_service",
]
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "container"
}

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

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

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

0 comments on commit 039f9d2

Please sign in to comment.