martin-helmich / prometheus-nginxlog-exporter

Export metrics from Nginx access log files to Prometheus
Apache License 2.0
958 stars 173 forks source link

no metrics but all nginx log parameters appear #309 - addon #394

Closed palansher closed 2 months ago

palansher commented 2 months ago

In addition to https://github.com/martin-helmich/prometheus-nginxlog-exporter/issues/309

v1.11.0

At a quick glance: In your exporter config there are two spaces before \"$http_user_agent\", instead of one (like in the NGINX config); this will most likely cause the generated regex to not match any of the log lines, because the exporter will then also expect double spaces in your log lines.

Not at all.

I have same issue with exactly same log format as in nginx log format. Would you please give me a tip?

sudo service prometheus-nginxlog-exporter status

prometheus-nginxlog-exporter/main.go:162        loading configuration file /etc/prometheus-nginxlog-exporter.hcl
        config/struct_namespace.go:98        No globs for /var/log/nginx/app.palansher.devops.rebrain.srwx.net.access.log
        config/struct_namespace.go:98        No globs for /var/log/nginx/app.palansher.devops.rebrain.srwx.net.app_access.log
        prometheus-nginxlog-exporter/main.go:139        starting listener for namespace nginx
        prometheus-nginxlog-exporter/main.go:148        running HTTP server on address 0.0.0.0:8080, serving metrics at /metrics

No globs for /var/log/nginx/app.palansher.devops.rebrain.srwx.net.access.log This is the problem

I have no extra space in format:

nginx -T

# configuration file /etc/nginx/conf.d/default.conf:
map $upstream_http_x_server_name $log_upstream1 {
    "app1.palansher.devops.rebrain.srwx.net" 1;
    default 0;
}

map $upstream_http_x_server_name $log_upstream2 {
    "app2.palansher.devops.rebrain.srwx.net" 1;
    default 0;
}

upstream app_upstream {

    server app1.palansher.devops.rebrain.srwx.net weight=1;
    server app2.palansher.devops.rebrain.srwx.net weight=2;

    check interval=1000 rise=1 fall=2 timeout=1000 type=http;
    check_http_send "GET /health HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}

log_format upstream_time "$request $request_time $upstream_response_time";

log_format  lb_access '$remote_addr [$time_local] "$request" [$status from $upstream_http_x_server_name] $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

server {
    listen 80;
    server_name app.palansher.devops.rebrain.srwx.net;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name app.palansher.devops.rebrain.srwx.net;

    root /var/www;

    error_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.error.log;
    access_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.access.log lb_access;

    ssl_certificate /etc/letsencrypt/live/app.palansher.devops.rebrain.srwx.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.palansher.devops.rebrain.srwx.net/privkey.pem;

    # underscores_in_headers on;
    add_header X-Info1 "I am $hostname in server" always;
    add_header X-Hostname $hostname always;

    location /app {
        # access_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.time.access.log upstream_time;
        # access_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.proxy-access.log proxied;
        access_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.app_access.log lb_access;
        access_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.upstream_app1.log upstream_time if=$log_upstream1;
        access_log /var/log/nginx/app.palansher.devops.rebrain.srwx.net.upstream_app2.log upstream_time if=$log_upstream2;
        #  if=;
        add_header X-Info1 "I am $hostname in /app" always;
        add_header X-Hostname $host always;
        proxy_set_header X-Hostname $host;
        proxy_pass_header X-Hostname;
        proxy_set_header X-Info-Pass1 "I am $hostname in /app";

        # real_ip_header X-Forwarded-For;
        # real_ip_header X-Real-IP;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://app_upstream;

    }

this is exporter config:

vladp@lb1:~$ cat /etc/prometheus-nginxlog-exporter.hcl 
listen {
  port = 8080
  metrics_endpoint = "/metrics"
}

namespace "nginx" {  
  format = "$remote_addr [$time_local] \"$request\" [$status from $upstream_http_x_server_name] $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
  //source_files = ["/var/log/nginx/app.palansher.devops.rebrain.srwx.net.access.log"]
  source {
    files = [
      "/var/log/nginx/app.palansher.devops.rebrain.srwx.net.access.log",
      "/var/log/nginx/app.palansher.devops.rebrain.srwx.net.app_access.log"
      ]

  }
}

Log format is absolutely same. Please help find the reason of issue..

palansher commented 2 months ago

I got the reason:

The message "No globs for /var/log/nginx/ ..." is just warning about fact that I did not use a glob in log file pattern. Such a tight supervision :) After I added a glob like

source {
    files = [
      "/var/log/nginx/app.palansher.devops.rebrain.srwx.net.*access.log"
      ]   
  }

That warning disappeared. Also I see the metrics now If somebody interested here is working config:

listen {
  port = 4040
  metrics_endpoint = "/metrics"
  address = "0.0.0.0"
}

consul {
  enable = false
  }

namespace "nginx" {  

  format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""

  source {
    files = [
      "/var/log/nginx/app.palansher.devops.rebrain.srwx.net.*access.log"
      ]   
  }

  labels {
    service = "nginx-final"
    hostname = "lb1"
  }

   # log can be printed to std out, e.g. for debugging purposes (disabled by default)
  print_log = false

}

Thank you for the nice exporter and for time that you given me to think about this situation :) closing issue ..