• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cshum / imagor / 4142091150

10 Feb 2023 08:24AM UTC coverage: 90.999% (-0.2%) from 91.179%
4142091150

Pull #282

github

Adrian Shum
refactor interfaces
Pull Request #282: feat(server): Add optional Prometheus metrics server

67 of 67 new or added lines in 4 files covered. (100.0%)

5530 of 6077 relevant lines covered (91.0%)

1.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.32
/metrics/prometheusmetrics/prometheus.go
1
package prometheusmetrics
2

3
import (
4
        "context"
5
        "net/http"
6

7
        "github.com/prometheus/client_golang/prometheus/promhttp"
8
        "go.uber.org/zap"
9
)
10

11
// PrometheusMetrics wraps the Service with additional http and app lifecycle handling
12
type PrometheusMetrics struct {
13
        http.Server
14

15
        Path   string
16
        Logger *zap.Logger
17
}
18

19
// New create new metrics PrometheusMetrics
20
func New(options ...Option) *PrometheusMetrics {
1✔
21
        s := &PrometheusMetrics{
1✔
22
                Logger: zap.NewNop(),
1✔
23
        }
1✔
24
        for _, option := range options {
2✔
25
                option(s)
1✔
26
        }
1✔
27
        if s.Path != "" {
2✔
28
                mux := http.NewServeMux()
1✔
29
                mux.Handle(s.Path, promhttp.Handler())
1✔
30
                mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1✔
31
                        http.Redirect(w, r, s.Path, http.StatusPermanentRedirect)
×
32
                })
×
33
                s.Handler = mux
1✔
34
        } else {
1✔
35
                s.Handler = promhttp.Handler()
1✔
36
        }
1✔
37
        return s
1✔
38
}
39

40
// Startup prometheus metrics server
41
func (s *PrometheusMetrics) Startup(_ context.Context) error {
×
42
        go func() {
×
43
                if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
×
44
                        s.Logger.Fatal("prometheus listen", zap.Error(err))
×
45
                }
×
46
        }()
47
        s.Logger.Info("prometheus listen", zap.String("addr", s.Addr), zap.String("path", s.Path))
×
48
        return nil
×
49
}
50

51
// Option PrometheusMetrics option
52
type Option func(s *PrometheusMetrics)
53

54
// WithAddr with server and port option
55
func WithAddr(addr string) Option {
1✔
56
        return func(s *PrometheusMetrics) {
2✔
57
                s.Addr = addr
1✔
58
        }
1✔
59
}
60

61
// WithPath with path option
62
func WithPath(path string) Option {
1✔
63
        return func(s *PrometheusMetrics) {
2✔
64
                s.Path = path
1✔
65
        }
1✔
66
}
67

68
// WithLogger with logger option
69
func WithLogger(logger *zap.Logger) Option {
1✔
70
        return func(s *PrometheusMetrics) {
2✔
71
                if logger != nil {
2✔
72
                        s.Logger = logger
1✔
73
                }
1✔
74
        }
75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc