Skip to content

Commit

Permalink
feat: Add solve trace
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Sep 12, 2024
1 parent 34fa9f6 commit ad172fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 26 additions & 2 deletions pkg/solver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/mediation"
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/storage"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -105,7 +107,7 @@ func (controller *SolverController) Start(ctx context.Context, cm *system.Cleanu
ctx,
CONTROL_LOOP_INTERVAL,
func() error {
err := controller.solve()
err := controller.solve(ctx)
if err != nil {
errorChan <- err
}
Expand Down Expand Up @@ -270,20 +272,42 @@ func (controller *SolverController) registerAsSolver() error {
*
*/

func (controller *SolverController) solve() error {
func (controller *SolverController) solve(ctx context.Context) error {
// Start solve trace
ctx, span := controller.tracer.Start(ctx, "solve")
defer span.End()

// When telemetry is disabled we use a Noop tracing provider,
// which does not export. We only log the trace ID when we are
// sending the trace somehwere.
if controller.options.Telemetry.Disable == false {
controller.log.Debug("starting solve with trace ID", span.SpanContext().TraceID())
}

// find out which deals we can make from matching the offers
span.AddEvent("get_matching_deals.start")
deals, err := getMatchingDeals(controller.store, controller.updateJobOfferState)
if err != nil {
span.SetStatus(codes.Error, "get matching deals failed")
span.RecordError(err)
return err
}
span.AddEvent("get_matching_deals.done")
span.SetAttributes(attribute.KeyValue{
Key: "deal_ids",
Value: attribute.StringSliceValue(GetDealIDs(deals)),
})

// loop over each of the deals add add them to the store and emit events
span.AddEvent("add_deals.start")
for _, deal := range deals {
_, err := controller.addDeal(deal)
if err != nil {
return err
}
}
span.AddEvent("add_deals.done")

return nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/solver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path/filepath"

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -52,6 +53,14 @@ func ServiceLogSolverEvent(service system.Service, ev SolverEvent) {
LogSolverEvent(system.GetServiceBadge(service), ev)
}

func GetDealIDs(deals []data.Deal) []string {
var ids []string
for _, deal := range deals {
ids = append(ids, deal.ID)
}
return ids
}

func GetDealsFilePath(id string) string {
return system.GetDataDir(filepath.Join(FILES_DIR, id))
}
Expand Down

0 comments on commit ad172fd

Please sign in to comment.