Skip to content

Commit

Permalink
feat: Adding pushgateway metrics push
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 20, 2021
1 parent c8a8f0f commit 308d8b2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ require (
github.com/ViBiOh/httputils/v4 v4.3.1
github.com/ViBiOh/mailer v1.16.2
github.com/lib/pq v1.10.0
github.com/prometheus/client_golang v1.9.0
gopkg.in/yaml.v2 v2.4.0
)
5 changes: 3 additions & 2 deletions infra/notifier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ spec:
name: vibioh/ketchup-notifier
tag: "202103201316" # {"$imagepolicy": "default:flux-ketchup-notifier:tag"}
config:
KETCHUP_LOGGER_JSON: "true"
KETCHUP_DB_HOST: ketchup-db
KETCHUP_DB_PORT: "80"
KETCHUP_DB_NAME: ketchup
KETCHUP_DB_PORT: "80"
KETCHUP_DB_USER: ketchup
KETCHUP_LOGGER_JSON: "true"
KETCHUP_NOTIFIER_PUSH_URL: http://pushgateway.monitoring
secrets:
KETCHUP_DB_PASS: AgAw+m39PXVIRub8NNvEenJC1CuqSraj0//3FUlhc5NV4cLT0lr15v7oIsd+LyLD65iirArC+03AJ7NQ6HQ/G4a0ARY/mljIFw82+lvZXCF22KvUrJ9CXRY/4ugARluGNwNbUmxO+2frON7G8Ek4GzxyUQK54h2esRWf9Ev7yTz/tfWCEg2JteMWMJ340glCIGUlY1V23GA2ucYmvSBEx8Svifs20lRFrWO0GHs5/gLYP9z4oFbEHO3fLH6a3WM0xiPZQ0I1wnJApRTKe/qraihlCxSz1PfJyELeoP3pEeuJ7M/J8kr/ryJDeI8f4ZSnJ1nbeP6e2VhWd/trGeNBnDbgnVwv/Ycsl15GC3O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fAh4ayUf9HsNL02SliN9OlEaVewT38dvKL9Hi3SIhnWhT20BJnAA5uHOibAqcrXl8b1Ym/l+6c9J0pd7jEmIao6IR4T1mRORlJ1X/67eAKBeFKZqrUdttg02lVFejfN/33
Expand Down
17 changes: 17 additions & 0 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"sort"
"strings"

authModel "github.com/ViBiOh/auth/v2/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/flags"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/ViBiOh/ketchup/pkg/service/repository"
mailer "github.com/ViBiOh/mailer/pkg/client"
mailerModel "github.com/ViBiOh/mailer/pkg/model"
"github.com/prometheus/client_golang/prometheus/push"
)

var (
Expand All @@ -29,27 +31,31 @@ type App interface {
// Config of package
type Config struct {
loginID *uint
pushURL *string
}

type app struct {
repositoryService repository.App
ketchupService ketchup.App
mailerApp mailer.App

pushURL string
loginID uint64
}

// Flags adds flags for configuring package
func Flags(fs *flag.FlagSet, prefix string) Config {
return Config{
loginID: flags.New(prefix, "notifier").Name("LoginID").Default(1).Label("Scheduler user ID").ToUint(fs),
pushURL: flags.New(prefix, "notifier").Name("PushUrl").Default("").Label("Pushgateway URL").ToString(fs),
}
}

// New creates new App from Config
func New(config Config, repositoryService repository.App, ketchupService ketchup.App, mailerApp mailer.App) App {
return app{
loginID: uint64(*config.loginID),
pushURL: strings.TrimSpace(*config.pushURL),

repositoryService: repositoryService,
ketchupService: ketchupService,
Expand Down Expand Up @@ -83,6 +89,17 @@ func (a app) Notify() error {
return err
}

if len(a.pushURL) != 0 {
registry, releasesMetrics, notificationsMetrics := configurePrometheus()

releasesMetrics.Set(float64(len(newReleases)))
notificationsMetrics.Set(float64(len(ketchupsToNotify)))

if err := push.New(a.pushURL, "ketchup").Gatherer(registry).Push(); err != nil {
logger.Error("unable to push metrics: %s", err)
}
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestFlags(t *testing.T) {
}{
{
"simple",
"Usage of simple:\n -loginID uint\n \t[notifier] Scheduler user ID {SIMPLE_LOGIN_ID} (default 1)\n",
"Usage of simple:\n -loginID uint\n \t[notifier] Scheduler user ID {SIMPLE_LOGIN_ID} (default 1)\n -pushUrl string\n \t[notifier] Pushgateway URL {SIMPLE_PUSH_URL}\n",
},
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/notifier/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package notifier

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

func configurePrometheus() (prometheus.Gatherer, prometheus.Gauge, prometheus.Gauge) {
namespace := "ketchup"
promRegistry := prometheus.NewRegistry()

releasesMetrics := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "releases",
})
promRegistry.MustRegister(releasesMetrics)

notificationMetrics := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "notification",
})
promRegistry.MustRegister(notificationMetrics)

return promRegistry, releasesMetrics, notificationMetrics
}

0 comments on commit 308d8b2

Please sign in to comment.