Skip to content

Commit

Permalink
Stop printing Protobuf message and its metadata in logs
Browse files Browse the repository at this point in the history
Added String() method to ProtoStoreMsg, which will print out only ID, org. ID, instance ID, revision, and table name.
  • Loading branch information
jeyhun committed Jul 27, 2023
1 parent 9b232f3 commit ca5a03a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 53 deletions.
91 changes: 48 additions & 43 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func main() {
// Attempts to find a record using the incorrect context of the Prod instance and should error out.
q4 := &Person{Id: "P3"}
err = ds.Find(ProdInstanceCtx, q4)
fmt.Println(err)
fmt.Printf("err != nil - %t\n", err != nil)

// Deletes a record using the context of the Dev instance and the specified uId.
rowsAffected, err = ds.Delete(DevInstanceCtx, q1)
Expand All @@ -559,8 +559,7 @@ func main() {
[Dev/P1337] Bob: 31 <nil>
[Prod/P1337] John: 36 <nil>
[Dev/P3] Pat: 39 <nil>
Unable to locate record [record=[/P3] : 0, dbName=postgres]
record not found
err != nil - true
1 <nil>
1 <nil>
0 <nil>
Expand Down Expand Up @@ -653,7 +652,7 @@ func main() {
// Attempts to find a record using the incorrect context of the Pepsi organization and should error out.
q4 := &User{Id: "P3"}
err = ds.Find(PepsiOrgCtx, q4)
fmt.Println(err)
fmt.Printf("err != nil - %t\n", err != nil)

// Deletes a record using the context of the Coke organization and the specified uId.
rowsAffected, err = ds.Delete(CokeOrgCtx, q1)
Expand All @@ -680,8 +679,7 @@ func main() {
[Coke/P1337] Bob: 31 <nil>
[Pepsi/P1337] John: 36 <nil>
[Coke/P3] Pat: 39 <nil>
Unable to locate record [record=[/P3] : 0, dbName=postgres]
record not found
err != nil - true
1 <nil>
1 <nil>
0 <nil>
Expand Down Expand Up @@ -1028,6 +1026,7 @@ This package exposes interface \[pkg.protostore.ProtoStore\] to the consumer, wh
- [type ProtoStore](<#type-protostore>)
- [func GetProtoStore(logger *logrus.Entry, ds datastore.DataStore) ProtoStore](<#func-getprotostore>)
- [type ProtoStoreMsg](<#type-protostoremsg>)
- [func (msg *ProtoStoreMsg) String() string](<#func-protostoremsg-string>)
- [func (msg *ProtoStoreMsg) TableName() string](<#func-protostoremsg-tablename>)
- [type ProtobufDataStore](<#type-protobufdatastore>)
- [func (p ProtobufDataStore) DeleteById(ctx context.Context, id string, msg proto.Message) (int64, error)](<#func-protobufdatastore-deletebyid>)
Expand All @@ -1050,19 +1049,19 @@ This package exposes interface \[pkg.protostore.ProtoStore\] to the consumer, wh
- [func (p ProtobufDataStore) UpsertWithMetadata(ctx context.Context, id string, msg proto.Message, metadata Metadata) (rowsAffected int64, md Metadata, err error)](<#func-protobufdatastore-upsertwithmetadata>)


## func [FromBytes](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L95>)
## func [FromBytes](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L108>)

```go
func FromBytes(bytes []byte, message proto.Message) error
```

## func [ToBytes](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L103>)
## func [ToBytes](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L116>)

```go
func ToBytes(message proto.Message) ([]byte, error)
```

## type [Metadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L69-L76>)
## type [Metadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L70-L77>)

```go
type Metadata struct {
Expand All @@ -1075,13 +1074,13 @@ type Metadata struct {
}
```

### func [MetadataFrom](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L111>)
### func [MetadataFrom](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L124>)

```go
func MetadataFrom(protoStoreMsg ProtoStoreMsg) Metadata
```

## type [ProtoStore](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L44-L67>)
## type [ProtoStore](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L45-L68>)

```go
type ProtoStore interface {
Expand Down Expand Up @@ -1193,164 +1192,170 @@ DeleteById:: rowsAffected: 1 err: <nil>
</p>
</details>

### func [GetProtoStore](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L127>)
### func [GetProtoStore](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L140>)

```go
func GetProtoStore(logger *logrus.Entry, ds datastore.DataStore) ProtoStore
```

## type [ProtoStoreMsg](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L78-L89>)
## type [ProtoStoreMsg](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L79-L90>)

```go
type ProtoStoreMsg struct {
Id string `gorm:"primaryKey"`
Msg []byte
ParentId string
Revision int64
OrgId string `gorm:"primaryKey"`
InstanceId string `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
XTableName string `gorm:"-"`
Id string `gorm:"primaryKey" json:"id"`
Msg []byte `json:"-"`
ParentId string `json:"parent_id,omitempty"`
Revision int64 `json:"revision"`
OrgId string `gorm:"primaryKey" json:"org_id"`
InstanceId string `gorm:"primaryKey" json:"instance_id,omitempty"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-"`
XTableName string `gorm:"-" json:"x_table_name"`
}
```

### func \(\*ProtoStoreMsg\) [TableName](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L91>)
### func \(\*ProtoStoreMsg\) [String](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L96>)

```go
func (msg *ProtoStoreMsg) String() string
```

### func \(\*ProtoStoreMsg\) [TableName](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L92>)

```go
func (msg *ProtoStoreMsg) TableName() string
```

## type [ProtobufDataStore](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L122-L125>)
## type [ProtobufDataStore](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L135-L138>)

```go
type ProtobufDataStore struct {
// contains filtered or unexported fields
}
```

### func \(ProtobufDataStore\) [DeleteById](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L406>)
### func \(ProtobufDataStore\) [DeleteById](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L419>)

```go
func (p ProtobufDataStore) DeleteById(ctx context.Context, id string, msg proto.Message) (int64, error)
```

### func \(ProtobufDataStore\) [DropTables](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L414>)
### func \(ProtobufDataStore\) [DropTables](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L427>)

```go
func (p ProtobufDataStore) DropTables(msgs ...proto.Message) error
```

### func \(ProtobufDataStore\) [FindAll](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L349>)
### func \(ProtobufDataStore\) [FindAll](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L362>)

```go
func (p ProtobufDataStore) FindAll(ctx context.Context, msgs interface{}, pagination *datastore.Pagination) (metadataMap map[string]Metadata, err error)
```

FindAll Finds all messages \(of the same type as the element of msgs\) in Protostore and stores the result in msgs. msgs must be a pointer to a slice of Protobuf structs or a pointer to a slice of pointers to Protobuf structs. It will be modified in\-place. Returns a map of Protobuf messages' IDs to their metadata \(parent ID & revision\).

### func \(ProtobufDataStore\) [FindAllAsMap](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L291>)
### func \(ProtobufDataStore\) [FindAllAsMap](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L304>)

```go
func (p ProtobufDataStore) FindAllAsMap(ctx context.Context, msgsMap interface{}, pagination *datastore.Pagination) (metadataMap map[string]Metadata, err error)
```

### func \(ProtobufDataStore\) [FindById](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L257>)
### func \(ProtobufDataStore\) [FindById](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L270>)

```go
func (p ProtobufDataStore) FindById(ctx context.Context, id string, msg proto.Message, metadata *Metadata) error
```

Finds a Protobuf message by ID. If metadata arg. is non\-nil, fills it with the metadata \(parent ID & revision\) of the Protobuf message that was found.

### func \(ProtobufDataStore\) [GetAuthorizer](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L135>)
### func \(ProtobufDataStore\) [GetAuthorizer](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L148>)

```go
func (p ProtobufDataStore) GetAuthorizer() authorizer.Authorizer
```

### func \(ProtobufDataStore\) [GetMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L273>)
### func \(ProtobufDataStore\) [GetMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L286>)

```go
func (p ProtobufDataStore) GetMetadata(ctx context.Context, id string, msg proto.Message) (md Metadata, err error)
```

### func \(ProtobufDataStore\) [GetRevision](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L282>)
### func \(ProtobufDataStore\) [GetRevision](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L295>)

```go
func (p ProtobufDataStore) GetRevision(ctx context.Context, id string, msg proto.Message) (int64, error)
```

### func \(ProtobufDataStore\) [Insert](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L152>)
### func \(ProtobufDataStore\) [Insert](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L165>)

```go
func (p ProtobufDataStore) Insert(ctx context.Context, id string, msg proto.Message) (rowsAffected int64, md Metadata, err error)
```

@DEPRECATED See \[InsertWithMetadata\].

### func \(ProtobufDataStore\) [InsertWithMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L161>)
### func \(ProtobufDataStore\) [InsertWithMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L174>)

```go
func (p ProtobufDataStore) InsertWithMetadata(ctx context.Context, id string, msg proto.Message, metadata Metadata) (rowsAffected int64, md Metadata, err error)
```

Inserts a new Protobuf record in the DB. Returns, rowsAffected \- 0 if insertion fails; 1 otherwise md \- metadata of the new Protobuf record err \- error that occurred during insertion, if any.

### func \(ProtobufDataStore\) [MsgToFilter](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L446>)
### func \(ProtobufDataStore\) [MsgToFilter](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L459>)

```go
func (p ProtobufDataStore) MsgToFilter(ctx context.Context, id string, msg proto.Message) (pMsg *ProtoStoreMsg, err error)
```

Return the ProtoStoreMsg that can be used for filtering with id/orgId filled up and error that occurred during extraction orgId from context.

### func \(ProtobufDataStore\) [MsgToPersist](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L427>)
### func \(ProtobufDataStore\) [MsgToPersist](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L440>)

```go
func (p ProtobufDataStore) MsgToPersist(ctx context.Context, id string, msg proto.Message, md Metadata) (pMsg *ProtoStoreMsg, err error)
```

Return the serialized ProtoStoreMsg that can be persisted to database and error that occurred during extraction orgId from context, or serialization.

### func \(ProtobufDataStore\) [Register](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L139>)
### func \(ProtobufDataStore\) [Register](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L152>)

```go
func (p ProtobufDataStore) Register(ctx context.Context, roleMapping map[string]dbrole.DbRole, msgs ...proto.Message) error
```

### func \(ProtobufDataStore\) [SoftDeleteById](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L398>)
### func \(ProtobufDataStore\) [SoftDeleteById](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L411>)

```go
func (p ProtobufDataStore) SoftDeleteById(ctx context.Context, id string, msg proto.Message) (int64, error)
```

### func \(ProtobufDataStore\) [Update](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L185>)
### func \(ProtobufDataStore\) [Update](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L198>)

```go
func (p ProtobufDataStore) Update(ctx context.Context, id string, msg proto.Message) (rowsAffected int64, md Metadata, err error)
```

Update Fetches metadata for the record and updates the Protobuf message. NOTE: Avoid using this method in user\-workflows and only in service\-to\-service workflows when the updates are already ordered by some other service/app.

### func \(ProtobufDataStore\) [UpdateWithMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L198>)
### func \(ProtobufDataStore\) [UpdateWithMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L211>)

```go
func (p ProtobufDataStore) UpdateWithMetadata(ctx context.Context, id string, msg proto.Message, metadata Metadata) (rowsAffected int64, md Metadata, err error)
```

Updates an existing Protobuf record in the DB. Returns, rowsAffected \- 0 if update fails; 1 otherwise md \- metadata of the updated Protobuf record err \- error that occurred during update, if any.

### func \(ProtobufDataStore\) [Upsert](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L217>)
### func \(ProtobufDataStore\) [Upsert](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L230>)

```go
func (p ProtobufDataStore) Upsert(ctx context.Context, id string, msg proto.Message) (rowsAffected int64, md Metadata, err error)
```

Upsert Fetches metadata for the record and upserts the Protobuf message. NOTE: Avoid using this method in user\-workflows and only in service\-to\-service workflows when the updates are already ordered by some other service/app.

### func \(ProtobufDataStore\) [UpsertWithMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L235-L237>)
### func \(ProtobufDataStore\) [UpsertWithMetadata](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/protostore/protostore.go#L248-L250>)

```go
func (p ProtobufDataStore) UpsertWithMetadata(ctx context.Context, id string, msg proto.Message, metadata Metadata) (rowsAffected int64, md Metadata, err error)
Expand Down
33 changes: 23 additions & 10 deletions pkg/protostore/protostore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package protostore

import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -76,22 +77,34 @@ type Metadata struct {
}

type ProtoStoreMsg struct {
Id string `gorm:"primaryKey"`
Msg []byte
ParentId string
Revision int64
OrgId string `gorm:"primaryKey"`
InstanceId string `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
XTableName string `gorm:"-"`
Id string `gorm:"primaryKey" json:"id"`
Msg []byte `json:"-"`
ParentId string `json:"parent_id,omitempty"`
Revision int64 `json:"revision"`
OrgId string `gorm:"primaryKey" json:"org_id"`
InstanceId string `gorm:"primaryKey" json:"instance_id,omitempty"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-"`
XTableName string `gorm:"-" json:"x_table_name"`
}

func (msg *ProtoStoreMsg) TableName() string {
return msg.XTableName
}

func (msg *ProtoStoreMsg) String() string {
if msg == nil {
return "{}"
}

if bytes, err := json.MarshalIndent(msg, "", "\t"); err != nil {
return ""
} else {
return string(bytes)
}
}

func FromBytes(bytes []byte, message proto.Message) error {
err := proto.Unmarshal(bytes, message)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/protostore/protostore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package protostore_test

import (
"context"
"fmt"
"io"
"sort"
"testing"
Expand Down Expand Up @@ -699,3 +700,28 @@ func BenchmarkCrudProtoStoreInDb(b *testing.B) {
testProtoStoreCrud(&t, p, AmericasCokeAdminCtx, true)
}
}

func TestProtoStoreMsg_String(t *testing.T) {
assert := assert.New(t)
p := setupDbContext(t, "TestProtoStoreMsg_String")
const id = "some-key"
var cpu = &pb.CPU{Brand: "Intel"}
var md = protostore.Metadata{Id: id, Revision: 10}
var orgId, _ = p.GetAuthorizer().GetOrgFromContext(AmericasPepsiAdminCtx)
pMsg, err := p.MsgToPersist(AmericasPepsiAdminCtx, id, cpu, md)
if err != nil {
assert.FailNow("Failed to generate ProtoStoreMsg", err)
}
var isJson = func(str string) bool { return len(str) >= 2 && str[0] == '{' && str[len(str)-1] == '}' }
for _, pMsgStr := range []string{pMsg.String(), fmt.Sprintf("%+v", pMsg)} {
assert.True(isJson(pMsgStr), "Expected string version of ProtoStoreMsg to be a JSON string")
assert.Contains(pMsgStr, id, "Expected unique ID of ProtoStoreMsg to be printed")
assert.Contains(pMsgStr, orgId, "Expected org. ID to be printed")
assert.NotContains(pMsgStr, cpu.Brand, "Expected Protobuf message contents not to be printed")
assert.NotContains(pMsgStr, "CreatedAt", "Expected Protobuf message contents not to be printed")
assert.NotContains(pMsgStr, "UpdatedAt", "Expected Protobuf message contents not to be printed")
assert.NotContains(pMsgStr, "DeletedAt", "Expected Protobuf message contents not to be printed")
}
assert.Equal(pMsg.String(), fmt.Sprintf("%+v", pMsg),
"Expected fmt.Printf(\"%+v\", pMsg) to print the same JSON as pMsg.String()")
}

0 comments on commit ca5a03a

Please sign in to comment.