Skip to content

Commit c288aec

Browse files
authored
Merge pull request #22 from wish/mjudsr
add metrics
2 parents 84c0f72 + b4a7d75 commit c288aec

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

pkg/mongoproxy/plugins/schema/schema.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ var (
2828
Name: "mongoproxy_plugins_schema_config_hash",
2929
Help: "The current hash of the schema config file loaded",
3030
})
31-
3231
schemaDeny = promauto.NewCounterVec(prometheus.CounterOpts{
3332
Name: "mongoproxy_plugins_schema_deny_total",
3433
Help: "The total deny returns of a command",
3534
}, []string{"db", "collection", "command"})
35+
36+
schemaDenyLogOnly = promauto.NewCounterVec(prometheus.CounterOpts{
37+
Name: "mongoproxy_plugins_schema_deny_logonly_total",
38+
Help: "The total deny returns of a command",
39+
}, []string{"collection", "command"})
3640
)
3741

3842
const (
@@ -132,13 +136,11 @@ func (p *SchemaPlugin) Process(ctx context.Context, r *plugins.Request, next plu
132136
for _, document := range cmd.Documents {
133137
if err := schema.ValidateInsert(ctx, cmd.Database, cmd.Collection, document); err != nil {
134138
schemaDeny.WithLabelValues(cmd.Database, cmd.Collection, r.CommandName).Inc()
139+
logrus.Warningf("ENFORCE SCHEMA ERROR: %s, in db: %s, collection: %s, with cmd: %s",
140+
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
135141
if !p.conf.EnforceSchemaLogOnly {
136-
logrus.Errorf("ENFORCE SCHEMA ERROR: %s, in db: %s, collection: %s, with cmd: %s",
137-
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
138142
return mongoerror.DocumentValidationFailure.ErrMessage(err.Error()), nil
139143
}
140-
logrus.Warningf("ENFORCE SCHEMA LOGONLY: %s, in db: %s, collection: %s, with cmd: %s",
141-
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
142144
}
143145
}
144146

@@ -148,13 +150,11 @@ func (p *SchemaPlugin) Process(ctx context.Context, r *plugins.Request, next plu
148150
logrus.Debugf("command findAndModify: %s", cmd.Update)
149151
if err := schema.ValidateUpdate(ctx, cmd.Database, cmd.Collection, cmd.Update, bsonutil.GetBoolDefault(cmd.Upsert, false)); err != nil {
150152
schemaDeny.WithLabelValues(cmd.Database, cmd.Collection, r.CommandName).Inc()
153+
logrus.Warningf("ENFORCE SCHEMA ERROR: %s, in db: %s, collection: %s, with cmd: %s",
154+
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
151155
if !p.conf.EnforceSchemaLogOnly {
152-
logrus.Errorf("ENFORCE SCHEMA ERROR: %s, in db: %s, collection: %s, with cmd: %s",
153-
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
154156
return mongoerror.DocumentValidationFailure.ErrMessage(err.Error()), nil
155157
}
156-
logrus.Warningf("ENFORCE SCHEMA LOGONLY: %s, in db: %s, collection: %s, with cmd: %s",
157-
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
158158
}
159159
}
160160

@@ -164,13 +164,11 @@ func (p *SchemaPlugin) Process(ctx context.Context, r *plugins.Request, next plu
164164
logrus.Debugf("command Update wiht doc: %v", updateDoc)
165165
if err := schema.ValidateUpdate(ctx, cmd.Database, cmd.Collection, updateDoc.U, bsonutil.GetBoolDefault(updateDoc.Upsert, false)); err != nil {
166166
schemaDeny.WithLabelValues(cmd.Database, cmd.Collection, r.CommandName).Inc()
167+
logrus.Warningf("ENFORCE SCHEMA ERROR: %s, in db: %s, collection: %s, with cmd: %s",
168+
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
167169
if !p.conf.EnforceSchemaLogOnly {
168-
logrus.Errorf("ENFORCE SCHEMA ERROR: %s, in db: %s, collection: %s, with cmd: %s",
169-
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
170170
return mongoerror.DocumentValidationFailure.ErrMessage(err.Error()), nil
171171
}
172-
logrus.Warningf("ENFORCE SCHEMA LOGONLY: %s, in db: %s, collection: %s, with cmd: %s",
173-
err.Error(), cmd.Database, cmd.Collection, r.CommandName)
174172
}
175173
}
176174
}

pkg/mongoproxy/plugins/schema/types.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"reflect"
88
"strings"
99

10-
"github.com/prometheus/client_golang/prometheus"
11-
"github.com/prometheus/client_golang/prometheus/promauto"
12-
1310
"github.com/sirupsen/logrus"
1411
"go.mongodb.org/mongo-driver/bson"
1512
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -45,10 +42,6 @@ const (
4542
)
4643

4744
var OpMap = BuildUpdateOpSet()
48-
var collectionSchemaLogOnlyDeny = promauto.NewCounterVec(prometheus.CounterOpts{
49-
Name: "mongoproxy_plugins_collection_level_logonly_schema_deny_total",
50-
Help: "The total deny returns of a command",
51-
}, []string{"collection", "command"})
5245

5346
type ClusterSchema struct {
5447
MongosEndpoint string `json:"mongosEndpoint"`
@@ -164,7 +157,7 @@ func (d *Database) ValidateInsert(ctx context.Context, collection string, obj bs
164157
}
165158
if c.EnforceSchemaByCollectionLogOnly {
166159
if err := c.ValidateInsert(ctx, obj); err != nil {
167-
collectionSchemaLogOnlyDeny.WithLabelValues(collection, "insert").Inc()
160+
schemaDenyLogOnly.WithLabelValues(collection, "insert").Inc()
168161
logrus.Errorf("COLLECTION ENFORCE LOG ONLY: %s", err.Error())
169162
return nil
170163
}
@@ -184,7 +177,7 @@ func (d *Database) ValidateUpdate(ctx context.Context, collection string, obj bs
184177
}
185178
if c.EnforceSchemaByCollectionLogOnly {
186179
if err := c.ValidateUpdate(ctx, obj, upsert); err != nil {
187-
collectionSchemaLogOnlyDeny.WithLabelValues(collection, "update").Inc()
180+
schemaDenyLogOnly.WithLabelValues(collection, "update").Inc()
188181
logrus.Errorf("COLLECTION ENFORCE LOG ONLY: %s", err.Error())
189182
return nil
190183
}

0 commit comments

Comments
 (0)