mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 07:26:36 +08:00
[优化] 代码优化
This commit is contained in:
@@ -221,41 +221,24 @@ func normalizeManagedPaths(cfg *Config) {
|
|||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if usesSlashPath(cfg.DataDir) {
|
paths := []*string{
|
||||||
cfg.DataDir = filepath.ToSlash(cfg.DataDir)
|
&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) {
|
for _, p := range paths {
|
||||||
cfg.MainConfigPath = filepath.ToSlash(cfg.MainConfigPath)
|
if usesSlashPath(*p) {
|
||||||
}
|
*p = filepath.ToSlash(*p)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,15 +389,6 @@ func detectHostname() string {
|
|||||||
return strings.TrimSpace(host)
|
return strings.TrimSpace(host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func firstNonEmpty(values ...string) string {
|
|
||||||
for _, value := range values {
|
|
||||||
if strings.TrimSpace(value) != "" {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func detectNodeIP() string {
|
func detectNodeIP() string {
|
||||||
if ip := detectOutboundNodeIP(); ip != "" {
|
if ip := detectOutboundNodeIP(); ip != "" {
|
||||||
return ip
|
return ip
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"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)
|
slog.Error("http request failed", "method", req.Method, "path", req.URL.Path, "error", err)
|
||||||
return 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 {
|
if res.StatusCode != http.StatusOK {
|
||||||
slog.Warn("http request returned non-200", "method", req.Method, "path", req.URL.Path, "status", res.Status)
|
slog.Warn("http request returned non-200", "method", req.Method, "path", req.URL.Path, "status", res.Status)
|
||||||
return errors.New(res.Status)
|
return errors.New(res.Status)
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func BuildProfile(cfg *config.Config, stateStore *state.Store) *protocol.NodeSys
|
|||||||
return profile
|
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()
|
now := time.Now().UTC()
|
||||||
metric := &protocol.NodeMetricSnapshot{
|
metric := &protocol.NodeMetricSnapshot{
|
||||||
CapturedAtUnix: now.Unix(),
|
CapturedAtUnix: now.Unix(),
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ const openRestyStubStatusPath = "/openflare/stub_status"
|
|||||||
|
|
||||||
var stubStatusActivePattern = regexp.MustCompile(`Active connections:\s+(\d+)`)
|
var stubStatusActivePattern = regexp.MustCompile(`Active connections:\s+(\d+)`)
|
||||||
|
|
||||||
type managedOpenRestyMetrics struct {
|
type ManagedOpenRestyMetrics struct {
|
||||||
TrafficReport *protocol.NodeTrafficReport
|
TrafficReport *protocol.NodeTrafficReport
|
||||||
OpenrestyRxBytes int64
|
OpenrestyRxBytes int64
|
||||||
OpenrestyTxBytes int64
|
OpenrestyTxBytes int64
|
||||||
@@ -38,7 +38,7 @@ type openRestyObservabilityResponse struct {
|
|||||||
OpenrestyTxBytes int64 `json:"openresty_tx_bytes"`
|
OpenrestyTxBytes int64 `json:"openresty_tx_bytes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CollectManagedOpenRestyMetrics(cfg *config.Config) *managedOpenRestyMetrics {
|
func CollectManagedOpenRestyMetrics(cfg *config.Config) *ManagedOpenRestyMetrics {
|
||||||
if cfg == nil || cfg.OpenrestyObservabilityPort <= 0 {
|
if cfg == nil || cfg.OpenrestyObservabilityPort <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ func CollectManagedOpenRestyMetrics(cfg *config.Config) *managedOpenRestyMetrics
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result := &managedOpenRestyMetrics{
|
result := &ManagedOpenRestyMetrics{
|
||||||
TrafficReport: &protocol.NodeTrafficReport{
|
TrafficReport: &protocol.NodeTrafficReport{
|
||||||
WindowStartedAtUnix: observabilityResp.WindowStartedAtUnix,
|
WindowStartedAtUnix: observabilityResp.WindowStartedAtUnix,
|
||||||
WindowEndedAtUnix: observabilityResp.WindowEndedAtUnix,
|
WindowEndedAtUnix: observabilityResp.WindowEndedAtUnix,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"openflare-agent/internal/config"
|
"openflare-agent/internal/config"
|
||||||
"openflare-agent/internal/protocol"
|
"openflare-agent/internal/protocol"
|
||||||
"openflare-agent/internal/state"
|
"openflare-agent/internal/state"
|
||||||
@@ -26,7 +27,7 @@ type accessLogRecord struct {
|
|||||||
RequestLength int64 `json:"request_length"`
|
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 {
|
type trafficAggregate struct {
|
||||||
windowStartedAt time.Time
|
windowStartedAt time.Time
|
||||||
@@ -41,12 +42,12 @@ type trafficAggregate struct {
|
|||||||
logs []protocol.NodeAccessLog
|
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)
|
report, _, _ := BuildTrafficObservability(cfg, stateStore, managed)
|
||||||
return report
|
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 cfg == nil || stateStore == nil {
|
||||||
if managed != nil && managed.TrafficReport != nil {
|
if managed != nil && managed.TrafficReport != nil {
|
||||||
return managed.TrafficReport, nil, managed
|
return managed.TrafficReport, nil, managed
|
||||||
@@ -55,7 +56,7 @@ func BuildTrafficObservability(cfg *config.Config, stateStore *state.Store, mana
|
|||||||
}
|
}
|
||||||
|
|
||||||
aggregate := readAccessLogDelta(cfg, stateStore)
|
aggregate := readAccessLogDelta(cfg, stateStore)
|
||||||
accessLogs := []protocol.NodeAccessLog{}
|
var accessLogs []protocol.NodeAccessLog
|
||||||
if aggregate != nil {
|
if aggregate != nil {
|
||||||
accessLogs = aggregate.accessLogs()
|
accessLogs = aggregate.accessLogs()
|
||||||
}
|
}
|
||||||
@@ -87,7 +88,12 @@ func readAccessLogDelta(cfg *config.Config, stateStore *state.Store) *trafficAgg
|
|||||||
}
|
}
|
||||||
return nil
|
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()
|
info, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -270,7 +276,7 @@ func (aggregate *trafficAggregate) accessLogs() []protocol.NodeAccessLog {
|
|||||||
return append([]protocol.NodeAccessLog(nil), aggregate.logs...)
|
return append([]protocol.NodeAccessLog(nil), aggregate.logs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aggregate *trafficAggregate) managedMetrics() *managedOpenRestyMetrics {
|
func (aggregate *trafficAggregate) managedMetrics() *ManagedOpenRestyMetrics {
|
||||||
if aggregate == nil {
|
if aggregate == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -278,7 +284,7 @@ func (aggregate *trafficAggregate) managedMetrics() *managedOpenRestyMetrics {
|
|||||||
if report == nil && aggregate.openrestyRxBytes <= 0 && aggregate.openrestyTxBytes <= 0 {
|
if report == nil && aggregate.openrestyRxBytes <= 0 && aggregate.openrestyTxBytes <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &managedOpenRestyMetrics{
|
return &ManagedOpenRestyMetrics{
|
||||||
TrafficReport: report,
|
TrafficReport: report,
|
||||||
OpenrestyRxBytes: aggregate.openrestyRxBytes,
|
OpenrestyRxBytes: aggregate.openrestyRxBytes,
|
||||||
OpenrestyTxBytes: aggregate.openrestyTxBytes,
|
OpenrestyTxBytes: aggregate.openrestyTxBytes,
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ func TestBuildTrafficReportParsesCombinedAccessLog(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildTrafficReportReturnsManagedWindowEvenWhenRequestCountZero(t *testing.T) {
|
func TestBuildTrafficReportReturnsManagedWindowEvenWhenRequestCountZero(t *testing.T) {
|
||||||
report := BuildTrafficReport(nil, nil, &managedOpenRestyMetrics{
|
report := BuildTrafficReport(nil, nil, &ManagedOpenRestyMetrics{
|
||||||
TrafficReport: &protocol.NodeTrafficReport{
|
TrafficReport: &protocol.NodeTrafficReport{
|
||||||
WindowStartedAtUnix: 1710403200,
|
WindowStartedAtUnix: 1710403200,
|
||||||
WindowEndedAtUnix: 1710403260,
|
WindowEndedAtUnix: 1710403260,
|
||||||
|
|||||||
Reference in New Issue
Block a user