This commit is contained in:
crusader 2017-09-27 22:55:29 +09:00
parent bb91b856de
commit b4a7f6fabe
3 changed files with 98 additions and 10 deletions

View File

@ -0,0 +1,5 @@
package module
const (
ProbeHeader_ProbeKey = "overFlow-Probe-Key"
)

View File

@ -161,7 +161,10 @@ func main() {
return
}
handler = probe.New(confDir)
if handler, err = probe.New(confDir); nil != err {
logging.Logger.Error(fmt.Sprintf("Probe: error: %v", err))
return
}
if err := handler.Serve(); err != nil {
logging.Logger.Error(fmt.Sprintf("Probe: error: %v", err))
return

View File

@ -2,32 +2,112 @@ package probe
import (
"context"
"errors"
"fmt"
"net/http"
"path"
lfcc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/central/api/module"
"git.loafle.net/overflow/overflow_probes/central/client"
"git.loafle.net/overflow/overflow_probes/commons"
"git.loafle.net/overflow/overflow_probes/config"
opuu "git.loafle.net/overflow/overflow_probes/util/url"
)
func New(configDir string) Probe {
p := &probe{
configDir: configDir,
}
return p
}
const (
probeEntryPoint = "/probe"
metricsEntryPoint = "/metrics"
)
type Probe interface {
commons.Handler
}
type probe struct {
configDir string
configDir string
probeConfigPath string
probeConfig config.ProbeConfig
probeEntryURL string
metricsEntryURL string
probeClient client.Client
metricsClient client.Client
shutdownChan chan bool
}
func New(configDir string) (Probe, error) {
p := &probe{
configDir: configDir,
shutdownChan: make(chan bool),
}
var err error
if p.probeEntryURL, err = opuu.Join(config.Config.Central.URL, probeEntryPoint); nil != err {
return nil, err
}
if p.metricsEntryURL, err = opuu.Join(config.Config.Central.URL, metricsEntryPoint); nil != err {
return nil, err
}
p.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
conf := lfcc.New()
if !lfcc.Exists(p.probeConfigPath) {
return nil, fmt.Errorf("Probe: Config file[%s] is not exist", p.probeConfigPath)
}
if err = conf.Load(&p.probeConfig, p.probeConfigPath); nil != err {
return nil, fmt.Errorf("Probe: Loading of Probe config file[%s] failed error[%v]", p.probeConfigPath, err)
}
return p, nil
}
func (p *probe) Serve() error {
var err error
return nil
if err = p.connectToCentral(); nil != err {
return err
}
ListenLoop:
for {
select {
case <-p.shutdownChan:
err = errors.New("Shutting down")
break ListenLoop
}
}
return err
}
func (p *probe) Shutdown(ctx context.Context) error {
p.shutdownChan <- true
return nil
}
func (p *probe) connectToCentral() error {
header := http.Header{}
header[module.ProbeHeader_ProbeKey] = []string{*p.probeConfig.ID}
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))
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))
return nil
}