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,15 +1,17 @@
package storage
import "go.dev.pztrn.name/metricator/internal/models"
// GenericStorage describes interface every other storage should embed
// and conform to as it contains essential things like context handling.
type GenericStorage interface {
// Get returns data from storage by key.
Get(string) string
Get(string) (models.Metric, error)
// GetDoneChan returns a channel which should be used to block execution
// until storage's routines are completed.
GetDoneChan() chan struct{}
// Put puts passed data into storage.
Put(map[string]string)
Put(map[string]models.Metric)
// Start starts asynchronous things if needed.
Start()
}

View File

@@ -2,17 +2,22 @@ package memory
import (
"context"
"errors"
"log"
"sync"
"go.dev.pztrn.name/metricator/internal/models"
)
var ErrMetricNotFound = errors.New("metric not found")
// Storage is an in-memory storage.
type Storage struct {
ctx context.Context
doneChan chan struct{}
name string
data map[string]string
data map[string]models.Metric
dataMutex sync.RWMutex
}
@@ -29,16 +34,16 @@ func NewStorage(ctx context.Context, name string) (*Storage, chan struct{}) {
}
// Get returns data from storage by key.
func (s *Storage) Get(key string) string {
func (s *Storage) Get(key string) (models.Metric, error) {
s.dataMutex.RLock()
defer s.dataMutex.RUnlock()
data, found := s.data[key]
if !found {
return "Not found"
return models.NewMetric("", "", nil), ErrMetricNotFound
}
return data
return data, nil
}
// GetDoneChan returns a channel which should be used to block execution
@@ -49,11 +54,11 @@ func (s *Storage) GetDoneChan() chan struct{} {
// Initializes internal things.
func (s *Storage) initialize() {
s.data = make(map[string]string)
s.data = make(map[string]models.Metric)
}
// Put puts passed data into storage.
func (s *Storage) Put(data map[string]string) {
func (s *Storage) Put(data map[string]models.Metric) {
s.dataMutex.Lock()
defer s.dataMutex.Unlock()