Skip to content

Commit

Permalink
Merge pull request #17 from islax/prometheusMetrics
Browse files Browse the repository at this point in the history
Prometheus metrics
  • Loading branch information
cyberinc-ashish authored May 27, 2021
2 parents c84205d + af5d437 commit b7793d3
Show file tree
Hide file tree
Showing 7 changed files with 560 additions and 33 deletions.
24 changes: 21 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ import (
microappCtx "github.com/islax/microapp/context"
"github.com/islax/microapp/event"
"github.com/islax/microapp/log"
"github.com/islax/microapp/metrics"
"github.com/islax/microapp/repository"
"github.com/islax/microapp/retry"
"github.com/islax/microapp/security"
gormmysqldriver "gorm.io/driver/mysql"
"gorm.io/gorm"

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
uuid "github.com/satori/go.uuid"
prometheusmetrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

// RouteSpecifier should be implemented by the class that sets routes for the API endpoints
Expand Down Expand Up @@ -156,12 +161,25 @@ func (app *App) Initialize(routeSpecifiers []RouteSpecifier) {
logger := app.log
app.Router = mux.NewRouter()
app.Router.Use(mux.CORSMethodMiddleware(app.Router))
app.Router.Use(app.loggingMiddleware)

for _, routeSpecifier := range routeSpecifiers {
routeSpecifier.RegisterRoutes(app.Router)
}

//prometheus
if app.Config.GetBool(config.EvSuffixForEnableMetrics) {
metrics.RegisterGormMetrics(app.DB, app.Config)
// Create our middleware.
mdlw := middleware.New(middleware.Config{
Recorder: prometheusmetrics.NewRecorder(prometheusmetrics.Config{}),
Service: app.Name,
})
app.Router.Use(std.HandlerProvider("", mdlw))
app.Router.Path("/metrics").Handler(promhttp.Handler())
}

app.Router.Use(app.loggingMiddleware)

//TODO: Revisit this logic
apiPort := "80"
if app.Config.IsSet("API_PORT") {
Expand Down Expand Up @@ -290,11 +308,11 @@ func (app *App) loggingMiddleware(next http.Handler) http.Handler {
logger := app.Logger("Ingress").With().Timestamp().Str("caller", r.Header.Get("X-Client")).Str("correlationId", r.Header.Get("X-Correlation-ID")).Str("method", r.Method).Str("requestURI", r.RequestURI).Logger()

rec := &httpStatusRecorder{ResponseWriter: w}
if !strings.HasSuffix(r.RequestURI, "/health") || app.Config.GetBool(config.EvSuffixForEnableHealthLog) {
if (!strings.HasSuffix(r.RequestURI, "/health") || app.Config.GetBool(config.EvSuffixForEnableHealthLog)) && !strings.HasSuffix(r.RequestURI, "/metrics") {
logger.Info().Msg("Begin")
}
next.ServeHTTP(rec, r)
if !strings.HasSuffix(r.RequestURI, "/health") || app.Config.GetBool(config.EvSuffixForEnableHealthLog) {
if (!strings.HasSuffix(r.RequestURI, "/health") || app.Config.GetBool(config.EvSuffixForEnableHealthLog)) && !strings.HasSuffix(r.RequestURI, "/metrics") {
if rec.status >= http.StatusInternalServerError {
logger.Error().Int("status", rec.status).Dur("responseTime", time.Now().Sub(startTime)).Msg("End.")
} else {
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewConfig(defaults map[string]interface{}) *Config {

config.viper.SetDefault("TLS_CRT", "/opt/isla/tls.crt")
config.viper.SetDefault("TLS_KEY", "/opt/isla/tls.key")

config.viper.SetDefault(EvSuffixForGormMetricsRefresh, 30)
for key, value := range defaults {
config.viper.SetDefault(key, value)
}
Expand Down
4 changes: 4 additions & 0 deletions config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ const (
EvSuffixForGormSlowThreshold = "GORM_SLOW_THRESHOLD"
// EvSuffixForEnableHealthLog environment variable name for log level
EvSuffixForEnableHealthLog = "ENABLE_HEALTH_LOG"
// EvSuffixForEnableMetrics environment variable name for enable metrics
EvSuffixForEnableMetrics = "ENABLE_METRICS"
// EvSuffixForGormMetricsRefresh environment variable name for gorm metrics refresh interval
EvSuffixForGormMetricsRefresh = "GORM_METRICS_REFRESH_INTERVAL"
)
15 changes: 5 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/distribution v2.7.0+incompatible // indirect
github.com/docker/docker v0.7.3-0.20190108045446-77df18c24acf // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang-migrate/migrate/v4 v4.2.1
github.com/golang/protobuf v1.3.1 // indirect
github.com/gorilla/mux v1.7.1
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.10.0
github.com/rs/zerolog v1.18.0
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/slok/go-http-metrics v0.9.0
github.com/spf13/viper v1.3.2
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734
golang.org/x/text v0.3.2 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb // indirect
google.golang.org/grpc v1.20.1 // indirect
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
gorm.io/driver/mysql v1.0.4
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.12
Expand Down
Loading

0 comments on commit b7793d3

Please sign in to comment.