-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prometheus metrics & endpoint
- Loading branch information
Showing
308 changed files
with
36,108 additions
and
1,658 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package controllers | ||
|
||
import ( | ||
"log" | ||
|
||
"garm/auth" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type GarmCollector struct { | ||
instanceMetric *prometheus.Desc | ||
apiController *APIController | ||
} | ||
|
||
func NewGarmCollector(a *APIController) *GarmCollector { | ||
return &GarmCollector{ | ||
apiController: a, | ||
instanceMetric: prometheus.NewDesc( | ||
"garm_runner_status", | ||
"Status of the runner", | ||
[]string{"name", "status", "runner_status", "pool", "pool_type", "hostname", "controller_id"}, nil, | ||
)} | ||
} | ||
|
||
func (c *GarmCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- c.instanceMetric | ||
} | ||
|
||
func (c *GarmCollector) Collect(ch chan<- prometheus.Metric) { | ||
c.CollectInstanceMetric(ch) | ||
} | ||
|
||
// CollectInstanceMetric collects the metrics for the runner instances | ||
// reflecting the statuses and the pool they belong to. | ||
func (c *GarmCollector) CollectInstanceMetric(ch chan<- prometheus.Metric) { | ||
|
||
ctx := auth.GetAdminContext() | ||
|
||
instances, err := c.apiController.r.ListAllInstances(ctx) | ||
if err != nil { | ||
log.Printf("cannot collect metrics, listing instances: %s", err) | ||
return | ||
} | ||
|
||
pools, err := c.apiController.r.ListAllPools(ctx) | ||
if err != nil { | ||
log.Printf("listing pools: %s", err) | ||
// continue anyway | ||
} | ||
|
||
type poolInfo struct { | ||
Name string | ||
Type string | ||
} | ||
|
||
poolNames := make(map[string]poolInfo) | ||
for _, pool := range pools { | ||
if pool.EnterpriseName != "" { | ||
poolNames[pool.ID] = poolInfo{ | ||
Name: pool.EnterpriseName, | ||
Type: "enterprise", | ||
} | ||
} else if pool.OrgName != "" { | ||
poolNames[pool.ID] = poolInfo{ | ||
Name: pool.OrgName, | ||
Type: "organization", | ||
} | ||
} else { | ||
poolNames[pool.ID] = poolInfo{ | ||
Name: pool.RepoName, | ||
Type: "repository", | ||
} | ||
} | ||
} | ||
|
||
hostname, controllerID := c.apiController.GetControllerInfo() | ||
|
||
for _, instance := range instances { | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
c.instanceMetric, | ||
prometheus.GaugeValue, | ||
1, | ||
instance.Name, | ||
string(instance.Status), | ||
string(instance.RunnerStatus), | ||
poolNames[instance.PoolID].Name, | ||
poolNames[instance.PoolID].Type, | ||
hostname, | ||
controllerID, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.