From 807d5a71f43ed808f90d772c58d5547251501262 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Wed, 4 Sep 2024 13:14:34 -0400 Subject: [PATCH] Integrate Collector into logging module --- logging/collector.go | 6 +++--- logging/collector_test.go | 10 +++++----- logging/logging.go | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/logging/collector.go b/logging/collector.go index 5c53386..5e535c0 100644 --- a/logging/collector.go +++ b/logging/collector.go @@ -12,7 +12,7 @@ import ( // Collector holds a fixed-size list of records type Collector struct { mu sync.Mutex - records []LogRecord + records []HTTPRecord maxSize int endpoint string httpClient *http.Client @@ -23,7 +23,7 @@ type Collector struct { func NewCollector(maxSize int, endpoint, login, password string) *Collector { auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", login, password))) return &Collector{ - records: make([]LogRecord, 0, maxSize), + records: make([]HTTPRecord, 0, maxSize), maxSize: maxSize, endpoint: endpoint, httpClient: &http.Client{}, @@ -32,7 +32,7 @@ func NewCollector(maxSize int, endpoint, login, password string) *Collector { } // CollectAndSend collects a new record. If the list reaches the maxSize, it sends the records to the configured endpoint and resets the list. -func (c *Collector) CollectAndSend(record LogRecord) error { +func (c *Collector) CollectAndSend(record HTTPRecord) error { c.mu.Lock() defer c.mu.Unlock() diff --git a/logging/collector_test.go b/logging/collector_test.go index 49c0643..dfc5267 100644 --- a/logging/collector_test.go +++ b/logging/collector_test.go @@ -30,11 +30,11 @@ func TestCollector(t *testing.T) { collector := NewCollector(3, server.URL, "user", "pass") // Create some test records - records := []LogRecord{ - {Method: "GET", API: "API1", Status: 200}, - {Method: "PUT", API: "API2", Status: 200}, - {Method: "POST", API: "API3", Status: 200}, - {Method: "DELETE", API: "API4", Status: 200}, + records := []HTTPRecord{ + {Data: LogRecord{Method: "GET", API: "API1", Status: 200}}, + {Data: LogRecord{Method: "PUT", API: "API2", Status: 200}}, + {Data: LogRecord{Method: "POST", API: "API3", Status: 200}}, + {Data: LogRecord{Method: "DELETE", API: "API4", Status: 200}}, } // Collect and send the records diff --git a/logging/logging.go b/logging/logging.go index 86fae63..9f066f8 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -33,6 +33,21 @@ var CMSMonitType string // CMSMonitProducer controls CMS Monit producer name var CMSMonitProducer string +// CollectorURL +var CollectorURL string + +// CollectorSize +var CollectorSize int + +// CollectorLogin +var CollectorLogin string + +// CollectorPassword +var CollectorPassword string + +// LogCollector pointer +var LogCollector *Collector + // HTTPRecord provides http record we send to logs endpoint type HTTPRecord struct { Producer string `json:"producer"` // name of the producer @@ -218,6 +233,15 @@ func LogRequest(w http.ResponseWriter, r *http.Request, start time.Time, cauth s defer logger.Sync() // flushes buffer, if any zapLog := logger.Sugar() // get sugar logger (JSON one) + // initialize log collector + if CollectorURL != "" && CollectorLogin != "" && CollectorPassword != "" && LogCollector == nil { + maxSize := CollectorSize + if maxSize == 0 { + maxSize = 1000 + } + LogCollector = NewCollector(maxSize, CollectorURL, CollectorLogin, CollectorPassword) + } + // our apache configuration // CustomLog "||@APACHE2_ROOT@/bin/rotatelogs -f @LOGDIR@/access_log_%Y%m%d.txt 86400" \ // "%t %v [client: %a] [backend: %h] \"%r\" %>s [data: %I in %O out %b body %D us ] [auth: %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%{SSL_CLIENT_S_DN}x\" \"%{cms-auth}C\" ] [ref: \"%{Referer}i\" \"%{User-Agent}i\" ]" @@ -355,11 +379,15 @@ func LogRequest(w http.ResponseWriter, r *http.Request, start time.Time, cauth s Host: hostname, Data: rec, } - data, err := json.Marshal(hr) - if err == nil { - fmt.Println(string(data)) + if LogCollector != nil { + err = LogCollector.CollectAndSend(hr) } else { - log.Println("unable to produce record for MONIT, error", err) + data, err := json.Marshal(hr) + if err == nil { + fmt.Println(string(data)) + } else { + log.Println("unable to produce record for MONIT, error", err) + } } }