The very basic metricator-client and package.
Package can be used in external things if needed.
This commit is contained in:
@@ -3,12 +3,12 @@ package application
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/models"
|
||||
"go.dev.pztrn.name/metricator/pkg/schema"
|
||||
)
|
||||
|
||||
// Parses passed body and returns a map suitable for pushing into storage.
|
||||
func (a *Application) parse(body string) map[string]models.Metric {
|
||||
data := make(map[string]models.Metric)
|
||||
func (a *Application) parse(body string) map[string]schema.Metric {
|
||||
data := make(map[string]schema.Metric)
|
||||
|
||||
// ToDo: switch to bytes buffer and maybe do not read body in caller?
|
||||
splittedBody := strings.Split(body, "\n")
|
||||
@@ -35,7 +35,7 @@ func (a *Application) parse(body string) map[string]models.Metric {
|
||||
if !found {
|
||||
a.logger.Debugln("Metric wasn't yet created, creating new structure")
|
||||
|
||||
metric = models.NewMetric(name, "", "", nil)
|
||||
metric = schema.NewMetric(name, "", "", nil)
|
||||
}
|
||||
|
||||
a.logger.Debugf("Got metric to use: %+v\n", metric)
|
||||
|
@@ -1,41 +0,0 @@
|
||||
package models
|
||||
|
||||
// Metric is a generic metric structure.
|
||||
type Metric struct {
|
||||
// BaseName is a metric's base name, used for constructing name.
|
||||
BaseName string
|
||||
// Name is a metric name.
|
||||
Name string
|
||||
// Description is a metric description from HELP line.
|
||||
Description string
|
||||
// Type is a metric type from TYPE line.
|
||||
Type string
|
||||
// Value is a metric value.
|
||||
Value string
|
||||
// Params is an additional params which are placed inside "{}".
|
||||
Params []string
|
||||
}
|
||||
|
||||
// NewMetric creates new structure for storing single metric data.
|
||||
func NewMetric(name, mType, description string, params []string) Metric {
|
||||
m := Metric{
|
||||
BaseName: name,
|
||||
Name: name,
|
||||
Description: description,
|
||||
Type: mType,
|
||||
Params: params,
|
||||
Value: "",
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// GetValue returns metric's value.
|
||||
func (m *Metric) GetValue() string {
|
||||
return m.Value
|
||||
}
|
||||
|
||||
// SetValue sets value for metric.
|
||||
func (m *Metric) SetValue(value string) {
|
||||
m.Value = value
|
||||
}
|
@@ -1,19 +1,19 @@
|
||||
package storage
|
||||
|
||||
import "go.dev.pztrn.name/metricator/internal/models"
|
||||
import "go.dev.pztrn.name/metricator/pkg/schema"
|
||||
|
||||
// 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) (models.Metric, error)
|
||||
Get(string) (schema.Metric, error)
|
||||
// GetAsSlice returns all data from storage as slice.
|
||||
GetAsSlice() []models.Metric
|
||||
GetAsSlice() []schema.Metric
|
||||
// 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]models.Metric)
|
||||
Put(map[string]schema.Metric)
|
||||
// Start starts asynchronous things if needed.
|
||||
Start()
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/logger"
|
||||
"go.dev.pztrn.name/metricator/internal/models"
|
||||
"go.dev.pztrn.name/metricator/pkg/schema"
|
||||
)
|
||||
|
||||
// ErrMetricNotFound appears if requested metric wasn't found in storage.
|
||||
@@ -17,7 +17,7 @@ type Storage struct {
|
||||
ctx context.Context
|
||||
doneChan chan struct{}
|
||||
logger *logger.Logger
|
||||
data map[string]models.Metric
|
||||
data map[string]schema.Metric
|
||||
name string
|
||||
dataMutex sync.RWMutex
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func NewStorage(ctx context.Context, name string, logger *logger.Logger) (*Stora
|
||||
}
|
||||
|
||||
// Get returns data from storage by key.
|
||||
func (s *Storage) Get(key string) (models.Metric, error) {
|
||||
func (s *Storage) Get(key string) (schema.Metric, error) {
|
||||
s.logger.Debugln("Retrieving data for", key, "key from storage...")
|
||||
|
||||
s.dataMutex.RLock()
|
||||
@@ -46,7 +46,7 @@ func (s *Storage) Get(key string) (models.Metric, error) {
|
||||
if !found {
|
||||
s.logger.Infoln("Key", key, "not found in storage!")
|
||||
|
||||
return models.NewMetric("", "", "", nil), ErrMetricNotFound
|
||||
return schema.NewMetric("", "", "", nil), ErrMetricNotFound
|
||||
}
|
||||
|
||||
s.logger.Debugf("Key %s found: %+v\n", key, data)
|
||||
@@ -55,10 +55,10 @@ func (s *Storage) Get(key string) (models.Metric, error) {
|
||||
}
|
||||
|
||||
// GetAsSlice returns all data from storage as slice.
|
||||
func (s *Storage) GetAsSlice() []models.Metric {
|
||||
func (s *Storage) GetAsSlice() []schema.Metric {
|
||||
s.logger.Debugln("Returning all stored metrics as slice...")
|
||||
|
||||
metrics := make([]models.Metric, 0, len(s.data))
|
||||
metrics := make([]schema.Metric, 0, len(s.data))
|
||||
|
||||
for _, metric := range s.data {
|
||||
metrics = append(metrics, metric)
|
||||
@@ -75,11 +75,11 @@ func (s *Storage) GetDoneChan() chan struct{} {
|
||||
|
||||
// Initializes internal things.
|
||||
func (s *Storage) initialize() {
|
||||
s.data = make(map[string]models.Metric)
|
||||
s.data = make(map[string]schema.Metric)
|
||||
}
|
||||
|
||||
// Put puts passed data into storage.
|
||||
func (s *Storage) Put(data map[string]models.Metric) {
|
||||
func (s *Storage) Put(data map[string]schema.Metric) {
|
||||
s.dataMutex.Lock()
|
||||
defer s.dataMutex.Unlock()
|
||||
|
||||
|
Reference in New Issue
Block a user