Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add deal making trace #336

Merged
merged 10 commits into from
Oct 18, 2024
Merged

feat: Add deal making trace #336

merged 10 commits into from
Oct 18, 2024

Conversation

bgins
Copy link
Contributor

@bgins bgins commented Sep 6, 2024

Summary

This pull request makes the following changes:

  • Add solve trace
    • Add get_matching_deals trace
      • Add get_targeted_deal trace
      • Add match trace
    • Add add_deal trace
  • Refactor match algorithm to make moduleID available one scope out

This pull request adds a trace to deal making in the solver. It observes matching and reports deals that were made and errors where a deal could not be made.

Preview image:

CleanShot 2024-10-16 at 15 54 33@2x

Task/Issue reference

Closes: #331

Test plan

Tracing over deal making includes a few spans:

  • solve. The outermost parent span traces over the solve function.
  • get_matching_deals. Child of the solve trace.
  • get_targeted_deal. Child of get_matching_deals.
  • match. Child of get_matching_deals.

Query for the solve span with:

{resource.service.name="solver" && name="solve"}

Add span.deal_ids != "[]" to filter out spans that did not match any deals:

{resource.service.name="solver" && name="solve" && span.deal_ids != "[]"}

Adding this filter will helps with testing because solve runs every ten seconds, but only one trace will make a match for a test job run.

The get_matching_deals span will appear as a child span. It can also be queried directly:

{resource.service.name="solver" && name="get_matching_deals" && span.job_offers != "[]"}

Filtering with span.job_offers != "[]" ignores spans without any matches because they lack a job offer to match on.

A get_matching_deals may contain one or more match spans. Each match span traces a match attempt between a job offer and a resource offer.

When using node targeting, a get_targeted_deal span will appear as a child of get_matching_deals. This can be queried directly:

{resource.service.name="solver" && name="get_targeted_deal"}

Use this query to observe targeting attempts that fail when the requested target is not available.

Details

We use two different methods for tracing a function. When the function does not contain a child span, we wrap the call with start and done span events. For example:

span.AddEvent("add_match_decision.start")
_, err := db.AddMatchDecision(matchingResourceOffer.ID, jobOffer.ID, addDealID, true)
if err != nil {
span.SetStatus(codes.Error, "unable to add match decision")
span.RecordError(err)
return nil, err
}
span.AddEvent("add_match_decision.done")
}

When the function contains a child span, we simply call it passing the context and tracer. For example:

deals, err := matcher.GetMatchingDeals(ctx, controller.store, controller.updateJobOfferState, controller.tracer)

The two approaches give us visibility on each function call, either as a pair of events in a span or a child span.

Related issues or PRs (optional)

Implements #75

@cla-bot cla-bot bot added the cla-signed label Sep 6, 2024
@bgins bgins force-pushed the bgins/feat-add-deal-making-trace branch 3 times, most recently from ad172fd to ab54c02 Compare September 12, 2024 20:54
@bgins bgins force-pushed the bgins/feat-add-deal-making-trace branch 4 times, most recently from 7cd6cf3 to d940b06 Compare October 16, 2024 21:48
@bgins bgins force-pushed the bgins/feat-add-deal-making-trace branch from d940b06 to 6713b01 Compare October 16, 2024 22:04
Comment on lines +49 to +55
func GetResourceOfferIDs(resourceOffers []ResourceOffer) []string {
var ids []string
for _, offer := range resourceOffers {
ids = append(ids, offer.ID)
}
return ids
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be confusing that that GetResourceOfferIDs has a different behavior than GetResourceOfferID. Open to naming suggestions.

}

func (_ priceMismatch) matched() bool { return false }
func (_ priceMismatch) message() string {
return "fixed price job offer cannot afford resource offer"
}
func (result priceMismatch) attributes() []attribute.KeyValue {
// If the module instruction price is not specified, this lookup will use the zero-value of 0
moduleInstructionPrice := result.resourceOffer.ModulePricing[result.moduleID].InstructionPrice
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't currently implement per module pricing, but this sets us up for it.

Comment on lines +46 to +49
span.SetAttributes(attribute.KeyValue{
Key: "resource_offers",
Value: attribute.StringSliceValue(data.GetResourceOfferContainerIDs(resourceOffers)),
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, span attributes should be set when the span is created because they are generally considered static information. But we're setting them dynamically here as a part of the initialization leading up to the real work.

Setting these values as span attributes makes it easier to query spans with them.

Comment on lines +87 to +90
_, matchSpan := tracer.Start(ctx, "match",
trace.WithAttributes(attribute.String("job_offer.id", jobOffer.ID),
attribute.String("resource_offer.id", resourceOffer.ID)),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future work. Would like to refactor this part of the algorithm out into a function. The change would make reading this code easier and we could avoid defining an child span in the scope of its parent.

@bgins bgins marked this pull request as ready for review October 17, 2024 18:20
@bgins bgins requested a review from a team as a code owner October 17, 2024 18:20
Copy link
Collaborator

@walkah walkah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and local testing worked. One question, but otherwise great work! 🎉

err: err,
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you move this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to access it in the price mismatch case:

return &priceMismatch{
jobOffer: jobOffer,
resourceOffer: resourceOffer,
moduleID: moduleID,
}

This price mismatch case is one scope out from where we were getting the module ID. Moving it avoids recomputing the module ID for this case.

@bgins bgins merged commit a8febc0 into main Oct 18, 2024
4 checks passed
@bgins bgins deleted the bgins/feat-add-deal-making-trace branch October 18, 2024 18:55
@bgins bgins self-assigned this Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add solver deal making trace
2 participants