From edb356f40e95e86e46294b2a2e402e258fa7389b Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 31 May 2026 20:09:40 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openflare_agent/internal/config/config.go | 60 ++++++------------- openflare_agent/internal/httpclient/client.go | 8 ++- .../internal/observability/collector.go | 2 +- .../internal/observability/openresty_local.go | 6 +- .../internal/observability/traffic.go | 20 ++++--- .../internal/observability/traffic_test.go | 2 +- 6 files changed, 42 insertions(+), 56 deletions(-) diff --git a/openflare_agent/internal/config/config.go b/openflare_agent/internal/config/config.go index b1e9d6bb..a5a0d399 100644 --- a/openflare_agent/internal/config/config.go +++ b/openflare_agent/internal/config/config.go @@ -221,41 +221,24 @@ func normalizeManagedPaths(cfg *Config) { if cfg == nil { return } - if usesSlashPath(cfg.DataDir) { - cfg.DataDir = filepath.ToSlash(cfg.DataDir) + paths := []*string{ + &cfg.DataDir, + &cfg.MainConfigPath, + &cfg.RouteConfigPath, + &cfg.AccessLogPath, + &cfg.CertDir, + &cfg.OpenrestyCertDir, + &cfg.LuaDir, + &cfg.OpenrestyLuaDir, + &cfg.RuntimeConfigDir, + &cfg.StatePath, + &cfg.ObservabilityBufferPath, + &cfg.MMDBPath, } - if usesSlashPath(cfg.MainConfigPath) { - cfg.MainConfigPath = filepath.ToSlash(cfg.MainConfigPath) - } - if usesSlashPath(cfg.RouteConfigPath) { - cfg.RouteConfigPath = filepath.ToSlash(cfg.RouteConfigPath) - } - if usesSlashPath(cfg.AccessLogPath) { - cfg.AccessLogPath = filepath.ToSlash(cfg.AccessLogPath) - } - if usesSlashPath(cfg.CertDir) { - cfg.CertDir = filepath.ToSlash(cfg.CertDir) - } - if usesSlashPath(cfg.OpenrestyCertDir) { - cfg.OpenrestyCertDir = filepath.ToSlash(cfg.OpenrestyCertDir) - } - if usesSlashPath(cfg.LuaDir) { - cfg.LuaDir = filepath.ToSlash(cfg.LuaDir) - } - if usesSlashPath(cfg.OpenrestyLuaDir) { - cfg.OpenrestyLuaDir = filepath.ToSlash(cfg.OpenrestyLuaDir) - } - if usesSlashPath(cfg.RuntimeConfigDir) { - cfg.RuntimeConfigDir = filepath.ToSlash(cfg.RuntimeConfigDir) - } - if usesSlashPath(cfg.StatePath) { - cfg.StatePath = filepath.ToSlash(cfg.StatePath) - } - if usesSlashPath(cfg.ObservabilityBufferPath) { - cfg.ObservabilityBufferPath = filepath.ToSlash(cfg.ObservabilityBufferPath) - } - if usesSlashPath(cfg.MMDBPath) { - cfg.MMDBPath = filepath.ToSlash(cfg.MMDBPath) + for _, p := range paths { + if usesSlashPath(*p) { + *p = filepath.ToSlash(*p) + } } } @@ -406,15 +389,6 @@ func detectHostname() string { return strings.TrimSpace(host) } -func firstNonEmpty(values ...string) string { - for _, value := range values { - if strings.TrimSpace(value) != "" { - return value - } - } - return "" -} - func detectNodeIP() string { if ip := detectOutboundNodeIP(); ip != "" { return ip diff --git a/openflare_agent/internal/httpclient/client.go b/openflare_agent/internal/httpclient/client.go index 22d1fe1c..3f0514fc 100644 --- a/openflare_agent/internal/httpclient/client.go +++ b/openflare_agent/internal/httpclient/client.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "io" "log/slog" "net/http" "strings" @@ -107,7 +108,12 @@ func (c *Client) do(req *http.Request, target any) error { slog.Error("http request failed", "method", req.Method, "path", req.URL.Path, "error", err) return err } - defer res.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + slog.Error("failed to close response body", "error", err) + } + }(res.Body) if res.StatusCode != http.StatusOK { slog.Warn("http request returned non-200", "method", req.Method, "path", req.URL.Path, "status", res.Status) return errors.New(res.Status) diff --git a/openflare_agent/internal/observability/collector.go b/openflare_agent/internal/observability/collector.go index 1767bc48..a5f3fdad 100644 --- a/openflare_agent/internal/observability/collector.go +++ b/openflare_agent/internal/observability/collector.go @@ -40,7 +40,7 @@ func BuildProfile(cfg *config.Config, stateStore *state.Store) *protocol.NodeSys return profile } -func BuildSnapshot(cfg *config.Config, stateStore *state.Store, managed *managedOpenRestyMetrics) *protocol.NodeMetricSnapshot { +func BuildSnapshot(cfg *config.Config, stateStore *state.Store, managed *ManagedOpenRestyMetrics) *protocol.NodeMetricSnapshot { now := time.Now().UTC() metric := &protocol.NodeMetricSnapshot{ CapturedAtUnix: now.Unix(), diff --git a/openflare_agent/internal/observability/openresty_local.go b/openflare_agent/internal/observability/openresty_local.go index cce5da57..876f7c48 100644 --- a/openflare_agent/internal/observability/openresty_local.go +++ b/openflare_agent/internal/observability/openresty_local.go @@ -18,7 +18,7 @@ const openRestyStubStatusPath = "/openflare/stub_status" var stubStatusActivePattern = regexp.MustCompile(`Active connections:\s+(\d+)`) -type managedOpenRestyMetrics struct { +type ManagedOpenRestyMetrics struct { TrafficReport *protocol.NodeTrafficReport OpenrestyRxBytes int64 OpenrestyTxBytes int64 @@ -38,7 +38,7 @@ type openRestyObservabilityResponse struct { OpenrestyTxBytes int64 `json:"openresty_tx_bytes"` } -func CollectManagedOpenRestyMetrics(cfg *config.Config) *managedOpenRestyMetrics { +func CollectManagedOpenRestyMetrics(cfg *config.Config) *ManagedOpenRestyMetrics { if cfg == nil || cfg.OpenrestyObservabilityPort <= 0 { return nil } @@ -51,7 +51,7 @@ func CollectManagedOpenRestyMetrics(cfg *config.Config) *managedOpenRestyMetrics return nil } - result := &managedOpenRestyMetrics{ + result := &ManagedOpenRestyMetrics{ TrafficReport: &protocol.NodeTrafficReport{ WindowStartedAtUnix: observabilityResp.WindowStartedAtUnix, WindowEndedAtUnix: observabilityResp.WindowEndedAtUnix, diff --git a/openflare_agent/internal/observability/traffic.go b/openflare_agent/internal/observability/traffic.go index 1f3be468..16b50ba5 100644 --- a/openflare_agent/internal/observability/traffic.go +++ b/openflare_agent/internal/observability/traffic.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "io" + "log/slog" "openflare-agent/internal/config" "openflare-agent/internal/protocol" "openflare-agent/internal/state" @@ -26,7 +27,7 @@ type accessLogRecord struct { RequestLength int64 `json:"request_length"` } -var combinedAccessLogPattern = regexp.MustCompile(`^(\S+)\s+\S+\s+\S+\s+\[([^\]]+)\]\s+"(?:\S+)\s+(\S+)(?:\s+[^"]*)?"\s+(\d{3})\s+\S+`) +var combinedAccessLogPattern = regexp.MustCompile(`^(\S+)\s+\S+\s+\S+\s+\[([^]]+)]\s+"\S+\s+(\S+)(?:\s+[^"]*)?"\s+(\d{3})\s+\S+`) type trafficAggregate struct { windowStartedAt time.Time @@ -41,12 +42,12 @@ type trafficAggregate struct { logs []protocol.NodeAccessLog } -func BuildTrafficReport(cfg *config.Config, stateStore *state.Store, managed *managedOpenRestyMetrics) *protocol.NodeTrafficReport { +func BuildTrafficReport(cfg *config.Config, stateStore *state.Store, managed *ManagedOpenRestyMetrics) *protocol.NodeTrafficReport { report, _, _ := BuildTrafficObservability(cfg, stateStore, managed) return report } -func BuildTrafficObservability(cfg *config.Config, stateStore *state.Store, managed *managedOpenRestyMetrics) (*protocol.NodeTrafficReport, []protocol.NodeAccessLog, *managedOpenRestyMetrics) { +func BuildTrafficObservability(cfg *config.Config, stateStore *state.Store, managed *ManagedOpenRestyMetrics) (*protocol.NodeTrafficReport, []protocol.NodeAccessLog, *ManagedOpenRestyMetrics) { if cfg == nil || stateStore == nil { if managed != nil && managed.TrafficReport != nil { return managed.TrafficReport, nil, managed @@ -55,7 +56,7 @@ func BuildTrafficObservability(cfg *config.Config, stateStore *state.Store, mana } aggregate := readAccessLogDelta(cfg, stateStore) - accessLogs := []protocol.NodeAccessLog{} + var accessLogs []protocol.NodeAccessLog if aggregate != nil { accessLogs = aggregate.accessLogs() } @@ -87,7 +88,12 @@ func readAccessLogDelta(cfg *config.Config, stateStore *state.Store) *trafficAgg } return nil } - defer file.Close() + defer func(file *os.File) { + err := file.Close() + if err != nil { + slog.Error("failed to close access log file", "error", err) + } + }(file) info, err := file.Stat() if err != nil { @@ -270,7 +276,7 @@ func (aggregate *trafficAggregate) accessLogs() []protocol.NodeAccessLog { return append([]protocol.NodeAccessLog(nil), aggregate.logs...) } -func (aggregate *trafficAggregate) managedMetrics() *managedOpenRestyMetrics { +func (aggregate *trafficAggregate) managedMetrics() *ManagedOpenRestyMetrics { if aggregate == nil { return nil } @@ -278,7 +284,7 @@ func (aggregate *trafficAggregate) managedMetrics() *managedOpenRestyMetrics { if report == nil && aggregate.openrestyRxBytes <= 0 && aggregate.openrestyTxBytes <= 0 { return nil } - return &managedOpenRestyMetrics{ + return &ManagedOpenRestyMetrics{ TrafficReport: report, OpenrestyRxBytes: aggregate.openrestyRxBytes, OpenrestyTxBytes: aggregate.openrestyTxBytes, diff --git a/openflare_agent/internal/observability/traffic_test.go b/openflare_agent/internal/observability/traffic_test.go index 33f47861..5c19e216 100644 --- a/openflare_agent/internal/observability/traffic_test.go +++ b/openflare_agent/internal/observability/traffic_test.go @@ -167,7 +167,7 @@ func TestBuildTrafficReportParsesCombinedAccessLog(t *testing.T) { } func TestBuildTrafficReportReturnsManagedWindowEvenWhenRequestCountZero(t *testing.T) { - report := BuildTrafficReport(nil, nil, &managedOpenRestyMetrics{ + report := BuildTrafficReport(nil, nil, &ManagedOpenRestyMetrics{ TrafficReport: &protocol.NodeTrafficReport{ WindowStartedAtUnix: 1710403200, WindowEndedAtUnix: 1710403260,