From f32dafc806de27f8786534fac7aab128747ccf63 Mon Sep 17 00:00:00 2001 From: postmannen Date: Mon, 12 Apr 2021 15:35:20 +0200 Subject: [PATCH] Added metric for running processes by name --- example/toShip1-REQHttpGet.json | 2 +- process.go | 4 ++++ server.go | 8 ++++++++ subscriber_method_types.go | 5 +++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/example/toShip1-REQHttpGet.json b/example/toShip1-REQHttpGet.json index d919119..25cef86 100644 --- a/example/toShip1-REQHttpGet.json +++ b/example/toShip1-REQHttpGet.json @@ -1,6 +1,6 @@ [ { - "directory": "metrics/network/sniffer", + "directory": "vg-sin-hjemmeside", "fileExtension": ".html", "toNode": "ship1", "data": ["http://vg.no"], diff --git a/process.go b/process.go index 01a7c11..bbc6b6b 100644 --- a/process.go +++ b/process.go @@ -9,6 +9,7 @@ import ( "time" "github.com/nats-io/nats.go" + "github.com/prometheus/client_golang/prometheus" ) // processKind are either kindSubscriber or kindPublisher, and are @@ -153,6 +154,9 @@ func (p process) spawnWorker(procs *processes, natsConn *nats.Conn) { pn = processNameGet(p.subject.name(), processKindSubscriber) } + processName := processNameGet(p.subject.name(), p.processKind) + p.processes.promProcessesVec.With(prometheus.Labels{"processName": string(processName)}) + // Start a publisher worker, which will start a go routine (process) // That will take care of all the messages for the subject it owns. if p.processKind == processKindPublisher { diff --git a/server.go b/server.go index c92e13f..83e24a2 100644 --- a/server.go +++ b/server.go @@ -32,6 +32,8 @@ type processes struct { lastProcessID int // promTotalProcesses prometheus.Gauge + // + promProcessesVec *prometheus.GaugeVec } // newProcesses will prepare and return a *processes @@ -45,6 +47,12 @@ func newProcesses(promRegistry *prometheus.Registry) *processes { Help: "The current number of total running processes", }) + p.promProcessesVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ + Name: "running_process", + Help: "Name of the running process", + }, []string{"processName"}, + ) + return &p } diff --git a/subscriber_method_types.go b/subscriber_method_types.go index 9254eea..3c5694a 100644 --- a/subscriber_method_types.go +++ b/subscriber_method_types.go @@ -43,6 +43,8 @@ import ( "os/exec" "path/filepath" "time" + + "github.com/prometheus/client_golang/prometheus" ) // Method is used to specify the actual function/method that @@ -351,6 +353,9 @@ func (m methodREQOpCommand) handler(proc process, message Message, nodeName stri log.Printf(" ** Error: failed to stop *nats.Subscription: %v\n", err) } + // Remove the prometheus label + proc.processes.promProcessesVec.Delete(prometheus.Labels{"processName": string(processName)}) + er := fmt.Errorf("info: stopProc: stoped %v on %v", sub, message.ToNode) sendErrorLogMessage(proc.toRingbufferCh, proc.node, er)