Rethink how metricator will work, golangci-lint and gitlab ci configs.

This commit is contained in:
2020-11-29 03:22:39 +05:00
parent e679fecf6e
commit f915470c0b
20 changed files with 256 additions and 112 deletions

View File

@@ -0,0 +1,63 @@
package application
import (
"context"
"log"
"go.dev.pztrn.name/metricator/internal/storage"
"go.dev.pztrn.name/metricator/internal/storage/memory"
)
// Application is a thing that responsible for all application-related
// actions like data fetching, storing, etc. on higher level.
type Application struct {
config *Config
ctx context.Context
doneChan chan struct{}
name string
storage storage.Metrics
storageDone chan struct{}
}
// NewApplication creates new application.
func NewApplication(ctx context.Context, name string, config *Config) *Application {
a := &Application{
config: config,
ctx: ctx,
doneChan: make(chan struct{}),
name: name,
}
a.initialize()
return a
}
// GetDoneChan returns a channel which should be used to block execution until
// application's routines are completed.
func (a *Application) GetDoneChan() chan struct{} {
return a.doneChan
}
// Initializes internal things like storage, HTTP client, etc.
func (a *Application) initialize() {
a.storage, a.storageDone = memory.NewStorage(a.ctx, a.name+" storage")
log.Printf("Application '%s' initialized with configuration: %+v\n", a.name, a.config)
}
// Start starts asynchronous things like data fetching, storage cleanup, etc.
func (a *Application) Start() {
a.storage.Start()
// The Context Listening Goroutine.
go func() {
<-a.ctx.Done()
// We should wait until storage routines are also stopped.
<-a.storage.GetDoneChan()
log.Println("Application", a.name, "stopped")
a.doneChan <- struct{}{}
}()
}

View File

@@ -0,0 +1,15 @@
package application
import "time"
// Config is a generic application configuration.
type Config struct {
// Endpoint is a remote application endpoint which should give us metrics
// in Prometheus format.
Endpoint string
// Headers is a list of headers that should be added to metrics request.
Headers map[string]string
// TimeBetweenRequests is a minimal amount of time which should pass
// between requests.
TimeBetweenRequests time.Duration
}

View File

@@ -0,0 +1,4 @@
package application
// Configures and starts Prometheus data fetching goroutine.
func (a *Application) startFetcher() {}

View File

@@ -8,8 +8,8 @@ import (
"os"
"path/filepath"
"strings"
"time"
"go.dev.pztrn.name/metricator/internal/application"
"gopkg.in/yaml.v2"
)
@@ -23,23 +23,7 @@ type Config struct {
configPath string
// Applications describes configuration for remote application's endpoints.
// Key is an application's name.
Applications map[string]struct {
// Endpoint is a remote application endpoint which should give us metrics
// in Prometheus format.
Endpoint string
// Headers is a list of headers that should be added to metrics request.
Headers map[string]string
// TimeBetweenRequests is a minimal amount of time which should pass
// between requests.
TimeBetweenRequests time.Duration
}
// Datastore describes data storage configuration.
Datastore struct {
// ValidTimeout is a timeout for which every data entry will be considered
// as valid. After that timeout if value wasn't updated it will be considered
// as invalid and purged from memory.
ValidTimeout time.Duration `yaml:"valid_timeout"`
} `yaml:"datastore"`
Applications map[string]*application.Config `yaml:"applications"`
}
// NewConfig returns new configuration.

View File

@@ -1,25 +0,0 @@
package datastore
import "sync"
// This is application-specific data storage.
type applicationStorage struct {
metrics map[string]string
metricsMutex sync.RWMutex
}
// Creates new application-specific storage.
func newApplicationStorage() *applicationStorage {
as := &applicationStorage{}
as.initialize()
return as
}
// Initializes internal things.
func (as *applicationStorage) initialize() {
as.metrics = make(map[string]string)
}
// Starts application-specific things, like goroutine for HTTP requests.
func (as *applicationStorage) start() {}

View File

@@ -1,56 +0,0 @@
package datastore
import (
"context"
"log"
"sync"
"go.dev.pztrn.name/metricator/internal/configuration"
)
// DataStore is a data storage structure. It keeps all gathered metrics and gives
// them away on request.
type DataStore struct {
config *configuration.Config
ctx context.Context
doneChan chan struct{}
datas map[string]*applicationStorage
datasMutex sync.RWMutex
}
// NewDataStore creates new data storage.
func NewDataStore(ctx context.Context, cfg *configuration.Config) (*DataStore, chan struct{}) {
ds := &DataStore{
config: cfg,
ctx: ctx,
doneChan: make(chan struct{}),
}
ds.initialize()
return ds, ds.doneChan
}
// Internal things initialization.
func (ds *DataStore) initialize() {
ds.datas = make(map[string]*applicationStorage)
// Create applications defined in configuration.
go func() {
<-ds.ctx.Done()
log.Println("Data storage stopped")
ds.doneChan <- struct{}{}
}()
}
// Start starts data storage asynchronous things.
func (ds *DataStore) Start() {
log.Println("Starting data storage...")
ds.datasMutex.RLock()
for _, storage := range ds.datas {
storage.start()
}
}

View File

@@ -40,6 +40,8 @@ func (h *HTTPServer) getRequestContext(_ net.Listener) context.Context {
// Initializes handler and HTTP server structure.
func (h *HTTPServer) initialize() {
h.handler = &handler{}
// We do not need to specify all possible parameters for HTTP server, so:
// nolint:exaustivestruct
h.server = &http.Server{
// ToDo: make it all configurable.
Addr: ":34421",

View File

@@ -0,0 +1,15 @@
package storage
// 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
// 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)
// Start starts asynchronous things if needed.
Start()
}

View File

@@ -0,0 +1,75 @@
package memory
import (
"context"
"log"
"sync"
)
// Storage is an in-memory storage.
type Storage struct {
ctx context.Context
doneChan chan struct{}
name string
data map[string]string
dataMutex sync.RWMutex
}
// NewStorage creates new in-memory storage to use.
func NewStorage(ctx context.Context, name string) (*Storage, chan struct{}) {
s := &Storage{
ctx: ctx,
doneChan: make(chan struct{}),
name: name,
}
s.initialize()
return s, s.doneChan
}
// Get returns data from storage by key.
func (s *Storage) Get(key string) string {
s.dataMutex.RLock()
defer s.dataMutex.RUnlock()
data, found := s.data[key]
if !found {
return "Not found"
}
return data
}
// GetDoneChan returns a channel which should be used to block execution
// until storage's routines are completed.
func (s *Storage) GetDoneChan() chan struct{} {
return s.doneChan
}
// Initializes internal things.
func (s *Storage) initialize() {
s.data = make(map[string]string)
}
// Put puts passed data into storage.
func (s *Storage) Put(data map[string]string) {
s.dataMutex.Lock()
defer s.dataMutex.Unlock()
for k, v := range data {
s.data[k] = v
}
}
// Start starts asynchronous things if needed.
func (s *Storage) Start() {
// The Context Listening Goroutine.
go func() {
<-s.ctx.Done()
log.Println("In-memory storage", s.name, "done")
s.doneChan <- struct{}{}
}()
}

View File

@@ -0,0 +1,6 @@
package storage
// Metrics describes generic metrics storage interface.
type Metrics interface {
GenericStorage
}