Skip to content

Commit

Permalink
feature: improve Sources' logging
Browse files Browse the repository at this point in the history
These changes make sure that we are informed about useful events while
Sorces is running, especially those ones that modify our database.

By having these logs, we might be able to increase the default logging
level to "INFO" and reduce the noise in our logs, while maintaining a
good level of debuggability.

RHCLOUD-36959
  • Loading branch information
MikelAlejoBR committed Jan 15, 2025
1 parent be02f38 commit f6e50e4
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 33 deletions.
24 changes: 21 additions & 3 deletions dao/application_authentication_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"

"github.com/RedHatInsights/sources-api-go/config"
"github.com/RedHatInsights/sources-api-go/logger"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -157,14 +159,26 @@ func (a *applicationAuthenticationDaoImpl) Create(appAuth *m.ApplicationAuthenti
appAuth.TenantID = *a.TenantID
err := DB.Debug().Create(appAuth).Error
if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID}).Errorf(`Unable to create application authentication: %s`, err)

return util.NewErrBadRequest("failed to create application_authentication: " + err.Error())
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_authentication_id": appAuth.ID}).Info("Application authentication created")
return nil
}
return err
}

func (a *applicationAuthenticationDaoImpl) Update(appAuth *m.ApplicationAuthentication) error {
result := a.getDb().Updates(appAuth)
return result.Error
err := a.getDb().Updates(appAuth).Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_authentication_id": appAuth.ID}).Errorf(`Unable to update application authentication: %s`, err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_authentication_id": appAuth.ID}).Info("Application authentication updated")
return nil
}
}

func (a *applicationAuthenticationDaoImpl) Delete(id *int64) (*m.ApplicationAuthentication, error) {
Expand All @@ -177,13 +191,17 @@ func (a *applicationAuthenticationDaoImpl) Delete(id *int64) (*m.ApplicationAuth
Delete(&applicationAuthentication)

if result.Error != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_authentication_id": id}).Errorf(`Unable to delete application authentication: %s`, result.Error)

return nil, fmt.Errorf(`failed to delete application authentication with id "%d": %s`, id, result.Error)
}

if result.RowsAffected == 0 {
return nil, util.NewErrNotFound("application authentication")
}

logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_authentication_id": id}).Info("Application authentication deleted")

return &applicationAuthentication, nil
}

Expand Down
71 changes: 63 additions & 8 deletions dao/application_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"strings"
"time"

"github.com/RedHatInsights/sources-api-go/logger"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/jackc/pgconn"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -156,23 +158,41 @@ func (a *applicationDaoImpl) GetByIdWithPreload(id *int64, preloads ...string) (

func (a *applicationDaoImpl) Create(app *m.Application) error {
app.TenantID = *a.TenantID
result := DB.Debug().Create(app)
err := DB.Debug().Create(app).Error

// Check if specific error code is returned
var pgErr *pgconn.PgError
if errors.As(result.Error, &pgErr) {
if errors.As(err, &pgErr) {
// unique constraint violation for index (source id + app type id + tenant id)
if pgErr.Code == PgUniqueConstraintViolation && strings.Contains(pgErr.Detail, "Key (source_id, application_type_id, tenant_id)") {
message := fmt.Sprintf("Application of application type = %d already exists for the source id = %d", app.ApplicationTypeID, app.SourceID)
return util.NewErrBadRequest(message)
}
}
return result.Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": app.SourceID}).Errorf("Unable to create application: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": app.SourceID, "application_id": app.ID}).Info("Application created")

return nil
}
}

func (a *applicationDaoImpl) Update(app *m.Application) error {
result := a.getDb().Omit(clause.Associations).Updates(app)
return result.Error
err := a.getDb().Omit(clause.Associations).Updates(app).Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": app.SourceID, "application_id": app.ID}).Errorf("Unable to update application: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": app.SourceID, "application_id": app.ID}).Info("Application updated")

return nil
}
}

func (a *applicationDaoImpl) Delete(id *int64) (*m.Application, error) {
Expand All @@ -184,13 +204,17 @@ func (a *applicationDaoImpl) Delete(id *int64) (*m.Application, error) {
Delete(&application)

if result.Error != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": *id}).Errorf("Unable to delete application: %s", result.Error)

return nil, fmt.Errorf(`failed to delete application with id "%d": %s`, id, result.Error)
}

if result.RowsAffected == 0 {
return nil, util.NewErrNotFound("application")
}

logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": *id}).Info("Application deleted")

return &application, nil
}

Expand Down Expand Up @@ -282,7 +306,15 @@ func (a *applicationDaoImpl) Pause(id int64) error {
Update("paused_at", time.Now()).
Error

return err
if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": id}).Errorf("Unable to pause application: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": id}).Info("Application paused")

return nil
}
}

func (a *applicationDaoImpl) Unpause(id int64) error {
Expand All @@ -291,7 +323,15 @@ func (a *applicationDaoImpl) Unpause(id int64) error {
Update("paused_at", nil).
Error

return err
if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": id}).Errorf("Unable to resume application: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": id}).Info("Application resumed")

return nil
}
}

func (a *applicationDaoImpl) DeleteCascade(applicationId int64) ([]m.ApplicationAuthentication, *m.Application, error) {
Expand Down Expand Up @@ -327,6 +367,8 @@ func (a *applicationDaoImpl) DeleteCascade(applicationId int64) ([]m.Application
Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "application_id": applicationId}).Errorf("Unable to cascade delete application: unable to delete application authentications: %s", err)

return err
}
}
Expand All @@ -346,9 +388,22 @@ func (a *applicationDaoImpl) DeleteCascade(applicationId int64) ([]m.Application
Error
}

return err
if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": application.SourceID, "application_id": applicationId}).Errorf("Unable to cascade delete application: %s", err)

return err
} else {
return nil
}
})

// Log all the changes for observability, traceability and debugging purposes.
for _, appAuth := range applicationAuthentications {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": application.SourceID, "application_id": applicationId, "application_authentication_id": appAuth.ID}).Info("Application authentication deleted")
}

logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": application.SourceID, "application_id": applicationId}).Info("Application deleted")

if err != nil {
return nil, nil, err
}
Expand Down
40 changes: 36 additions & 4 deletions dao/authentication_db_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"strings"

"github.com/RedHatInsights/sources-api-go/logger"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -302,10 +304,20 @@ func (add *authenticationDaoDbImpl) Create(authentication *m.Authentication) err

authentication.TenantID = *add.TenantID // the TenantID gets injected in the middleware

return DB.
err := DB.
Debug().
Create(authentication).
Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": authentication.ResourceType, "resource_id": authentication.ResourceID}).Errorf("Unable to create authentication: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": authentication.ResourceType, "resource_id": authentication.ResourceID, "authentication_id": authentication.ID}).Info("Authentication created")

return nil
}
}

// BulkCreate method _without_ checking if the resource exists. Basically since this is the bulk-create method the
Expand All @@ -316,10 +328,20 @@ func (add *authenticationDaoDbImpl) BulkCreate(auth *m.Authentication) error {
}

func (add *authenticationDaoDbImpl) Update(authentication *m.Authentication) error {
return add.getDb().
err := add.getDb().
Omit(clause.Associations).
Updates(authentication).
Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": authentication.ResourceType, "resource_id": authentication.ResourceID}).Errorf("Unable to update authentication: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": authentication.ResourceType, "resource_id": authentication.ResourceID, "authentication_id": authentication.ID}).Info("Authentication updated")

return nil
}
}

func (add *authenticationDaoDbImpl) Delete(id string) (*m.Authentication, error) {
Expand All @@ -339,10 +361,14 @@ func (add *authenticationDaoDbImpl) Delete(id string) (*m.Authentication, error)
Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": authentication.ResourceType, "resource_id": authentication.ResourceID}).Errorf("Unable to delete authentication: %s", err)

return nil, fmt.Errorf(`failed to delete authentication with id "%s"`, id)
}
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": authentication.ResourceType, "resource_id": authentication.ResourceID, "authentication_id": authentication.ID}).Info("Authentication deleted")

return &authentication, nil
return &authentication, nil
}
}

func (add *authenticationDaoDbImpl) Tenant() *int64 {
Expand Down Expand Up @@ -477,9 +503,15 @@ func (add *authenticationDaoDbImpl) BulkDelete(authentications []m.Authenticatio
Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "authentication_ids": authIds}).Errorf("Unable to bulk delete authentications: %s", err)

return nil, err
}
}

for _, auth := range dbAuths {
logger.Log.WithFields(logrus.Fields{"tenant_id": *add.TenantID, "resource_type": auth.ResourceType, "resource_id": auth.ResourceID, "authentication_id": auth}).Info("Authentication deleted")
}

return dbAuths, nil
}
2 changes: 1 addition & 1 deletion dao/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GetFromResourceType(resourceType string, tenantID int64) (m.EventModelDao,
case "source":
resource = GetSourceDao(&RequestParams{TenantID: &tenantID})
case "endpoint":
resource = GetEndpointDao(nil)
resource = GetEndpointDao(&tenantID)
case "application":
resource = GetApplicationDao(&RequestParams{TenantID: &tenantID})
case "authentication":
Expand Down
45 changes: 38 additions & 7 deletions dao/endpoint_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"fmt"

"github.com/RedHatInsights/sources-api-go/logger"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/sirupsen/logrus"
"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -112,16 +114,33 @@ func (a *endpointDaoImpl) GetById(id *int64) (*m.Endpoint, error) {
return &endpoint, nil
}

func (a *endpointDaoImpl) Create(app *m.Endpoint) error {
app.TenantID = *a.TenantID
func (a *endpointDaoImpl) Create(endpoint *m.Endpoint) error {
endpoint.TenantID = *a.TenantID

result := DB.Debug().Create(app)
return result.Error
err := DB.Debug().Create(endpoint).Error
if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": endpoint.SourceID}).Errorf("Unable to create endpoint: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": endpoint.SourceID, "endpoint_id": endpoint.ID}).Info("Endpoint created")

return nil
}
}

func (a *endpointDaoImpl) Update(app *m.Endpoint) error {
result := DB.Omit(clause.Associations).Updates(app)
return result.Error
func (a *endpointDaoImpl) Update(endpoint *m.Endpoint) error {
err := DB.Omit(clause.Associations).Updates(endpoint).Error

if err != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": endpoint.SourceID, "endpoint_id": endpoint.ID}).Errorf("Unable to update endpoint: %s", err)

return err
} else {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": endpoint.SourceID, "endpoint_id": endpoint.ID}).Info("Endpoint updated")

return nil
}
}

func (a *endpointDaoImpl) Delete(id *int64) (*m.Endpoint, error) {
Expand All @@ -135,13 +154,17 @@ func (a *endpointDaoImpl) Delete(id *int64) (*m.Endpoint, error) {
Delete(&endpoint)

if result.Error != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": endpoint.SourceID, "endpoint_id": *id}).Errorf("Unable to delete endpoint: %s", result.Error)

return nil, fmt.Errorf(`failed to delete endpoint with id "%d": %s`, id, result.Error)
}

if result.RowsAffected == 0 {
return nil, util.NewErrNotFound("endpoint")
}

logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "source_id": endpoint.SourceID, "endpoint_id": *id}).Info("Endpoint deleted")

return &endpoint, nil
}

Expand Down Expand Up @@ -199,10 +222,18 @@ func (a *endpointDaoImpl) FetchAndUpdateBy(resource util.Resource, updateAttribu
Where("id = ?", resource.ResourceID).
Updates(updateAttributes)

if result.Error != nil {
logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "resource_type": resource.ResourceType, "resource_id": resource.ResourceID}).Errorf("Unable to update endpoint: %s", result.Error)

return nil, result.Error
}

if result.RowsAffected == 0 {
return nil, fmt.Errorf("endpoint not found %v", resource)
}

logger.Log.WithFields(logrus.Fields{"tenant_id": *a.TenantID, "resource_type": resource.ResourceType, "resource_id": resource.ResourceID}).Info("Endpoint updated")

a.TenantID = &resource.TenantID
endpoint, err := a.GetByIdWithPreload(&resource.ResourceID, "Source")
if err != nil {
Expand Down
Loading

0 comments on commit f6e50e4

Please sign in to comment.