songjiayang / nginx-log-exporter

A HTTP log exporter for prometheus metrics.
88 stars 22 forks source link

Issue with exemplars #7

Closed frenkye closed 1 year ago

frenkye commented 1 year ago

Hi, I wanted to test the exemplars part of this exporter, if it would be to our interest, but I have problem to enable this feature.

First I found out the exemplars was added after the 0.2.0 version, so i build from source, but still can't manage to show them on histograms.

Any advice?

- name: test
  format: $remote_addr [$time_local] "$method $request $protocol" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time "$upstream_addr" $upstream_response_time $upstream_cache_status $cookie_SESSID $sent_http_x_request_id request_id=$request_id trace_id=$trace_id
  source_files:
    - ./path/to/log
  relabel_config:
    source_labels:
      - request
      - method
      - status
    replacement:
      request:
        trim: "?"
      status:
        replace:
          - target: 4.+
            value: 4xx
          - target: 5.+
            value: 5xx
  histogram_buckets: [0.1, 0.3, 0.5, 1, 2]
  exemplar_config:
    match:
      request_time: ">= 0.3"
    labels:
      - request_id
      - trace_id
      - remote_addr

Log line (is a bit messy but i trying the new fields):

192.168.21.1 [07/Mar/2023:13:19:17 +0100] "GET /XXXX HTTP/1.1" 200 122524 "https://referer/XXXX" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0" 3.680 "unix:/var/lib/php/7.4/fpm/name.sock" 3.680 - a7b4j3m1cq205qurit808a9qfk 1c6394284038c167766bfd4ab3b3be72 request_id=1c6394284038c167766bfd4ab3b3be72 trace_id=ef499069fb92418e
songjiayang commented 1 year ago

Sorry to reply you so late.

When new line log writing, it will trigger the one before it to collect. So if we want the example works, just write two lines logs or a blank line as end.

The new test.log file:

192.168.21.1 [07/Mar/2023:13:19:17 +0100] "GET /XXXX HTTP/1.1" 200 122524 "https://referer/XXXX" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0" 3.680 "unix:/var/lib/php/7.4/fpm/name.sock" 3.680 - a7b4j3m1cq205qurit808a9qfk 1c6394284038c167766bfd4ab3b3be72 request_id=1c6394284038c167766bfd4ab3b3be72 trace_id=ef499069fb92418e
192.168.21.1 [07/Mar/2023:13:19:17 +0100] "GET /XXXX HTTP/1.1" 200 122524 "https://referer/XXXX" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0" 3.680 "unix:/var/lib/php/7.4/fpm/name.sock" 3.680 - a7b4j3m1cq205qurit808a9qfk 1c6394284038c167766bfd4ab3b3be72 request_id=1c6394284038c167766bfd4ab3b3be72 trace_id=ef499069fb92418e

CURL with openmetrics accept header:

$ curl -H 'accept: application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1' http://localhost:9999/metrics | grep test_http_response_time_seconds_bucket

The output:

test_http_response_time_seconds_bucket{method="GET",request="/XXXX",status="200",le="0.1"} 0
test_http_response_time_seconds_bucket{method="GET",request="/XXXX",status="200",le="0.3"} 0
test_http_response_time_seconds_bucket{method="GET",request="/XXXX",status="200",le="0.5"} 0
test_http_response_time_seconds_bucket{method="GET",request="/XXXX",status="200",le="1.0"} 0
test_http_response_time_seconds_bucket{method="GET",request="/XXXX",status="200",le="2.0"} 0
test_http_response_time_seconds_bucket{method="GET",request="/XXXX",status="200",le="+Inf"} 1 # {trace_id="ef499069fb92418e",remote_addr="192.168.21.1",request_id="1c6394284038c167766bfd4ab3b3be72"} 3.68 1.6784166345202973e+09