Skip to content

Commit

Permalink
Add CollectorVerbose flag to provide verbose information about collec…
Browse files Browse the repository at this point in the history
…tor requests to MONIT
  • Loading branch information
vkuznet committed Sep 11, 2024
1 parent 72fabcb commit 6340465
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
21 changes: 21 additions & 0 deletions logging/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"sync"
)

Expand Down Expand Up @@ -40,6 +42,9 @@ func (c *Collector) CollectAndSend(record HTTPRecord) error {

if len(c.records) >= c.maxSize {
if err := c.Send(); err != nil {
if CollectorVerbose > 0 {
log.Println("ERROR: collector fails to send record to MONIT with", err)
}
return err
}
// Reset the list after sending
Expand All @@ -64,6 +69,14 @@ func (c *Collector) Send() error {
if err != nil {
return fmt.Errorf("failed to create HTTP request: %w", err)
}
if CollectorVerbose > 2 {
reqDump, err := httputil.DumpRequestOut(req, true)
if err == nil {
log.Println("collector request dump", string(reqDump))
} else {
log.Println("ERROR: fail to perform dump of HTTP request, error:", err)
}
}
// Set the content type and authorization headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", c.authHeader)
Expand All @@ -72,6 +85,14 @@ func (c *Collector) Send() error {
if err != nil {
return fmt.Errorf("failed to send HTTP request: %w", err)
}
if CollectorVerbose > 2 {
respDump, err := httputil.DumpResponse(resp, true)
if err == nil {
log.Println("collector response dump", string(respDump))
} else {
log.Println("ERROR: fail to perform dump of HTTP response, error:", err)
}
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
Expand Down
12 changes: 11 additions & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var CollectorLogin string
// CollectorPassword
var CollectorPassword string

// CollectorVerbose
var CollectorVerbose int

// LogCollector pointer
var LogCollector *Collector

Expand Down Expand Up @@ -381,12 +384,19 @@ func LogRequest(w http.ResponseWriter, r *http.Request, start time.Time, cauth s
}
if LogCollector != nil {
err = LogCollector.CollectAndSend(hr)
if err == nil {
if CollectorVerbose > 0 {
log.Println("collector successfully send", CollectorSize, "records to MONIT")
}
} else {
log.Println("ERROR: unable to send collector log records, error:", err)
}
} else {
data, err := json.Marshal(hr)
if err == nil {
fmt.Println(string(data))
} else {
log.Println("unable to produce record for MONIT, error", err)
log.Println("ERROR: unable to produce record for MONIT, error", err)
}
}
}
Expand Down

0 comments on commit 6340465

Please sign in to comment.