Skip to content

Commit

Permalink
refactor: update some tracing operations on organization repository 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
qnen committed Nov 8, 2024
1 parent 4c7944b commit e258d16
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ func (r *OrganizationPostgreSQLRepository) Create(ctx context.Context, organizat
record := &o.OrganizationPostgreSQLModel{}
record.FromEntity(organization)

err = mopentelemetry.SetSpanAttributesFromStruct(&span, "organization_repository_output", record)
address, err := json.Marshal(record.Address)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to convert organization record from entity to JSON string", err)
mopentelemetry.HandleSpanError(&span, "Failed to marshal address", err)

return nil, err
}

address, err := json.Marshal(record.Address)
ctx, spanExec := tracer.Start(ctx, "postgres.create.exec")

err = mopentelemetry.SetSpanAttributesFromStruct(&spanExec, "organization_repository_input", record)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to marshal address", err)
mopentelemetry.HandleSpanError(&spanExec, "Failed to convert organization record from entity to JSON string", err)

return nil, err
}

ctx, spanExec := tracer.Start(ctx, "postgres.create.exec")

result, err := db.ExecContext(ctx, `INSERT INTO organization VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *`,
record.ID,
record.ParentOrganizationID,
Expand Down Expand Up @@ -110,9 +110,9 @@ func (r *OrganizationPostgreSQLRepository) Create(ctx context.Context, organizat
}

if rowsAffected == 0 {
mopentelemetry.HandleSpanError(&span, "Failed to create organization. Rows affected is 0", err)
err := common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())

err = common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())
mopentelemetry.HandleSpanError(&span, "Failed to create organization. Rows affected is 0", err)

return nil, err
}
Expand All @@ -137,13 +137,6 @@ func (r *OrganizationPostgreSQLRepository) Update(ctx context.Context, id uuid.U
record := &o.OrganizationPostgreSQLModel{}
record.FromEntity(organization)

err = mopentelemetry.SetSpanAttributesFromStruct(&span, "organization_repository_output", record)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to convert organization record from entity to JSON string", err)

return nil, err
}

var updates []string

var args []any
Expand Down Expand Up @@ -194,6 +187,13 @@ func (r *OrganizationPostgreSQLRepository) Update(ctx context.Context, id uuid.U

ctx, spanExec := tracer.Start(ctx, "postgres.update.exec")

err = mopentelemetry.SetSpanAttributesFromStruct(&spanExec, "organization_repository_input", record)
if err != nil {
mopentelemetry.HandleSpanError(&spanExec, "Failed to convert organization record from entity to JSON string", err)

return nil, err
}

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
mopentelemetry.HandleSpanError(&spanExec, "Failed to execute query", err)
Expand All @@ -216,9 +216,11 @@ func (r *OrganizationPostgreSQLRepository) Update(ctx context.Context, id uuid.U
}

if rowsAffected == 0 {
err := common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())

mopentelemetry.HandleSpanError(&span, "Failed to update organization. Rows affected is 0", err)

return nil, common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())
return nil, err
}

return record.ToEntity(), nil
Expand All @@ -245,10 +247,13 @@ func (r *OrganizationPostgreSQLRepository) Find(ctx context.Context, id uuid.UUI
ctx, spanQuery := tracer.Start(ctx, "postgres.find.query")

row := db.QueryRowContext(ctx, `SELECT * FROM organization WHERE id = $1`, id)

spanQuery.End()

if err := row.Scan(&organization.ID, &organization.ParentOrganizationID, &organization.LegalName,
&organization.DoingBusinessAs, &organization.LegalDocument, &address, &organization.Status, &organization.StatusDescription,
&organization.CreatedAt, &organization.UpdatedAt, &organization.DeletedAt); err != nil {
mopentelemetry.HandleSpanError(&spanQuery, "Failed to scan row", err)
mopentelemetry.HandleSpanError(&span, "Failed to scan row", err)

if errors.Is(err, sql.ErrNoRows) {
return nil, common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())
Expand All @@ -257,8 +262,6 @@ func (r *OrganizationPostgreSQLRepository) Find(ctx context.Context, id uuid.UUI
return nil, err
}

spanQuery.End()

err = json.Unmarshal([]byte(address), &organization.Address)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to unmarshal address", err)
Expand Down Expand Up @@ -438,9 +441,11 @@ func (r *OrganizationPostgreSQLRepository) Delete(ctx context.Context, id uuid.U
}

if rowsAffected == 0 {
err := common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())

mopentelemetry.HandleSpanError(&span, "Failed to delete organization. Rows affected is 0", err)

return common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(o.Organization{}).Name())
return err
}

return nil
Expand Down

0 comments on commit e258d16

Please sign in to comment.