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

cshum / imagor / 4061833823

01 Feb 2023 07:10AM UTC coverage: 91.143% (-0.04%) from 91.179%
4061833823

Pull #282

github

Adrian Shum
revert readme
Pull Request #282: feat(server): Add optional Prometheus metrics server

44 of 44 new or added lines in 2 files covered. (100.0%)

5495 of 6029 relevant lines covered (91.14%)

1.07 hits per line

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

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

3
import (
4
        "github.com/prometheus/client_golang/prometheus/promhttp"
5
        "go.uber.org/zap"
6
        "net/http"
7
)
8

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

13
        Namespace string
14
        Logger    *zap.Logger
15
}
16

17
// New create new metrics PrometheusMetrics
18
func New(options ...Option) *PrometheusMetrics {
1✔
19
        s := &PrometheusMetrics{
1✔
20
                Logger: zap.NewNop(),
1✔
21
        }
1✔
22
        for _, option := range options {
2✔
23
                option(s)
1✔
24
        }
1✔
25

26
        s.Handler = promhttp.Handler()
1✔
27

1✔
28
        return s
1✔
29
}
30

31
// Run http metrics server
32
func (s *PrometheusMetrics) Run() {
×
33
        go func() {
×
34
                if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
×
35
                        s.Logger.Fatal("prometheus listen", zap.Error(err))
×
36
                }
×
37
        }()
38
        s.Logger.Info("prometheus listen", zap.String("addr", s.Addr), zap.String("namespace", s.Namespace))
×
39
}
40

41
// Option PrometheusMetrics option
42
type Option func(s *PrometheusMetrics)
43

44
// WithAddr with server and port option
45
func WithAddr(addr string) Option {
1✔
46
        return func(s *PrometheusMetrics) {
2✔
47
                s.Addr = addr
1✔
48
        }
1✔
49
}
50

51
// WithNamespace with path option
52
func WithNamespace(path string) Option {
1✔
53
        return func(s *PrometheusMetrics) {
2✔
54
                s.Namespace = path
1✔
55
        }
1✔
56
}
57

58
// WithLogger with logger option
59
func WithLogger(logger *zap.Logger) Option {
1✔
60
        return func(s *PrometheusMetrics) {
2✔
61
                if logger != nil {
2✔
62
                        s.Logger = logger
1✔
63
                }
1✔
64
        }
65
}
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