Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active July 11, 2024 11:28
Show Gist options
  • Save gilangvperdana/e8154a294e5139268ab631dba6a7061a to your computer and use it in GitHub Desktop.
Save gilangvperdana/e8154a294e5139268ab631dba6a7061a to your computer and use it in GitHub Desktop.
Integrate Logging with Promtail & Loki

General

Hello world, in this topic actually I'm making a logging visualization in grafana for GeoIP purposes. As we know, GeoIP cannot use Prometheus, we have to use metrics in the form of raw logs, which is called logging. Because I don't have a lot of resources for running the ELK Stack, in the end I just ran logging with Promtail + Loki. Follow this technical for installation and integration of Promtail & Loki.

I will continue from this article to visualize it with Promtail + Loki.

Change nginx.conf

	log_format json_analytics escape=json '{'
						'"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
						'"connection": "$connection", ' # connection serial number
						'"connection_requests": "$connection_requests", ' # number of requests made in connection
						'"pid": "$pid", ' # process pid
						'"request_id": "$request_id", ' # the unique request id
						'"request_length": "$request_length", ' # request length (including headers and body)
						'"remote_addr": "$remote_addr", ' # client IP
						'"remote_user": "$remote_user", ' # client HTTP username
						'"remote_port": "$remote_port", ' # client port
						'"time_local": "$time_local", '
						'"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
						'"request": "$request", ' # full path no arguments if the request
						'"request_uri": "$request_uri", ' # full path and arguments if the request
						'"geoip_city_code": "$geoip2_data_city_name - $geoIP", ' # args -> geoIPCity
						'"status": "$status", ' # response status code
						'"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
						'"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
						'"http_referer": "$http_referer", ' # HTTP referer
						'"http_user_agent": "$http_user_agent", ' # user agent
						'"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
						'"http_host": "$http_host", ' # the request Host: header
						'"server_name": "$server_name", ' # the name of the vhost serving the request
						'"request_time": "$request_time", ' # request processing time in seconds with msec resolution
						'"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
						'"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
						'"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
						'"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
						'"upstream_response_length": "$upstream_response_length", ' # upstream response length
						'"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
						'"ssl_protocol": "$ssl_protocol", ' # TLS protocol
						'"ssl_cipher": "$ssl_cipher", ' # TLS cipher
						'"scheme": "$scheme", ' # http or https
						'"request_method": "$request_method", ' # request method
						'"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
						'"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
						'"gzip_ratio": "$gzip_ratio", '
						'"http_cf_ray": "$http_cf_ray",'
						'"geoip_country_code": "$geoIP" '
						'}';

        access_log /var/log/nginx/access.log json_analytics;

Configure our Promtail promtail-config.yaml

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://YOURLOKI'SVM:3100/loki/api/v1/push

scrape_configs:
- job_name: nginx
  static_configs:
  - targets:
      - localhost
    labels:
      job: nginx
      host: gatewaylab
      agent: promtail
      __path__: /var/log/nginx/access.log

Run Promtail as Container

to simplify this setup, i preffer deploy Promtail on Continaer

sudo mv promtail-config.yaml /mnt/config/
docker create --name promtail --restart always -v /mnt/config:/mnt/config -v /var/log:/var/log grafana/promtail:2.1.0 -config.file=/mnt/config/promtail-config.yaml
docker start promtail

Install Loki as a Service

curl -O -L "https://github.com/grafana/loki/releases/download/v2.4.1/loki-linux-amd64.zip"
unzip loki-linux-amd64.zip
chmod a+x loki-linux-amd64
mkdir -p /usr/local/bin/loki
sudo cp loki-linux-amd64 /usr/local/bin/loki/
  • Create loki default configuration
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

common:
  path_prefix: /etc/loki
  storage:
    filesystem:
      chunks_directory: /etc/loki/chunks
      rules_directory: /etc/loki/rules
  replication_factor: 1
  ring:
    instance_addr: 0.0.0.0
    kvstore:
      store: inmemory

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

limits_config:
  allow_structured_metadata: false
  max_query_series: 100000
  max_query_parallelism: 2

ruler:
  alertmanager_url: http://localhost:9093

table_manager:
  retention_deletes_enabled: true
  retention_period: 72h
  • Create Loki as systemd
nano /etc/systemd/system/loki.service
[Unit] 
Description=Loki service 
After=network.target 
 
[Service] 
Type=simple 
User=root
ExecStart=/usr/local/bin/loki/loki-linux-amd64 -config.file /etc/loki/loki-local-config.yaml 
Restart=on-failure 
RestartSec=20 
StandardOutput=append:/etc/loki/logs/loki.log 
StandardError=append:/etc/loki/logs/loki.log 
 
[Install] 
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable loki
systemctl start loki
systemctl restart loki
systemctl status loki

Mounting Loki as a Datasource on Grafana

image

Goals

image image

Dashboard template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment