Metrics data is stored as structure, HTTP requests logging, got rid of context.

Metrics data now stored as structures. This is a first step for autodiscovery
helping for NMSes.

HTTP requests now logged.

Got rid of context.Context for getting metric data in applications
because context.Context is useless here.
This commit is contained in:
2020-12-23 13:27:17 +05:00
parent de5b55da43
commit 529113a41a
10 changed files with 210 additions and 54 deletions

View File

@@ -1,13 +1,16 @@
package application
import (
"context"
"go.dev.pztrn.name/metricator/internal/common"
"go.dev.pztrn.name/metricator/internal/models"
)
func (a *Application) respond(ctx context.Context) string {
metricName := ctx.Value(common.ContextKeyMetric).(string)
// Responds with needed data. First parameter is a type of data needed (like metric name),
// second parameter is actual metric name. Second parameter also can be empty.
func (a *Application) respond(rInfo *models.RequestInfo) string {
metric, err := a.storage.Get(rInfo.Metric)
if err != nil {
return ""
}
return a.storage.Get(metricName)
return metric.GetValue()
}

View File

@@ -3,11 +3,13 @@ package application
import (
"log"
"strings"
"go.dev.pztrn.name/metricator/internal/models"
)
// Parses passed body and returns a map suitable for pushing into storage.
func (a *Application) parse(body string) map[string]string {
data := make(map[string]string)
func (a *Application) parse(body string) map[string]models.Metric {
data := make(map[string]models.Metric)
// ToDo: switch to bytes buffer and maybe do not read body in caller?
splittedBody := strings.Split(body, "\n")
@@ -46,7 +48,10 @@ func (a *Application) parse(body string) map[string]string {
}
}
data[name] = value
metric := models.NewMetric(name, "", params)
metric.SetValue(value)
data[name] = metric
}
log.Printf("Data parsed: %+v\n", data)