feat: domain finished
This commit is contained in:
Executable
+64
@@ -0,0 +1,64 @@
|
||||
package metric
|
||||
|
||||
import "time"
|
||||
|
||||
//CLI define a CLI app
|
||||
type CLI struct {
|
||||
Name string
|
||||
StartedAt time.Time
|
||||
FinishedAt time.Time
|
||||
Duration float64
|
||||
}
|
||||
|
||||
// NewCLI create a new CLI app
|
||||
func NewCLI(name string) *CLI {
|
||||
return &CLI{
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
//Started start monitoring the app
|
||||
func (c *CLI) Started() {
|
||||
c.StartedAt = time.Now()
|
||||
}
|
||||
|
||||
// Finished app finished
|
||||
func (c *CLI) Finished() {
|
||||
c.FinishedAt = time.Now()
|
||||
c.Duration = time.Since(c.StartedAt).Seconds()
|
||||
}
|
||||
|
||||
//HTTP application
|
||||
type HTTP struct {
|
||||
Handler string
|
||||
Method string
|
||||
StatusCode string
|
||||
StartedAt time.Time
|
||||
FinishedAt time.Time
|
||||
Duration float64
|
||||
}
|
||||
|
||||
//NewHTTP create a new HTTP app
|
||||
func NewHTTP(handler string, method string) *HTTP {
|
||||
return &HTTP{
|
||||
Handler: handler,
|
||||
Method: method,
|
||||
}
|
||||
}
|
||||
|
||||
//Started start monitoring the app
|
||||
func (h *HTTP) Started() {
|
||||
h.StartedAt = time.Now()
|
||||
}
|
||||
|
||||
// Finished app finished
|
||||
func (h *HTTP) Finished() {
|
||||
h.FinishedAt = time.Now()
|
||||
h.Duration = time.Since(h.StartedAt).Seconds()
|
||||
}
|
||||
|
||||
//UseCase definition
|
||||
type UseCase interface {
|
||||
SaveCLI(c *CLI) error
|
||||
SaveHTTP(h *HTTP)
|
||||
}
|
||||
Executable
+55
@@ -0,0 +1,55 @@
|
||||
package metric
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/push"
|
||||
"github.com/eminetto/clean-architecture-go/config"
|
||||
)
|
||||
|
||||
//Service implements UseCase interface
|
||||
type Service struct {
|
||||
pHistogram *prometheus.HistogramVec
|
||||
httpRequestHistogram *prometheus.HistogramVec
|
||||
}
|
||||
|
||||
//NewPrometheusService create a new prometheus service
|
||||
func NewPrometheusService() (*Service, error) {
|
||||
cli := prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: "pushgateway",
|
||||
Name: "cmd_duration_seconds",
|
||||
Help: "CLI application execution in seconds",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}, []string{"name"})
|
||||
http := prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: "http",
|
||||
Name: "request_duration_seconds",
|
||||
Help: "The latency of the HTTP requests.",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}, []string{"handler", "method", "code"})
|
||||
|
||||
s := &Service{
|
||||
pHistogram: cli,
|
||||
httpRequestHistogram: http,
|
||||
}
|
||||
err := prometheus.Register(s.pHistogram)
|
||||
if err != nil && err.Error() != "duplicate metrics collector registration attempted" {
|
||||
return nil, err
|
||||
}
|
||||
err = prometheus.Register(s.httpRequestHistogram)
|
||||
if err != nil && err.Error() != "duplicate metrics collector registration attempted" {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
//SaveCLI send metrics to server
|
||||
func (s *Service) SaveCLI(c *CLI) error {
|
||||
gatewayURL := config.PROMETHEUS_PUSHGATEWAY
|
||||
s.pHistogram.WithLabelValues(c.Name).Observe(c.Duration)
|
||||
return push.New(gatewayURL, "cmd_job").Collector(s.pHistogram).Push()
|
||||
}
|
||||
|
||||
//SaveHTTP send metrics to server
|
||||
func (s *Service) SaveHTTP(h *HTTP) {
|
||||
s.httpRequestHistogram.WithLabelValues(h.Handler, h.Method, h.StatusCode).Observe(h.Duration)
|
||||
}
|
||||
Reference in New Issue
Block a user