Init commit repo setup
This commit is contained in:
36
pkg/metrics/http_client_metrics_recorder.go
Normal file
36
pkg/metrics/http_client_metrics_recorder.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"cybertron/models/instrumentation"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ClientHttpCall func(req *http.Request) (*http.Response, error)
|
||||
|
||||
func RecordClientHttpCallMetrics(req *http.Request, method ClientHttpCall) (*http.Response, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
resp, err := method(req)
|
||||
|
||||
endTime := time.Now()
|
||||
duration := endTime.Sub(startTime)
|
||||
|
||||
metricsPublisher := NewMetricPublisher()
|
||||
|
||||
clientHttpCallMetrics := instrumentation.ClientHttpCallMetric{
|
||||
Url: req.URL.Path,
|
||||
StartTime: startTime.Unix(),
|
||||
EndTime: endTime.Unix(),
|
||||
DurationInMs: duration.Milliseconds(),
|
||||
}
|
||||
|
||||
if resp != nil {
|
||||
clientHttpCallMetrics.ResponseCode = resp.StatusCode
|
||||
}
|
||||
|
||||
metricsPublisher.PublishMetrics(instrumentation.MetricAttributes{ClientHttpCallMetric: clientHttpCallMetrics},
|
||||
instrumentation.CLIENT_HTTP_CALL_METRICS)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
44
pkg/metrics/metric_publisher.go
Normal file
44
pkg/metrics/metric_publisher.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"cybertron/models/instrumentation"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
PublishMetrics(metricAttributes map[string]interface{}, metricType instrumentation.MetricType)
|
||||
}
|
||||
|
||||
type PublisherImpl struct {
|
||||
}
|
||||
|
||||
func NewMetricPublisher() *PublisherImpl {
|
||||
return &PublisherImpl{}
|
||||
}
|
||||
|
||||
func (amp *PublisherImpl) PublishMetrics(metricAttributes instrumentation.MetricAttributes, metricType instrumentation.MetricType) {
|
||||
switch metricType {
|
||||
case instrumentation.API_METRICS:
|
||||
publishApiMetric(metricAttributes.ApiMetric)
|
||||
return
|
||||
case instrumentation.CLIENT_HTTP_CALL_METRICS:
|
||||
publishClientHttpCallMetric(metricAttributes.ClientHttpCallMetric)
|
||||
return
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func publishApiMetric(apiMetrics instrumentation.ApiMetric) {
|
||||
status := strconv.Itoa(apiMetrics.ResponseCode)
|
||||
duration := float64(apiMetrics.DurationInMs)
|
||||
ApiRequestCounter.WithLabelValues(apiMetrics.Url, status).Inc()
|
||||
ApiRequestLatencyHistogram.WithLabelValues(apiMetrics.Url, status).Observe(duration)
|
||||
}
|
||||
|
||||
func publishClientHttpCallMetric(clientHttpCallMetric instrumentation.ClientHttpCallMetric) {
|
||||
status := strconv.Itoa(clientHttpCallMetric.ResponseCode)
|
||||
duration := float64(clientHttpCallMetric.DurationInMs)
|
||||
HttpCallRequestCounter.WithLabelValues(clientHttpCallMetric.Url, status).Inc()
|
||||
HttpCallRequestLatencyHistogram.WithLabelValues(clientHttpCallMetric.Url, status).Observe(duration)
|
||||
}
|
||||
42
pkg/metrics/metrics.go
Normal file
42
pkg/metrics/metrics.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
var metricsBuckets = []float64{5, 10, 20, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 20000, 30000, 60000}
|
||||
|
||||
var ApiRequestCounter = promauto.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "cybertron_api_request_total",
|
||||
Help: "api request counter",
|
||||
},
|
||||
[]string{"url", "response_code"},
|
||||
)
|
||||
|
||||
var ApiRequestLatencyHistogram = promauto.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "cybertron_api_request_latency_histogram",
|
||||
Help: "api latency histogram",
|
||||
Buckets: metricsBuckets,
|
||||
},
|
||||
[]string{"url", "response_code"},
|
||||
)
|
||||
|
||||
var HttpCallRequestCounter = promauto.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "cybertron_http_call_request_total",
|
||||
Help: "http call request counter",
|
||||
},
|
||||
[]string{"url", "response_code"},
|
||||
)
|
||||
|
||||
var HttpCallRequestLatencyHistogram = promauto.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "cybertron_http_call_request_latency_histogram",
|
||||
Help: "http call latency histogram",
|
||||
Buckets: metricsBuckets,
|
||||
},
|
||||
[]string{"url", "response_code"},
|
||||
)
|
||||
24
pkg/metrics/server.go
Normal file
24
pkg/metrics/server.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"cybertron/configs"
|
||||
"cybertron/pkg/log"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func AdminHandler() {
|
||||
ginServer := gin.New()
|
||||
port := configs.GetMetricsPort()
|
||||
log.Log.GetLog().Info("Starting metrics on port", zap.Int("port", port))
|
||||
ginServer.GET("/metrics", gin.WrapH(promhttp.Handler()))
|
||||
go func() {
|
||||
err := ginServer.Run(fmt.Sprintf(":%v", port))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user