martin-helmich / prometheus-nginxlog-exporter

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

cannot read file nginxlog-exporter.conf #390

Open scottie1996 opened 4 months ago

scottie1996 commented 4 months ago

Issue Summary

Title: Prometheus Nginxlog Exporter: Config File Unsupported File Type Error

Description:

I encountered an issue while setting up the prometheus-nginxlog-exporter. Despite following the instructions to create a configuration file, the exporter fails to start, citing an "unsupported file type" error. Below are the detailed steps, error messages, and attempted resolutions.

Steps to Reproduce:

  1. Environment Details:

    • OS: CentOS 7
    • Prometheus Nginxlog Exporter version: 1.11.0
  2. Initial Configuration:

    • Created a configuration file /opt/nginxlog-exporter.conf in YAML format:

      log_file: /var/log/nginx/access.log
      metrics:
      - name: http_requests_total
       match: 'GET /'
       labels:
         method: GET
      - name: http_requests_total
       match: 'POST /'
       labels:
         method: POST
  3. Service Configuration:

    • Created a systemd service file /etc/systemd/system/nginxlog-exporter.service:

      [Unit]
      Description=Prometheus Nginxlog Exporter
      After=network.target
      
      [Service]
      ExecStart=/opt/prometheus-nginxlog-exporter -config-file /opt/nginxlog-exporter.conf
      Restart=always
      
      [Install]
      WantedBy=multi-user.target
  4. Errors Encountered:

    • When starting the service, the following error is logged:
      config file '/opt/nginxlog-exporter.conf' has unsupported file type

Error Logs:

2024-06-14T17:33:07.870+0800 fatal prometheus-nginxlog-exporter/main.go:164 config file '/opt/nginxlog-exporter.conf' has unsupported file type
main.loadConfig
/home/runner/work/prometheus-nginxlog-exporter/prometheus-nginxlog-exporter/main.go:164
main.main
/home/runner/work/prometheus-nginxlog-exporter/prometheus-nginxlog-exporter/main.go:117
runtime.main
/opt/hostedtoolcache/go/1.18.10/x64/src/runtime/proc.go:250

Resolution Steps Taken:

  1. Identified Issue with File Format:

    • Realized that the prometheus-nginxlog-exporter expects an HCL (HashiCorp Configuration Language) formatted configuration file.
  2. Updated Configuration File to HCL Format:

    • Modified /opt/nginxlog-exporter.conf to the following HCL format:

      listen {
      port = 9113
      }
      
      namespace "nginx" {
      format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\""
      
      source {
       files = ["/var/log/nginx/access.log"]
      }
      
      labels {
       app = "nginx"
      }
      
      counters {
       request_count {
         help = "Number of HTTP requests"
         match = ".*"
         name = "http_requests_total"
       }
      }
      
      histograms {
       request_duration {
         help = "Histogram of request duration"
         match = ".*"
         name = "http_request_duration_seconds"
         buckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]
       }
      }
      }
  3. Manual Testing:

    • Ran the exporter manually to verify configuration:

      /opt/prometheus-nginxlog-exporter -config-file /opt/nginxlog-exporter.conf
    • This command worked without errors.

  4. Restarted Service:

    • Restarted the systemd service:

      sudo systemctl start nginxlog-exporter
      sudo systemctl status nginxlog-exporter
  5. Verified Prometheus Target:

    • Checked Prometheus targets at http://10.0.89.39:9090/targets and confirmed that the nginxlog-exporter is now running and reporting metrics.

Conclusion:

The issue was caused by the incorrect file format for the configuration file. The exporter expects HCL format, not YAML. Updating the configuration file to HCL format resolved the issue.