-
Notifications
You must be signed in to change notification settings - Fork 10
Move stats to prometheus instead of statsd #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2bc471a
a82928a
f4e640b
234ec2e
cf40dfd
bf036ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,28 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "errors" | ||
| "log" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/julsemaan/anyfile-notepad/api/internal/contact" | ||
| "github.com/julsemaan/anyfile-notepad/api/internal/httpapi" | ||
| "github.com/julsemaan/anyfile-notepad/api/internal/logging" | ||
| "github.com/julsemaan/anyfile-notepad/api/internal/resources" | ||
| "github.com/julsemaan/anyfile-notepad/api/internal/stats" | ||
| cache "github.com/patrickmn/go-cache" | ||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| "github.com/rs/rest-layer/resource" | ||
| "github.com/rs/rest-layer/rest" | ||
| "github.com/rs/rest-layer/schema" | ||
| "gopkg.in/alexcesaro/statsd.v2" | ||
| ) | ||
|
|
||
| func Run(cfg Config) error { | ||
| schema.CreatedField.ReadOnly = false | ||
| schema.UpdatedField.ReadOnly = false | ||
|
|
||
| statsConn, err := statsd.New(statsd.Address(cfg.StatsdAddress)) | ||
| if err != nil { | ||
| logging.Errorf("statsd initialization failed: %v", err) | ||
| } | ||
| if statsConn != nil { | ||
| defer statsConn.Close() | ||
| } | ||
|
|
||
| var metrics stats.Metrics | ||
| if statsConn != nil { | ||
| metrics = statsConn | ||
| } | ||
| metrics := stats.NewPrometheusMetrics() | ||
| statsService := stats.NewService(metrics) | ||
| contactCache := cache.New(24*time.Hour, time.Minute) | ||
| contactService := contact.NewService(contactCache, cfg.MaxContactRequestsPerDay, cfg.SupportEmail, sendEmailWithOptionalTLS) | ||
|
|
@@ -54,6 +44,52 @@ func Run(cfg Config) error { | |
| cfg.Password, | ||
| ) | ||
|
|
||
| log.Printf("Serving API on http://localhost%s", cfg.ListenAddr) | ||
| return http.ListenAndServe(cfg.ListenAddr, router) | ||
| metricsMux := http.NewServeMux() | ||
| metricsMux.Handle("/metrics", promhttp.Handler()) | ||
|
|
||
| metricsServer := &http.Server{ | ||
| Addr: cfg.MetricsListenAddr, | ||
| Handler: metricsMux, | ||
| ReadHeaderTimeout: 5 * time.Second, | ||
| ReadTimeout: 10 * time.Second, | ||
| WriteTimeout: 10 * time.Second, | ||
| IdleTimeout: 60 * time.Second, | ||
| } | ||
|
|
||
| apiServer := &http.Server{ | ||
| Addr: cfg.ListenAddr, | ||
| Handler: router, | ||
| ReadHeaderTimeout: 5 * time.Second, | ||
| ReadTimeout: 10 * time.Second, | ||
| WriteTimeout: 30 * time.Second, | ||
| IdleTimeout: 60 * time.Second, | ||
| } | ||
|
|
||
| errCh := make(chan error, 2) | ||
| go func() { | ||
| log.Printf("Serving Prometheus metrics on %s", logURL(cfg.MetricsListenAddr, "/metrics")) | ||
| errCh <- metricsServer.ListenAndServe() | ||
| }() | ||
|
|
||
| go func() { | ||
| log.Printf("Serving API on %s", logURL(cfg.ListenAddr, "")) | ||
| errCh <- apiServer.ListenAndServe() | ||
| }() | ||
|
Comment on lines
+68
to
+77
|
||
|
|
||
| for { | ||
| err := <-errCh | ||
| if errors.Is(err, http.ErrServerClosed) { | ||
| continue | ||
| } | ||
|
|
||
| return err | ||
| } | ||
| } | ||
|
|
||
| func logURL(addr string, path string) string { | ||
| if strings.HasPrefix(addr, ":") { | ||
| addr = "localhost" + addr | ||
| } | ||
|
|
||
| return "http://" + addr + path | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package app | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestLogURL(t *testing.T) { | ||
| t.Run("adds localhost for port only address", func(t *testing.T) { | ||
| got := logURL(":8080", "") | ||
| if got != "http://localhost:8080" { | ||
| t.Fatalf("expected localhost url, got %q", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("uses host qualified address as is", func(t *testing.T) { | ||
| got := logURL("0.0.0.0:9090", "/metrics") | ||
| if got != "http://0.0.0.0:9090/metrics" { | ||
| t.Fatalf("expected host-qualified url, got %q", got) | ||
| } | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package stats | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| ) | ||
|
|
||
| var statsHitsCounter = promauto.NewCounter(prometheus.CounterOpts{ | ||
| Name: "afn_stats_hits_total", | ||
| Help: "Number of accepted stats payloads.", | ||
| }) | ||
|
|
||
| var statsIncrementCounter = promauto.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: "afn_stats_increment_total", | ||
| Help: "Number of accepted increment stats by key.", | ||
| }, | ||
| []string{"key"}, | ||
| ) | ||
|
Comment on lines
+13
to
+19
|
||
|
|
||
| type PrometheusMetrics struct{} | ||
|
|
||
| func NewPrometheusMetrics() *PrometheusMetrics { | ||
| return &PrometheusMetrics{} | ||
| } | ||
|
|
||
| func (m *PrometheusMetrics) IncrementStatsHits() { | ||
| statsHitsCounter.Inc() | ||
| } | ||
|
|
||
| func (m *PrometheusMetrics) IncrementKey(key string) { | ||
| statsIncrementCounter.WithLabelValues(key).Inc() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github.com/julsemaan/anyfile-notepad/utilsis still a direct requirement in go.mod, but api/go.sum no longer contains any checksums for that module. This will causego mod tidy/go testto reintroduce go.sum changes and can break CI if it enforces a clean module graph. Regenerate go.sum (e.g., viago mod tidy) so the required module has matching sum entries.