-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(o11y): add templates and code for docker
- Loading branch information
1 parent
e3ade52
commit cd8c0a1
Showing
5 changed files
with
196 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
- name: Grafana Cloud Setup | ||
hosts: '{{ variable_host | default("null") }}' | ||
become: true | ||
gather_facts: true | ||
|
||
vars: | ||
prometheus_push_endpoint: "{{ lookup('env', 'GRAFANA_CLOUD_URL_Prometheus') }}" | ||
prometheus_username: "{{ lookup('env', 'GRAFANA_CLOUD_USERNAME_Prometheus') }}" | ||
prometheus_password: "{{ lookup('env', 'GRAFANA_CLOUD_API_KEY_Prometheus') }}" | ||
loki_push_endpoint: "{{ lookup('env', 'GRAFANA_CLOUD_URL_Loki') }}" | ||
loki_username: "{{ lookup('env', 'GRAFANA_CLOUD_USERNAME_Loki') }}" | ||
loki_password: "{{ lookup('env', 'GRAFANA_CLOUD_API_KEY_Loki') }}" | ||
is_enabled_metrics: "{{ variable_is_enabled_metrics | default(True) }}" | ||
is_enabled_logs: "{{ variable_is_enabled_logs | default(False) }}" | ||
|
||
tasks: | ||
- name: Check if required environment variables are set | ||
ansible.builtin.assert: | ||
that: | ||
- prometheus_push_endpoint != "" | ||
- prometheus_username != "" | ||
- prometheus_password != "" | ||
- loki_push_endpoint != "" | ||
- loki_username != "" | ||
- loki_password != "" | ||
fail_msg: | ||
"One or more required environment variables are not set. Please set | ||
all required variables." | ||
|
||
- name: Check if Alloy is installed | ||
ansible.builtin.command: which alloy | ||
register: alloy_check | ||
ignore_errors: true | ||
changed_when: false | ||
|
||
- name: Check if Alloy service is enabled | ||
ansible.builtin.command: systemctl is-enabled alloy | ||
register: alloy_service_check | ||
ignore_errors: true | ||
changed_when: false | ||
|
||
- name: Check if Docker is installed | ||
ansible.builtin.command: which docker | ||
register: docker_check | ||
ignore_errors: true | ||
changed_when: false | ||
|
||
- name: Exit if checks fail | ||
ansible.builtin.fail: | ||
msg: "Checks failed. Please check the logs for more information." | ||
when: docker_check.rc != 0 or alloy_check.rc != 0 or alloy_service_check.rc != 0 | ||
|
||
- name: Setup Docker integration | ||
when: docker_check.rc == 0 and alloy_check.rc == 0 and alloy_service_check.rc == 0 | ||
block: | ||
- name: Append Docker config to Alloy config file | ||
when: is_enabled_metrics | bool | ||
ansible.builtin.blockinfile: | ||
path: /etc/alloy/config.alloy | ||
prepend_newline: true | ||
marker: "// {mark} ANSIBLE MANAGED BLOCK -- Docker Integration - Metrics" | ||
block: | | ||
{{ lookup('template', 'grafana-labs/docker.metrics.config.alloy.j2') }} | ||
- name: Append Docker logs config to Alloy config file | ||
when: is_enabled_logs | bool | ||
ansible.builtin.blockinfile: | ||
path: /etc/alloy/config.alloy | ||
prepend_newline: true | ||
marker: "// {mark} ANSIBLE MANAGED BLOCK -- Docker Integration - Logs" | ||
block: | | ||
{{ lookup('template', 'grafana-labs/docker.logs.config.alloy.j2') }} | ||
- name: Add alloy user to docker group | ||
ansible.builtin.user: | ||
name: alloy | ||
groups: docker | ||
append: true | ||
|
||
- name: Change the user to root in Alloy service user | ||
ansible.builtin.lineinfile: | ||
path: /etc/systemd/system/alloy.service | ||
regexp: "^User=alloy" | ||
line: "User=root" | ||
state: present | ||
|
||
- name: Reload the systemd daemon | ||
ansible.builtin.systemd: | ||
daemon_reload: true | ||
|
||
- name: Restart the Alloy service | ||
ansible.builtin.systemd: | ||
name: alloy | ||
state: restarted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ansible/templates/grafana-labs/docker.logs.config.alloy.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
discovery.docker "logs_integrations_docker" { | ||
host = "unix:///var/run/docker.sock" | ||
refresh_interval = "5s" | ||
} | ||
discovery.relabel "logs_integrations_docker" { | ||
targets = [] | ||
|
||
rule { | ||
target_label = "job" | ||
replacement = "integrations/docker" | ||
} | ||
|
||
rule { | ||
target_label = "instance" | ||
replacement = constants.hostname | ||
} | ||
|
||
rule { | ||
source_labels = ["__meta_docker_container_name"] | ||
regex = "/(.*)" | ||
target_label = "container" | ||
} | ||
|
||
rule { | ||
source_labels = ["__meta_docker_container_log_stream"] | ||
target_label = "stream" | ||
} | ||
} | ||
loki.source.docker "logs_integrations_docker" { | ||
host = "unix:///var/run/docker.sock" | ||
targets = discovery.docker.logs_integrations_docker.targets | ||
forward_to = [loki.write.grafana_cloud_loki.receiver] | ||
relabel_rules = discovery.relabel.logs_integrations_docker.rules | ||
refresh_interval = "5s" | ||
} |
31 changes: 31 additions & 0 deletions
31
ansible/templates/grafana-labs/docker.metrics.config.alloy.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
prometheus.exporter.cadvisor "integrations_cadvisor" { | ||
docker_only = true | ||
} | ||
discovery.relabel "integrations_cadvisor" { | ||
targets = prometheus.exporter.cadvisor.integrations_cadvisor.targets | ||
|
||
rule { | ||
target_label = "job" | ||
replacement = "integrations/docker" | ||
} | ||
|
||
rule { | ||
target_label = "instance" | ||
replacement = constants.hostname | ||
} | ||
} | ||
|
||
prometheus.relabel "integrations_cadvisor" { | ||
forward_to = [prometheus.remote_write.metrics_service.receiver] | ||
|
||
rule { | ||
source_labels = ["__name__"] | ||
regex = "up|container_cpu_usage_seconds_total|container_fs_inodes_free|container_fs_inodes_total|container_fs_limit_bytes|container_fs_usage_bytes|container_last_seen|container_memory_usage_bytes|container_network_receive_bytes_total|container_network_tcp_usage_total|container_network_transmit_bytes_total|container_spec_memory_reservation_limit_bytes|machine_memory_bytes|machine_scrape_error" | ||
action = "keep" | ||
} | ||
} | ||
|
||
prometheus.scrape "integrations_cadvisor" { | ||
targets = discovery.relabel.integrations_cadvisor.output | ||
forward_to = [prometheus.relabel.integrations_cadvisor.receiver] | ||
} |