From 89a517ec34f007f1527616c3a4128feaaaaeb186 Mon Sep 17 00:00:00 2001 From: Kush <3647166+kushsharma@users.noreply.github.com> Date: Thu, 1 Jun 2023 12:43:59 +0530 Subject: [PATCH] fix: refactor new relic telemetry to a common method (#250) Signed-off-by: Kush Sharma --- internal/store/postgres/flow_repository.go | 72 ++------- internal/store/postgres/group_repository.go | 118 ++------------- .../store/postgres/invitation_repository.go | 66 +------- .../store/postgres/metaschema_repository.go | 66 +------- .../store/postgres/namespace_repository.go | 42 +---- .../store/postgres/organization_repository.go | 113 ++------------ .../store/postgres/permission_repository.go | 79 +--------- internal/store/postgres/policy_repository.go | 63 +------- internal/store/postgres/project_repository.go | 115 ++------------ .../store/postgres/relation_repository.go | 79 +--------- .../store/postgres/resource_repository.go | 92 +---------- internal/store/postgres/role_repository.go | 79 +--------- internal/store/postgres/session_repository.go | 66 +------- internal/store/postgres/user_repository.go | 143 ++---------------- pkg/db/db.go | 16 +- 15 files changed, 115 insertions(+), 1094 deletions(-) diff --git a/internal/store/postgres/flow_repository.go b/internal/store/postgres/flow_repository.go index 3e51fc463..f0957229b 100644 --- a/internal/store/postgres/flow_repository.go +++ b/internal/store/postgres/flow_repository.go @@ -8,7 +8,6 @@ import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/salt/log" "github.com/odpf/shield/core/authenticate" "github.com/odpf/shield/pkg/db" @@ -65,18 +64,7 @@ func (s *FlowRepository) Set(ctx context.Context, flow *authenticate.Flow) error } var flowModel Flow - if err = s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_FLOWS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = s.dbc.WithTimeout(ctx, TABLE_FLOWS, "Set", func(ctx context.Context) error { return s.dbc.QueryRowxContext(ctx, query, params...).StructScan(&flowModel) }); err != nil { err = checkPostgresError(err) @@ -98,18 +86,7 @@ func (s *FlowRepository) Get(ctx context.Context, id uuid.UUID) (*authenticate.F return nil, fmt.Errorf("%w: %s", queryErr, err) } - if err = s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_FLOWS, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = s.dbc.WithTimeout(ctx, TABLE_FLOWS, "Get", func(ctx context.Context) error { return s.dbc.QueryRowxContext(ctx, query, params...).StructScan(&flowModel) }); err != nil { err = checkPostgresError(err) @@ -130,30 +107,20 @@ func (s *FlowRepository) Delete(ctx context.Context, id uuid.UUID) error { return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_FLOWS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), + return s.dbc.WithTimeout(ctx, TABLE_FLOWS, + "Delete", func(ctx context.Context) error { + result, err := s.dbc.ExecContext(ctx, query, params...) + if err != nil { + err = checkPostgresError(err) + return fmt.Errorf("%w: %s", dbErr, err) } - defer nr.End() - } - result, err := s.dbc.ExecContext(ctx, query, params...) - if err != nil { - err = checkPostgresError(err) - return fmt.Errorf("%w: %s", dbErr, err) - } - - if count, _ := result.RowsAffected(); count > 0 { - return nil - } + if count, _ := result.RowsAffected(); count > 0 { + return nil + } - return fmt.Errorf("no entry to delete") - }) + return fmt.Errorf("no entry to delete") + }) } func (s *FlowRepository) DeleteExpiredFlows(ctx context.Context) error { @@ -167,18 +134,7 @@ func (s *FlowRepository) DeleteExpiredFlows(ctx context.Context) error { return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_FLOWS, - Operation: "DeleteExpiredFlows", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return s.dbc.WithTimeout(ctx, TABLE_FLOWS, "DeleteExpiredFlows", func(ctx context.Context) error { result, err := s.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) diff --git a/internal/store/postgres/group_repository.go b/internal/store/postgres/group_repository.go index 352b1f9e6..e4fa74129 100644 --- a/internal/store/postgres/group_repository.go +++ b/internal/store/postgres/group_repository.go @@ -13,7 +13,6 @@ import ( "github.com/odpf/shield/core/user" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/group" "github.com/odpf/shield/core/namespace" "github.com/odpf/shield/core/organization" @@ -54,18 +53,7 @@ func (r GroupRepository) GetByID(ctx context.Context, id string) (group.Group, e } var groupModel Group - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "GetByID", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "GetByID", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &groupModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -106,18 +94,7 @@ func (r GroupRepository) GetByIDs(ctx context.Context, groupIDs []string) ([]gro // this query will return empty array of groups if no UUID is not matched // TODO: check and fox what to do in this scenerio - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "GetByIDs", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "GetByIDs", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedGroups, query, params...) }); err != nil { err = checkPostgresError(err) @@ -169,18 +146,7 @@ func (r GroupRepository) Create(ctx context.Context, grp group.Group) (group.Gro } var groupModel Group - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&groupModel) }); err != nil { err = checkPostgresError(err) @@ -221,18 +187,7 @@ func (r GroupRepository) List(ctx context.Context, flt group.Filter) ([]group.Gr } var fetchedGroups []Group - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedGroups, query, params...) }); err != nil { err = checkPostgresError(err) @@ -287,18 +242,7 @@ func (r GroupRepository) UpdateByID(ctx context.Context, grp group.Group) (group } var groupModel Group - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&groupModel) }); err != nil { err = checkPostgresError(err) @@ -362,18 +306,7 @@ func (r GroupRepository) ListUserGroups(ctx context.Context, userID string, role } var fetchedGroups []Group - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "ListUserGroups", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "ListUserGroups", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedGroups, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -419,18 +352,7 @@ func (r GroupRepository) ListGroupRelations(ctx context.Context, objectId string } var fetchedRelations []Relation - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "ListGroupRelations", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "ListGroupRelations", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedRelations, query, params...) }); err != nil { // List should return empty list and no error instead @@ -461,18 +383,7 @@ func (r GroupRepository) SetState(ctx context.Context, id string, state group.St return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "SetState", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "SetState", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } @@ -499,18 +410,7 @@ func (r GroupRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_GROUPS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_GROUPS, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/invitation_repository.go b/internal/store/postgres/invitation_repository.go index 72a453d18..551f70282 100644 --- a/internal/store/postgres/invitation_repository.go +++ b/internal/store/postgres/invitation_repository.go @@ -12,7 +12,6 @@ import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/salt/log" "github.com/odpf/shield/pkg/db" ) @@ -65,18 +64,7 @@ func (s *InvitationRepository) Set(ctx context.Context, invite invitation.Invita } var inviteModel Invitation - if err = s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_INVITATIONS, - Operation: "Set", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = s.dbc.WithTimeout(ctx, TABLE_INVITATIONS, "Set", func(ctx context.Context) error { return s.dbc.QueryRowxContext(ctx, query, params...).StructScan(&inviteModel) }); err != nil { err = checkPostgresError(err) @@ -97,18 +85,7 @@ func (s *InvitationRepository) Get(ctx context.Context, id uuid.UUID) (invitatio return invitation.Invitation{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_INVITATIONS, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = s.dbc.WithTimeout(ctx, TABLE_INVITATIONS, "Get", func(ctx context.Context) error { return s.dbc.QueryRowxContext(ctx, query, params...).StructScan(&inviteModel) }); err != nil { err = checkPostgresError(err) @@ -141,18 +118,7 @@ func (s *InvitationRepository) List(ctx context.Context, flt invitation.Filter) return nil, fmt.Errorf("%w: %s", queryErr, err) } - if err = s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = s.dbc.WithTimeout(ctx, TABLE_INVITATIONS, "List", func(ctx context.Context) error { return s.dbc.SelectContext(ctx, &fetchedInvitations, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -184,18 +150,7 @@ func (s *InvitationRepository) Delete(ctx context.Context, id uuid.UUID) error { return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_INVITATIONS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return s.dbc.WithTimeout(ctx, TABLE_INVITATIONS, "Delete", func(ctx context.Context) error { _, err := s.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) @@ -218,18 +173,7 @@ func (s *InvitationRepository) GarbageCollect(ctx context.Context) error { return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_INVITATIONS, - Operation: "GarbageCollect", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return s.dbc.WithTimeout(ctx, TABLE_INVITATIONS, "GarbageCollect", func(ctx context.Context) error { result, err := s.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) diff --git a/internal/store/postgres/metaschema_repository.go b/internal/store/postgres/metaschema_repository.go index 7487b01af..58cc8fce7 100644 --- a/internal/store/postgres/metaschema_repository.go +++ b/internal/store/postgres/metaschema_repository.go @@ -12,7 +12,6 @@ import ( "github.com/pkg/errors" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/metaschema" "github.com/odpf/shield/pkg/db" ) @@ -70,18 +69,7 @@ func (m MetaSchemaRepository) Get(ctx context.Context, id string) (metaschema.Me } var fetchedMetaSchema MetaSchema - if err = m.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_METASCHEMA, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = m.dbc.WithTimeout(ctx, TABLE_METASCHEMA, "Get", func(ctx context.Context) error { return m.dbc.QueryRowxContext(ctx, query, params...).StructScan(&fetchedMetaSchema) }); err != nil { err = checkPostgresError(err) @@ -115,18 +103,7 @@ func (m MetaSchemaRepository) Create(ctx context.Context, mschema metaschema.Met } var schemaModel MetaSchema - if err = m.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_METASCHEMA, - Operation: "Create", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = m.dbc.WithTimeout(ctx, TABLE_METASCHEMA, "Create", func(ctx context.Context) error { return m.dbc.QueryRowxContext(ctx, createQuery, params...).StructScan(&schemaModel) }); err != nil { err = checkPostgresError(err) @@ -148,18 +125,7 @@ func (m MetaSchemaRepository) List(ctx context.Context) ([]metaschema.MetaSchema } var schemaModels []MetaSchema - if err = m.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_METASCHEMA, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = m.dbc.WithTimeout(ctx, TABLE_METASCHEMA, "List", func(ctx context.Context) error { return m.dbc.SelectContext(ctx, &schemaModels, query, params...) }); err != nil { err = checkPostgresError(err) @@ -189,18 +155,7 @@ func (m MetaSchemaRepository) Delete(ctx context.Context, id string) (string, er } var name string - if err = m.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_METASCHEMA, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = m.dbc.WithTimeout(ctx, TABLE_METASCHEMA, "Delete", func(ctx context.Context) error { return m.dbc.QueryRowxContext(ctx, query, params...).Scan(&name) }); err != nil { err = checkPostgresError(err) @@ -229,18 +184,7 @@ func (m MetaSchemaRepository) Update(ctx context.Context, id string, mschema met } var schemaModel MetaSchema - if err = m.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_METASCHEMA, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = m.dbc.WithTimeout(ctx, TABLE_METASCHEMA, "Update", func(ctx context.Context) error { return m.dbc.QueryRowxContext(ctx, query, params...).StructScan(&schemaModel) }); err != nil { err = checkPostgresError(err) diff --git a/internal/store/postgres/namespace_repository.go b/internal/store/postgres/namespace_repository.go index 6081cf5a5..9e577d9a0 100644 --- a/internal/store/postgres/namespace_repository.go +++ b/internal/store/postgres/namespace_repository.go @@ -12,7 +12,6 @@ import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/namespace" "github.com/odpf/shield/pkg/db" ) @@ -48,7 +47,7 @@ func (r NamespaceRepository) Get(ctx context.Context, id string) (namespace.Name } var fetchedNamespace Namespace - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { + if err = r.dbc.WithTimeout(ctx, TABLE_NAMESPACES, "Get", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedNamespace, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -87,18 +86,7 @@ func (r NamespaceRepository) Upsert(ctx context.Context, ns namespace.Namespace) } var nsModel Namespace - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_NAMESPACES, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_NAMESPACES, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&nsModel) }); err != nil { err = checkPostgresError(err) @@ -120,18 +108,7 @@ func (r NamespaceRepository) List(ctx context.Context) ([]namespace.Namespace, e } var fetchedNamespaces []Namespace - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_NAMESPACES, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_NAMESPACES, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedNamespaces, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -176,18 +153,7 @@ func (r NamespaceRepository) Update(ctx context.Context, ns namespace.Namespace) } var nsModel Namespace - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_NAMESPACES, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_NAMESPACES, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&nsModel) }); err != nil { err = checkPostgresError(err) diff --git a/internal/store/postgres/organization_repository.go b/internal/store/postgres/organization_repository.go index 55cde6490..e3a0aa423 100644 --- a/internal/store/postgres/organization_repository.go +++ b/internal/store/postgres/organization_repository.go @@ -13,7 +13,6 @@ import ( "github.com/odpf/shield/core/project" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/organization" "github.com/odpf/shield/pkg/db" ) @@ -50,18 +49,7 @@ func (r OrganizationRepository) GetByID(ctx context.Context, id string) (organiz } var orgModel Organization - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "GetByID", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "GetByID", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &orgModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -98,18 +86,7 @@ func (r OrganizationRepository) GetByIDs(ctx context.Context, ids []string) ([]o var orgs []Organization // TODO(kushsharma): clean up this unnecessary newrelic blot over each query // abstract it over database using a facade - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "GetByIDs", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "GetByIDs", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &orgs, query, params...) }); err != nil { err = checkPostgresError(err) @@ -147,17 +124,7 @@ func (r OrganizationRepository) GetByName(ctx context.Context, name string) (org } var orgModel Organization - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "GetByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "GetByName", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &orgModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -203,17 +170,7 @@ func (r OrganizationRepository) Create(ctx context.Context, org organization.Org } var orgModel Organization - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&orgModel) }); err != nil { err = checkPostgresError(err) @@ -248,17 +205,7 @@ func (r OrganizationRepository) List(ctx context.Context, flt organization.Filte } var orgModels []Organization - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &orgModels, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -306,17 +253,7 @@ func (r OrganizationRepository) UpdateByID(ctx context.Context, org organization } var orgModel Organization - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&orgModel) }); err != nil { err = checkPostgresError(err) @@ -364,17 +301,7 @@ func (r OrganizationRepository) UpdateByName(ctx context.Context, org organizati } var orgModel Organization - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "UpdateByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "UpdateByName", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&orgModel) }); err != nil { err = checkPostgresError(err) @@ -409,18 +336,7 @@ func (r OrganizationRepository) SetState(ctx context.Context, id string, state o return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "SetState", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "SetState", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } @@ -447,18 +363,7 @@ func (r OrganizationRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ORGANIZATIONS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/permission_repository.go b/internal/store/postgres/permission_repository.go index 7acdbb962..e18f24299 100644 --- a/internal/store/postgres/permission_repository.go +++ b/internal/store/postgres/permission_repository.go @@ -14,7 +14,6 @@ import ( "github.com/odpf/shield/core/permission" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/namespace" "github.com/odpf/shield/pkg/db" ) @@ -48,18 +47,7 @@ func (r PermissionRepository) Get(ctx context.Context, id string) (permission.Pe return permission.Permission{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PERMISSIONS, "Get", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedPermission, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -82,18 +70,7 @@ func (r PermissionRepository) GetBySlug(ctx context.Context, slug string) (permi return permission.Permission{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "GetByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PERMISSIONS, "GetByName", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedPermission, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -134,18 +111,7 @@ func (r PermissionRepository) Upsert(ctx context.Context, perm permission.Permis } var actionModel Permission - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PERMISSIONS, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&actionModel) }); err != nil { err = checkPostgresError(err) @@ -179,18 +145,7 @@ func (r PermissionRepository) List(ctx context.Context, flt permission.Filter) ( return []permission.Permission{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PERMISSIONS, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedActions, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -233,18 +188,7 @@ func (r PermissionRepository) Update(ctx context.Context, act permission.Permiss } var actionModel Permission - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PERMISSIONS, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&actionModel) }); err != nil { err = checkPostgresError(err) @@ -271,18 +215,7 @@ func (r PermissionRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PERMISSIONS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PERMISSIONS, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/policy_repository.go b/internal/store/postgres/policy_repository.go index 62a3af3cf..90a85dc3d 100644 --- a/internal/store/postgres/policy_repository.go +++ b/internal/store/postgres/policy_repository.go @@ -11,7 +11,6 @@ import ( "github.com/odpf/shield/core/user" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/namespace" "github.com/odpf/shield/core/policy" "github.com/odpf/shield/pkg/db" @@ -54,18 +53,7 @@ func (r PolicyRepository) Get(ctx context.Context, id string) (policy.Policy, er } var policyModel Policy - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_POLICIES, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "Get", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &policyModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -121,17 +109,7 @@ func (r PolicyRepository) List(ctx context.Context, flt policy.Filter) ([]policy return []policy.Policy{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_POLICIES, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedPolicies, query, params...) }); err != nil { err = checkPostgresError(err) @@ -177,17 +155,7 @@ func (r PolicyRepository) Upsert(ctx context.Context, pol policy.Policy) (string } var policyID string - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_POLICIES, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).Scan(&policyID) }); err != nil { err = checkPostgresError(err) @@ -223,17 +191,7 @@ func (r PolicyRepository) Update(ctx context.Context, toUpdate policy.Policy) (s } var policyID string - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_POLICIES, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).Scan(&policyID) }); err != nil { err = checkPostgresError(err) @@ -264,18 +222,7 @@ func (r PolicyRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_POLICIES, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/project_repository.go b/internal/store/postgres/project_repository.go index 075f70fae..e225d055e 100644 --- a/internal/store/postgres/project_repository.go +++ b/internal/store/postgres/project_repository.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/organization" "github.com/odpf/shield/core/project" "github.com/odpf/shield/core/user" @@ -48,18 +47,7 @@ func (r ProjectRepository) GetByID(ctx context.Context, id string) (project.Proj } var projectModel Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "GetByID", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "GetByID", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &projectModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -94,18 +82,7 @@ func (r ProjectRepository) GetByIDs(ctx context.Context, ids []string) ([]projec } var projects []Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "GetByIDs", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "GetByIDs", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &projects, query, params...) }); err != nil { err = checkPostgresError(err) @@ -143,17 +120,7 @@ func (r ProjectRepository) GetByName(ctx context.Context, name string) (project. } var projectModel Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "GetByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "GetByName", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &projectModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -200,17 +167,7 @@ func (r ProjectRepository) Create(ctx context.Context, prj project.Project) (pro } var projectModel Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&projectModel) }); err != nil { err = checkPostgresError(err) @@ -254,18 +211,7 @@ func (r ProjectRepository) List(ctx context.Context, flt project.Filter) ([]proj } var projectModels []Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &projectModels, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -309,18 +255,7 @@ func (r ProjectRepository) UpdateByID(ctx context.Context, prj project.Project) } var projectModel Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "Update", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &projectModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -368,17 +303,7 @@ func (r ProjectRepository) UpdateByName(ctx context.Context, prj project.Project } var projectModel Project - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "UpdateByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "UpdateByName", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &projectModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -417,18 +342,7 @@ func (r ProjectRepository) SetState(ctx context.Context, id string, state projec return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "SetState", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "SetState", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } @@ -455,18 +369,7 @@ func (r ProjectRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_PROJECTS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/relation_repository.go b/internal/store/postgres/relation_repository.go index bdc9102da..e994d982b 100644 --- a/internal/store/postgres/relation_repository.go +++ b/internal/store/postgres/relation_repository.go @@ -9,7 +9,6 @@ import ( "database/sql" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/relation" "github.com/odpf/shield/pkg/db" ) @@ -42,18 +41,7 @@ func (r RelationRepository) Upsert(ctx context.Context, relationToCreate relatio } var relationModel Relation - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RELATIONS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RELATIONS, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&relationModel) }); err != nil { err = checkPostgresError(err) @@ -75,18 +63,7 @@ func (r RelationRepository) List(ctx context.Context) ([]relation.Relation, erro } var fetchedRelations []Relation - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RELATIONS, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RELATIONS, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedRelations, query, params...) }); err != nil { // List should return empty list and no error instead @@ -118,18 +95,7 @@ func (r RelationRepository) Get(ctx context.Context, id string) (relation.Relati } var relationModel Relation - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RELATIONS, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RELATIONS, "Get", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &relationModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -157,18 +123,7 @@ func (r RelationRepository) DeleteByID(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - return r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RELATIONS, - Operation: "DeleteByID", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return r.dbc.WithTimeout(ctx, TABLE_RELATIONS, "DeleteByID", func(ctx context.Context) error { result, err := r.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) @@ -227,18 +182,7 @@ func (r RelationRepository) GetByFields(ctx context.Context, rel relation.Relati if err != nil { return nil, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RELATIONS, - Operation: "GetByFields", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RELATIONS, "GetByFields", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedRelations, query) }); err != nil { err = checkPostgresError(err) @@ -275,18 +219,7 @@ func (r RelationRepository) ListByFields(ctx context.Context, rel relation.Relat if err != nil { return nil, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RELATIONS, - Operation: "GetByFields", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RELATIONS, "GetByFields", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedRelation, query) }); err != nil { err = checkPostgresError(err) diff --git a/internal/store/postgres/resource_repository.go b/internal/store/postgres/resource_repository.go index 6868a1b8d..17834b30e 100644 --- a/internal/store/postgres/resource_repository.go +++ b/internal/store/postgres/resource_repository.go @@ -14,7 +14,6 @@ import ( "database/sql" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/resource" "github.com/odpf/shield/pkg/db" ) @@ -61,18 +60,7 @@ func (r ResourceRepository) Create(ctx context.Context, res resource.Resource) ( } var resourceModel Resource - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&resourceModel) }); err != nil { err = checkPostgresError(err) @@ -107,18 +95,7 @@ func (r ResourceRepository) List(ctx context.Context, flt resource.Filter) ([]re return nil, err } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedResources, query, params...) }); err != nil { err = checkPostgresError(err) @@ -156,18 +133,7 @@ func (r ResourceRepository) GetByID(ctx context.Context, id string) (resource.Re } var resourceModel Resource - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "GetByID", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "GetByID", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &resourceModel, query, params...) }); err != nil { err = checkPostgresError(err) @@ -206,18 +172,7 @@ func (r ResourceRepository) Update(ctx context.Context, res resource.Resource) ( } var resourceModel Resource - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&resourceModel) }); err != nil { err = checkPostgresError(err) @@ -252,18 +207,7 @@ func (r ResourceRepository) GetByURN(ctx context.Context, urn string) (resource. } var resourceModel Resource - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "GetByURN", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "GetByURN", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &resourceModel, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -292,18 +236,7 @@ func (r ResourceRepository) GetByNamespace(ctx context.Context, name string, ns return resource.Resource{}, fmt.Errorf("%w: %s", queryErr, err) } - err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "GetByNamespace", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "GetByNamespace", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedResource, query) }) @@ -328,18 +261,7 @@ func (r ResourceRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_RESOURCES, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_RESOURCES, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/role_repository.go b/internal/store/postgres/role_repository.go index 36fbe783d..938ddf03e 100644 --- a/internal/store/postgres/role_repository.go +++ b/internal/store/postgres/role_repository.go @@ -13,7 +13,6 @@ import ( "database/sql" "github.com/doug-martin/goqu/v9" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/namespace" "github.com/odpf/shield/core/role" "github.com/odpf/shield/pkg/db" @@ -55,18 +54,7 @@ func (r RoleRepository) Get(ctx context.Context, id string) (role.Role, error) { } var roleModel Role - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ROLES, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ROLES, "Get", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &roleModel, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -95,18 +83,7 @@ func (r RoleRepository) GetByName(ctx context.Context, orgID, name string) (role } var roleModel Role - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ROLES, - Operation: "GetByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ROLES, "GetByName", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &roleModel, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -154,18 +131,7 @@ func (r RoleRepository) Upsert(ctx context.Context, rl role.Role) (string, error } var roleID string - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ROLES, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ROLES, "Upsert", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, rl.ID, rl.OrgID, rl.Name, marshaledPermissions, rl.State, marshaledMetadata).Scan(&roleID) }); err != nil { err = checkPostgresError(err) @@ -196,18 +162,7 @@ func (r RoleRepository) List(ctx context.Context, flt role.Filter) ([]role.Role, } var fetchedRoles []Role - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ROLES, - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ROLES, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedRoles, query, params...) }); err != nil { return []role.Role{}, fmt.Errorf("%w: %s", dbErr, err) @@ -257,18 +212,7 @@ func (r RoleRepository) Update(ctx context.Context, rl role.Role) (string, error } var roleID string - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ROLES, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ROLES, "Update", func(ctx context.Context) error { return r.dbc.QueryRowxContext(ctx, query, rl.ID, rl.Name, marshaledPermissions, rl.State, marshaledMetadata).Scan(&roleID) }); err != nil { err = checkPostgresError(err) @@ -297,18 +241,7 @@ func (r RoleRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_ROLES, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_ROLES, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/internal/store/postgres/session_repository.go b/internal/store/postgres/session_repository.go index 918bf8d5c..4202d5581 100644 --- a/internal/store/postgres/session_repository.go +++ b/internal/store/postgres/session_repository.go @@ -13,7 +13,6 @@ import ( "github.com/doug-martin/goqu/v9" "github.com/google/uuid" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/pkg/db" ) @@ -58,18 +57,7 @@ func (s *SessionRepository) Set(ctx context.Context, session *shieldsession.Sess } var sessionModel Session - if err = s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_SESSIONS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = s.dbc.WithTimeout(ctx, TABLE_SESSIONS, "Upsert", func(ctx context.Context) error { return s.dbc.QueryRowxContext(ctx, query, params...).StructScan(&sessionModel) }); err != nil { err = checkPostgresError(err) @@ -89,18 +77,7 @@ func (s *SessionRepository) Get(ctx context.Context, id uuid.UUID) (*shieldsessi return nil, fmt.Errorf("%w: %s", queryErr, err) } - if err := s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_SESSIONS, - Operation: "Get", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err := s.dbc.WithTimeout(ctx, TABLE_SESSIONS, "Get", func(ctx context.Context) error { return s.dbc.QueryRowxContext(ctx, query, params...).StructScan(&session) }); err != nil { err = checkPostgresError(err) @@ -126,18 +103,7 @@ func (s *SessionRepository) Delete(ctx context.Context, id uuid.UUID) error { return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_SESSIONS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return s.dbc.WithTimeout(ctx, TABLE_SESSIONS, "Delete", func(ctx context.Context) error { result, err := s.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) @@ -168,18 +134,7 @@ func (s *SessionRepository) DeleteExpiredSessions(ctx context.Context) error { return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_SESSIONS, - Operation: "DeleteAllExpired", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return s.dbc.WithTimeout(ctx, TABLE_SESSIONS, "DeleteAllExpired", func(ctx context.Context) error { result, err := s.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) @@ -205,18 +160,7 @@ func (s *SessionRepository) UpdateValidity(ctx context.Context, id uuid.UUID, va return fmt.Errorf("%w: %s", queryErr, err) } - return s.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_SESSIONS, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + return s.dbc.WithTimeout(ctx, TABLE_SESSIONS, "Update", func(ctx context.Context) error { result, err := s.dbc.ExecContext(ctx, query, params...) if err != nil { err = checkPostgresError(err) diff --git a/internal/store/postgres/user_repository.go b/internal/store/postgres/user_repository.go index 68f6a96e5..50f4869a4 100644 --- a/internal/store/postgres/user_repository.go +++ b/internal/store/postgres/user_repository.go @@ -14,7 +14,6 @@ import ( "github.com/doug-martin/goqu/v9" "github.com/jmoiron/sqlx" - newrelic "github.com/newrelic/go-agent" "github.com/odpf/shield/core/user" "github.com/odpf/shield/pkg/db" ) @@ -63,18 +62,7 @@ func (r UserRepository) GetByID(ctx context.Context, id string) (user.User, erro return user.User{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "GetByID", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "GetByID", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedUser, userQuery, params...) }); err != nil { err = checkPostgresError(err) @@ -112,17 +100,7 @@ func (r UserRepository) GetByName(ctx context.Context, name string) (user.User, return user.User{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "GetByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "GetByName", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedUser, query, params...) }); err != nil { err = checkPostgresError(err) @@ -166,18 +144,7 @@ func (r UserRepository) Create(ctx context.Context, usr user.User) (user.User, e } var userModel User - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "Upsert", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "Upsert", func(ctx context.Context) error { return tx.QueryRowContext(ctx, createQuery, params...). Scan(&userModel.CreatedAt, &userModel.DeletedAt, @@ -247,18 +214,7 @@ func (r UserRepository) List(ctx context.Context, flt user.Filter) ([]user.User, return []user.User{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: fmt.Sprintf("%s.%s", TABLE_USERS, TABLE_METADATA), - Operation: "List", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "List", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedJoinUserMetadata, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -316,18 +272,7 @@ func (r UserRepository) GetByIDs(ctx context.Context, userIDs []string) ([]user. return []user.User{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "GetByIDs", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "GetByIDs", func(ctx context.Context) error { return r.dbc.SelectContext(ctx, &fetchedUsers, query, params...) }); err != nil { err = checkPostgresError(err) @@ -378,18 +323,7 @@ func (r UserRepository) UpdateByEmail(ctx context.Context, usr user.User) (user. } var userModel User - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "UpdateByEmail", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "UpdateByEmail", func(ctx context.Context) error { return tx.QueryRowContext(ctx, updateQuery, params...). Scan(&userModel.CreatedAt, &userModel.DeletedAt, @@ -450,18 +384,7 @@ func (r UserRepository) UpdateByID(ctx context.Context, usr user.User) (user.Use } var userModel User - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "Update", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "Update", func(ctx context.Context) error { return tx.QueryRowContext(ctx, query, params...).Scan(&userModel.CreatedAt, &userModel.DeletedAt, &userModel.Email, @@ -525,18 +448,7 @@ func (r UserRepository) UpdateByName(ctx context.Context, usr user.User) (user.U } var userModel User - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "UpdateByName", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "UpdateByName", func(ctx context.Context) error { return tx.QueryRowContext(ctx, query, params...).Scan(&userModel.CreatedAt, &userModel.DeletedAt, &userModel.Email, @@ -589,18 +501,7 @@ func (r UserRepository) GetByEmail(ctx context.Context, email string) (user.User return user.User{}, fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "GetByEmail", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "GetByEmail", func(ctx context.Context) error { return r.dbc.GetContext(ctx, &fetchedUser, query, params...) }); err != nil { if errors.Is(err, sql.ErrNoRows) { @@ -632,18 +533,7 @@ func (r UserRepository) SetState(ctx context.Context, id string, state user.Stat return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "SetState", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "SetState", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } @@ -670,18 +560,7 @@ func (r UserRepository) Delete(ctx context.Context, id string) error { return fmt.Errorf("%w: %s", queryErr, err) } - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_USERS, - Operation: "Delete", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - + if err = r.dbc.WithTimeout(ctx, TABLE_USERS, "Delete", func(ctx context.Context) error { if _, err = r.dbc.DB.ExecContext(ctx, query, params...); err != nil { return err } diff --git a/pkg/db/db.go b/pkg/db/db.go index 7daeb577b..390b6b029 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -6,6 +6,8 @@ import ( "fmt" "time" + newrelic "github.com/newrelic/go-agent" + "github.com/jmoiron/sqlx" "github.com/pkg/errors" ) @@ -32,10 +34,20 @@ func New(cfg Config) (*Client, error) { return &Client{DB: d, queryTimeOut: cfg.MaxQueryTimeoutInMS}, err } -func (c Client) WithTimeout(ctx context.Context, op func(ctx context.Context) error) (err error) { +func (c Client) WithTimeout(ctx context.Context, collection, operation string, op func(ctx context.Context) error) (err error) { + nrCtx := newrelic.FromContext(ctx) + if nrCtx != nil { + nr := newrelic.DatastoreSegment{ + Product: newrelic.DatastorePostgres, + Collection: collection, + Operation: operation, + StartTime: nrCtx.StartSegmentNow(), + } + defer nr.End() + } + ctxWithTimeout, cancel := context.WithTimeout(ctx, c.queryTimeOut) defer cancel() - return op(ctxWithTimeout) }