add trace starts for each object type

This commit is contained in:
Mike Mason
2023-07-21 18:11:34 +00:00
parent c27e50ea0b
commit be1b480968
6 changed files with 75 additions and 0 deletions

View File

@@ -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),

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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),

View File

@@ -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,

View File

@@ -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)