Skip to content

Commit

Permalink
feat: collect node state and users per service
Browse files Browse the repository at this point in the history
  • Loading branch information
theurichde committed Jun 9, 2022
1 parent 78ac8cf commit 37a818c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 40 deletions.
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module aiven-prometheus-exporter

require github.com/prometheus/client_golang v1.12.2
require (
github.com/aiven/aiven-go-client v1.7.0
github.com/prometheus/client_golang v1.12.2
github.com/sirupsen/logrus v1.6.0
)

require (
github.com/aiven/aiven-go-client v1.7.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -190,6 +191,7 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
53 changes: 19 additions & 34 deletions internal/pkg/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pkg
import (
"github.com/aiven/aiven-go-client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
)

Expand All @@ -17,14 +16,10 @@ func (ac AivenCollector) Describe(descs chan<- *prometheus.Desc) {

func (ac AivenCollector) Collect(ch chan<- prometheus.Metric) {

//https://dev.to/metonymicsmokey/custom-prometheus-metrics-with-go-520n

projects := ac.GetProjects()
numProjects.Set(float64(len(projects)))
ch <- prometheus.MustNewConstMetric(projectInfo, prometheus.GaugeValue, float64(len(projects)))

ac.processProjects(projects, ch)

}

func (ac *AivenCollector) processProjects(projects []*aiven.Project, ch chan<- prometheus.Metric) {
Expand All @@ -36,26 +31,32 @@ func (ac *AivenCollector) processProjects(projects []*aiven.Project, ch chan<- p
}

func processServices(client *aiven.Client, project *aiven.Project, ch chan<- prometheus.Metric) {

services, _ := client.Services.List(project.Name)
for _, service := range services {
log.Debug("Fetching infos for " + project.Name + " and " + service.Name)
log.Debug("Fetching service infos for " + project.Name + " and " + service.Name)
collectServiceNodeCount(ch, service, project)
collectServiceNodeStates(ch, service, project)
collectServiceUsersPerService(ch, service, project)
}
}

func collectServiceNodeCount(ch chan<- prometheus.Metric, service *aiven.Service, project *aiven.Project) {
ch <- prometheus.MustNewConstMetric(nodeCount, prometheus.GaugeValue, float64(service.NodeCount), project.Name, service.Name)
}

func collectServiceNodeStates(ch chan<- prometheus.Metric, service *aiven.Service, project *aiven.Project) {
for _, state := range service.NodeStates {
ch <- prometheus.MustNewConstMetric(nodeState, prometheus.CounterValue, float64(1), project.Name, service.Name, state.Name, state.State)
}
}

func countClustersPerProject(client *aiven.Client, project *aiven.Project, ch chan<- prometheus.Metric) {
services, _ := client.Services.List(project.Name)
//servicesPerProject.WithLabelValues(project.Name).Set(float64(len(services)))
ch <- prometheus.MustNewConstMetric(serviceInfo, prometheus.GaugeValue, float64(len(services)), project.Name)
}

func countServiceUsersPerService(client *aiven.Client, service string) {

func collectServiceUsersPerService(ch chan<- prometheus.Metric, service *aiven.Service, project *aiven.Project) {
ch <- prometheus.MustNewConstMetric(serviceUserCount, prometheus.GaugeValue, float64(len(service.Users)), project.Name, service.Name)
}

func (ac *AivenCollector) GetProjects() []*aiven.Project {
Expand All @@ -68,28 +69,12 @@ func (ac *AivenCollector) GetProjects() []*aiven.Project {
}

var (
numProjects = promauto.NewGauge(prometheus.GaugeOpts{
Name: "aiven_projects_count_total",
Help: "The total number of registered Aiven projects",
})

numServiceUsers = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "aiven_service_user_count",
}, []string{"project", "service"})

servicesPerProject = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "aiven_services_count",
}, []string{"project"})

nodeCount = prometheus.NewDesc("aiven_service_node_count", "Node Count per Service", []string{"project", "service"}, nil)

projectInfo = prometheus.NewDesc(prometheus.BuildFQName("aiven", "project", "count"),
"The number of projects registered in the account",
nil, nil)

serviceInfo = prometheus.NewDesc(
prometheus.BuildFQName("aiven", "service", "count"),
"The number of services per project",
[]string{"project"},
nil)
// Basic Info
projectInfo = prometheus.NewDesc("aiven_project_count_total", "The number of projects registered in the account", nil, nil)
serviceInfo = prometheus.NewDesc("aiven_service_count", "The number of services per project", []string{"project"}, nil)

// Service related info
nodeCount = prometheus.NewDesc("aiven_service_node_count", "Node Count per Service", []string{"project", "service"}, nil)
nodeState = prometheus.NewDesc("aiven_service_node_state", "Node State per Service", []string{"project", "service", "node_name", "state"}, nil)
serviceUserCount = prometheus.NewDesc("aiven_service_serviceuser_count", "Serviceuser Count per Service", []string{"project", "service"}, nil)
)
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ func main() {
if err != nil {
log.Fatal(err)
}
//projects := getProjects(aivenClient)
//numProjects.Set(float64(len(projects)))

exporter := AivenCollector{Client: aivenClient}
prometheus.MustRegister(exporter)
r := prometheus.NewRegistry()
r.MustRegister(exporter)

http.Handle("/metrics", promhttp.Handler())
http.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(":2112", nil))

}
Expand Down

0 comments on commit 37a818c

Please sign in to comment.