mtail 提取应用日志数据到时序数据库的工具-支持prometheus

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
 
 

说明

这个demo很简单,但是实际上mtail 可以做好多很方便的事情,对于我们搞微服务以及devops,监控平台是一个很不错的工具

参考资料

https://github.com/google/mtail/blob/master/docs/Building.md
https://github.com/rongfengliang/mtail-nginx-docker-compose-demo

posted on   荣锋亮  阅读(5387)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-12-13 gradle asciidoc 使用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示