Skip to content

Commit

Permalink
Improve GoDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Mar 15, 2023
1 parent 14c2b96 commit 860e334
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions collectors/collector_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Collector interface {
Collect(context.Context, chan<- prometheus.Metric) (err error)
}

// CollectorManager is a collector that executes all other collectors
type CollectorManager struct {
collectors map[string]Collector
scrapeDurations *prometheus.SummaryVec
Expand All @@ -41,6 +42,8 @@ func getCollectors(client logstashclient.Client) map[string]Collector {
return collectors
}

// Collect executes all collectors and sends the collected metrics to the provided channel.
// It also sends the duration of the collection to the scrapeDurations collector.
func (manager *CollectorManager) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithTimeout(context.Background(), config.HttpTimeout)

Expand Down
1 change: 1 addition & 0 deletions collectors/nodeinfo/nodeinfo_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kuskoman/logstash-exporter/prometheus_helper"
)

// NodestatsCollector is a custom collector for the /_node/stats endpoint
type NodestatsCollector struct {
client logstashclient.Client

Expand Down
1 change: 1 addition & 0 deletions collectors/nodestats/nodestats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
namespace = config.PrometheusNamespace
)

// NodestatsCollector is a custom collector for the /_node/stats endpoint
type NodestatsCollector struct {
client logstashclient.Client
pipelineSubcollector *PipelineSubcollector
Expand Down
3 changes: 3 additions & 0 deletions collectors/nodestats/pipeline_subcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/kuskoman/logstash-exporter/prometheus_helper"
)

// PipelineSubcollector is a subcollector that collects metrics about the
// pipelines of a logstash node.
// The collector is created once for each pipeline of the node.
type PipelineSubcollector struct {
EventsOut *prometheus.Desc
EventsFiltered *prometheus.Desc
Expand Down
1 change: 1 addition & 0 deletions config/env_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/joho/godotenv"
)

// InitializeEnv loads the environment variables from the .env file
func InitializeEnv() error {
return godotenv.Load()
}
Expand Down
1 change: 1 addition & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package config

import "time"

// HttpTimeout is the timeout for http requests utilized by multiple contexts
const HttpTimeout = time.Second * 2
6 changes: 6 additions & 0 deletions prometheus_helper/prometheus_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ type SimpleDescHelper struct {
Subsystem string
}

// NewDesc creates a new prometheus.Desc with the namespace and subsystem. The help text is set to the name.
func (h *SimpleDescHelper) NewDesc(name string) *prometheus.Desc {
help := name
return prometheus.NewDesc(prometheus.BuildFQName(h.Namespace, h.Subsystem, name), help, nil, nil)
}

// NewDescWithHelp creates a new prometheus.Desc with the namespace and subsystem.
func (h *SimpleDescHelper) NewDescWithHelp(name string, help string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(h.Namespace, h.Subsystem, name), help, nil, nil)
}

// NewDescWithLabel creates a new prometheus.Desc with the namespace and subsystem.
// Labels are used to differentiate between different sources of the same metric.
func (h *SimpleDescHelper) NewDescWithHelpAndLabel(name, help, label string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(h.Namespace, h.Subsystem, name), help, []string{label}, nil)
}

// ExtractFqName extracts the fqName from a prometheus.Desc string.
// This is useful for testing collectors.
func ExtractFqName(metric string) (string, error) {
regex := regexp.MustCompile(`fqName:\s*"([a-zA-Z_-]+)"`)
matches := regex.FindStringSubmatch(metric)
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// NewAppServer creates a new http server with the given host and port
// and registers the prometheus handler and the healthcheck handler
// to the server's mux. The prometheus handler is managed under the
// hood by the prometheus client library.
func NewAppServer(host, port string) *http.Server {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
Expand Down

0 comments on commit 860e334

Please sign in to comment.