Merge pull request #184 from openziti/probe.no.exit

Ziti Probe API Session Reuse
This commit is contained in:
Andrew
2020-08-26 08:15:39 -04:00
committed by GitHub
3 changed files with 123 additions and 32 deletions
+5 -5
View File
@@ -3,9 +3,9 @@
package version
const (
Version = "v0.9.0"
Revision = "ea556fc18740"
Branch = "travis-build"
BuildUser = "Paul Lorenz"
BuildDate = "2020-02-11 16:09:08"
Version = "v0.16.2-beta07"
Revision = "local"
Branch = "local"
BuildUser = "Andrew Martinez"
BuildDate = "2020-08-24 09:00:00"
)
+83 -27
View File
@@ -2,6 +2,7 @@ package subcmd
import (
"context"
"fmt"
influxdb "github.com/influxdata/influxdb1-client"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/foundation/metrics"
@@ -32,6 +33,7 @@ const (
DefaultDbName = "ziti"
DefaultDbType = "influxdb"
DefaultInterval = 5 * 60 // 5 minutes
ProbeService = "probe-service"
)
func init() {
@@ -55,11 +57,11 @@ var defaultConfig = probeCfg{
}
type probe struct {
cfg *probeCfg
indb *influxdb.Client
ctx ziti.Context
latest atomic.Value
closer chan interface{}
cfg *probeCfg
influxDb *influxdb.Client
ctx ziti.Context
latest atomic.Value
closer chan interface{}
}
var theProbe = &probe{
@@ -80,9 +82,25 @@ func (p *probe) sendMetrics() {
return
}
bp.Database = p.cfg.dbName
_, err = p.indb.Write(*bp)
if err != nil {
logrus.Errorln(err)
_, err = p.influxDb.Write(*bp)
if err == nil {
logrus.Debug("write complete")
} else {
logrus.Errorf("error writing to influxdb: %v", err)
logrus.Info("attempting to reconnect")
if service, ok := p.ctx.GetService(ProbeService); ok {
if err := p.connectToInfluxDb(service); err == nil {
logrus.Info("reconnected, attempting to write")
if _, err = p.influxDb.Write(*bp); err != nil {
logrus.Errorf("failed to write after reconnect, giving up")
}
} else {
logrus.Errorf("failed to reconnect: %v", err)
}
} else {
logrus.Errorf("could not find %s", ProbeService)
}
}
}
}
@@ -104,28 +122,48 @@ func (p *probe) run(_ *cobra.Command, args []string) {
}
if _, err = p.ctx.GetServices(); err != nil {
log.Fatal("failed to load available services")
for {
if err = p.ctx.Authenticate(); err != nil {
sleepDuration := 5 * time.Second
log.Errorf("failed to authenticate, trying again in %.2f seconds: %v", sleepDuration.Seconds(), err)
time.Sleep(sleepDuration)
} else {
break
}
}
service, found := p.ctx.GetService("probe-service")
if !found {
log.Fatal("required service was not found")
for {
if _, err = p.ctx.GetServices(); err != nil {
sleepDuration := 5 * time.Second
log.Error("failed to load available services, try again in %.2f seconds: %v", sleepDuration.Seconds(), err)
time.Sleep(sleepDuration)
} else {
break
}
}
p.cfg = getProbeConfig(service)
p.indb, err = p.createInfluxDbClient(p.cfg)
if err != nil {
log.Error(err)
return
var service *edge.Service
for {
var found bool
service, found = p.ctx.GetService(ProbeService)
if !found {
sleepDuration := 5 * time.Second
log.Errorf("required service was not found, try again in %.2f seconds", sleepDuration.Seconds())
time.Sleep(sleepDuration)
} else {
break
}
}
_, res, err := p.indb.Ping()
if err != nil {
log.WithError(err).Fatal("failed to get server info")
for {
err := p.connectToInfluxDb(service)
if err != nil {
sleepDuration := 5 * time.Second
log.Errorf("could not connect to influxDb, trying again in %.2f seconds: %v", sleepDuration.Seconds(), err)
time.Sleep(sleepDuration)
} else {
break
}
}
log.Info("connected to influx version = ", res)
go p.sendMetrics()
@@ -136,6 +174,24 @@ func (p *probe) run(_ *cobra.Command, args []string) {
p.ctx.Close()
}
func (p *probe) connectToInfluxDb(service *edge.Service) error {
p.cfg = getProbeConfig(service)
var err error
p.influxDb, err = p.createInfluxDbClient(p.cfg)
if err != nil {
return fmt.Errorf("could not create influxDb client: %v", err)
}
_, res, err := p.influxDb.Ping()
if err != nil {
return fmt.Errorf("failed to get influxDb server info: %v", err)
}
log.Info("connected to influx version = ", res)
return nil
}
func getProbeConfig(service *edge.Service) *probeCfg {
cfg, found := service.Configs["ziti-probe-config.v1"]
if !found {
@@ -198,7 +254,7 @@ func (p *probe) createInfluxDbClient(cfg *probeCfg) (*influxdb.Client, error) {
}
influxConfig := influxdb.NewConfig()
u, _ := url.Parse("http://probe-service")
u, _ := url.Parse("http://" + ProbeService)
influxConfig.URL = *u
influxConfig.Username = cfg.dbUser
influxConfig.Password = cfg.dbPassword
@@ -210,8 +266,8 @@ func (p *probe) createInfluxDbClient(cfg *probeCfg) (*influxdb.Client, error) {
hcp := unsafe.Pointer(uintptr(unsafe.Pointer(clt)) + hcf.Offset)
hcpp := (**http.Client)(hcp)
*hcpp = httpC
httpCppClient := (**http.Client)(hcp)
*httpCppClient = httpC
return clt, nil
}
+35
View File
@@ -0,0 +1,35 @@
/*
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package subcmd
import (
"fmt"
"github.com/openziti/ziti/common/version"
"github.com/spf13/cobra"
)
func init() {
root.AddCommand(versionCmd)
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show component version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.GetBuildMetadata(verbose))
},
}