Skip to content

Commit

Permalink
Merge commit 'bba3d6fd67d6567bf09852c902c72feb565c1096' into beta
Browse files Browse the repository at this point in the history
* commit 'bba3d6fd67d6567bf09852c902c72feb565c1096':
  jitsucom#1012 Prometheus custom endpoint and authorization
  • Loading branch information
absorbb committed Apr 18, 2023
2 parents c21d8ca + bba3d6f commit c93ca29
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 4 additions & 1 deletion documentation/other-features/application-metrics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ By default metrics collection is disabled. For enabling it put the following key
server:
admin_token: your_admin_token
metrics:
prometheus.enabled: true
prometheus:
enabled: true
auth: true
endpoint: prometheus

destinations:
...
Expand Down
2 changes: 2 additions & 0 deletions server/appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func setDefaultParams(containerized bool) {
viper.SetDefault("server.cache.events.trim_interval_ms", 500)
viper.SetDefault("server.cache.events.max_malformed_event_size_bytes", 10_000)
viper.SetDefault("server.cache.pool.size", 10)
viper.SetDefault("server.metrics.prometheus.auth", true)
viper.SetDefault("server.metrics.prometheus.endpoint", "prometheus")
viper.SetDefault("server.strict_auth_tokens", false)
viper.SetDefault("server.max_columns", 100)
viper.SetDefault("server.max_event_size", 51200)
Expand Down
4 changes: 3 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ func main() {
metricsExported := viper.GetBool("server.metrics.prometheus.enabled")
metricsRelay := metrics.InitRelay(clusterID, viper.Sub("server.metrics.relay"))
if metricsExported || metricsRelay != nil {
metrics.Init(metricsExported)
metricsAuth := viper.GetBool("server.metrics.prometheus.auth")
metricsEndpoint := viper.GetString("server.metrics.prometheus.endpoint")
metrics.Init(metricsExported, metricsAuth, metricsEndpoint)
if metricsRelay != nil {
interval := 5 * time.Minute
if viper.IsSet("server.metrics.relay.interval") {
Expand Down
8 changes: 7 additions & 1 deletion server/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const (

var Exported bool

var Auth bool

var Endpoint string

var Registry *prometheus.Registry

func Enabled() bool {
Expand All @@ -45,8 +49,10 @@ func NewHistogramVec(opts prometheus.HistogramOpts, labels []string) *prometheus

const Unknown = "unknown"

func Init(exported bool) {
func Init(exported bool, auth bool, endpoint string) {
Exported = exported
Auth = auth
Endpoint = endpoint
if Exported {
logging.Info("✅ Initializing Prometheus metrics..")
}
Expand Down
6 changes: 5 additions & 1 deletion server/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ func SetupRouter(adminToken string, metaStorage meta.Storage, destinations *dest
router.POST("/api.:ignored", middleware.TokenFuncAuth(jsEventHandler.PostHandler, appconfig.Instance.AuthorizationService.GetClientOrigins, ""))

if metrics.Exported {
router.GET("/prometheus", middleware.TokenAuth(gin.WrapH(metrics.Handler()), adminToken))
handler := gin.WrapH(metrics.Handler())
if metrics.Auth {
handler = middleware.TokenAuth(handler, adminToken)
}
router.GET("/" + metrics.Endpoint, handler)
}

//Setup profiler
Expand Down

0 comments on commit c93ca29

Please sign in to comment.