URL shortener and file dump for hashru.link https://hashru.link
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

63 lines
1.7 KiB

  1. package rushlink
  2. import (
  3. "log"
  4. "net/http"
  5. "time"
  6. "gitea.hashru.nl/dsprenkels/rushlink/internal/db"
  7. "github.com/gorilla/mux"
  8. "github.com/pkg/errors"
  9. "github.com/prometheus/client_golang/prometheus"
  10. "github.com/prometheus/client_golang/prometheus/promhttp"
  11. bolt "go.etcd.io/bbolt"
  12. )
  13. const metricNamespace = "rushlink"
  14. var metricRequestsTotalCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
  15. Namespace: metricNamespace,
  16. Subsystem: "http",
  17. Name: "requests_total",
  18. Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
  19. }, []string{"code", "method"})
  20. func metricURLsTotal(database *db.Database) float64 {
  21. var metric float64
  22. if err := database.Bolt.View(func(tx *bolt.Tx) error {
  23. bucket := tx.Bucket([]byte("pastes"))
  24. if bucket == nil {
  25. return errors.New("bucket 'pastes' could not be found")
  26. }
  27. metric = float64(bucket.Stats().KeyN)
  28. return nil
  29. }); err != nil {
  30. log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
  31. return 0
  32. }
  33. return metric
  34. }
  35. // StartMetricsServer starts sering Prometheus metrics exports on addr
  36. func StartMetricsServer(addr string, database *db.Database, fs *db.FileStore) {
  37. prometheus.MustRegister(metricRequestsTotalCounter)
  38. prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  39. Namespace: metricNamespace,
  40. Subsystem: "pastes",
  41. Name: "urls_total",
  42. Help: "The current amount of pastes in the database.",
  43. }, func() float64 { return metricURLsTotal(database) }))
  44. router := mux.NewRouter()
  45. router.Handle("/metrics", promhttp.Handler()).Methods("GET")
  46. srv := &http.Server{
  47. Handler: router,
  48. Addr: addr,
  49. WriteTimeout: 15 * time.Second,
  50. ReadTimeout: 15 * time.Second,
  51. }
  52. log.Fatal(srv.ListenAndServe())
  53. }