From be1b4809687ffad36b6b219c8adc147f02aa6b17 Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Fri, 21 Jul 2023 18:11:34 +0000 Subject: [PATCH] add trace starts for each object type --- internal/service/organizations.go | 10 ++++++++ internal/service/process_memberships.go | 11 +++++++++ internal/service/process_relationships.go | 11 +++++++++ internal/service/projects.go | 10 ++++++++ internal/service/service.go | 3 +++ internal/service/users.go | 30 +++++++++++++++++++++++ 6 files changed, 75 insertions(+) diff --git a/internal/service/organizations.go b/internal/service/organizations.go index f83c605..55d9ddf 100644 --- a/internal/service/organizations.go +++ b/internal/service/organizations.go @@ -5,6 +5,8 @@ import ( "go.infratographer.com/x/events" "go.infratographer.com/x/gidx" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "go.equinixmetal.net/infra9-metal-bridge/internal/metal/models" ) @@ -58,6 +60,10 @@ func (s *service) IsOrganizationID(id gidx.PrefixedID) bool { // TouchOrganization initializes a sync for the provided organization id for relationships and memberships. func (s *service) TouchOrganization(ctx context.Context, id gidx.PrefixedID) error { + ctx, span := tracer.Start(ctx, "TouchOrganization", trace.WithAttributes(attribute.String("resource.id", id.String()))) + + defer span.End() + logger := s.logger.With("organization.id", id.String()) org, err := s.metal.GetOrganizationDetails(ctx, id) @@ -89,6 +95,10 @@ func (s *service) TouchOrganization(ctx context.Context, id gidx.PrefixedID) err // DeleteOrganization deletes the provided organization id. func (s *service) DeleteOrganization(ctx context.Context, id gidx.PrefixedID) error { + ctx, span := tracer.Start(ctx, "DeleteOrganization", trace.WithAttributes(attribute.String("resource.id", id.String()))) + + defer span.End() + err := s.publisher.PublishChange(ctx, organizationEvent, events.ChangeMessage{ SubjectID: id, EventType: string(events.DeleteChangeType), diff --git a/internal/service/process_memberships.go b/internal/service/process_memberships.go index 611c4dc..a1c8d9c 100644 --- a/internal/service/process_memberships.go +++ b/internal/service/process_memberships.go @@ -5,6 +5,8 @@ import ( "strings" "go.infratographer.com/x/gidx" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "golang.org/x/exp/slices" "go.equinixmetal.net/infra9-metal-bridge/internal/permissions" @@ -13,6 +15,15 @@ import ( // processMemberships determines the changes between what is wanted and what is live and executes on the differences. // If skipDeletions is true, no deletes will be executed. func (s *service) processMemberships(ctx context.Context, relationships Relationships, skipDeletions bool) (int, int) { + ctx, span := tracer.Start(ctx, "processMemberships", + trace.WithAttributes( + attribute.String("resource.id", relationships.Resource.PrefixedID().String()), + attribute.Int("resource.memberships", len(relationships.Memberships)), + ), + ) + + defer span.End() + if len(relationships.Memberships) == 0 { return 0, 0 } diff --git a/internal/service/process_relationships.go b/internal/service/process_relationships.go index e0a7ec2..379aaa1 100644 --- a/internal/service/process_relationships.go +++ b/internal/service/process_relationships.go @@ -5,6 +5,8 @@ import ( "go.infratographer.com/x/events" "go.infratographer.com/x/gidx" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "go.equinixmetal.net/infra9-metal-bridge/internal/permissions" ) @@ -13,6 +15,15 @@ import ( // Relationship creations use events. // Relationship deletions use the api, as delete events delete all related resources and not just the provided ones. func (s *service) processRelationships(ctx context.Context, eventType string, relationships Relationships) int { + ctx, span := tracer.Start(ctx, "processRelationships", + trace.WithAttributes( + attribute.String("resource.id", relationships.Resource.PrefixedID().String()), + attribute.Int("resource.subject_relationships", len(relationships.SubjectRelationships)), + ), + ) + + defer span.End() + rlogger := s.logger.With("resource.id", relationships.Resource.PrefixedID()) wantParentRelationship, wantSubjectRelationships := s.mapRelationWants(relationships) diff --git a/internal/service/projects.go b/internal/service/projects.go index d47c428..7e11081 100644 --- a/internal/service/projects.go +++ b/internal/service/projects.go @@ -5,6 +5,8 @@ import ( "go.infratographer.com/x/events" "go.infratographer.com/x/gidx" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "go.equinixmetal.net/infra9-metal-bridge/internal/metal/models" ) @@ -51,6 +53,10 @@ func (s *service) IsProjectID(id gidx.PrefixedID) bool { // TouchProject initializes a sync for the provided project id for relationships and memberships. func (s *service) TouchProject(ctx context.Context, id gidx.PrefixedID) error { + ctx, span := tracer.Start(ctx, "TouchProject", trace.WithAttributes(attribute.String("resource.id", id.String()))) + + defer span.End() + logger := s.logger.With("project.id", id.String()) project, err := s.metal.GetProjectDetails(ctx, id) @@ -82,6 +88,10 @@ func (s *service) TouchProject(ctx context.Context, id gidx.PrefixedID) error { // DeleteProject deletes the provided project id. func (s *service) DeleteProject(ctx context.Context, id gidx.PrefixedID) error { + ctx, span := tracer.Start(ctx, "DeleteProject", trace.WithAttributes(attribute.String("resource.id", id.String()))) + + defer span.End() + err := s.publisher.PublishChange(ctx, projectEvent, events.ChangeMessage{ SubjectID: id, EventType: string(events.DeleteChangeType), diff --git a/internal/service/service.go b/internal/service/service.go index 5ea1ab7..8b63ef3 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -5,6 +5,7 @@ import ( "go.infratographer.com/x/events" "go.infratographer.com/x/gidx" + "go.opentelemetry.io/otel" "go.uber.org/zap" "go.equinixmetal.net/infra9-metal-bridge/internal/metal" @@ -22,6 +23,8 @@ const ( TypeUser ObjectType = "user" ) +var tracer = otel.Tracer("go.equinixmetal.net/infra9-metal-bridge/internal/service") + // DefaultPrefixMap is the default id prefix to type relationship. var DefaultPrefixMap = map[string]ObjectType{ TypeOrganization.Prefix(): TypeOrganization, diff --git a/internal/service/users.go b/internal/service/users.go index a46e50b..35cb7ab 100644 --- a/internal/service/users.go +++ b/internal/service/users.go @@ -4,6 +4,8 @@ import ( "context" "go.infratographer.com/x/gidx" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" ) // IsUser checks the provided id has the metal user prefix. @@ -29,8 +31,27 @@ func (s *service) IsAssignableResource(id gidx.PrefixedID) bool { return false } +func stringIDs(ids []gidx.PrefixedID) []string { + result := make([]string, len(ids)) + + for i, id := range ids { + result[i] = id.String() + } + + return result +} + // Assignuser assigns the provided users to the given resource ids. func (s *service) AssignUser(ctx context.Context, userID gidx.PrefixedID, resourceIDs ...gidx.PrefixedID) error { + ctx, span := tracer.Start(ctx, "AssignUser", + trace.WithAttributes( + attribute.String("user.id", userID.String()), + attribute.StringSlice("user.resource_assignments", stringIDs(resourceIDs)), + ), + ) + + defer span.End() + var totalResources, rolesChanged, assignmentsChanged int mlogger := s.logger.With("member.id", userID.String()) @@ -74,6 +95,15 @@ func (s *service) AssignUser(ctx context.Context, userID gidx.PrefixedID, resour // UnassignUser removes the assignment for the provided user id to the given resources. func (s *service) UnassignUser(ctx context.Context, userID gidx.PrefixedID, resourceIDs ...gidx.PrefixedID) error { + ctx, span := tracer.Start(ctx, "UnassignUser", + trace.WithAttributes( + attribute.String("user.id", userID.String()), + attribute.StringSlice("user.resource_assignments", stringIDs(resourceIDs)), + ), + ) + + defer span.End() + for _, resourceID := range resourceIDs { rlogger := s.logger.With("user.id", userID, "resource.id", resourceID)