Add native Traefik support to matrix-prometheus-nginxlog-exporter

This commit is contained in:
Slavi Pantaleev
2024-01-13 16:50:44 +02:00
parent a78a749f75
commit 262caf0d59
10 changed files with 165 additions and 63 deletions

View File

@ -3,10 +3,16 @@
# See: https://github.com/martin-helmich/prometheus-nginxlog-exporter/
matrix_prometheus_nginxlog_exporter_enabled: true
matrix_prometheus_nginxlog_exporter_identifier: ''
# You don't need to configure a hostname and path prefix if you're not exposing this.
matrix_prometheus_nginxlog_exporter_hostname: ''
matrix_prometheus_nginxlog_exporter_path_prefix: "/metrics/nginxlog"
# renovate: datasource=docker depName=ghcr.io/martin-helmich/prometheus-nginxlog-exporter/exporter
matrix_prometheus_nginxlog_exporter_version: v1.10.0
matrix_prometheus_nginxlog_exporter_container_hostname: 'matrix-prometheus-nginxlog-exporter'
matrix_prometheus_nginxlog_exporter_container_metrics_port: '4040'
matrix_prometheus_nginxlog_exporter_container_syslog_port: '6514'
@ -19,7 +25,8 @@ matrix_prometheus_nginxlog_exporter_docker_image_arch_check_enabled: true
# Controls whether prometheus-nginxlog-exporter metrics should be proxied (exposed) on a public URL
matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled: false
# TODO - add more variables for controlling the hostname, path prefix, etc.
matrix_prometheus_nginxlog_exporter_metrics_proxying_hostname: ''
matrix_prometheus_nginxlog_exporter_metrics_proxying_path_prefix: ''
# matrix_prometheus_nginxlog_exporter_dashboard_urls contains a list of URLs with Grafana dashboard definitions.
# If the Grafana role is enabled, these dashboards will be downloaded.
@ -28,13 +35,42 @@ matrix_prometheus_nginxlog_exporter_dashboard_urls: []
matrix_prometheus_nginxlog_exporter_base_path: "{{ matrix_base_data_path }}/prometheus-nginxlog-exporter"
matrix_prometheus_nginxlog_exporter_config_path: "{{ matrix_prometheus_nginxlog_exporter_base_path }}/config"
matrix_prometheus_nginxlog_exporter_container_network: "{{ matrix_docker_network }}"
matrix_prometheus_nginxlog_exporter_container_network: "{{ matrix_prometheus_nginxlog_exporter_identifier }}"
# matrix_prometheus_nginxlog_exporter_container_labels_traefik_enabled controls whether labels to assist a Traefik reverse-proxy will be attached to the container.
# See `../templates/labels.j2` for details.
#
# To inject your own other container labels, see `matrix_prometheus_nginxlog_exporter_container_labels_additional_labels`.
matrix_prometheus_nginxlog_exporter_container_labels_traefik_enabled: "{{ true if matrix_prometheus_nginxlog_exporter_hostname else false }}"
matrix_prometheus_nginxlog_exporter_container_labels_traefik_docker_network: "{{ matrix_prometheus_nginxlog_exporter_container_network }}"
matrix_prometheus_nginxlog_exporter_container_labels_traefik_hostname: "{{ matrix_prometheus_nginxlog_exporter_hostname }}"
# The path prefix must either be `/` or not end with a slash (e.g. `/metrics`).
matrix_prometheus_nginxlog_exporter_container_labels_traefik_path_prefix: "{{ matrix_prometheus_nginxlog_exporter_path_prefix }}"
matrix_prometheus_nginxlog_exporter_container_labels_traefik_rule: "Host(`{{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_hostname }}`) && PathPrefix(`{{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_path_prefix | quote }}`)"
matrix_prometheus_nginxlog_exporter_container_labels_traefik_entrypoints: web-secure
matrix_prometheus_nginxlog_exporter_container_labels_traefik_tls: "{{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_entrypoints != 'web' }}"
matrix_prometheus_nginxlog_exporter_container_labels_traefik_tls_certResolver: default # noqa var-naming
matrix_prometheus_nginxlog_exporter_container_labels_metrics_middleware_basic_auth_enabled: false
# See: https://doc.traefik.io/traefik/middlewares/http/basicauth/#users
matrix_prometheus_nginxlog_exporter_container_labels_metrics_middleware_basic_auth_users: ''
# matrix_prometheus_nginxlog_exporter_container_labels_additional_labels contains a multiline string with additional labels to add to the container label file.
# See `../templates/labels.j2` for details.
#
# Example:
# matrix_prometheus_nginxlog_exporter_container_labels_additional_labels: |
# my.label=1
# another.label="here"
matrix_prometheus_nginxlog_exporter_container_labels_additional_labels: ''
# A list of extra docker arguments to pass to the container
matrix_prometheus_nginxlog_exporter_container_extra_arguments: []
# List of systemd services that matrix-prometheus_nginxlog_exporter.service depends on
matrix_prometheus_nginxlog_exporter_systemd_required_services_list: ['docker.service']
matrix_prometheus_nginxlog_exporter_systemd_required_services_list: "{{ matrix_prometheus_nginxlog_exporter_systemd_required_services_list_default + matrix_prometheus_nginxlog_exporter_systemd_required_services_list_auto + matrix_prometheus_nginxlog_exporter_systemd_required_services_list_custom }}"
matrix_prometheus_nginxlog_exporter_systemd_required_services_list_default: ['docker.service']
matrix_prometheus_nginxlog_exporter_systemd_required_services_list_auto: []
matrix_prometheus_nginxlog_exporter_systemd_required_services_list_custom: []
# List of systemd services that matrix-prometheus_nginxlog_exporter.service wants
matrix_prometheus_nginxlog_exporter_systemd_wanted_services_list: []
@ -49,3 +85,7 @@ matrix_prometheus_nginxlog_exporter_container_http_host_bind_port: ''
# Takes an "<ip>:<port>" or "<port>" value (e.g. "127.0.0.1:6514"), or empty string to not expose.
# NOTE: is an UDP connection!
matrix_prometheus_nginxlog_exporter_container_syslog_host_bind_port: ''
matrix_prometheus_nginxlog_exporter_config_namespaces_matrix_source_tags: "{{ matrix_prometheus_nginxlog_exporter_config_namespaces_matrix_source_tags_auto + matrix_prometheus_nginxlog_exporter_config_namespaces_matrix_source_tags_custom }}"
matrix_prometheus_nginxlog_exporter_config_namespaces_matrix_source_tags_auto: []
matrix_prometheus_nginxlog_exporter_config_namespaces_matrix_source_tags_custom: []

View File

@ -1,23 +0,0 @@
---
- name: Fail if matrix-nginx-proxy role already executed
ansible.builtin.fail:
msg: >-
Trying to append prometheus-nginxlog-exporter's configuration to matrix-nginx-proxy,
but it's pointless since the matrix-nginx-proxy role is already executed.
To fix this, please change the order of roles in your playbook to make sure that
the matrix-nginx-proxy role is run after the prometheus-nginxlog-exporter role.
when: matrix_nginx_proxy_role_executed | default(False) | bool
- name: Generate prometheus-nginxlog-exporter's metrics proxying configuration for matrix-nginx-proxy (matrix.DOMAIN/metrics/nginxlog)
ansible.builtin.set_fact:
matrix_prometheus_nginxlog_exporter_matrix_nginx_proxy_metrics_configuration_matrix_domain: |
location /metrics/nginxlog {
{# Use the embedded DNS resolver in Docker containers to discover the service #}
resolver 127.0.0.11 valid=5s;
set $backend "{{ matrix_prometheus_nginxlog_exporter_container_hostname }}:{{ matrix_prometheus_nginxlog_exporter_container_metrics_port }}";
proxy_pass http://$backend/metrics;
}
when: matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled | bool and matrix_nginx_proxy_enabled | bool
# Injection code was here

View File

@ -1,14 +1,5 @@
---
- tags:
- setup-all
- setup-prometheus-nginxlog-exporter
- install-all
- install-prometheus-nginxlog-exporter
block:
- when: matrix_prometheus_nginxlog_exporter_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml"
- tags:
- setup-all
- setup-prometheus-nginxlog-exporter

View File

@ -30,6 +30,16 @@
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
- name: Ensure prometheus-nginxlog-exporter support files installed
ansible.builtin.template:
src: "{{ role_path }}/templates/{{ item }}.j2"
dest: "{{ matrix_prometheus_nginxlog_exporter_base_path }}/{{ item }}"
mode: 0640
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
with_items:
- labels
- name: Ensure prometheus-nginxlog-exporter container network is created
community.general.docker_network:
name: "{{ matrix_prometheus_nginxlog_exporter_container_network }}"

View File

@ -1,5 +1,14 @@
---
- name: (Deprecation) Catch and report renamed prometheus-nginxlog-exporter settings
ansible.builtin.fail:
msg: >-
Your configuration contains a variable, which now has a different name.
Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`).
when: "item.old in vars"
with_items:
- {'old': 'matrix_prometheus_nginxlog_exporter_container_hostname', 'new': 'matrix_prometheus_nginxlog_exporter_identifier'}
- name: Fail if docker image not availble for arch
ansible.builtin.fail:
msg: >
@ -16,3 +25,13 @@
in vars.yml
when: matrix_prometheus_nginxlog_exporter_docker_image_arch_check_enabled and matrix_architecture not in matrix_prometheus_nginxlog_exporter_docker_image_arch
- name: Fail if required prometheus-nginxlog-exporter settings not defined
ansible.builtin.fail:
msg: >-
You need to define a required configuration setting (`{{ item.name }}`).
when: "item.when | bool and vars[item.name] == ''"
with_items:
- {'name': 'matrix_prometheus_nginxlog_exporter_metrics_proxying_hostname', when: "{{ matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled }}"}
- {'name': 'matrix_prometheus_nginxlog_exporter_metrics_proxying_path_prefix', when: "{{ matrix_prometheus_nginxlog_exporter_metrics_proxying_enabled }}"}
- {'name': 'matrix_prometheus_nginxlog_exporter_container_network', when: true}

View File

@ -0,0 +1,36 @@
{% if matrix_prometheus_nginxlog_exporter_container_labels_traefik_enabled %}
traefik.enable=true
{% if matrix_prometheus_nginxlog_exporter_container_labels_traefik_docker_network %}
traefik.docker.network={{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_docker_network }}
{% endif %}
traefik.http.services.{{ matrix_prometheus_nginxlog_exporter_identifier }}.loadbalancer.server.port={{ matrix_prometheus_nginxlog_exporter_container_metrics_port }}
{% set middlewares = [] %}
{% if matrix_prometheus_nginxlog_exporter_container_labels_metrics_middleware_basic_auth_enabled %}
{% set middlewares = middlewares + [matrix_prometheus_nginxlog_exporter_identifier + '-basic-auth'] %}
traefik.http.middlewares.{{ matrix_prometheus_nginxlog_exporter_identifier }}-basic-auth.basicauth.users={{ matrix_prometheus_nginxlog_exporter_container_labels_metrics_middleware_basic_auth_users }}
{% endif %}
{% set middlewares = middlewares + [matrix_prometheus_nginxlog_exporter_identifier + '-replacepath'] %}
traefik.http.middlewares.{{ matrix_prometheus_nginxlog_exporter_identifier }}-replacepath.replacepath.path=/metrics
traefik.http.routers.{{ matrix_prometheus_nginxlog_exporter_identifier }}.rule={{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_rule }}
{% if middlewares | length > 0 %}
traefik.http.routers.{{ matrix_prometheus_nginxlog_exporter_identifier }}.middlewares={{ middlewares | join(',') }}
{% endif %}
traefik.http.routers.{{ matrix_prometheus_nginxlog_exporter_identifier }}.service={{ matrix_prometheus_nginxlog_exporter_identifier }}
traefik.http.routers.{{ matrix_prometheus_nginxlog_exporter_identifier }}.entrypoints={{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_entrypoints }}
traefik.http.routers.{{ matrix_prometheus_nginxlog_exporter_identifier }}.tls={{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_tls | to_json }}
{% if matrix_prometheus_nginxlog_exporter_container_labels_traefik_tls %}
traefik.http.routers.{{ matrix_prometheus_nginxlog_exporter_identifier }}.tls.certResolver={{ matrix_prometheus_nginxlog_exporter_container_labels_traefik_tls_certResolver }}
{% endif %}
{% endif %}
{{ matrix_prometheus_nginxlog_exporter_container_labels_additional_labels }}

View File

@ -16,7 +16,7 @@ namespaces:
listen_address: "udp://0.0.0.0:{{ matrix_prometheus_nginxlog_exporter_container_syslog_port }}"
# format options: "rfc3164" ,"rfc5424", "rfc6587", "auto"
format: auto
tags: ["matrix_nginx_proxy", "matrix_nginx_proxy_companion"]
tags: {{ matrix_prometheus_nginxlog_exporter_config_namespaces_matrix_source_tags | to_json }}
relabel_configs:
- target_label: request_uri
from: request
@ -54,4 +54,4 @@ namespaces:
- target_label: request_host
from: host
- target_label: log_source
from: log_source
from: log_source

View File

@ -1,6 +1,6 @@
#jinja2: lstrip_blocks: "True"
[Unit]
Description={{ matrix_prometheus_nginxlog_exporter_container_hostname }}
Description=prometheus-nginxlog-exporteer ({{ matrix_prometheus_nginxlog_exporter_identifier }})
{% for service in matrix_prometheus_nginxlog_exporter_systemd_required_services_list %}
Requires={{ service }}
After={{ service }}
@ -13,11 +13,12 @@ DefaultDependencies=no
[Service]
Type=simple
Environment="HOME={{ devture_systemd_docker_base_systemd_unit_home_path }}"
ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} stop --time={{ devture_systemd_docker_base_container_stop_grace_time_seconds }} {{ matrix_prometheus_nginxlog_exporter_container_hostname }} 2>/dev/null || true'
ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm {{ matrix_prometheus_nginxlog_exporter_container_hostname }} 2>/dev/null || true'
ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} stop --time={{ devture_systemd_docker_base_container_stop_grace_time_seconds }} {{ matrix_prometheus_nginxlog_exporter_identifier }} 2>/dev/null || true'
ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm {{ matrix_prometheus_nginxlog_exporter_identifier }} 2>/dev/null || true'
ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name {{ matrix_prometheus_nginxlog_exporter_container_hostname }} \
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \
--rm \
--name={{ matrix_prometheus_nginxlog_exporter_identifier }} \
--log-driver=none \
--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
--cap-drop=ALL \
@ -30,17 +31,24 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name
-p {{ matrix_prometheus_nginxlog_exporter_container_syslog_host_bind_port }}:{{ matrix_prometheus_nginxlog_exporter_container_syslog_port }}/udp \
{% endif %}
--mount type=bind,src={{ matrix_prometheus_nginxlog_exporter_config_path }},dst=/etc/prometheus-nginxlog-exporter \
--label-file={{ matrix_prometheus_nginxlog_exporter_base_path }}/labels \
{% for arg in matrix_prometheus_nginxlog_exporter_container_extra_arguments %}
{{ arg }} \
{% endfor %}
{{ matrix_prometheus_nginxlog_exporter_docker_image }} \
-config-file /etc/prometheus-nginxlog-exporter/prometheus-nginxlog-exporter.yaml
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} stop --time={{ devture_systemd_docker_base_container_stop_grace_time_seconds }} {{ matrix_prometheus_nginxlog_exporter_container_hostname }} 2>/dev/null || true'
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm {{ matrix_prometheus_nginxlog_exporter_container_hostname }} 2>/dev/null || true'
{% for network in matrix_mautrix_facebook_container_additional_networks %}
ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} {{ matrix_prometheus_nginxlog_exporter_identifier }}
{% endfor %}
ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach {{ matrix_prometheus_nginxlog_exporter_identifier }}
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} stop --time={{ devture_systemd_docker_base_container_stop_grace_time_seconds }} {{ matrix_prometheus_nginxlog_exporter_identifier }} 2>/dev/null || true'
ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm {{ matrix_prometheus_nginxlog_exporter_identifier }} 2>/dev/null || true'
Restart=always
RestartSec=30
SyslogIdentifier={{ matrix_prometheus_nginxlog_exporter_container_hostname }}
SyslogIdentifier={{ matrix_prometheus_nginxlog_exporter_identifier }}
[Install]
WantedBy=multi-user.target