Skip to content

Commit

Permalink
Integrate Collector into logging module
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Sep 4, 2024
1 parent 0127258 commit 807d5a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
6 changes: 3 additions & 3 deletions logging/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{},
Expand All @@ -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()

Expand Down
10 changes: 5 additions & 5 deletions logging/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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\" ]"
Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit 807d5a7

Please sign in to comment.