本代码实现了从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