Add simple debug logging.
This commit is contained in:
@@ -2,11 +2,11 @@ package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/common"
|
||||
"go.dev.pztrn.name/metricator/internal/logger"
|
||||
"go.dev.pztrn.name/metricator/internal/storage"
|
||||
"go.dev.pztrn.name/metricator/internal/storage/memory"
|
||||
)
|
||||
@@ -17,6 +17,7 @@ type Application struct {
|
||||
config *Config
|
||||
ctx context.Context
|
||||
doneChan chan struct{}
|
||||
logger *logger.Logger
|
||||
name string
|
||||
|
||||
storage storage.Metrics
|
||||
@@ -29,11 +30,12 @@ type Application struct {
|
||||
}
|
||||
|
||||
// NewApplication creates new application.
|
||||
func NewApplication(ctx context.Context, name string, config *Config) *Application {
|
||||
func NewApplication(ctx context.Context, name string, config *Config, logger *logger.Logger) *Application {
|
||||
a := &Application{
|
||||
config: config,
|
||||
ctx: ctx,
|
||||
doneChan: make(chan struct{}),
|
||||
logger: logger,
|
||||
name: name,
|
||||
}
|
||||
a.initialize()
|
||||
@@ -54,9 +56,9 @@ func (a *Application) GetHandler() common.HTTPHandlerFunc {
|
||||
|
||||
// Initializes internal things like storage, HTTP client, etc.
|
||||
func (a *Application) initialize() {
|
||||
a.storage, a.storageDone = memory.NewStorage(a.ctx, a.name+" storage")
|
||||
a.storage, a.storageDone = memory.NewStorage(a.ctx, a.name+" storage", a.logger)
|
||||
|
||||
log.Printf("Application '%s' initialized with configuration: %+v\n", a.name, a.config)
|
||||
a.logger.Debugf("Application '%s' initialized with configuration: %+v\n", a.name, a.config)
|
||||
}
|
||||
|
||||
// Start starts asynchronous things like data fetching, storage cleanup, etc.
|
||||
@@ -71,7 +73,7 @@ func (a *Application) Start() {
|
||||
// We should wait until storage routines are also stopped.
|
||||
<-a.storage.GetDoneChan()
|
||||
|
||||
log.Println("Application", a.name, "stopped")
|
||||
a.logger.Infoln("Application", a.name, "stopped")
|
||||
|
||||
a.doneChan <- struct{}{}
|
||||
}()
|
||||
|
@@ -2,7 +2,6 @@ package application
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
@@ -23,11 +22,11 @@ func (a *Application) fetch() {
|
||||
a.fetchIsRunning = true
|
||||
a.fetchIsRunningMutex.Unlock()
|
||||
|
||||
log.Println("Fetching data for", a.name)
|
||||
a.logger.Infoln("Fetching data for", a.name)
|
||||
|
||||
req, err := http.NewRequestWithContext(a.ctx, "GET", a.config.Endpoint, nil)
|
||||
if err != nil {
|
||||
log.Println("Failed to create request for", a.name, "metrics:", err.Error())
|
||||
a.logger.Infoln("Failed to create request for", a.name, "metrics:", err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
@@ -38,7 +37,7 @@ func (a *Application) fetch() {
|
||||
|
||||
resp, err := a.httpClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("Failed to execute request for", a.name, "metrics:", err.Error())
|
||||
a.logger.Infoln("Failed to execute request for", a.name, "metrics:", err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
@@ -47,7 +46,7 @@ func (a *Application) fetch() {
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Println("Failed to read response body for", a.name, "metrics:", err.Error())
|
||||
a.logger.Infoln("Failed to read response body for", a.name, "metrics:", err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
@@ -70,7 +69,7 @@ func (a *Application) startFetcher() {
|
||||
Timeout: time.Second * 5,
|
||||
}
|
||||
|
||||
defer log.Println("Fetcher for", a.name, "completed")
|
||||
defer a.logger.Debugln("Fetcher for", a.name, "completed")
|
||||
|
||||
// First fetch should be executed ASAP.
|
||||
a.fetch()
|
||||
|
@@ -26,7 +26,7 @@ func (a *Application) parse(body string) map[string]models.Metric {
|
||||
continue
|
||||
}
|
||||
|
||||
// log.Println("Analyzing line:", line)
|
||||
a.logger.Debugln("Analyzing line:", line)
|
||||
|
||||
name = a.getMetricName(line)
|
||||
metric, found := data[name]
|
||||
@@ -76,12 +76,12 @@ func (a *Application) parse(body string) map[string]models.Metric {
|
||||
|
||||
metric.Value = a.getMetricValue(line)
|
||||
|
||||
// log.Printf("Got metric: %+v\n", metric)
|
||||
a.logger.Debugln("Got metric: %+v\n", metric)
|
||||
|
||||
data[name] = metric
|
||||
}
|
||||
|
||||
// log.Printf("Data parsed: %+v\n", data)
|
||||
a.logger.Debugln("Data parsed: %+v\n", data)
|
||||
|
||||
return data
|
||||
}
|
||||
|
Reference in New Issue
Block a user