websitestat

package
v0.0.0-...-c7cde22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPageView

func IsPageView(entry *LogEntry) bool

IsPageView 判定请求是否为页面浏览(PV)

func IsStaticResource

func IsStaticResource(uri string) bool

IsStaticResource 判定 URI 是否为静态资源(js/css/图片/字体等)

func ParseSyslog

func ParseSyslog(msg []byte) (string, []byte)

ParseSyslog 从 syslog 消息中提取 tag 和 JSON 体 格式: <PRI>MMM DD HH:MM:SS tag: JSON(nohostname 模式下无主机名)

func ParseUA

func ParseUA(rawUA string) (browser, os string)

ParseUA 解析 User-Agent,返回浏览器和操作系统名称

func SpiderName

func SpiderName(ua string) string

SpiderName 返回蜘蛛名称,非蜘蛛返回空字符串

Types

type Aggregator

type Aggregator struct {

	// 可配置上限
	ErrBufMaxSize int  // 错误缓冲最大条目数,默认 10000
	UVMaxKeys     int  // 每站每天 UV 去重 set 上限,默认 1000000
	IPMaxKeys     int  // 每站每天 IP 去重 set 上限,默认 500000
	DetailMaxKeys int  // 详细统计(蜘蛛/客户端/IP/URI)每站每天最大条目数,默认 50000
	BodyEnabled   bool // 是否记录错误请求体,默认 false
	// contains filtered or unexported fields
}

Aggregator 内存聚合器,将日志条目聚合为站点统计快照

func NewAggregator

func NewAggregator() *Aggregator

NewAggregator 创建聚合器实例

func (*Aggregator) DrainDetailStats

func (a *Aggregator) DrainDetailStats() (detailsByDate map[string]map[string]*SiteDetailSnapshot, commit func())

DrainDetailStats 导出详细统计增量(蜘蛛/客户端/IP/URI) 返回 commit 函数,调用方写库成功后必须调用 commit 来清空计数器

func (*Aggregator) DrainErrors

func (a *Aggregator) DrainErrors() (entries []*ErrorEntry, commit func())

DrainErrors 取出错误缓冲快照 返回 commit 函数,写库成功后调用以移除已消费的条目

func (*Aggregator) DrainSnapshot

func (a *Aggregator) DrainSnapshot() (snapshotsByDate map[string]map[string]*SiteSnapshot, commit func())

DrainSnapshot 返回自上次 drain 以来的增量快照 返回 commit 函数,调用方写库成功后必须调用 commit 来消费已快照的增量 若写库失败不调用 commit,数据保留在内存中,下次 drain 会再次包含

func (*Aggregator) Realtime

func (a *Aggregator) Realtime() RealtimeStats

Realtime 返回最近一段时间的实时流量和 RPS

func (*Aggregator) Record

func (a *Aggregator) Record(entry *LogEntry)

Record 记录一条访问日志

func (*Aggregator) Reset

func (a *Aggregator) Reset()

Reset 清空所有内存聚合数据(配合 DB 清空使用)

func (*Aggregator) SiteStats

func (a *Aggregator) SiteStats() map[string]*SiteSnapshot

SiteStats 返回各站点自上次刷新以来的未刷新增量(用于实时展示叠加到 DB 数据上)

type ClientCount

type ClientCount struct {
	Requests uint64
}

ClientCount 客户端计数

type ErrorEntry

type ErrorEntry struct {
	Site   string
	URI    string
	Method string
	IP     string
	UA     string
	Body   string
	Status int
}

ErrorEntry 错误日志条目

type HourSnapshot

type HourSnapshot struct {
	PV               uint64 `json:"pv"`
	UV               uint64 `json:"uv"`
	IP               uint64 `json:"ip"`
	Bandwidth        uint64 `json:"bandwidth"`
	BandwidthIn      uint64 `json:"bandwidth_in"`
	Requests         uint64 `json:"requests"`
	Errors           uint64 `json:"errors"`
	Spiders          uint64 `json:"spiders"`
	RequestTimeSum   uint64 `json:"request_time_sum"`
	RequestTimeCount uint64 `json:"request_time_count"`
	Status2xx        uint64 `json:"status_2xx"`
	Status3xx        uint64 `json:"status_3xx"`
	Status4xx        uint64 `json:"status_4xx"`
	Status5xx        uint64 `json:"status_5xx"`
}

HourSnapshot 小时粒度快照

type IPCount

type IPCount struct {
	Requests  uint64
	Bandwidth uint64
}

IPCount IP 计数

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener 通过 Unix Datagram Socket 监听 nginx syslog 消息

func NewListener

func NewListener(path string, log *slog.Logger) (*Listener, error)

NewListener 创建并绑定 Unix Datagram Socket 监听器

func (*Listener) Close

func (l *Listener) Close() error

Close 关闭监听器并清理 socket 文件

func (*Listener) Read

func (l *Listener) Read() (string, []byte, error)

Read 读取一条 syslog 消息,返回 tag 和 JSON 数据

type LogEntry

type LogEntry struct {
	Site        string  // 来自 syslog tag
	URI         string  // 请求 URI
	Status      int     // HTTP 状态码
	Bytes       uint64  // 响应体大小
	UA          string  // User-Agent
	IP          string  // 客户端 IP
	Method      string  // 请求方法
	Body        string  // 请求体(仅 400-599 状态码时保留)
	ContentType string  // 响应 Content-Type(PV 判定用)
	ReqLength   uint64  // 请求大小(入站流量)
	RequestTime float64 // 请求耗时(秒)
}

LogEntry 解析后的访问日志条目

func ParseLogEntry

func ParseLogEntry(tag string, data []byte) (*LogEntry, error)

ParseLogEntry 将 JSON 数据解析为 LogEntry

type RealtimeStats

type RealtimeStats struct {
	Bandwidth   float64 `json:"bandwidth"`    // 出站字节/秒
	BandwidthIn float64 `json:"bandwidth_in"` // 入站字节/秒
	RPS         float64 `json:"rps"`          // 请求/秒
}

RealtimeStats 实时统计

type SiteDetailSnapshot

type SiteDetailSnapshot struct {
	Spiders map[string]uint64       // spider_name → requests
	Clients map[string]*ClientCount // "browser|os" → count
	IPs     map[string]*IPCount     // ip → count
	URIs    map[string]*URICount    // uri → count
}

SiteDetailSnapshot 站点详细统计快照(用于 DrainDetailStats)

type SiteSnapshot

type SiteSnapshot struct {
	PV               uint64            `json:"pv"`
	UV               uint64            `json:"uv"`
	IP               uint64            `json:"ip"`
	Bandwidth        uint64            `json:"bandwidth"`
	BandwidthIn      uint64            `json:"bandwidth_in"`
	Requests         uint64            `json:"requests"`
	Errors           uint64            `json:"errors"`
	Spiders          uint64            `json:"spiders"`
	RequestTimeSum   uint64            `json:"request_time_sum"`
	RequestTimeCount uint64            `json:"request_time_count"`
	Status2xx        uint64            `json:"status_2xx"`
	Status3xx        uint64            `json:"status_3xx"`
	Status4xx        uint64            `json:"status_4xx"`
	Status5xx        uint64            `json:"status_5xx"`
	Hours            [24]*HourSnapshot `json:"-"`
}

SiteSnapshot 站点快照(用于 DB flush)

type URICount

type URICount struct {
	Requests         uint64
	Bandwidth        uint64
	Errors           uint64
	RequestTimeSum   uint64
	RequestTimeCount uint64
}

URICount URI 计数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL