Skip to content

Commit f4cc6db

Browse files
authored
Added more options to monitoring; Waiting for local scrape first. (#12)
Signed-off-by: Bartlomiej Plotka <[email protected]>
1 parent b28b6c2 commit f4cc6db

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ if err != nil {
123123
This will start Prometheus with automatic discovery for every new and old instrumented runnables being scraped. It also runs cadvisor that monitors docker itself if `env.DockerEnvironment` is started and show generic performance metrics per container (e.g `container_memory_rss`). Run `OpenUserInterfaceInBrowser()` to open Prometheus UI in browser.
124124

125125
```go mdox-exec="sed -n '86,89p' examples/thanos/standalone.go"
126+
}
126127
// Open monitoring page with all metrics.
127128
if err := mon.OpenUserInterfaceInBrowser(); err != nil {
128129
return errors.Wrap(err, "open monitoring UI in browser")
129-
}
130130
```
131131
132132
To see how it works in practice run our example code in [standalone.go](examples/thanos/standalone.go) by running `make run-example`. At the end, three UIs should show in your browser. Thanos one, monitoring (Prometheus) one and tracing (Jaeger) one. In monitoring UI you can then e.g query docker container metrics using `container_memory_working_set_bytes{id!="/"}` metric e.g:

examples/thanos/standalone.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func deployWithMonitoring(ctx context.Context) error {
4444
}).
4545
Init(e2e.StartOptions{Image: "jaegertracing/all-in-one:1.25"})
4646

47-
jaegerConfig := fmt.Sprintf(`type: JAEGER
47+
jaegerConfig := fmt.Sprintf(
48+
`type: JAEGER
4849
config:
4950
service_name: thanos
5051
sampler_type: const
@@ -98,19 +99,6 @@ config:
9899
return nil
99100
}
100101

101-
//func Heap(dir string) (err error) {
102-
// if err := os.MkdirAll(dir, os.ModePerm); err != nil {
103-
// return err
104-
// }
105-
//
106-
// f, err := os.Create(filepath.Join(dir, "mem.pprof"))
107-
// if err != nil {
108-
// return err
109-
// }
110-
// defer errcapture.Do(&err, f.Close, "close")
111-
// return pprof.WriteHeapProfile(f)
112-
//}
113-
114102
// In order to run it, invoke make run-example from repo root or just go run it.
115103
func main() {
116104
g := &run.Group{}

monitoring/monitoring.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"path/filepath"
1212
"strings"
13+
"sync"
1314
"time"
1415

1516
"github.com/containerd/cgroups"
@@ -35,7 +36,8 @@ type Service struct {
3536
type listener struct {
3637
p *e2edb.Prometheus
3738

38-
localAddr string
39+
localAddr string
40+
scrapeInterval time.Duration
3941
}
4042

4143
func (l *listener) updateConfig(started map[string]e2e.Instrumented) error {
@@ -44,7 +46,7 @@ func (l *listener) updateConfig(started map[string]e2e.Instrumented) error {
4446
cfg := promconfig.Config{
4547
GlobalConfig: promconfig.GlobalConfig{
4648
ExternalLabels: map[model.LabelName]model.LabelValue{"prometheus": model.LabelValue(l.p.Name())},
47-
ScrapeInterval: model.Duration(15 * time.Second),
49+
ScrapeInterval: model.Duration(l.scrapeInterval),
4850
},
4951
}
5052

@@ -106,6 +108,7 @@ func (l *listener) OnRunnableChange(started []e2e.Runnable) error {
106108

107109
type opt struct {
108110
currentProcessAsContainer bool
111+
scrapeInterval time.Duration
109112
}
110113

111114
// WithCurrentProcessAsContainer makes Start put current process PID into cgroups and organize
@@ -117,12 +120,19 @@ func WithCurrentProcessAsContainer() func(*opt) {
117120
}
118121
}
119122

123+
// WithScrapeInterval changes how often metrics are scrape by Prometheus. 5s by default.
124+
func WithScrapeInterval(interval time.Duration) func(*opt) {
125+
return func(o *opt) {
126+
o.scrapeInterval = interval
127+
}
128+
}
129+
120130
type Option func(*opt)
121131

122132
// Start deploys monitoring service which deploys Prometheus that monitors all registered InstrumentedServices
123133
// in environment.
124134
func Start(env e2e.Environment, opts ...Option) (_ *Service, err error) {
125-
opt := opt{}
135+
opt := opt{scrapeInterval: 5 * time.Second}
126136
for _, o := range opts {
127137
o(&opt)
128138
}
@@ -136,7 +146,13 @@ func Start(env e2e.Environment, opts ...Option) (_ *Service, err error) {
136146
)
137147

138148
m := http.NewServeMux()
139-
m.Handle("/metrics", promhttp.HandlerFor(metrics, promhttp.HandlerOpts{}))
149+
h := promhttp.HandlerFor(metrics, promhttp.HandlerOpts{})
150+
o := sync.Once{}
151+
scraped := make(chan struct{})
152+
m.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
153+
o.Do(func() { close(scraped) })
154+
h.ServeHTTP(w, req)
155+
}))
140156

141157
// Listen on all addresses, since we need to connect to it from docker container.
142158
list, err := net.Listen("tcp", "0.0.0.0:0")
@@ -154,7 +170,7 @@ func Start(env e2e.Environment, opts ...Option) (_ *Service, err error) {
154170
if err != nil {
155171
return nil, err
156172
}
157-
l := &listener{p: p, localAddr: net.JoinHostPort(env.HostAddr(), port)}
173+
l := &listener{p: p, localAddr: net.JoinHostPort(env.HostAddr(), port), scrapeInterval: opt.scrapeInterval}
158174
if err := l.updateConfig(map[string]e2e.Instrumented{}); err != nil {
159175
return nil, err
160176
}
@@ -172,7 +188,18 @@ func Start(env e2e.Environment, opts ...Option) (_ *Service, err error) {
172188
if err := newCadvisor(env, "cadvisor", path...).Start(); err != nil {
173189
return nil, err
174190
}
175-
return &Service{p: p}, e2e.StartAndWaitReady(p)
191+
192+
if err := e2e.StartAndWaitReady(p); err != nil {
193+
return nil, err
194+
}
195+
196+
select {
197+
case <-time.After(2 * time.Minute):
198+
return nil, errors.New("Prometheus failed to scrape local endpoint after 2 minutes, check monitoring Prometheus logs")
199+
case <-scraped:
200+
}
201+
202+
return &Service{p: p}, nil
176203
}
177204

178205
func (s *Service) OpenUserInterfaceInBrowser() error {

0 commit comments

Comments
 (0)