Skip to content

Commit

Permalink
Improve logging at trace level
Browse files Browse the repository at this point in the history
Removed or improved trace-level logging statements which were unnecessary
or generated hard-to-read logs
  • Loading branch information
jeyhun committed Aug 7, 2023
1 parent 63463fe commit 89c6a9c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
12 changes: 7 additions & 5 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Checks if a Postgres DB exists and returns true.
func GetCompLogger() *logrus.Entry
```

## func [GetFieldValue](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L400>)
## func [GetFieldValue](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L399>)

```go
func GetFieldValue(record Record, fieldName, columnName string) (string, bool)
Expand All @@ -300,7 +300,7 @@ Returns the requested fields value from record, which is a pointer to a struct i
func GetGormLogger(l *logrus.Entry) logger.Interface
```

## func [GetInstanceId](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L388>)
## func [GetInstanceId](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L387>)

```go
func GetInstanceId(record Record) (string, bool)
Expand All @@ -320,7 +320,7 @@ func GetLogLevel() string
func GetLogger() *logrus.Logger
```

## func [GetOrgId](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L380>)
## func [GetOrgId](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L379>)

```go
func GetOrgId(record Record) (string, bool)
Expand Down Expand Up @@ -380,13 +380,13 @@ func IsRowLevelSecurityRequired(record Record, tableName string, instancerConfig

Row Level Security to used to partition tables for multi\-tenancy and multi\-instance support.

## func [SetFieldValue](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L421>)
## func [SetFieldValue](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L420>)

```go
func SetFieldValue(record Record, fieldName, columnName, value string) bool
```

## func [SetInstanceId](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L392>)
## func [SetInstanceId](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/sql_struct.go#L391>)

```go
func SetInstanceId(record Record, value string) bool
Expand All @@ -398,6 +398,8 @@ func SetInstanceId(record Record, value string) bool
func TypeName(x interface{}) string
```

TypeName returns name of the data type of the given variable

## type [DBConfig](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L50-L57>)

```go
Expand Down
2 changes: 1 addition & 1 deletion pkg/datastore/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func (db *relationalDb) RegisterHelper(_ context.Context, roleMapping map[string
roleMapping = make(map[string]dbrole.DbRole)
}

db.logger.Debugf("Registering the struct %q with DAL (backed by Postgres)... Using authorizer %s...", tableName, GetTableName(db.authorizer))
db.logger.Debugf("Registering the struct %q with DAL... Using authorizer %s...", tableName, TypeName(db.authorizer))

tx, err := db.GetDBConn(dbrole.MAIN)
if err != nil {
Expand Down
42 changes: 39 additions & 3 deletions pkg/datastore/db_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
package datastore

import (
"encoding/json"
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
"hash/fnv"
"os"
"strconv"

"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
)

// Specifications for database user.
Expand All @@ -36,6 +36,41 @@ type dbUserSpec struct {
newRowsCond string // SQL conditional expression to be checked for rows being inserted or updated. Only those rows for which the condition is true will be inserted/updated
}

func (m dbUserSpec) MarshalJSON() ([]byte, error) {
data := struct {
Username dbrole.DbRole
Password string
PolicyName string
Commands []string
ExistingRowsCond string
NewRowsCond string
}{
Username: m.username,
Password: "*****",
PolicyName: m.policyName,
Commands: m.commands,
ExistingRowsCond: m.existingRowsCond,
NewRowsCond: m.newRowsCond,
}

// Marshal the anonymous struct to JSON
jsonData, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return nil, err
}

return jsonData, nil
}

func (spec dbUserSpec) String() string {
jsonBytes, err := spec.MarshalJSON()
if err != nil {
return "{}"
}

return string(jsonBytes)
}

func getDbUser(dbRole dbrole.DbRole) dbUserSpec {
for _, spec := range getAllDbUsers() {
if spec.username == dbRole {
Expand Down Expand Up @@ -105,7 +140,8 @@ func getDbUsers(tableName string, withTenantIdCheck, withInstanceIdCheck bool) [
dbUsers[i].password = getPassword(string(dbUsers[i].username))
dbUsers[i].policyName = getRlsPolicyName(string(dbUsers[i].username), tableName)
}
TRACE("Returning db user specs for %s, %s, %s: %+v", tableName, withTenantIdCheck, withInstanceIdCheck, dbUsers)
TRACE("Returning DB user specs for table %q:\n\twithTenantIdCheck - %t\n\twithInstanceIdCheck - %t\n\tdbUsers - %+v\n",
tableName, withTenantIdCheck, withInstanceIdCheck, dbUsers)
return dbUsers
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/datastore/db_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package datastore

Check failure on line 1 in pkg/datastore/db_user_test.go

View workflow job for this annotation

GitHub Actions / Check lint

package should be `datastore_test` instead of `datastore` (testpackage)

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/dbrole"
"testing"
)

func TestDbUserSpecString(t *testing.T) {
assert := assert.New(t)
dbUserSpec := dbUserSpec{
username: dbrole.WRITER,
password: "1234546789",
policyName: "policy_name",
commands: []string{"SELECT", "INSERT", "UPDATE", "DELETE"},
existingRowsCond: "true",
newRowsCond: "true",
}

dbUserSpecJson := dbUserSpec.String()
assert.True(json.Valid([]byte(dbUserSpecJson)), "Expected string version of dbUserSpec to be a valid JSON")
assert.NotEqual("{}", dbUserSpecJson, "Expected string version of dbUserSpec not to be an empty JSON struct")
assert.NotContains(dbUserSpecJson, dbUserSpec.password, "Expected string version of dbUserSpec not to contain DB user's password")
}
7 changes: 3 additions & 4 deletions pkg/datastore/sql_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func getCreatePolicyStmt(tableName string, _ Record, dbUser dbUserSpec) string {

func typeName(x interface{}, prefix string) string {
t := reflect.TypeOf(x)
TRACE("type for %+v is %s\n", x, t.Kind())
for t.Kind() == reflect.Ptr {
t = t.Elem()
prefix += "*"
Expand All @@ -215,6 +214,7 @@ func typeName(x interface{}, prefix string) string {
return prefix + t.Name()
}

// TypeName returns name of the data type of the given variable

Check failure on line 217 in pkg/datastore/sql_struct.go

View workflow job for this annotation

GitHub Actions / Check lint

Comment should end in a period (godot)
func TypeName(x interface{}) string {
return typeName(x, "")
}
Expand Down Expand Up @@ -245,9 +245,8 @@ func GetTableName(x interface{}) (tableName string) {
tableNameMap[typ] = s.Table
}
tableName = tableNameMap[typ]
TRACE("TableName is %s for %s(%+v)\n", tableName, typ, x)
TRACE("TableName is %q for %s\n", tableName, typ)
}
TRACE("TableNameMap is %+v\n", tableNameMap)
return tableName
}

Expand Down Expand Up @@ -331,7 +330,7 @@ func getCheckAndUpdateRevisionFunc() (functionName, functionBody string) {
stmt.WriteString(COLUMN_REVISION)
stmt.WriteString(";\n")
stmt.WriteString("\t\tEND IF;")
return "check_and_update_revision", stmt.String()
return "check_and_update_revision", stmt.String() //TODO add version # to function name

Check failure on line 333 in pkg/datastore/sql_struct.go

View workflow job for this annotation

GitHub Actions / Check lint

commentFormatting: put a space between `//` and comment text (gocritic)
}

func getCreateTriggerFunctionStmt(functionName, functionBody string) string {
Expand Down

0 comments on commit 89c6a9c

Please sign in to comment.