Initial commit
This commit is contained in:
62
internal/service/monitoring/config/metrics.go
Normal file
62
internal/service/monitoring/config/metrics.go
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) New Cloud Technologies, Ltd., 2013-2026
|
||||
*
|
||||
* You can not use the contents of the file in any way without New Cloud Technologies Ltd. written permission.
|
||||
* To obtain such a permit, you should contact New Cloud Technologies, Ltd. at https://myoffice.ru/contacts/
|
||||
*/
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
)
|
||||
|
||||
type CommaSeparatedFloat64Slice []float64
|
||||
|
||||
func CommaSeparatedFloat64SliceHookFunc() mapstructure.DecodeHookFuncType {
|
||||
return func(
|
||||
f reflect.Type,
|
||||
t reflect.Type,
|
||||
data interface{},
|
||||
) (interface{}, error) {
|
||||
// Check that the data is string
|
||||
if f.Kind() != reflect.String {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Check that the target type is our custom type
|
||||
if t != reflect.TypeOf(CommaSeparatedFloat64Slice{}) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
stringSlice := strings.Split(data.(string), ",")
|
||||
floatSlice := make([]float64, 0)
|
||||
for _, str := range stringSlice {
|
||||
val, err := strconv.ParseFloat(strings.TrimSpace(str), 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
floatSlice = append(floatSlice, val)
|
||||
}
|
||||
|
||||
// Return the parsed value
|
||||
return CommaSeparatedFloat64Slice(floatSlice), nil
|
||||
}
|
||||
}
|
||||
|
||||
// HttpMetrics configuration properties for http monitoring
|
||||
type HttpMetrics struct {
|
||||
HistogramEnabled bool
|
||||
Buckets CommaSeparatedFloat64Slice
|
||||
}
|
||||
|
||||
// Metrics configuration properties for monitoring
|
||||
type Metrics struct {
|
||||
Endpoint string
|
||||
HistogramBuckets CommaSeparatedFloat64Slice
|
||||
Http HttpMetrics
|
||||
}
|
||||
95
internal/service/monitoring/metrics.go
Normal file
95
internal/service/monitoring/metrics.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
|
||||
"payouts/internal/service/monitoring/config"
|
||||
)
|
||||
|
||||
const (
|
||||
METRICS_NAMESPACE = "payouts"
|
||||
)
|
||||
|
||||
// Metrics represents the metrics service
|
||||
type Metrics interface {
|
||||
GetMiddleware() func(next http.Handler) http.Handler
|
||||
}
|
||||
|
||||
// metrics represents the metrics service implementation
|
||||
type metrics struct {
|
||||
httpDuration prometheus.ObserverVec
|
||||
}
|
||||
|
||||
// NewMetrics instantiates metrics service
|
||||
func NewMetrics(config config.Metrics) (Metrics, error) {
|
||||
var httpDuration prometheus.ObserverVec
|
||||
if config.Http.HistogramEnabled {
|
||||
httpDuration = CreateHttpHistogram(config.Http.Buckets)
|
||||
} else {
|
||||
httpDuration = CreateHttpSummary(config.Http.Buckets)
|
||||
|
||||
}
|
||||
return &metrics{
|
||||
httpDuration: httpDuration,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMiddleware returns the middleware to be used in mux router
|
||||
func (m *metrics) GetMiddleware() func(next http.Handler) http.Handler {
|
||||
return GetMiddleware(m.httpDuration)
|
||||
}
|
||||
|
||||
func CreateHttpSummary(buckets []float64) *prometheus.SummaryVec {
|
||||
return promauto.NewSummaryVec(prometheus.SummaryOpts{
|
||||
Name: "http_server_requests_seconds",
|
||||
Help: "Duration of HTTP requests.",
|
||||
}, []string{"uri", "method", "code"})
|
||||
}
|
||||
|
||||
func CreateHttpHistogram(buckets []float64) *prometheus.HistogramVec {
|
||||
return promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Name: "http_server_requests_seconds",
|
||||
Help: "Duration of HTTP requests.",
|
||||
Buckets: buckets,
|
||||
}, []string{"uri", "method", "code"})
|
||||
|
||||
}
|
||||
|
||||
func GetMiddleware(histogramVec prometheus.ObserverVec) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
route := mux.CurrentRoute(r)
|
||||
path, _ := route.GetPathTemplate()
|
||||
wd := &writerDelegate{
|
||||
ResponseWriter: w,
|
||||
statusCode: http.StatusOK,
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
duration := time.Since(start).Seconds()
|
||||
code := strconv.Itoa(wd.statusCode)
|
||||
histogramVec.WithLabelValues(path, r.Method, code).Observe(duration)
|
||||
}()
|
||||
|
||||
next.ServeHTTP(wd, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type writerDelegate struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
}
|
||||
|
||||
// override the WriteHeader method
|
||||
func (w *writerDelegate) WriteHeader(statusCode int) {
|
||||
w.statusCode = statusCode
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
24
internal/service/monitoring/module.go
Normal file
24
internal/service/monitoring/module.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"go.uber.org/fx"
|
||||
|
||||
"payouts/internal/config"
|
||||
)
|
||||
|
||||
// Module is a fx module
|
||||
var Module = fx.Options(
|
||||
fx.Provide(New),
|
||||
)
|
||||
|
||||
// Params represents the module input params
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
AppConfig *config.App
|
||||
}
|
||||
|
||||
// New instantiates the metrics service
|
||||
func New(p Params) (Metrics, error) {
|
||||
return NewMetrics(p.AppConfig.Metrics)
|
||||
}
|
||||
Reference in New Issue
Block a user