Golang并发实现Nginx日志分析与监控(2)——完整代码

该代码示例演示了如何利用Golang的并发特性,从Nginx access.log中提取数据,通过正则表达式解析请求时间,并通过特定端口对外暴露,以便Prometheus进行监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本代码实现了从Nginx日志access.log中读取信息,并用正则解析,最后将requestTime从端口暴露,以便让prometheus抓取。使用Golang并发实现。

package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"net/url"
	"os"
	"regexp"
	"strconv"
	"strings"
	"time"
	"net/http"

	"github.com/prometheus/client_golang/prometheus/promhttp"
    "github.com/prometheus/client_golang/prometheus"
)

type Reader interface {
   
	Read(rc chan []byte)
}

type Writer interface {
   
	Write(wc chan *Message)
}

type LogProcess struct {
   
	rc    chan []byte
	wc    chan *Message
	read  Reader //接收和读取写入模块
	write Writer //
}

type ReadFromFile struct {
   
	path string
}

type WriteToProm struct {
   
	
}

type Message struct {
   
	TimeLocal                    time.Time
	BytesSent                    int
	Path, Method, Scheme, Status string
	UpStreamTime, RequestTime    float64
}

func (r *ReadFromFile) Read(rc chan []byte) {
   
	//读取模块
	//打开文件
	f, err := os.Open(r.path)
	if err != nil {
   
		panic(fmt.Sprintf("open file error:%s", err.Error()))
	}

	//从末位开始逐行读取
	f.Seek
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值