Skip to content

Commit

Permalink
Update various dependencies (#366)
Browse files Browse the repository at this point in the history
* gitignore: Un-ignore content of vendor directory

* gomod: Switch to github.com/google/uuid

* Switch to github.com/google/uuid

* gomod: Update github.com/PuerkitoBio/rehttp to v1.4.0

* gomod: Update github.com/caarlos0/env to v10

* Update to github.com/caarlos0/env v10

* gomod: Update github.com/certifi/gocertifi

* gomod: Update github.com/dave/jennifer

* gomod: Update github.com/golang-jwt/jwt to v5

* Update to github.com/golang-jwt/jwt/v5

* gomod: Update github.com/gorilla/mux to v1.8.1

* gomod: Update github.com/grpc-ecosystem/go-grpc-middleware

* gomod: Add github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus

* Use github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus

* gomod: Update github.com/mattn/go-isatty

* gomod: Update github.com/mattn/goveralls

* gomod: Update github.com/prometheus/client_golang

* gomod: Update github.com/rs/xid

* gomod: Update github.com/rs/zerolog

* gomod: Update github.com/shopspring/decimal

* gomod: Update github.com/sony/gobreaker

* gomod: Update github.com/spf13/cobra

* gomod: Update github.com/stretchr/testify

* gomod: Update github.com/zenazn/goji

* gomod: Remove golang.org/x/tools/cmd/cover (deprecated)

* tools.go: Remove golang.org/x/tools/cmd/cover (deprecated)

* gomod: Update golang.org/x/vuln

* gomod: Update google.golang.org/grpc

* gomod: Update google.golang.org/grpc/cmd/protoc-gen-go-grpc

* gomod: Update go-pg to v10

* Update to go-pg/v10
  • Loading branch information
monstermunchkin authored Apr 12, 2024
1 parent 9a58ec6 commit a4d7cc1
Show file tree
Hide file tree
Showing 1,005 changed files with 55,726 additions and 44,043 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ tmp/*

# ignore goland project files
.idea/

# Un-ignore anything in the vendor directory
!vendor/**
2 changes: 1 addition & 1 deletion backend/couchdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package couchdb
import (
"context"

"github.com/caarlos0/env"
"github.com/caarlos0/env/v10"
"github.com/go-kivik/couchdb/v3"
kivik "github.com/go-kivik/kivik/v3"

Expand Down
2 changes: 1 addition & 1 deletion backend/k8sapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"os"
"strings"

"github.com/caarlos0/env"
"github.com/caarlos0/env/v10"
"github.com/pace/bricks/http/transport"
"github.com/pace/bricks/maintenance/log"
)
Expand Down
2 changes: 1 addition & 1 deletion backend/objstore/objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/caarlos0/env"
"github.com/caarlos0/env/v10"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"

Expand Down
2 changes: 1 addition & 1 deletion backend/postgres/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io"
"net"

"github.com/go-pg/pg"
"github.com/go-pg/pg/v10"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion backend/postgres/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"testing"

"github.com/go-pg/pg"
"github.com/go-pg/pg/v10"
"github.com/stretchr/testify/require"

pbpostgres "github.com/pace/bricks/backend/postgres"
Expand Down
2 changes: 1 addition & 1 deletion backend/postgres/health_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"time"

"github.com/go-pg/pg/orm"
"github.com/go-pg/pg/v10/orm"
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
)

Expand Down
2 changes: 1 addition & 1 deletion backend/postgres/health_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/go-pg/pg/orm"
"github.com/go-pg/pg/v10/orm"
http2 "github.com/pace/bricks/http"
"github.com/pace/bricks/maintenance/errors"
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
Expand Down
136 changes: 136 additions & 0 deletions backend/postgres/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package postgres

import (
"context"
"math"
"time"

"github.com/go-pg/pg/v10"
"github.com/opentracing/opentracing-go"
olog "github.com/opentracing/opentracing-go/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"

"github.com/pace/bricks/maintenance/log"
)

type QueryLogger struct{}

func (QueryLogger) BeforeQuery(_ context.Context, _ *pg.QueryEvent) (context.Context, error) {
return nil, nil
}

func (QueryLogger) AfterQuery(ctx context.Context, event *pg.QueryEvent) error {
q, qe := event.UnformattedQuery()
if qe == nil {
if !(cfg.LogRead || cfg.LogWrite) {
return nil
}
// we can only and should only perfom the following check if we have the information availaible
mode := determineQueryMode(string(q))
if mode == readMode && !cfg.LogRead {
return nil
}
if mode == writeMode && !cfg.LogWrite {
return nil
}

}

dur := float64(time.Since(event.StartTime)) / float64(time.Millisecond)

// check if log context is given
var logger *zerolog.Logger
if ctx != nil {
logger = log.Ctx(ctx)
} else {
logger = log.Logger()
}

// add general info
le := logger.Debug().
Float64("duration", dur).
Str("sentry:category", "postgres")

// add error or result set info
if event.Err != nil {
le = le.Err(event.Err)
} else {
le = le.Int("affected", event.Result.RowsAffected()).
Int("rows", event.Result.RowsReturned())
}

if qe != nil {
// this is only a display issue not a "real" issue
le.Msgf("%v", qe)
}
le.Msg(string(q))

return nil
}

type OpenTracingAdapter struct{}

func (OpenTracingAdapter) BeforeQuery(_ context.Context, _ *pg.QueryEvent) (context.Context, error) {
return nil, nil
}

func (OpenTracingAdapter) AfterQuery(ctx context.Context, event *pg.QueryEvent) error {
// start span with general info
q, qe := event.UnformattedQuery()
if qe != nil {
// this is only a display issue not a "real" issue
q = []byte(qe.Error())
}

span, _ := opentracing.StartSpanFromContext(event.DB.Context(), "sql: "+getQueryType(string(q)),
opentracing.StartTime(event.StartTime))

span.SetTag("db.system", "postgres")

fields := []olog.Field{
olog.String("query", string(q)),
}

// add error or result set info
if event.Err != nil {
fields = append(fields, olog.Error(event.Err))
} else {
fields = append(fields,
olog.Int("affected", event.Result.RowsAffected()),
olog.Int("rows", event.Result.RowsReturned()))
}

span.LogFields(fields...)
span.Finish()

return nil
}

type MetricsAdapter struct {
opts *pg.Options
}

func (MetricsAdapter) BeforeQuery(_ context.Context, _ *pg.QueryEvent) (context.Context, error) {
return nil, nil
}

func (m MetricsAdapter) AfterQuery(ctx context.Context, event *pg.QueryEvent) error {
dur := float64(time.Since(event.StartTime)) / float64(time.Millisecond)
labels := prometheus.Labels{
"database": m.opts.Addr + "/" + m.opts.Database,
}

metricQueryTotal.With(labels).Inc()

if event.Err != nil {
metricQueryFailed.With(labels).Inc()
} else {
r := event.Result
metricQueryRowsTotal.With(labels).Add(float64(r.RowsReturned()))
metricQueryAffectedTotal.With(labels).Add(math.Max(0, float64(r.RowsAffected())))
}
metricQueryDurationSeconds.With(labels).Observe(dur)

return nil
}
2 changes: 1 addition & 1 deletion backend/postgres/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sync"
"time"

"github.com/go-pg/pg"
"github.com/go-pg/pg/v10"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down
124 changes: 9 additions & 115 deletions backend/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ package postgres
import (
"context"
"fmt"
"math"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"

"github.com/caarlos0/env"
"github.com/go-pg/pg"
"github.com/opentracing/opentracing-go"
olog "github.com/opentracing/opentracing-go/log"
"github.com/caarlos0/env/v10"
"github.com/go-pg/pg/v10"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"

"github.com/pace/bricks/maintenance/health/servicehealthcheck"
"github.com/pace/bricks/maintenance/log"
Expand Down Expand Up @@ -224,7 +220,8 @@ func ConnectionPool(opts ...ConfigOption) *pg.DB {
//
// Fot a health check for this connection a PgHealthCheck needs to
// be registered:
// servicehealthcheck.RegisterHealthCheck(...)
//
// servicehealthcheck.RegisterHealthCheck(...)
func CustomConnectionPool(opts *pg.Options) *pg.DB {
log.Logger().Info().Str("addr", opts.Addr).
Str("user", opts.User).
Expand All @@ -233,14 +230,14 @@ func CustomConnectionPool(opts *pg.Options) *pg.DB {
Msg("PostgreSQL connection pool created")
db := pg.Connect(opts)
if cfg.LogWrite || cfg.LogRead {
db.OnQueryProcessed(queryLogger)
db.AddQueryHook(QueryLogger{})
} else {
log.Logger().Warn().Msg("Connection pool has logging queries disabled completely")
}
db.OnQueryProcessed(openTracingAdapter)
db.OnQueryProcessed(func(event *pg.QueryProcessedEvent) {
metricsAdapter(event, opts)
})

db.AddQueryHook(OpenTracingAdapter{})
db.AddQueryHook(MetricsAdapter{opts})

return db
}

Expand All @@ -260,57 +257,6 @@ func determineQueryMode(qry string) queryMode {
return writeMode
}

func queryLogger(event *pg.QueryProcessedEvent) {
q, qe := event.UnformattedQuery()
if qe == nil {
if !(cfg.LogRead || cfg.LogWrite) {
return
}
// we can only and should only perfom the following check if we have the information availaible
mode := determineQueryMode(q)
if mode == readMode && !cfg.LogRead {
return
}
if mode == writeMode && !cfg.LogWrite {
return
}

}
ctx := event.DB.Context()
dur := float64(time.Since(event.StartTime)) / float64(time.Millisecond)

// check if log context is given
var logger *zerolog.Logger
if ctx != nil {
logger = log.Ctx(ctx)
} else {
logger = log.Logger()
}

// add general info
le := logger.Debug().
Str("file", event.File).
Int("line", event.Line).
Str("func", event.Func).
Int("attempt", event.Attempt).
Float64("duration", dur).
Str("sentry:category", "postgres")

// add error or result set info
if event.Error != nil {
le = le.Err(event.Error)
} else {
le = le.Int("affected", event.Result.RowsAffected()).
Int("rows", event.Result.RowsReturned())
}

if qe != nil {
// this is only a display issue not a "real" issue
le.Msgf("%v", qe)
}
le.Msg(q)
}

var reQueryType = regexp.MustCompile(`(\s)`)
var reQueryTypeCleanup = regexp.MustCompile(`(?m)(\s+|\n)`)

Expand All @@ -324,55 +270,3 @@ func getQueryType(s string) string {
}
return strings.ToUpper(s)
}

func openTracingAdapter(event *pg.QueryProcessedEvent) {
// start span with general info
q, qe := event.UnformattedQuery()
if qe != nil {
// this is only a display issue not a "real" issue
q = qe.Error()
}

span, _ := opentracing.StartSpanFromContext(event.DB.Context(), "sql: "+getQueryType(q),
opentracing.StartTime(event.StartTime))

span.SetTag("db.system", "postgres")

fields := []olog.Field{
olog.String("file", event.File),
olog.Int("line", event.Line),
olog.String("func", event.Func),
olog.Int("attempt", event.Attempt),
olog.String("query", q),
}

// add error or result set info
if event.Error != nil {
fields = append(fields, olog.Error(event.Error))
} else {
fields = append(fields,
olog.Int("affected", event.Result.RowsAffected()),
olog.Int("rows", event.Result.RowsReturned()))
}

span.LogFields(fields...)
span.Finish()
}

func metricsAdapter(event *pg.QueryProcessedEvent, opts *pg.Options) {
dur := float64(time.Since(event.StartTime)) / float64(time.Millisecond)
labels := prometheus.Labels{
"database": opts.Addr + "/" + opts.Database,
}

metricQueryTotal.With(labels).Inc()

if event.Error != nil {
metricQueryFailed.With(labels).Inc()
} else {
r := event.Result
metricQueryRowsTotal.With(labels).Add(float64(r.RowsReturned()))
metricQueryAffectedTotal.With(labels).Add(math.Max(0, float64(r.RowsAffected())))
}
metricQueryDurationSeconds.With(labels).Observe(dur)
}
4 changes: 2 additions & 2 deletions backend/postgres/query_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package postgres
import (
"context"

"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)

type pgPoolAdapter struct {
Expand Down
Loading

0 comments on commit a4d7cc1

Please sign in to comment.