mirror of
https://github.com/dragonheim/gagent.git
synced 2025-04-25 14:38:59 -07:00
Extending prometheus support.
This commit is contained in:
parent
16d40fd93a
commit
8a7842e6bc
4 changed files with 62 additions and 37 deletions
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
ioutil "io/ioutil"
|
ioutil "io/ioutil"
|
||||||
log "log"
|
log "log"
|
||||||
|
http "net/http"
|
||||||
os "os"
|
os "os"
|
||||||
sync "sync"
|
sync "sync"
|
||||||
time "time"
|
time "time"
|
||||||
|
@ -13,17 +15,25 @@ import (
|
||||||
gr "git.dragonheim.net/dragonheim/gagent/internal/router"
|
gr "git.dragonheim.net/dragonheim/gagent/internal/router"
|
||||||
gw "git.dragonheim.net/dragonheim/gagent/internal/worker"
|
gw "git.dragonheim.net/dragonheim/gagent/internal/worker"
|
||||||
|
|
||||||
|
cty "github.com/zclconf/go-cty/cty"
|
||||||
|
|
||||||
docopt "github.com/aviddiviner/docopt-go"
|
docopt "github.com/aviddiviner/docopt-go"
|
||||||
|
|
||||||
hclsimple "github.com/hashicorp/hcl/v2/hclsimple"
|
hclsimple "github.com/hashicorp/hcl/v2/hclsimple"
|
||||||
hclwrite "github.com/hashicorp/hcl/v2/hclwrite"
|
hclwrite "github.com/hashicorp/hcl/v2/hclwrite"
|
||||||
|
|
||||||
logutils "github.com/hashicorp/logutils"
|
logutils "github.com/hashicorp/logutils"
|
||||||
|
|
||||||
|
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
uuid "github.com/jakehl/goid"
|
uuid "github.com/jakehl/goid"
|
||||||
cty "github.com/zclconf/go-cty/cty"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
semVER = "0.0.2"
|
semVER = "0.0.2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,6 +59,8 @@ func main() {
|
||||||
}
|
}
|
||||||
log.SetOutput(filter)
|
log.SetOutput(filter)
|
||||||
|
|
||||||
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
var config gs.GagentConfig
|
var config gs.GagentConfig
|
||||||
config.File = "/etc/gagent/gagent.hcl"
|
config.File = "/etc/gagent/gagent.hcl"
|
||||||
|
|
||||||
|
@ -135,6 +147,13 @@ func main() {
|
||||||
config.File = opts["--config"].(string)
|
config.File = opts["--config"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Start Prometheus metrics exporter
|
||||||
|
*/
|
||||||
|
go func() {
|
||||||
|
http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ClientPort), nil)
|
||||||
|
}()
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Let the command line mode override the configuration.
|
* Let the command line mode override the configuration.
|
||||||
*/
|
*/
|
||||||
|
@ -162,13 +181,6 @@ func main() {
|
||||||
|
|
||||||
switch config.Mode {
|
switch config.Mode {
|
||||||
case "client":
|
case "client":
|
||||||
/*
|
|
||||||
* Client mode will send an agent file to a router for processing
|
|
||||||
* Clients do not process the agent files, only send them as
|
|
||||||
* requests to a router. If started without arguments, the client
|
|
||||||
* will contact the router and attempt to retrieve the results
|
|
||||||
* of it's most recent request.
|
|
||||||
*/
|
|
||||||
log.Printf("[INFO] Running in client mode\n")
|
log.Printf("[INFO] Running in client mode\n")
|
||||||
|
|
||||||
if len(config.Routers) == 0 {
|
if len(config.Routers) == 0 {
|
||||||
|
@ -193,13 +205,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "router":
|
case "router":
|
||||||
/*
|
|
||||||
* The 'router' processes routing requests from the agent. The router does
|
|
||||||
* not handle any of the agent activities beyond processing the agent's
|
|
||||||
* list of tags and passing the agent and it's storage to either a member
|
|
||||||
* or client node. Tags are used by the agent to give hints as to where
|
|
||||||
* it should be routed.
|
|
||||||
*/
|
|
||||||
log.Printf("[INFO] Running in router mode\n")
|
log.Printf("[INFO] Running in router mode\n")
|
||||||
|
|
||||||
if len(config.Workers) == 0 {
|
if len(config.Workers) == 0 {
|
||||||
|
@ -211,12 +216,6 @@ func main() {
|
||||||
go gr.Main(&wg, config)
|
go gr.Main(&wg, config)
|
||||||
|
|
||||||
case "worker":
|
case "worker":
|
||||||
/*
|
|
||||||
* The 'worker' processes the agent code. The worker nodes do not know
|
|
||||||
* anything about the network structure. Instead they know only to which
|
|
||||||
* router(s) they are connected. The worker will execute the agent code and
|
|
||||||
* pass the agent and it's results to a router.
|
|
||||||
*/
|
|
||||||
log.Printf("[INFO] Running in worker mode\n")
|
log.Printf("[INFO] Running in worker mode\n")
|
||||||
|
|
||||||
if len(config.Routers) == 0 {
|
if len(config.Routers) == 0 {
|
||||||
|
|
|
@ -12,7 +12,13 @@ import (
|
||||||
zmq "github.com/pebbe/zmq4"
|
zmq "github.com/pebbe/zmq4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Main is the initiation function for a Client
|
/*
|
||||||
|
Client mode will send an agent file to a router for processing
|
||||||
|
Clients do not process the agent files, only send them as
|
||||||
|
requests to a router. If started without arguments, the client
|
||||||
|
will contact the router and attempt to retrieve the results
|
||||||
|
of it's most recent request.
|
||||||
|
*/
|
||||||
func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int, agent string) {
|
func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int, agent string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
log.Printf("[INFO] Starting client\n")
|
log.Printf("[INFO] Starting client\n")
|
||||||
|
|
|
@ -8,7 +8,8 @@ import (
|
||||||
|
|
||||||
gs "git.dragonheim.net/dragonheim/gagent/internal/gstructs"
|
gs "git.dragonheim.net/dragonheim/gagent/internal/gstructs"
|
||||||
|
|
||||||
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
|
prometheus "github.com/prometheus/client_golang/prometheus"
|
||||||
|
promauto "github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
|
|
||||||
zmq "github.com/pebbe/zmq4"
|
zmq "github.com/pebbe/zmq4"
|
||||||
)
|
)
|
||||||
|
@ -18,10 +19,21 @@ const (
|
||||||
WORKER_READY = "\001" // Signals worker is ready
|
WORKER_READY = "\001" // Signals worker is ready
|
||||||
)
|
)
|
||||||
|
|
||||||
// Main is the initiation function for a Router
|
var (
|
||||||
|
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "client_requests_recieved",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
The 'router' processes routing requests from the agent. The router does
|
||||||
|
not handle any of the agent activities beyond processing the agent's
|
||||||
|
list of tags and passing the agent and it's storage to either a member
|
||||||
|
or client node. Tags are used by the agent to give hints as to where
|
||||||
|
it should be routed.
|
||||||
|
*/
|
||||||
func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
|
||||||
http.HandleFunc("/hello", answerClient)
|
http.HandleFunc("/hello", answerClient)
|
||||||
|
|
||||||
log.Printf("[INFO] Starting router\n")
|
log.Printf("[INFO] Starting router\n")
|
||||||
|
@ -31,13 +43,8 @@ func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
||||||
|
|
||||||
workerSock, _ := zmq.NewSocket(zmq.DEALER)
|
workerSock, _ := zmq.NewSocket(zmq.DEALER)
|
||||||
defer workerSock.Close()
|
defer workerSock.Close()
|
||||||
|
|
||||||
workerListener := fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.WorkerPort)
|
workerListener := fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.WorkerPort)
|
||||||
clientListener := fmt.Sprintf("%s:%d", config.ListenAddr, config.ClientPort)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
http.ListenAndServe(clientListener, nil)
|
|
||||||
}()
|
|
||||||
|
|
||||||
workerSock.Bind(workerListener)
|
workerSock.Bind(workerListener)
|
||||||
|
|
||||||
workers := make([]string, 0)
|
workers := make([]string, 0)
|
||||||
|
@ -106,8 +113,8 @@ func unwrap(msg []string) (head string, tail []string) {
|
||||||
|
|
||||||
func answerClient(w http.ResponseWriter, r *http.Request) {
|
func answerClient(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
fmt.Fprintf(w, "%v\n", r)
|
// fmt.Fprintf(w, "%v\n", r)
|
||||||
// http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,6 +122,7 @@ func answerClient(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
|
opsProcessed.Inc()
|
||||||
fmt.Fprintf(w, "%v\n", r)
|
fmt.Fprintf(w, "%v\n", r)
|
||||||
// Handle the GET request...
|
// Handle the GET request...
|
||||||
|
|
||||||
|
|
|
@ -9,26 +9,38 @@ import (
|
||||||
gs "git.dragonheim.net/dragonheim/gagent/internal/gstructs"
|
gs "git.dragonheim.net/dragonheim/gagent/internal/gstructs"
|
||||||
|
|
||||||
// picol "git.dragonheim.net/dragonheim/gagent/src/picol"
|
// picol "git.dragonheim.net/dragonheim/gagent/src/picol"
|
||||||
|
|
||||||
|
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
zmq "github.com/pebbe/zmq4"
|
zmq "github.com/pebbe/zmq4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Main is the initiation function for a Worker
|
/*
|
||||||
|
The "worker" processes the agent code. The worker nodes do not know
|
||||||
|
anything about the network structure. Instead they know only to which
|
||||||
|
router(s) they are connected. The worker will execute the agent code and
|
||||||
|
pass the agent and it's results to a router.
|
||||||
|
*/
|
||||||
func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int) {
|
func Main(wg *sync.WaitGroup, config gs.GagentConfig, rid int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
log.Printf("[INFO] Starting worker\n")
|
log.Printf("[INFO] Starting worker\n")
|
||||||
|
|
||||||
// Generate connect string for this router.
|
// Generate connect string for this router.
|
||||||
var rport = int64(config.WorkerPort)
|
var rport = config.WorkerPort
|
||||||
if config.Routers[rid].WorkerPort != 0 {
|
if config.Routers[rid].WorkerPort != 0 {
|
||||||
rport = config.Routers[rid].WorkerPort
|
rport = config.Routers[rid].WorkerPort
|
||||||
}
|
}
|
||||||
connectString := fmt.Sprintf("tcp://%s:%d", config.Routers[rid].RouterAddr, rport)
|
connectString := fmt.Sprintf("tcp://%s:%d", config.Routers[rid].RouterAddr, rport)
|
||||||
|
// workerListener := fmt.Sprintf("tcp://%s:%d", config.ListenAddr, config.WorkerPort)
|
||||||
|
clientListener := fmt.Sprintf("%s:%d", config.ListenAddr, config.ClientPort)
|
||||||
|
|
||||||
subscriber, _ := zmq.NewSocket(zmq.REP)
|
subscriber, _ := zmq.NewSocket(zmq.REP)
|
||||||
defer subscriber.Close()
|
defer subscriber.Close()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ClientPort), nil)
|
http.ListenAndServe(clientListener, nil)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Printf("[DEBUG] Attempting to connect to %s\n", connectString)
|
log.Printf("[DEBUG] Attempting to connect to %s\n", connectString)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue