2021-02-18 11:29:14 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
2021-08-03 11:57:29 +00:00
|
|
|
"fmt"
|
2021-02-22 08:33:37 +00:00
|
|
|
"net"
|
2021-02-18 11:29:14 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-08-18 10:16:21 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
2021-02-18 11:29:14 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2021-02-19 15:58:16 +00:00
|
|
|
// metrics are generally used to hold the structure around metrics
|
|
|
|
// handling
|
2021-02-18 11:29:14 +00:00
|
|
|
type metrics struct {
|
2021-08-18 13:41:53 +00:00
|
|
|
// The channel to pass metrics that should be processed.
|
2021-04-12 08:51:26 +00:00
|
|
|
promRegistry *prometheus.Registry
|
2021-08-18 13:41:53 +00:00
|
|
|
// host and port where prometheus metrics will be exported.
|
2021-02-22 08:33:37 +00:00
|
|
|
hostAndPort string
|
2021-08-18 13:57:33 +00:00
|
|
|
|
2021-09-10 08:21:33 +00:00
|
|
|
// The build version
|
|
|
|
promVersion *prometheus.GaugeVec
|
|
|
|
|
2021-08-18 13:57:33 +00:00
|
|
|
// --- Processes
|
2021-08-18 13:41:53 +00:00
|
|
|
// Prometheus metrics for total processes.
|
|
|
|
promProcessesTotal prometheus.Gauge
|
|
|
|
// Prometheus metrics for vector of process names.
|
|
|
|
promProcessesAllRunning *prometheus.GaugeVec
|
2021-08-18 13:57:33 +00:00
|
|
|
|
|
|
|
// --- Methods
|
2021-08-18 13:41:53 +00:00
|
|
|
// Prometheus metrics for number of hello nodes.
|
|
|
|
promHelloNodesTotal prometheus.Gauge
|
|
|
|
// Prometheus metrics for the vector of hello nodes.
|
|
|
|
promHelloNodesContactLast *prometheus.GaugeVec
|
2021-08-18 13:57:33 +00:00
|
|
|
|
|
|
|
// --- Ringbuffer
|
2021-08-18 13:41:53 +00:00
|
|
|
// Prometheus metrics for the last processed DB id in key
|
|
|
|
// value store.
|
2021-08-26 09:41:46 +00:00
|
|
|
promMessagesProcessedIDLast prometheus.Gauge
|
2021-08-18 13:57:33 +00:00
|
|
|
// Prometheus metrics for the total count of stalled
|
|
|
|
// messages in the ringbuffer.
|
2021-08-18 13:41:53 +00:00
|
|
|
promRingbufferStalledMessagesTotal prometheus.Counter
|
2021-08-26 08:50:40 +00:00
|
|
|
// Prometheus metrics for current messages in memory buffer.
|
2021-08-18 13:57:33 +00:00
|
|
|
promInMemoryBufferMessagesCurrent prometheus.Gauge
|
2021-08-26 08:50:40 +00:00
|
|
|
// Prometheus metrics for current messages delivered by a
|
|
|
|
// user into the system.
|
|
|
|
promUserMessagesTotal prometheus.Counter
|
2021-08-26 09:41:46 +00:00
|
|
|
// Metrics for nats messages delivered total.
|
2021-08-26 08:50:40 +00:00
|
|
|
promNatsDeliveredTotal prometheus.Counter
|
2021-08-26 09:41:46 +00:00
|
|
|
// Metrics for messages that failed to get ack replies.
|
|
|
|
promNatsMessagesFailedACKsTotal prometheus.Counter
|
|
|
|
// Metrics for messages that missed to get ack replies.
|
|
|
|
promNatsMessagesMissedACKsTotal prometheus.Counter
|
|
|
|
// Metrics for received error messages
|
|
|
|
promErrorMessagesReceivedTotal prometheus.Counter
|
|
|
|
// Metrics for sent error messages
|
|
|
|
promErrorMessagesSentTotal prometheus.Counter
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:57:33 +00:00
|
|
|
// newMetrics will prepare and return a *metrics.
|
2021-02-22 08:33:37 +00:00
|
|
|
func newMetrics(hostAndPort string) *metrics {
|
2021-08-18 10:16:21 +00:00
|
|
|
reg := prometheus.NewRegistry()
|
2021-08-18 13:57:33 +00:00
|
|
|
//prometheus.Unregister(prometheus.NewGoCollector()).
|
2021-08-18 10:16:21 +00:00
|
|
|
reg.MustRegister(collectors.NewGoCollector())
|
2021-08-18 13:57:33 +00:00
|
|
|
// prometheus.MustRegister(collectors.NewGoCollector()).
|
2021-02-18 11:29:14 +00:00
|
|
|
m := metrics{
|
2021-08-18 10:16:21 +00:00
|
|
|
promRegistry: reg,
|
2021-04-12 08:51:26 +00:00
|
|
|
hostAndPort: hostAndPort,
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 08:21:33 +00:00
|
|
|
m.promVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
Name: "steward_build_version",
|
|
|
|
Help: "Build version of steward",
|
|
|
|
}, []string{"version"},
|
|
|
|
)
|
|
|
|
m.promRegistry.MustRegister(m.promVersion)
|
|
|
|
|
2021-08-26 08:50:40 +00:00
|
|
|
m.promProcessesTotal = prometheus.NewGauge(prometheus.GaugeOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_processes_total",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "The current number of total running processes",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promProcessesTotal)
|
|
|
|
|
|
|
|
m.promProcessesAllRunning = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_processes_all_running",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "Name of the running processes",
|
|
|
|
}, []string{"processName"},
|
|
|
|
)
|
|
|
|
m.promRegistry.MustRegister(m.promProcessesAllRunning)
|
|
|
|
|
|
|
|
m.promHelloNodesTotal = prometheus.NewGauge(prometheus.GaugeOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_hello_nodes_total",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "The current number of total nodes who have said hello",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promHelloNodesTotal)
|
|
|
|
|
|
|
|
m.promHelloNodesContactLast = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_hello_node_contact_last",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "Name of the nodes who have said hello",
|
2021-09-10 08:21:33 +00:00
|
|
|
}, []string{"nodeName"})
|
2021-08-26 08:50:40 +00:00
|
|
|
m.promRegistry.MustRegister(m.promHelloNodesContactLast)
|
|
|
|
|
2021-08-26 09:41:46 +00:00
|
|
|
m.promMessagesProcessedIDLast = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "steward_messages_processed_id_last",
|
|
|
|
Help: "The last processed id in key value/store db",
|
2021-08-26 08:50:40 +00:00
|
|
|
})
|
2021-08-26 09:41:46 +00:00
|
|
|
m.promRegistry.MustRegister(m.promMessagesProcessedIDLast)
|
2021-08-26 08:50:40 +00:00
|
|
|
|
|
|
|
m.promRingbufferStalledMessagesTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_ringbuffer_stalled_messages_total",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "Number of stalled messages in ringbuffer",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promRingbufferStalledMessagesTotal)
|
|
|
|
|
|
|
|
m.promInMemoryBufferMessagesCurrent = prometheus.NewGauge(prometheus.GaugeOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_in_memory_buffer_messages_current",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "The current value of messages in memory buffer",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promInMemoryBufferMessagesCurrent)
|
|
|
|
|
|
|
|
// Register som metrics for messages delivered by users into the system.
|
|
|
|
m.promUserMessagesTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_user_messages_total",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "Number of total messages delivered by users into the system",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promUserMessagesTotal)
|
|
|
|
|
|
|
|
m.promNatsDeliveredTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
2021-08-26 09:41:46 +00:00
|
|
|
Name: "steward_nats_delivered_total",
|
2021-08-26 08:50:40 +00:00
|
|
|
Help: "Number of total messages delivered by nats",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promNatsDeliveredTotal)
|
|
|
|
|
2021-08-26 09:41:46 +00:00
|
|
|
m.promNatsMessagesFailedACKsTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "steward_nats_messages_failed_acks_total",
|
|
|
|
Help: "Number of messages that never received an ack total",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promNatsMessagesFailedACKsTotal)
|
|
|
|
|
|
|
|
m.promNatsMessagesMissedACKsTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "steward_nats_messages_missed_acks_total",
|
|
|
|
Help: "Number of messages missed receiving an ack total",
|
|
|
|
})
|
|
|
|
m.promRegistry.MustRegister(m.promNatsMessagesMissedACKsTotal)
|
|
|
|
|
|
|
|
m.promErrorMessagesReceivedTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "steward_error_messages_received_total",
|
|
|
|
Help: "Number of error messages received total",
|
|
|
|
})
|
2021-08-26 10:26:08 +00:00
|
|
|
m.promRegistry.MustRegister(m.promErrorMessagesReceivedTotal)
|
2021-08-26 09:41:46 +00:00
|
|
|
|
|
|
|
m.promErrorMessagesSentTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "steward_error_messages_sent_total",
|
|
|
|
Help: "Number of error messages sent total",
|
|
|
|
})
|
2021-08-26 10:26:08 +00:00
|
|
|
m.promRegistry.MustRegister(m.promErrorMessagesSentTotal)
|
2021-08-26 09:41:46 +00:00
|
|
|
|
2021-02-18 11:29:14 +00:00
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
2021-08-16 11:01:12 +00:00
|
|
|
// Start the http interface for Prometheus metrics.
|
2021-08-04 08:37:24 +00:00
|
|
|
func (m *metrics) start() error {
|
2021-02-18 11:29:14 +00:00
|
|
|
|
2021-02-22 08:33:37 +00:00
|
|
|
//http.Handle("/metrics", promhttp.Handler())
|
|
|
|
//http.ListenAndServe(":2112", nil)
|
2021-08-04 08:37:24 +00:00
|
|
|
n, err := net.Listen("tcp", m.hostAndPort)
|
2021-02-22 08:33:37 +00:00
|
|
|
if err != nil {
|
2021-08-03 11:57:29 +00:00
|
|
|
return fmt.Errorf("error: startMetrics: failed to open prometheus listen port: %v", err)
|
2021-02-22 08:33:37 +00:00
|
|
|
}
|
2021-08-18 10:16:21 +00:00
|
|
|
//mux := http.NewServeMux()
|
|
|
|
//mux.Handle("/metrics", promhttp.Handler())
|
2021-08-03 11:57:29 +00:00
|
|
|
|
2021-08-18 10:16:21 +00:00
|
|
|
http.Handle("/metrics", promhttp.HandlerFor(m.promRegistry, promhttp.HandlerOpts{}))
|
|
|
|
|
|
|
|
err = http.Serve(n, nil)
|
2021-08-03 11:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error: startMetrics: failed to start http.Serve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|