mtail 是谷歌开源的一款很不错的应用日志提取工具,我们可以方便的用来提取应用的数据
到常见的监控系统(prometheus,stats,collectd,gragphite。。。。)
说明: demo 使用此工具转换nginx error message 为prometheus 的metrics
环境准备
- docker-compose 文件
version: "3"
services:
nginx-log:
build: ./
ports:
- "8090:80"
- "3903:3903"
volumes:
- "./examples/linecount.mtail:/progs/linecount.mtail"
- nginx dockerfile
FROM dalongrong/mtail as builder
FROM openresty/openresty:alpine
ENV TINI_VERSION v0.18.0
RUN apk add --update
&& apk add --no-cache tini
ADD entrypoint.sh /entrypoint.sh
ADD mtail.sh /mtail.sh
COPY nginx.conf usr/local/openresty/nginx/conf/
COPY --from=builder /usr/bin/mtail /usr/bin/
EXPOSE 80 3903
ENTRYPOINT ["/sbin/tini","-s", "--", "/entrypoint.sh"]
- entrypoint shell
#!/bin/sh
sh mtail.sh
exec /usr/local/openresty/bin/openresty -g "daemon off;"
mtail.sh:
#!/bin/sh
nohup /usr/bin/mtail -logtostderr -progs /progs/linecount.mtail -logs /var/log/error.log &
- nginx log 配置
nginx.conf
+ error_log /var/log/error.log error;
+ user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
resolver 114.114.114.114;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
+ log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
+ access_log /var/log/access.log main;
- metric 脚本
counter line_count
/$/ {
line_count++
}
- 说明
mtail 镜像是自己构建的,使用代码中的dockerfile
启动&&测试
- 构建镜像
docker-compose build
- 启动
docker-compose up -d
- 生成错误日志
访问不存在的地址即可, open http://localhost:8090/eeeee - mtail 生成的metrics api
open http://localhost:3903/ - 效果
prometheus metrics 效果
说明
这个demo很简单,但是实际上mtail 可以做好多很方便的事情,对于我们搞微服务以及devops,监控平台是一个很不错的工具
参考资料
https://github.com/google/mtail/blob/master/docs/Building.md
https://github.com/rongfengliang/mtail-nginx-docker-compose-demo