Skip to content

Commit

Permalink
feat: improve the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon committed Nov 12, 2024
1 parent f1aa3a5 commit 7256bf3
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions core/provider/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *Service) Create(ctx context.Context, p *domain.Provider) error {
go func() {
s.logger.Info(ctx, "provider create fetching resources", "provider_urn", p.URN)
ctx := audit.WithActor(context.Background(), domain.SystemActorName)
resources, err := s.getResources(ctx, p)
resources, err := s.getResources(ctx, p, nil, nil)
if err != nil {
s.logger.Error(ctx, "failed to fetch resources", "error", err)
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (s *Service) Update(ctx context.Context, p *domain.Provider) error {
go func() {
s.logger.Info(ctx, "provider update fetching resources", "provider_urn", p.URN)
ctx := audit.WithActor(context.Background(), domain.SystemActorName)
resources, err := s.getResources(ctx, p)
resources, err := s.getResources(ctx, p, nil, nil)
if err != nil {
s.logger.Error(ctx, "failed to fetch resources", "error", err)
}
Expand All @@ -261,11 +261,12 @@ func (s *Service) FetchResources(ctx context.Context) error {
return err
}
failedProviders := map[string]error{}
updatedProviders := 0
resourcesCount := 0
upsertedResourceCount := 0
for _, p := range providers {
startTime := time.Now()
s.logger.Info(ctx, "fetching resources", "provider_urn", p.URN)
resources, err := s.getResources(ctx, p)
resources, err := s.getResources(ctx, p, &resourcesCount, &upsertedResourceCount)
if err != nil {
s.logger.Error(ctx, "failed to get resources", "error", err)
continue
Expand All @@ -281,10 +282,8 @@ func (s *Service) FetchResources(ctx context.Context) error {
s.logger.Error(ctx, "failed to add resources", "provider_urn", p.URN, "error", err)
}
s.logger.Info(ctx, "fetching resources completed", "provider_urn", p.URN, "duration", time.Since(startTime))
updatedProviders++
}
s.logger.Info(ctx, "existing provider", "count", len(providers))
s.logger.Info(ctx, "updated providers", "count", updatedProviders)
s.logger.Info(ctx, "resources", "count", resourcesCount, "upserted", upsertedResourceCount)
if len(failedProviders) > 0 {
var urns []string
for providerURN, err := range failedProviders {
Expand Down Expand Up @@ -569,7 +568,7 @@ func (s *Service) IsExclusiveRoleAssignment(ctx context.Context, providerType, r
return false
}

func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*domain.Resource, error) {
func (s *Service) getResources(ctx context.Context, p *domain.Provider, resourcesCount, upsertedResourcesCount *int) ([]*domain.Resource, error) {
c := s.getClient(p.Type)
if c == nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidProviderType, p.Type)
Expand All @@ -594,7 +593,6 @@ func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*doma
if err != nil {
return nil, fmt.Errorf("error fetching resources for %v: %w", p.ID, err)
}

filteredResources := make([]*domain.Resource, 0)
for _, r := range newProviderResources {
if filterExpression, ok := resourceTypeFilterMap[r.Type]; ok {
Expand All @@ -612,7 +610,9 @@ func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*doma
flattenedProviderResources := flattenResources(filteredResources)

existingProviderResources := map[string]bool{}

if resourcesCount != nil {
*resourcesCount += len(flattenedProviderResources)
}
UpdatedResourceCount := 0
for _, newResource := range flattenedProviderResources {
for _, existingResource := range existingGuardianResources {
Expand All @@ -630,6 +630,9 @@ func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*doma
if isUpdated, diff := compareResources(*existingResource, *newResource); isUpdated {
s.logger.Info(ctx, "diff", "resources", diff)
UpdatedResourceCount++
if upsertedResourcesCount != nil {
*upsertedResourcesCount++
}
s.logger.Info(ctx, "Resources is updated", "resource", newResource.Name)
}
}
Expand All @@ -641,9 +644,6 @@ func (s *Service) getResources(ctx context.Context, p *domain.Provider) ([]*doma
if UpdatedResourceCount == 0 && len(existingGuardianResources) == len(flattenedProviderResources) {
return []*domain.Resource{}, nil
}
s.logger.Info(ctx, "Existing Resource", "Count", len(existingGuardianResources))
s.logger.Info(ctx, "New Resource", "Count", len(flattenedProviderResources))
s.logger.Info(ctx, "Updated Resource", "Count", UpdatedResourceCount)

// mark IsDeleted of guardian resources that no longer exist in provider
updatedResources := []*domain.Resource{}
Expand Down

0 comments on commit 7256bf3

Please sign in to comment.