Skip to content

Commit f3f11f1

Browse files
authored
[-] remove passwords from sink log messages, fixes #612 (#614)
1 parent 1bd10e6 commit f3f11f1

File tree

8 files changed

+37
-26
lines changed

8 files changed

+37
-26
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/gorilla/websocket v1.5.3
99
github.com/jackc/pgx/v5 v5.7.2
1010
github.com/jessevdk/go-flags v1.6.1
11-
github.com/pashagolub/pgxmock/v4 v4.3.0
11+
github.com/pashagolub/pgxmock/v4 v4.4.0
1212
github.com/prometheus/client_golang v1.20.5
1313
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
1414
github.com/sethvargo/go-retry v0.3.0
@@ -87,10 +87,10 @@ require (
8787
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
8888
go.opentelemetry.io/otel/trace v1.31.0 // indirect
8989
go.uber.org/multierr v1.11.0 // indirect
90-
golang.org/x/crypto v0.31.0 // indirect
90+
golang.org/x/crypto v0.32.0 // indirect
9191
golang.org/x/net v0.29.0 // indirect
9292
golang.org/x/sync v0.10.0 // indirect
93-
golang.org/x/sys v0.28.0 // indirect
93+
golang.org/x/sys v0.29.0 // indirect
9494
golang.org/x/text v0.21.0 // indirect
9595
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
9696
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ github.com/pashagolub/pgxmock/v3 v3.4.0 h1:87VMr2q7m2+6VzXo4Tsp9kMklGlj6mMN19Hp/
116116
github.com/pashagolub/pgxmock/v3 v3.4.0/go.mod h1:FvCl7xqPbLLI3XohihJ1NzXnikjM3q/NWSixg4t9hrU=
117117
github.com/pashagolub/pgxmock/v4 v4.3.0 h1:DqT7fk0OCK6H0GvqtcMsLpv8cIwWqdxWgfZNLeHCb/s=
118118
github.com/pashagolub/pgxmock/v4 v4.3.0/go.mod h1:9VoVHXwS3XR/yPtKGzwQvwZX1kzGB9sM8SviDcHDa3A=
119+
github.com/pashagolub/pgxmock/v4 v4.4.0 h1:zrZHBzqlzIFrq5Iw6nQpmpEd77eLqGIC2ol4ZTeojz0=
120+
github.com/pashagolub/pgxmock/v4 v4.4.0/go.mod h1:9VoVHXwS3XR/yPtKGzwQvwZX1kzGB9sM8SviDcHDa3A=
119121
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
120122
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
121123
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -201,6 +203,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
201203
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
202204
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
203205
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
206+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
207+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
204208
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
205209
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
206210
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
@@ -226,8 +230,11 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
226230
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
227231
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
228232
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
233+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
234+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
229235
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
230236
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
237+
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
231238
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
232239
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
233240
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=

internal/log/formatter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313

1414
func newFormatter(noColors bool) *Formatter {
1515
return &Formatter{
16-
HideKeys: false,
17-
FieldsOrder: []string{"source", "sink", "metric", "rows", "sql", "params"},
16+
HideKeys: false,
17+
FieldsOrder: []string{
18+
"source", "metric",
19+
"sink", "db", "filename", "address",
20+
"rows", "sql", "params"},
1821
TimestampFormat: "2006-01-02 15:04:05.000",
1922
ShowFullLevel: true,
2023
NoColors: noColors,

internal/sinks/json.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package sinks
33
import (
44
"context"
55
"encoding/json"
6+
"time"
67

8+
"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
79
"github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
810
"gopkg.in/natefinch/lumberjack.v2"
911
)
@@ -18,6 +20,8 @@ type JSONWriter struct {
1820
}
1921

2022
func NewJSONWriter(ctx context.Context, fname string) (*JSONWriter, error) {
23+
l := log.GetLogger(ctx).WithField("sink", "jsonfile").WithField("filename", fname)
24+
ctx = log.WithLogger(ctx, l)
2125
jw := &JSONWriter{
2226
ctx: ctx,
2327
lw: &lumberjack.Logger{Filename: fname, Compress: true},
@@ -34,6 +38,7 @@ func (jw *JSONWriter) Write(msgs []metrics.MeasurementEnvelope) error {
3438
return nil
3539
}
3640
enc := json.NewEncoder(jw.lw)
41+
t1 := time.Now()
3742
for _, msg := range msgs {
3843
dataRow := map[string]any{
3944
"metric": msg.MetricName,
@@ -45,6 +50,8 @@ func (jw *JSONWriter) Write(msgs []metrics.MeasurementEnvelope) error {
4550
return err
4651
}
4752
}
53+
diff := time.Since(t1)
54+
log.GetLogger(jw.ctx).WithField("rows", len(msgs)).WithField("elapsed", diff).Info("measurements written")
4855
return nil
4956
}
5057

internal/sinks/multiwriter.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ type MultiWriter struct {
2626
// NewMultiWriter creates and returns new instance of MultiWriter struct.
2727
func NewMultiWriter(ctx context.Context, opts *CmdOpts, metricDefs *metrics.Metrics) (mw *MultiWriter, err error) {
2828
var w Writer
29-
logger := log.GetLogger(ctx)
3029
mw = &MultiWriter{}
3130
for _, s := range opts.Sinks {
32-
l := logger.WithField("sink", s)
33-
ctx = log.WithLogger(ctx, l)
3431
scheme, path, found := strings.Cut(s, "://")
3532
if !found || scheme == "" || path == "" {
3633
return nil, fmt.Errorf("malformed sink URI %s", s)
@@ -51,9 +48,7 @@ func NewMultiWriter(ctx context.Context, opts *CmdOpts, metricDefs *metrics.Metr
5148
return nil, err
5249
}
5350
mw.AddWriter(w)
54-
l.Info(`Measurements sink activated`)
5551
}
56-
5752
if len(mw.writers) == 0 {
5853
return nil, errors.New("no sinks specified for measurements")
5954
}

internal/sinks/postgres.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func NewPostgresWriter(ctx context.Context, connstr string, opts *CmdOpts, metri
3131
}
3232

3333
func NewWriterFromPostgresConn(ctx context.Context, conn db.PgxPoolIface, opts *CmdOpts, metricDefs *metrics.Metrics) (pgw *PostgresWriter, err error) {
34+
l := log.GetLogger(ctx).WithField("sink", "postgres").WithField("db", conn.Config().ConnConfig.Database)
35+
ctx = log.WithLogger(ctx, l)
3436
pgw = &PostgresWriter{
3537
ctx: ctx,
3638
metricDefs: metricDefs,
@@ -40,7 +42,7 @@ func NewWriterFromPostgresConn(ctx context.Context, conn db.PgxPoolIface, opts *
4042
sinkDb: conn,
4143
}
4244
if err = db.Init(ctx, pgw.sinkDb, func(ctx context.Context, conn db.PgxIface) error {
43-
log.GetLogger(ctx).Info("Initialising measurements database...")
45+
l.Info("initialising measurements database...")
4446
exists, err := db.DoesSchemaExist(ctx, conn, "admin")
4547
if err != nil || exists {
4648
return err
@@ -63,6 +65,7 @@ func NewWriterFromPostgresConn(ctx context.Context, conn db.PgxPoolIface, opts *
6365
go pgw.deleteOldPartitions(deleterDelay)
6466
go pgw.maintainUniqueSources()
6567
go pgw.poll()
68+
l.Info(`measurements sink is activated`)
6669
return
6770
}
6871

@@ -178,7 +181,7 @@ func (pgw *PostgresWriter) EnsureMetricDummy(metric string) (err error) {
178181
return
179182
}
180183

181-
// Write send the measurements to the cache channel
184+
// Write sends the measurements to the cache channel
182185
func (pgw *PostgresWriter) Write(msgs []metrics.MeasurementEnvelope) error {
183186
if pgw.ctx.Err() != nil {
184187
return pgw.ctx.Err()
@@ -232,9 +235,7 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
232235
if len(msgs) == 0 {
233236
return
234237
}
235-
logger := log.GetLogger(pgw.ctx).
236-
WithField("sink", "postgres").
237-
WithField("db", pgw.sinkDb.Config().ConnConfig.Database)
238+
logger := log.GetLogger(pgw.ctx)
238239
tsWarningPrinted := false
239240
metricsToStorePerMetric := make(map[string][]MeasurementMessagePostgres)
240241
rowsBatched := 0
@@ -247,7 +248,7 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
247248
if len(msg.Data) == 0 {
248249
continue
249250
}
250-
logger.WithField("data", msg.Data).WithField("len", len(msg.Data)).Debug("Sending To Postgres")
251+
logger.WithField("data", msg.Data).WithField("len", len(msg.Data)).Debug("sending to postgres")
251252

252253
for _, dataRow := range msg.Data {
253254
var epochTime time.Time
@@ -337,7 +338,7 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
337338
} else if pgw.metricSchema == DbStorageSchemaTimescale {
338339
err = pgw.EnsureMetricTimescale(pgPartBounds, forceRecreatePGMetricPartitions)
339340
} else {
340-
logger.Fatal("should never happen...")
341+
logger.Fatal("unknown storage schema...")
341342
}
342343
if forceRecreatePGMetricPartitions {
343344
forceRecreatePGMetricPartitions = false

internal/sinks/prometheus.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func NewPrometheusWriter(ctx context.Context, connstr string) (promw *Prometheus
3636
if !found {
3737
namespace = "pgwatch"
3838
}
39+
l := log.GetLogger(ctx).WithField("sink", "prometheus").WithField("address", addr)
40+
ctx = log.WithLogger(ctx, l)
3941
promw = &PrometheusWriter{
4042
ctx: ctx,
4143
PrometheusNamespace: namespace,
@@ -179,7 +181,7 @@ func (promw *PrometheusWriter) setInstanceUpDownState(ch chan<- prometheus.Metri
179181
if len(pm) > 0 {
180182
ch <- pm[0]
181183
} else {
182-
logger.Errorf("Could not formulate an instance state report - should not happen")
184+
logger.Error("Could not formulate an instance state report - should not happen")
183185
}
184186
}
185187

internal/sinks/rpc.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func NewRPCWriter(ctx context.Context, address string) (*RPCWriter, error) {
2323
if err != nil {
2424
return nil, err
2525
}
26-
26+
l := log.GetLogger(ctx).WithField("sink", "rpc").WithField("address", address)
27+
ctx = log.WithLogger(ctx, l)
2728
rw := &RPCWriter{
2829
ctx: ctx,
2930
address: address,
@@ -41,16 +42,13 @@ func (rw *RPCWriter) Write(msgs []metrics.MeasurementEnvelope) error {
4142
if len(msgs) == 0 {
4243
return nil
4344
}
44-
l := log.GetLogger(rw.ctx).
45-
WithField("sink", "rpc").
46-
WithField("address", rw.address)
4745
for _, msg := range msgs {
4846
var logMsg string
4947
if err := rw.client.Call("Receiver.UpdateMeasurements", &msg, &logMsg); err != nil {
5048
return err
5149
}
5250
if len(logMsg) > 0 {
53-
l.Info(logMsg)
51+
log.GetLogger(rw.ctx).Info(logMsg)
5452
}
5553
}
5654
return nil
@@ -72,9 +70,7 @@ func (rw *RPCWriter) SyncMetric(dbUnique string, metricName string, op string) e
7270
return err
7371
}
7472
if len(logMsg) > 0 {
75-
log.GetLogger(rw.ctx).
76-
WithField("sink", "rpc").
77-
WithField("address", rw.address).Info(logMsg)
73+
log.GetLogger(rw.ctx).Info(logMsg)
7874
}
7975
return nil
8076
}

0 commit comments

Comments
 (0)