-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change jicofo metrics prefix (+refactor)
- Loading branch information
Showing
10 changed files
with
886 additions
and
424 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
package generic | ||
|
||
import "github.com/prometheus/client_golang/prometheus" | ||
|
||
type BasicMetricOptions struct { | ||
component string | ||
variableLabels []string | ||
constLabels prometheus.Labels | ||
getLabels LabelGetter | ||
} | ||
|
||
type BasicMetricOption func(*BasicMetricOptions) | ||
|
||
func NewBasicMetric(path string, valueType prometheus.ValueType, help string, opts ...BasicMetricOption) Metric { | ||
options := &BasicMetricOptions{} | ||
for _, apply := range opts { | ||
apply(options) | ||
} | ||
return NewMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, options.component, PathToMetricName(path)), help, options.variableLabels, options.constLabels), | ||
FloatGetter(path, valueType, options.getLabels), | ||
) | ||
} | ||
|
||
func WithComponent(component string) BasicMetricOption { | ||
return func(opts *BasicMetricOptions) { | ||
opts.component = component | ||
} | ||
} | ||
|
||
func WithVariableLabels(variableLabels []string) BasicMetricOption { | ||
return func(opts *BasicMetricOptions) { | ||
opts.variableLabels = variableLabels | ||
} | ||
} | ||
|
||
func WithConstLabels(constLabels prometheus.Labels) BasicMetricOption { | ||
return func(opts *BasicMetricOptions) { | ||
opts.constLabels = constLabels | ||
} | ||
} | ||
|
||
func WithLabelGetter(getLabels LabelGetter) BasicMetricOption { | ||
return func(opts *BasicMetricOptions) { | ||
opts.getLabels = getLabels | ||
} | ||
} |
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,59 @@ | ||
package generic | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Collector struct { | ||
client *http.Client | ||
target string | ||
metrics []Metric | ||
} | ||
|
||
func NewCollector(client *http.Client, target string, metrics []Metric) *Collector { | ||
return &Collector{ | ||
client: client, | ||
target: target, | ||
metrics: metrics, | ||
} | ||
} | ||
|
||
func (c *Collector) Describe(ch chan<- *prometheus.Desc) { | ||
for _, m := range c.metrics { | ||
ch <- m.desc | ||
} | ||
} | ||
|
||
func (c *Collector) Collect(ch chan<- prometheus.Metric) { | ||
data, err := c.probe() | ||
|
||
if err != nil { | ||
log.Error(err) | ||
return | ||
} | ||
|
||
for _, m := range c.metrics { | ||
pm := m.get(m.desc, data) | ||
if pm != nil { | ||
ch <- pm | ||
} | ||
} | ||
} | ||
|
||
func (c *Collector) probe() (string, error) { | ||
resp, err := c.client.Get(c.target) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} |
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 |
---|---|---|
@@ -1,97 +1,40 @@ | ||
package generic | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
type MetricDecription struct { | ||
ValueType prometheus.ValueType | ||
Path string | ||
Help string | ||
} | ||
|
||
type MetricGetter func(string, Prober) prometheus.Metric | ||
|
||
type Prober interface { | ||
Name() string | ||
Probe() (string, bool) | ||
MetricGetters() []MetricGetter | ||
Labels() []string | ||
} | ||
|
||
type Collector struct { | ||
prober Prober | ||
} | ||
|
||
func NewMetricDecription(valueType prometheus.ValueType, path, help string) MetricDecription { | ||
return MetricDecription{ | ||
ValueType: valueType, | ||
Path: path, | ||
Help: help, | ||
} | ||
} | ||
|
||
func NewCollector(prober Prober) *Collector { | ||
return &Collector{prober: prober} | ||
} | ||
|
||
func (c *Collector) Describe(ch chan<- *prometheus.Desc) { | ||
prometheus.DescribeByCollect(c, ch) | ||
} | ||
|
||
func (c *Collector) Collect(ch chan<- prometheus.Metric) { | ||
data, ok := c.prober.Probe() | ||
|
||
var up float64 | ||
if ok { | ||
up = 1 | ||
} | ||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc(prometheus.BuildFQName("jitsi", c.prober.Name(), "up"), "", c.prober.Labels(), nil), prometheus.GaugeValue, up, getLabels(data, c.prober)...) | ||
|
||
for _, getter := range c.prober.MetricGetters() { | ||
ch <- getter(data, c.prober) | ||
} | ||
} | ||
|
||
func NewMetricGetter(valueType prometheus.ValueType, path, help string) MetricGetter { | ||
return newMetricGetter(valueType, PathToMetricName(path), help, func(data string) float64 { | ||
return gjson.Get(data, path).Float() | ||
}) | ||
} | ||
|
||
func NewBoolMetricGetter(valueType prometheus.ValueType, path, help string) MetricGetter { | ||
return newMetricGetter(valueType, PathToMetricName(path), help, func(data string) float64 { | ||
var value float64 | ||
if gjson.Get(data, path).Bool() { | ||
value = 1 | ||
} | ||
return value | ||
}) | ||
} | ||
|
||
func PathToMetricName(path string) string { | ||
regex := regexp.MustCompile(`\.|-`) | ||
return regex.ReplaceAllString(path, "_") | ||
} | ||
|
||
func newMetricGetter(valueType prometheus.ValueType, name, help string, valueGetter func(data string) float64) MetricGetter { | ||
return func(data string, p Prober) prometheus.Metric { | ||
labels := getLabels(data, p) | ||
return prometheus.MustNewConstMetric( | ||
prometheus.NewDesc(prometheus.BuildFQName("jitsi", p.Name(), name), help, p.Labels(), nil), | ||
prometheus.GaugeValue, | ||
valueGetter(data), labels...) | ||
} | ||
} | ||
|
||
func getLabels(data string, p Prober) []string { | ||
labels := make([]string, len(p.Labels())) | ||
for k, v := range p.Labels() { | ||
labels[k] = gjson.Get(data, v).String() | ||
} | ||
return labels | ||
} | ||
// func NewMetricGetter(valueType prometheus.ValueType, path, help string) MetricGetter { | ||
// return newMetricGetter(valueType, PathToMetricName(path), help, func(data string) float64 { | ||
// return gjson.Get(data, path).Float() | ||
// }) | ||
// } | ||
|
||
// func NewBoolMetricGetter(valueType prometheus.ValueType, path, help string) MetricGetter { | ||
// return newMetricGetter(valueType, PathToMetricName(path), help, func(data string) float64 { | ||
// var value float64 | ||
// if gjson.Get(data, path).Bool() { | ||
// value = 1 | ||
// } | ||
// return value | ||
// }) | ||
// } | ||
|
||
// func PathToMetricName(path string) string { | ||
// regex := regexp.MustCompile(`\.|-`) | ||
// return regex.ReplaceAllString(path, "_") | ||
// } | ||
|
||
// func newMetricGetter(valueType prometheus.ValueType, name, help string, valueGetter func(data string) float64) MetricGetter { | ||
// return func(data string, p Prober) prometheus.Metric { | ||
// labels := getLabels(data, p) | ||
// return prometheus.MustNewConstMetric( | ||
// prometheus.NewDesc(prometheus.BuildFQName("jitsi", p.Name(), name), help, p.Labels(), nil), | ||
// prometheus.GaugeValue, | ||
// valueGetter(data), labels...) | ||
// } | ||
// } | ||
|
||
// func getLabels(data string, p Prober) []string { | ||
// labels := make([]string, len(p.Labels())) | ||
// for k, v := range p.Labels() { | ||
// labels[k] = gjson.Get(data, v).String() | ||
// } | ||
// return labels | ||
// } |
Oops, something went wrong.