Skip to content

Commit

Permalink
add some metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Dec 22, 2022
1 parent 98d42c9 commit 169e796
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
29 changes: 28 additions & 1 deletion aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func (ao *aggregateOptions) formatIgnoredLabels() {
sort.Strings(ao.ignoredLabels)
}

func (a *aggregate) Len() int {
a.familiesLock.RLock()
count := len(a.families)
a.familiesLock.RUnlock()
return count
}

// setFamilyOrGetExistingFamily either sets a new family or returns an existing family
func (a *aggregate) setFamilyOrGetExistingFamily(familyName string, family *dto.MetricFamily) *metricFamily {
a.familiesLock.Lock()
Expand Down Expand Up @@ -126,8 +133,12 @@ func (a *aggregate) parseAndMerge(r io.Reader, job string) error {
return err
}

MetricCountByFamily.WithLabelValues(name).Set(float64(len(family.Metric)))

}

TotalFamiliesGauge.Set(float64(a.Len()))

return nil
}

Expand All @@ -140,9 +151,18 @@ func (a *aggregate) handleRender(c *gin.Context) {
defer a.familiesLock.RUnlock()

metricNames := []string{}
for name := range a.families {
metricTypeCounts := make(map[string]int)
for name, family := range a.families {
metricNames = append(metricNames, name)
var typeName string
if family.Type == nil {
typeName = "unknown"
} else {
typeName = dto.MetricType_name[int32(*family.Type)]
}
metricTypeCounts[typeName]++
}

sort.Strings(metricNames)

for _, name := range metricNames {
Expand All @@ -151,6 +171,11 @@ func (a *aggregate) handleRender(c *gin.Context) {
}
}

MetricCountByType.Reset()
for typeName, count := range metricTypeCounts {
MetricCountByType.WithLabelValues(typeName).Set(float64(count))
}

// TODO reset gauges
}

Expand Down Expand Up @@ -180,4 +205,6 @@ func (a *aggregate) handleInsert(c *gin.Context) {
http.Error(c.Writer, err.Error(), http.StatusBadRequest)
return
}

MetricPushes.WithLabelValues(job).Inc()
}
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"syscall"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
)

Expand Down Expand Up @@ -41,7 +40,6 @@ func runServers(corsDomain string, apiListen string, lifecycleListen string) {

agg := newAggregate()

promRegistry := prometheus.NewRegistry()
promMetricsConfig := metrics.Config{
Registry: promRegistry,
}
Expand Down
56 changes: 56 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

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

const MetricsNamespace = "prom_agg_gateway"

var promRegistry = prometheus.NewRegistry()

func init() {
promRegistry.MustRegister(
TotalFamiliesGauge,
MetricCountByFamily,
MetricPushes,
)
}

var TotalFamiliesGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "total_families",
Help: "Total number of metric families",
},
)

var MetricCountByFamily = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "metrics_by_family",
Help: "Metric count by family",
},
[]string{
"family",
},
)

var MetricCountByType = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: MetricsNamespace,
Name: "metrics_by_type",
Help: "Metric count by type",
},
[]string{
"metric_type",
},
);

var MetricPushes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: MetricsNamespace,
Name: "metric_pushes",
Help: "Total number of metric push requests, per job",
},
[]string{
"push_job",
},
)

0 comments on commit 169e796

Please sign in to comment.