-
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
315 changed files
with
36,352 additions
and
1,667 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,99 @@ | ||
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 { | ||
|
||
m, err := prometheus.NewConstMetric( | ||
c.instanceMetric, | ||
prometheus.GaugeValue, | ||
1, | ||
instance.Name, | ||
string(instance.Status), | ||
string(instance.RunnerStatus), | ||
poolNames[instance.PoolID].Name, | ||
poolNames[instance.PoolID].Type, | ||
hostname, | ||
controllerID, | ||
) | ||
|
||
if err != nil { | ||
log.Printf("cannot collect metrics, creating metric: %s", err) | ||
continue | ||
} | ||
ch <- m | ||
} | ||
} |
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
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,64 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"garm/config" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/golang-jwt/jwt" | ||
) | ||
|
||
type MetricsMiddleware struct { | ||
cfg config.JWTAuth | ||
} | ||
|
||
func NewMetricsMiddleware(cfg config.JWTAuth) *MetricsMiddleware { | ||
return &MetricsMiddleware{ | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
func (m *MetricsMiddleware) Middleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
ctx := r.Context() | ||
authorizationHeader := r.Header.Get("authorization") | ||
if authorizationHeader == "" { | ||
invalidAuthResponse(w) | ||
return | ||
} | ||
|
||
bearerToken := strings.Split(authorizationHeader, " ") | ||
if len(bearerToken) != 2 { | ||
invalidAuthResponse(w) | ||
return | ||
} | ||
|
||
claims := &JWTClaims{} | ||
token, err := jwt.ParseWithClaims(bearerToken[1], claims, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("invalid signing method") | ||
} | ||
return []byte(m.cfg.Secret), nil | ||
}) | ||
|
||
if err != nil { | ||
invalidAuthResponse(w) | ||
return | ||
} | ||
|
||
if !token.Valid { | ||
invalidAuthResponse(w) | ||
return | ||
} | ||
|
||
// we fully trust the claims | ||
if !claims.ReadMetrics { | ||
invalidAuthResponse(w) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
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.