MCPcopy Create free account
hub / github.com/efficientgo/e2e / Start

Function Start

monitoring/monitoring.go:234–301  ·  view source on GitHub ↗

Start deploys monitoring service which deploys Prometheus that monitors all InstrumentedRunnable instances in an environment created with AsInstrumented.

(env e2e.Environment, opts ...Option)

Source from the content-addressed store, hash-verified

232// Start deploys monitoring service which deploys Prometheus that monitors all
233// InstrumentedRunnable instances in an environment created with AsInstrumented.
234func Start(env e2e.Environment, opts ...Option) (_ *Service, err error) {
235 opt := opt{
236 scrapeInterval: 5 * time.Second,
237 useCadvisor: true,
238 }
239 for _, o := range opts {
240 o(&opt)
241 }
242
243 // Expose metrics from the current process.
244 metrics := opt.customRegistry
245 if metrics == nil {
246 metrics = prometheus.NewRegistry()
247 metrics.MustRegister(
248 collectors.NewGoCollector(),
249 collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
250 )
251 }
252
253 m := http.NewServeMux()
254 h := promhttp.HandlerFor(metrics, promhttp.HandlerOpts{})
255 o := sync.Once{}
256 scraped := make(chan struct{})
257 m.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
258 o.Do(func() { close(scraped) })
259 h.ServeHTTP(w, req)
260 }))
261
262 // Listen on all addresses, since we need to connect to it from docker container.
263 list, err := net.Listen("tcp", "0.0.0.0:0")
264 if err != nil {
265 return nil, err
266 }
267 s := http.Server{Handler: m}
268
269 go func() { _ = s.Serve(list) }()
270 env.AddCloser(func() { _ = s.Close() })
271
272 p := NewPrometheus(env, "monitoring", opt.customPromImage, nil)
273
274 _, port, err := net.SplitHostPort(list.Addr().String())
275 if err != nil {
276 return nil, err
277 }
278 l := &listener{p: p, localAddr: net.JoinHostPort(env.HostAddr(), port), scrapeInterval: opt.scrapeInterval}
279 if err := l.updateConfig(map[string]Instrumented{}); err != nil {
280 return nil, err
281 }
282 env.AddListener(l)
283
284 if opt.useCadvisor {
285 c := newCadvisor(env, "cadvisor")
286 if err := e2e.StartAndWaitReady(c); err != nil {
287 return nil, errors.Wrap(err, "starting cadvisor and waiting until ready")
288 }
289 }
290 if err := e2e.StartAndWaitReady(p); err != nil {
291 return nil, errors.Wrap(err, "starting monitoring and waiting until ready")

Callers

nothing calls this directly

Calls 9

updateConfigMethod · 0.95
StartAndWaitReadyFunction · 0.92
newCadvisorFunction · 0.85
NewPrometheusFunction · 0.70
AddCloserMethod · 0.65
CloseMethod · 0.65
HostAddrMethod · 0.65
AddListenerMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…