-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
9a58ec6
commit a4d7cc1
Showing
1,005 changed files
with
55,726 additions
and
44,043 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -16,3 +16,6 @@ tmp/* | |
|
||
# ignore goland project files | ||
.idea/ | ||
|
||
# Un-ignore anything in the vendor directory | ||
!vendor/** |
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
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ import ( | |
"io" | ||
"net" | ||
|
||
"github.com/go-pg/pg" | ||
"github.com/go-pg/pg/v10" | ||
) | ||
|
||
var ( | ||
|
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
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,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 | ||
} |
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
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
Oops, something went wrong.