-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- all bot.Stats values - go runtime stats
- Loading branch information
Showing
8 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package config | ||
|
||
type Metrics struct { | ||
// e.g. use ":8082" to expose metrics on all interfaces | ||
PrometheusListener string `mapstructure:"prometheus_listener"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package bot | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/innogames/slack-bot/v2/bot/config" | ||
"github.com/innogames/slack-bot/v2/bot/stats" | ||
"github.com/innogames/slack-bot/v2/bot/util" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type statRegistry struct{} | ||
|
||
// Describe returns all descriptions of the collector. | ||
func (c *statRegistry) Describe(_ chan<- *prometheus.Desc) { | ||
// unused in our simple case... | ||
} | ||
|
||
// Collect returns the current state of all metrics of our slack-bot stats | ||
func (c *statRegistry) Collect(ch chan<- prometheus.Metric) { | ||
for _, key := range stats.GetKeys() { | ||
metric := prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: "slack_bot", | ||
Name: strings.ReplaceAll(key, "-", "_"), | ||
}) | ||
value, _ := stats.Get(key) | ||
metric.Set(float64(value)) | ||
metric.Collect(ch) | ||
} | ||
} | ||
|
||
func initMetrics(cfg config.Config, ctx *util.ServerContext) { | ||
if cfg.Metrics.PrometheusListener == "" { | ||
return | ||
} | ||
|
||
registry := prometheus.NewRegistry() | ||
registry.MustRegister( | ||
&statRegistry{}, | ||
collectors.NewGoCollector(), | ||
) | ||
|
||
go func() { | ||
ctx.RegisterChild() | ||
defer ctx.ChildDone() | ||
|
||
log.Infof("Init prometheus handler on http://%s/metrics", cfg.Metrics.PrometheusListener) | ||
|
||
server := &http.Server{ | ||
Addr: cfg.Metrics.PrometheusListener, | ||
ReadHeaderTimeout: 3 * time.Second, | ||
} | ||
|
||
http.Handle( | ||
"/metrics", promhttp.HandlerFor( | ||
registry, | ||
promhttp.HandlerOpts{}, | ||
), | ||
) | ||
|
||
go func() { | ||
_ = server.ListenAndServe() | ||
}() | ||
|
||
<-ctx.Done() | ||
_ = server.Shutdown(ctx) | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package bot | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/innogames/slack-bot/v2/bot/config" | ||
"github.com/innogames/slack-bot/v2/bot/stats" | ||
"github.com/innogames/slack-bot/v2/bot/util" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMetrics(t *testing.T) { | ||
ctx := util.NewServerContext() | ||
defer ctx.StopTheWorld() | ||
|
||
metricsPort := getPort() | ||
|
||
cfg := config.Config{ | ||
Metrics: config.Metrics{ | ||
PrometheusListener: metricsPort, | ||
}, | ||
} | ||
|
||
stats.Set("test_value", 500) | ||
|
||
initMetrics(cfg, ctx) | ||
time.Sleep(time.Millisecond * 100) | ||
|
||
resp, err := http.Get("http://" + metricsPort + "/metrics") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, 200, resp.StatusCode) | ||
|
||
content, _ := io.ReadAll(resp.Body) | ||
assert.Contains(t, string(content), "slack_bot_test_value 500") | ||
} | ||
|
||
// get a random free port on the host | ||
func getPort() string { | ||
l, _ := net.Listen("tcp4", "localhost:0") | ||
defer l.Close() | ||
|
||
return l.Addr().String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters