Skip to content

Commit

Permalink
Add nodestats collector
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Feb 11, 2023
1 parent 0e8cc6c commit fe9e2a1
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
2 changes: 1 addition & 1 deletion collector/collector.go → collectors/collector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package collector
package collectors

import "github.com/prometheus/client_golang/prometheus"

Expand Down
113 changes: 113 additions & 0 deletions collectors/nodestats/nodeinfo_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package nodestats

import (
"strconv"

"github.com/kuskoman/logstash-exporter/config"
logstashclient "github.com/kuskoman/logstash-exporter/fetcher/logstash_client"
"github.com/kuskoman/logstash-exporter/helpers"
"github.com/prometheus/client_golang/prometheus"

"log"
)

type NodestatsCollector struct {
client *logstashclient.Client

NodeInfos *prometheus.Desc
BuildInfos *prometheus.Desc

PipelineWorkers *prometheus.Desc
PipelineBatchSize *prometheus.Desc
PipelineBatchDelay *prometheus.Desc

Status *prometheus.Desc
}

func NewNodestatsCollector(client *logstashclient.Client) *NodestatsCollector {
const subsystem = "info"
namespace := config.PrometheusNamespace
descHelper := helpers.SimpleDescHelper{Namespace: namespace, Subsystem: subsystem}

return &NodestatsCollector{
client: client,
NodeInfos: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "node"),
"A metric with a constant '1' value labeled by node name, version, host, http_address, and id.",
[]string{"name", "version", "http_address", "host", "id"},
nil,
),
BuildInfos: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "build"),
"A metric with a constant '1' value labeled by build date, sha, and snapshot.",
[]string{"date", "sha", "snapshot"},
nil,
),

PipelineWorkers: descHelper.NewDesc("pipeline_workers"),
PipelineBatchSize: descHelper.NewDesc("pipeline_batch_size"),
PipelineBatchDelay: descHelper.NewDesc("pipeline_batch_delay"),

Status: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "status"),
"A metric with a constant '1' value labeled by status.",
[]string{"status"},
nil,
),
}
}

func (c *NodestatsCollector) Collect(ch chan<- prometheus.Metric) error {
nodeInfo, err := c.client.GetNodeInfo()
if err != nil {
log.Printf("Error while fetching node info: %s", err)
return err
}

ch <- prometheus.MustNewConstMetric(
c.NodeInfos,
prometheus.CounterValue,
float64(1),
nodeInfo.Name,
nodeInfo.Version,
nodeInfo.Host,
nodeInfo.HTTPAddress,
nodeInfo.ID,
)

ch <- prometheus.MustNewConstMetric(
c.BuildInfos,
prometheus.CounterValue,
float64(1),
nodeInfo.BuildDate,
nodeInfo.BuildSHA,
strconv.FormatBool(nodeInfo.BuildSnapshot),
)

ch <- prometheus.MustNewConstMetric(
c.PipelineWorkers,
prometheus.CounterValue,
float64(nodeInfo.Pipeline.Workers),
)

ch <- prometheus.MustNewConstMetric(
c.PipelineBatchSize,
prometheus.CounterValue,
float64(nodeInfo.Pipeline.BatchSize),
)

ch <- prometheus.MustNewConstMetric(
c.PipelineBatchDelay,
prometheus.CounterValue,
float64(nodeInfo.Pipeline.BatchDelay),
)

ch <- prometheus.MustNewConstMetric(
c.Status,
prometheus.CounterValue,
float64(1),
nodeInfo.Status,
)

return nil
}
3 changes: 3 additions & 0 deletions config/prometheus_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

const PrometheusNamespace = "logstash"
13 changes: 13 additions & 0 deletions helpers/prometheus_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package helpers

import "github.com/prometheus/client_golang/prometheus"

type SimpleDescHelper struct {
Namespace string
Subsystem string
}

func (h *SimpleDescHelper) NewDesc(name string) *prometheus.Desc {
help := name
return prometheus.NewDesc(prometheus.BuildFQName(h.Namespace, h.Subsystem, name), help, nil, nil)
}

0 comments on commit fe9e2a1

Please sign in to comment.