mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
fb5a4e5b59
OpenResty 日志输出 $upstream_cache_status;Agent/协议/ClickHouse 贯通入库。 明细列表与详情按 HIT/MISS 等推导命中、回源、未缓存三态标签。
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package analytics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/db"
|
|
"github.com/Rain-kl/Wavelet/internal/db/idgen"
|
|
analyticsmodel "github.com/Rain-kl/Wavelet/internal/model/analytics"
|
|
)
|
|
|
|
// BatchInsertNodeAccessLogs writes node access logs to ClickHouse using the native batch API.
|
|
func BatchInsertNodeAccessLogs(ctx context.Context, logs []analyticsmodel.NodeAccessLog) error {
|
|
if len(logs) == 0 {
|
|
return nil
|
|
}
|
|
if db.ChConn == nil {
|
|
return fmt.Errorf("clickhouse connection is not initialized")
|
|
}
|
|
|
|
batch, err := db.ChConn.PrepareBatch(ctx, analyticsmodel.NodeAccessLog{}.BatchInsertSQL())
|
|
if err != nil {
|
|
return fmt.Errorf("prepare clickhouse batch: %w", err)
|
|
}
|
|
|
|
now := time.Now().UTC()
|
|
for _, logItem := range logs {
|
|
id := logItem.ID
|
|
if id == 0 {
|
|
id = idgen.NextUint64ID()
|
|
}
|
|
createdAt := logItem.CreatedAt
|
|
if createdAt.IsZero() {
|
|
createdAt = now
|
|
}
|
|
if err := batch.Append(
|
|
id,
|
|
logItem.NodeID,
|
|
logItem.LoggedAt.UTC(),
|
|
strings.TrimSpace(logItem.RemoteAddr),
|
|
logItem.Region,
|
|
logItem.Host,
|
|
logItem.Path,
|
|
strings.TrimSpace(logItem.UserAgent),
|
|
strings.TrimSpace(logItem.CacheStatus),
|
|
logItem.StatusCode,
|
|
logItem.BytesSent,
|
|
logItem.RequestLength,
|
|
logItem.RequestTimeMs,
|
|
createdAt.UTC(),
|
|
); err != nil {
|
|
return fmt.Errorf("append node access log to batch: %w", err)
|
|
}
|
|
}
|
|
|
|
if err := batch.Send(); err != nil {
|
|
return fmt.Errorf("send clickhouse batch: %w", err)
|
|
}
|
|
return nil
|
|
}
|