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

cshum / imagor / 4142608967

10 Feb 2023 09:36AM UTC coverage: 91.071% (-0.1%) from 91.179%
4142608967

Pull #282

github

Adrian Shum
fix tests
Pull Request #282: feat(server): Add optional Prometheus metrics server

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

5538 of 6081 relevant lines covered (91.07%)

1.07 hits per line

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

76.92
/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
                Path:   "/",
1✔
23
                Logger: zap.NewNop(),
1✔
24
        }
1✔
25
        for _, option := range options {
2✔
26
                option(s)
1✔
27
        }
1✔
28
        if s.Path != "" && s.Path != "/" {
2✔
29
                mux := http.NewServeMux()
1✔
30
                mux.Handle(s.Path, promhttp.Handler())
1✔
31
                mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1✔
32
                        http.Redirect(w, r, s.Path, http.StatusPermanentRedirect)
×
33
                })
×
34
                s.Handler = mux
1✔
35
        } else {
1✔
36
                s.Handler = promhttp.Handler()
1✔
37
        }
1✔
38
        return s
1✔
39
}
40

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

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

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

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

69
// WithLogger with logger option
70
func WithLogger(logger *zap.Logger) Option {
1✔
71
        return func(s *PrometheusMetrics) {
2✔
72
                if logger != nil {
2✔
73
                        s.Logger = logger
1✔
74
                }
1✔
75
        }
76
}
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