Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 1ceda29

Browse files
author
Noah Hanjun Lee
authored
Add opt-in option for Prometheus (#162)
* Enable to opt-in prometheus * Add docs
1 parent ff50d6e commit 1ceda29

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

cmd/server/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type (
6060
}
6161

6262
Prometheus struct {
63+
PrometheusEnabled bool `split_words:"true"`
6364
PrometheusAuthSecret string `split_words:"true"`
6465
}
6566
)

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func newServerConfig(c *Config) *server.ServerConfig {
9696
ProxyHost: proxyHost,
9797
ProxyProto: proxyProto,
9898
WebhookSecret: c.WebhookSecret,
99+
PrometheusEnabled: c.PrometheusEnabled,
99100
PrometheusAuthSecret: c.PrometheusAuthSecret,
100101
}
101102
}

docs/concepts/metrics.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Metrics
22

3-
Gtiploy publishes and exposes metrics that Prometheus can consume at the standard `/metrics` endpoint.
3+
Gtiploy publishes and exposes metrics that Prometheus can consume at the standard `/metrics` endpoint.
44

55
## Configuration
66

77
1\. Configure the server:
88

99
```
10+
GITPLOY_PROMETHEUS_ENABLED=true
1011
GITPLOY_PROMETHEUS_AUTH_SECRET=YOUR_SECRET
1112
```
1213

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# GITPLOY_PROMETHEUS_ENABLED
2+
3+
Optional string value to enable exposing metrics that Prometheus can consume. The default value is `false`.
4+
5+
```
6+
GITPLOY_PROMETHEUS_ENABLED=true
7+
```

docs/references/configurations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Index of server configuration settings:
1010
* [GITPLOY_LICENSE](./GITPLOY_LICENSE.md)
1111
* [GITPLOY_MEMBER_ENTRIES](./GITPLOY_MEMBER_ENTRIES.md)
1212
* [GITPLOY_PROMETHEUS_AUTH_SECRET](./GITPLOY_PROMETHEUS_AUTH_SECRET.md)
13+
* [GITPLOY_PROMETHEUS_ENABLED](./GITPLOY_PROMETHEUS_ENABLED.md)
1314
* [GITPLOY_ORGANIZATION_ENTRIES](./GITPLOY_ORGANIZATION_ENTRIES.md)
1415
* [GITPLOY_PROXY_SERVER_HOST](./GITPLOY_PROXY_SERVER_HOST.md)
1516
* [GITPLOY_PROXY_SERVER_PROTO](./GITPLOY_PROXY_SERVER_PROTO.md)

internal/server/router.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ type (
4242
ProxyHost string
4343
ProxyProto string
4444

45-
WebhookSecret string
45+
WebhookSecret string
46+
47+
PrometheusEnabled bool
4648
PrometheusAuthSecret string
4749
}
4850

@@ -101,7 +103,6 @@ func NewRouter(c *RouterConfig) *gin.Engine {
101103
gm := gb.NewMiddleware(c.Interactor)
102104
r.Use(
103105
gm.SetUser(),
104-
metrics.CollectRequestMetrics(),
105106
)
106107

107108
v1 := r.Group("/api/v1")
@@ -201,11 +202,13 @@ func NewRouter(c *RouterConfig) *gin.Engine {
201202

202203
metricsapi := r.Group("/metrics")
203204
{
205+
r.Use(metrics.CollectRequestMetrics())
206+
204207
m := metrics.NewMetric(&metrics.MetricConfig{
205208
Interactor: c.Interactor,
206209
PrometheusAuthSecret: c.PrometheusAuthSecret,
207210
})
208-
metricsapi.GET("", m.CollectMetrics)
211+
metricsapi.GET("", hasOptIn(c.PrometheusEnabled), m.CollectMetrics)
209212
}
210213

211214
r.HEAD("/slack", func(gc *gin.Context) {
@@ -303,3 +306,12 @@ func newSlackOauthConfig(c *RouterConfig) *oauth2.Config {
303306
func isSlackEnabled(c *RouterConfig) bool {
304307
return c.ChatConfig != nil && c.ChatConfig.Type == ChatTypeSlack
305308
}
309+
310+
func hasOptIn(enabled bool) gin.HandlerFunc {
311+
return func(c *gin.Context) {
312+
if !enabled {
313+
c.AbortWithStatus(http.StatusNotFound)
314+
return
315+
}
316+
}
317+
}

0 commit comments

Comments
 (0)