overflow_probes/probe/probe.go

97 lines
2.0 KiB
Go
Raw Normal View History

2017-09-18 09:21:58 +00:00
package probe
import (
"context"
2017-09-27 13:55:29 +00:00
"errors"
"fmt"
"net/http"
2017-09-21 11:04:30 +00:00
2017-09-27 13:55:29 +00:00
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/central/api/module"
"git.loafle.net/overflow/overflow_probes/central/client"
2017-09-21 11:04:30 +00:00
"git.loafle.net/overflow/overflow_probes/commons"
2017-09-27 13:55:29 +00:00
"git.loafle.net/overflow/overflow_probes/config"
opuu "git.loafle.net/overflow/overflow_probes/util/url"
2017-09-18 09:21:58 +00:00
)
2017-09-27 13:55:29 +00:00
const (
probeEntryPoint = "/probe"
metricsEntryPoint = "/metrics"
2017-09-28 10:09:33 +00:00
fileEntryPoint = "/file"
2017-09-27 13:55:29 +00:00
)
2017-09-18 09:21:58 +00:00
type Probe interface {
2017-09-21 11:04:30 +00:00
commons.Handler
2017-09-18 09:21:58 +00:00
}
type probe struct {
2017-09-27 13:55:29 +00:00
probeEntryURL string
metricsEntryURL string
probeClient client.Client
metricsClient client.Client
shutdownChan chan bool
}
2017-09-28 10:09:33 +00:00
func New() (Probe, error) {
2017-09-27 13:55:29 +00:00
p := &probe{
shutdownChan: make(chan bool),
}
var err error
2017-09-28 10:09:33 +00:00
if p.probeEntryURL, err = opuu.Join(*config.CFG.Central.URL, probeEntryPoint); nil != err {
2017-09-27 13:55:29 +00:00
return nil, err
}
2017-09-28 10:09:33 +00:00
if p.metricsEntryURL, err = opuu.Join(*config.CFG.Central.URL, metricsEntryPoint); nil != err {
2017-09-27 13:55:29 +00:00
return nil, err
}
return p, nil
2017-09-18 09:21:58 +00:00
}
2017-09-22 09:20:07 +00:00
func (p *probe) Serve() error {
2017-09-27 13:55:29 +00:00
var err error
2017-09-18 09:21:58 +00:00
2017-09-27 13:55:29 +00:00
if err = p.connectToCentral(); nil != err {
return err
}
2017-09-28 10:09:33 +00:00
// ListenLoop:
2017-09-27 13:55:29 +00:00
for {
select {
case <-p.shutdownChan:
2017-09-28 10:09:33 +00:00
return errors.New("Shutting down")
2017-09-27 13:55:29 +00:00
}
}
2017-09-28 10:09:33 +00:00
return nil
2017-09-18 09:21:58 +00:00
}
func (p *probe) Shutdown(ctx context.Context) error {
2017-09-27 13:55:29 +00:00
p.shutdownChan <- true
return nil
}
func (p *probe) connectToCentral() error {
header := http.Header{}
2017-09-28 10:09:33 +00:00
header[module.ProbeHeader_ProbeKey] = []string{*config.CFG.Probe.Key}
2017-09-27 13:55:29 +00:00
var res *http.Response
var err error
p.probeClient = client.New()
if res, err = p.probeClient.Dial(p.probeEntryURL, header, 4096, 4096); nil != err {
return err
}
logging.Logger.Debug(fmt.Sprintf("Probe: Connect Probe HTTP Status[%s]", res.Status))
2017-09-28 06:08:43 +00:00
p.probeClient.OnNotify(p.onNotify)
2017-09-27 13:55:29 +00:00
p.metricsClient = client.New()
if res, err = p.metricsClient.Dial(p.metricsEntryURL, header, 4096, 4096); nil != err {
return err
}
logging.Logger.Debug(fmt.Sprintf("Probe: Connect Metrics HTTP Status[%s]", res.Status))
2017-09-18 09:21:58 +00:00
return nil
}