Return list of parsed metrics, for autodiscovery.
This commit is contained in:
parent
529113a41a
commit
7e90814fde
@ -1,16 +1,33 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"go.dev.pztrn.name/metricator/internal/models"
|
"go.dev.pztrn.name/metricator/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Responds with needed data. First parameter is a type of data needed (like metric name),
|
// 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.
|
// second parameter is actual metric name. Second parameter also can be empty.
|
||||||
func (a *Application) respond(rInfo *models.RequestInfo) string {
|
func (a *Application) respond(rInfo *models.RequestInfo) string {
|
||||||
metric, err := a.storage.Get(rInfo.Metric)
|
// If metric was requested - return only it.
|
||||||
|
if rInfo.Metric != "" {
|
||||||
|
metric, err := a.storage.Get(rInfo.Metric)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return metric.GetValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise we should get all metrics as slice and return them as string.
|
||||||
|
// This is needed for metrics autodiscovery.
|
||||||
|
metrics := a.storage.GetAsSlice()
|
||||||
|
|
||||||
|
metricsBytes, err := json.Marshal(metrics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// ToDo: log error
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return metric.GetValue()
|
return string(metricsBytes)
|
||||||
}
|
}
|
||||||
|
@ -3,21 +3,21 @@ package models
|
|||||||
// Metric is a generic metric structure.
|
// Metric is a generic metric structure.
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
// Metric name.
|
// Metric name.
|
||||||
name string
|
Name string
|
||||||
// HELP data, if present.
|
// HELP data, if present.
|
||||||
description string
|
Description string
|
||||||
// Additional parameters, data inside "{}".
|
// Additional parameters, data inside "{}".
|
||||||
params []string
|
Params []string
|
||||||
// Metric value.
|
// Metric value.
|
||||||
value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMetric creates new structure for storing single metric data.
|
// NewMetric creates new structure for storing single metric data.
|
||||||
func NewMetric(name, description string, params []string) Metric {
|
func NewMetric(name, description string, params []string) Metric {
|
||||||
m := Metric{
|
m := Metric{
|
||||||
name: name,
|
Name: name,
|
||||||
description: description,
|
Description: description,
|
||||||
params: params,
|
Params: params,
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
@ -25,10 +25,10 @@ func NewMetric(name, description string, params []string) Metric {
|
|||||||
|
|
||||||
// GetValue returns metric's value.
|
// GetValue returns metric's value.
|
||||||
func (m *Metric) GetValue() string {
|
func (m *Metric) GetValue() string {
|
||||||
return m.value
|
return m.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue sets value for metric.
|
// SetValue sets value for metric.
|
||||||
func (m *Metric) SetValue(value string) {
|
func (m *Metric) SetValue(value string) {
|
||||||
m.value = value
|
m.Value = value
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ import "go.dev.pztrn.name/metricator/internal/models"
|
|||||||
type GenericStorage interface {
|
type GenericStorage interface {
|
||||||
// Get returns data from storage by key.
|
// Get returns data from storage by key.
|
||||||
Get(string) (models.Metric, error)
|
Get(string) (models.Metric, error)
|
||||||
|
// GetAsSlice returns all data from storage as slice.
|
||||||
|
GetAsSlice() []models.Metric
|
||||||
// GetDoneChan returns a channel which should be used to block execution
|
// GetDoneChan returns a channel which should be used to block execution
|
||||||
// until storage's routines are completed.
|
// until storage's routines are completed.
|
||||||
GetDoneChan() chan struct{}
|
GetDoneChan() chan struct{}
|
||||||
|
@ -46,6 +46,17 @@ func (s *Storage) Get(key string) (models.Metric, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAsSlice returns all data from storage as slice.
|
||||||
|
func (s *Storage) GetAsSlice() []models.Metric {
|
||||||
|
metrics := make([]models.Metric, 0, len(s.data))
|
||||||
|
|
||||||
|
for _, metric := range s.data {
|
||||||
|
metrics = append(metrics, metric)
|
||||||
|
}
|
||||||
|
|
||||||
|
return metrics
|
||||||
|
}
|
||||||
|
|
||||||
// GetDoneChan returns a channel which should be used to block execution
|
// GetDoneChan returns a channel which should be used to block execution
|
||||||
// until storage's routines are completed.
|
// until storage's routines are completed.
|
||||||
func (s *Storage) GetDoneChan() chan struct{} {
|
func (s *Storage) GetDoneChan() chan struct{} {
|
||||||
|
Loading…
Reference in New Issue
Block a user