Skip to content

Commit

Permalink
Stop printing Protobuf message as byte array 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,
  • Loading branch information
jeyhun committed Jul 27, 2023
1 parent 9b232f3 commit 7a32f03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
31 changes: 22 additions & 9 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
Id string `gorm:"primaryKey" json:"id"`
Msg []byte `json:"-"`
ParentId string `json:"parent_id,omitempty"`
Revision int64
OrgId string `gorm:"primaryKey"`
InstanceId string `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
XTableName string `gorm:"-"`
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 7a32f03

Please sign in to comment.