Skip to content

Commit

Permalink
add more metrics and make port configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
canercidam committed Jul 11, 2023
1 parent 1ebf85e commit 15ac68b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ type AdvancedConfig struct {
MulticallAddress string `yaml:"multicallAddress" json:"multicallAddress"`
}

type PrometheusConfig struct {
Port int `yaml:"port" json:"port" default:"9107"`
}

type Config struct {
// runtime values

Expand Down Expand Up @@ -234,6 +238,7 @@ type Config struct {
InspectionConfig InspectionConfig `yaml:"inspection" json:"inspection"`
StorageConfig StorageConfig `yaml:"storage" json:"storage"`
CombinerConfig CombinerConfig `yaml:"combiner" json:"combiner"`
PrometheusConfig PrometheusConfig `yaml:"prometheus" json:"prometheus"`
AdvancedConfig AdvancedConfig `yaml:"advanced" json:"advanced"`
}

Expand Down
61 changes: 52 additions & 9 deletions services/runner/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/forta-network/forta-core-go/clients/health"
"github.com/forta-network/forta-core-go/utils"
Expand All @@ -29,7 +30,7 @@ func (l promHTTPLogger) Println(v ...interface{}) {
}

// StartPrometheusExporter starts an exporter.
func StartPrometheusCollector(serviceHealth ServiceHealth) {
func StartPrometheusCollector(serviceHealth ServiceHealth, port int) {
var collector prometheus.Collector = &promCollector{serviceHealth: serviceHealth}
prometheus.MustRegister(collector)

Expand All @@ -55,7 +56,7 @@ func StartPrometheusCollector(serviceHealth ServiceHealth) {
))

server := &http.Server{
Addr: ":9107", // TODO: Make configurable
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}

Expand All @@ -67,22 +68,64 @@ func (pc *promCollector) Describe(ch chan<- *prometheus.Desc) {

func (pc *promCollector) Collect(ch chan<- prometheus.Metric) {
for _, report := range pc.serviceHealth.CheckServiceHealth() {
if !strings.Contains(report.Name, "service.inspector") {
parts := strings.Split(report.Name, ".service.")
if len(parts) != 2 {
continue
}

name := strings.Replace(strings.Replace(report.Name, ".", "_", -1), "-", "_", -1)
val, err := strconv.ParseFloat(report.Details, 64)
if err != nil {
parts = strings.Split(parts[1], ".")
if len(parts) != 2 {
continue
}
serviceName := parts[0]
reportName := strings.Join(parts[1:], ".")

metricName := reportName
metricName = strings.ReplaceAll(metricName, "-", "_")
metricName = strings.ReplaceAll(metricName, ".", "_")
metricName = fmt.Sprintf("forta_%s_%s", serviceName, metricName)

// converting three types of data to float: timestamp, number, boolean
// finally, converting the error messages like value=1 and label=message
var (
value float64
labels []string
)
if t, err := time.Parse(time.RFC3339, report.Details); err != nil {
value = float64(t.UTC().Unix())
} else if n, err := strconv.ParseFloat(report.Details, 64); err != nil {
value = n
} else if b, err := strconv.ParseBool(report.Details); err != nil {
if b {
value = 1
}
} else {

// important note: the logic in here is used only if we are trying to use an error message

if report.Status != health.StatusOK {
labels = append(labels, string(report.Status), report.Details)
}

switch report.Status {
case health.StatusOK, health.StatusInfo, health.StatusUnknown:
value = 0

case health.StatusFailing, health.StatusLagging:
value = 1

case health.StatusDown:
value = -1
}

}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("forta", "", name),
prometheus.BuildFQName("forta", serviceName, metricName),
"", nil, nil,
),
prometheus.GaugeValue,
val,
value,
)
}
}
2 changes: 1 addition & 1 deletion services/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (runner *Runner) Start() error {

go runner.keepContainersAlive()

StartPrometheusCollector(runner)
StartPrometheusCollector(runner, runner.cfg.PrometheusConfig.Port)

return nil
}
Expand Down

0 comments on commit 15ac68b

Please sign in to comment.