Added a metrics storage driver wrapper.

This commit is contained in:
Nick Gerakines 2020-04-03 22:15:57 -04:00
parent 33233646cd
commit 11e863e8d7
No known key found for this signature in database
GPG Key ID: 33D43D854F96B2E4
6 changed files with 73 additions and 10 deletions

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by herr at 2020-03-30 15:10:19.379177842 -0400 EDT m=+0.009779068
// This file was generated by herr at 2020-04-03 22:14:36.2860856 -0400 EDT m=+0.009914079
package errors
import (

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by herr at 2020-03-30 15:10:19.411094446 -0400 EDT m=+0.041695628
// This file was generated by herr at 2020-04-03 22:14:36.316998079 -0400 EDT m=+0.040826470
package errors
import (

View File

@ -370,6 +370,9 @@ func (s pgStorage) ActorAliasSubjectExists(ctx context.Context, alias string) (b
}
func (s pgStorage) FilterGroupsByActorID(ctx context.Context, actorIDs []string) ([]string, error) {
if len(actorIDs) == 0 {
return []string{}, nil
}
query := fmt.Sprintf(`SELECT actor_id FROM actors WHERE payload->>'type' = 'Group' AND actor_id in (%s)`, strings.Join(common.DollarForEach(len(actorIDs)), ","))
return s.selectStrings(errors.WrapActorQueryFailedError, ctx, query, common.StringsToInterfaces(actorIDs)...)
}

View File

@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
@ -13,6 +15,12 @@ type QueryExecute interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
type MetricDriver struct {
Driver QueryExecute
Gauge prometheus.Gauge
Counter prometheus.Counter
}
type TransactionSQLDriver struct {
Driver *sql.Tx
}
@ -64,3 +72,46 @@ func (d TransactionSQLDriver) QueryContext(ctx context.Context, query string, ar
func (d TransactionSQLDriver) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return d.Driver.QueryRowContext(ctx, query, args...)
}
func (d MetricDriver) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
d.Counter.Inc()
d.Gauge.Inc()
defer d.Gauge.Dec()
return d.Driver.ExecContext(ctx, query, args...)
}
func (d MetricDriver) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
d.Counter.Inc()
d.Gauge.Inc()
defer d.Gauge.Dec()
return d.Driver.QueryContext(ctx, query, args...)
}
func (d MetricDriver) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
d.Counter.Inc()
d.Gauge.Inc()
defer d.Gauge.Dec()
return d.Driver.QueryRowContext(ctx, query, args...)
}
func WrapMetricDriver(metricFactory promauto.Factory, namespace, subsystem string, driver QueryExecute) QueryExecute {
return MetricDriver{
Driver: driver,
Gauge: metricFactory.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "queries_in_flight",
Help: "A gauge of in-flight DB queries.",
}),
Counter: metricFactory.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "queries_total",
Help: "A counter for queries.",
}),
}
}

View File

@ -142,7 +142,13 @@ func serverCommandAction(cliCtx *cli.Context) error {
}
defer dbClose()
s := storage.DefaultStorage(storage.LoggingSQLDriver{Driver: db, Logger: logger})
registry := prometheus.NewRegistry()
if err = registry.Register(prometheus.NewGoCollector()); err != nil {
return err
}
fact := promauto.With(registry)
s := storage.DefaultStorage(storage.WrapMetricDriver(fact, "tavern", "web", storage.LoggingSQLDriver{Driver: db, Logger: logger}))
utrans, err := config.Trans(cliCtx)
if err != nil {
@ -243,13 +249,6 @@ func serverCommandAction(cliCtx *cli.Context) error {
crawlQueue := common.NewStringQueue()
assetQueue := common.NewStringQueue()
registry := prometheus.NewRegistry()
if err = registry.Register(prometheus.NewGoCollector()); err != nil {
return err
}
fact := promauto.With(registry)
{
tavernInfoG := fact.NewGauge(prometheus.GaugeOpts{
Namespace: "tavern",

View File

@ -160,6 +160,16 @@ func (h handler) dashboardGroupsCreate(c *gin.Context) {
return err
}
err = tx.CreatePendingFollowing(ctx, localUser.ID, actorRowID, followGroup)
if err != nil {
return err
}
err = tx.UpdateFollowingAccepted(ctx, localUser.ID, actorRowID)
if err != nil {
return err
}
return nil
})
if txErr != nil {