Skip to content

Commit

Permalink
feat: Add job creator tracer (#411)
Browse files Browse the repository at this point in the history
* chore: Add telemetry options

* feat: Add onchain job-creator tracer

* feat: Add run job-creator tracer

* chore: Add integration test noop tracer

* chore: Remove comment

We can read the yacspin docs: https://github.com/theckman/yacspin

* chore: Disable telemetry in CI

* fix: Process onchain telemetry options
  • Loading branch information
bgins authored Oct 23, 2024
1 parent 22a310c commit b467d0f
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- name: Run unit tests
run: ./stack unit-tests

run-integration-tests:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -50,6 +50,7 @@ jobs:
- name: Run tests
env:
LOG_LEVEL: debug
DISABLE_TELEMETRY: true
run: ./stack integration-tests

- name: Display resource provider logs
Expand Down
18 changes: 12 additions & 6 deletions cmd/lilypad/jobcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
optionsfactory "github.com/lilypad-tech/lilypad/pkg/options"
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/lilypad-tech/lilypad/pkg/web3"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel/trace/noop"
)

func newJobCreatorCmd() *cobra.Command {
Expand All @@ -24,7 +24,7 @@ func newJobCreatorCmd() *cobra.Command {
if err != nil {
return err
}
return runJobCreator(cmd, options)
return runJobCreator(cmd, options, network)
},
}

Expand All @@ -33,18 +33,24 @@ func newJobCreatorCmd() *cobra.Command {
return solverCmd
}

func runJobCreator(cmd *cobra.Command, options jobcreator.JobCreatorOptions) error {
func runJobCreator(cmd *cobra.Command, options jobcreator.JobCreatorOptions, network string) error {
commandCtx := system.NewCommandContext(cmd)
defer commandCtx.Cleanup()

noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.JobCreatorService))
web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, noopTracer)
telemetry, err := configureTelemetry(commandCtx.Ctx, system.JobCreatorService, network, options.Telemetry, options.Web3)
if err != nil {
log.Warn().Msgf("failed to setup opentelemetry: %s", err)
}
commandCtx.Cm.RegisterCallbackWithContext(telemetry.Shutdown)
tracer := telemetry.TracerProvider.Tracer(system.GetOTelServiceName(system.JobCreatorService))

web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, tracer)
if err != nil {
return err
}

// create the job creator and start it's control loop
jobCreatorService, err := jobcreator.NewOnChainJobCreator(options, web3SDK)
jobCreatorService, err := jobcreator.NewOnChainJobCreator(options, web3SDK, tracer)
if err != nil {
return err
}
Expand Down
27 changes: 12 additions & 15 deletions cmd/lilypad/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
optionsfactory "github.com/lilypad-tech/lilypad/pkg/options"
"github.com/lilypad-tech/lilypad/pkg/solver"
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/theckman/yacspin"
Expand All @@ -33,7 +34,7 @@ func newRunCmd() *cobra.Command {
if err != nil {
return err
}
return runJob(cmd, options)
return runJob(cmd, options, network)
},
}

Expand All @@ -42,7 +43,7 @@ func newRunCmd() *cobra.Command {
return runCmd
}

func runJob(cmd *cobra.Command, options jobcreator.JobCreatorOptions) error {
func runJob(cmd *cobra.Command, options jobcreator.JobCreatorOptions, network string) error {
c := color.New(color.FgCyan).Add(color.Bold)
header := `
⠀⠀⠀⠀⠀⠀⣀⣤⣤⢠⣤⣀⠀⠀⠀⠀⠀
Expand Down Expand Up @@ -72,18 +73,6 @@ func runJob(cmd *cobra.Command, options jobcreator.JobCreatorOptions) error {
return fmt.Errorf("failed to start spinner: %w", err)
}

// update message
// spinner.Message("uploading files")

// let spinner render some more
// time.Sleep(1 * time.Second)

// if you wanted to print a failure message...
//
// if err := spinner.StopFail(); err != nil {
// return fmt.Errorf("failed to stop spinner: %w", err)
// }

if err := spinner.Stop(); err != nil {
return fmt.Errorf("failed to stop spinner: %w", err)
}
Expand All @@ -95,7 +84,15 @@ func runJob(cmd *cobra.Command, options jobcreator.JobCreatorOptions) error {

commandCtx := system.NewCommandContext(cmd)
defer commandCtx.Cleanup()
result, err := jobcreator.RunJob(commandCtx, options, func(evOffer data.JobOfferContainer) {

telemetry, err := configureTelemetry(commandCtx.Ctx, system.JobCreatorService, network, options.Telemetry, options.Web3)
if err != nil {
log.Warn().Msgf("failed to setup opentelemetry: %s", err)
}
commandCtx.Cm.RegisterCallbackWithContext(telemetry.Shutdown)
tracer := telemetry.TracerProvider.Tracer(system.GetOTelServiceName(system.JobCreatorService))

result, err := jobcreator.RunJob(commandCtx, options, tracer, func(evOffer data.JobOfferContainer) {
spinner.Stop()
st := data.GetAgreementStateString(evOffer.State)
var desc string
Expand Down
4 changes: 4 additions & 0 deletions pkg/jobcreator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/lilypad-tech/lilypad/pkg/web3"
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/storage"
"go.opentelemetry.io/otel/trace"
)

type JobOfferSubscriber func(offer data.JobOfferContainer)
Expand All @@ -25,6 +26,7 @@ type JobCreatorController struct {
loop *system.ControlLoop
log *system.ServiceLogger
jobOfferSubscriptions []JobOfferSubscriber
tracer trace.Tracer
}

// the background "even if we have not heard of an event" loop
Expand All @@ -36,6 +38,7 @@ const CONTROL_LOOP_INTERVAL = 10 * time.Second
func NewJobCreatorController(
options JobCreatorOptions,
web3SDK *web3.Web3SDK,
tracer trace.Tracer,
) (*JobCreatorController, error) {
// we know the address of the solver but what is it's url?
solverUrl, err := web3SDK.GetSolverUrl(options.Offer.Services.Solver)
Expand Down Expand Up @@ -63,6 +66,7 @@ func NewJobCreatorController(
web3Events: web3.NewEventChannels(),
log: system.NewServiceLogger(system.JobCreatorService),
jobOfferSubscriptions: []JobOfferSubscriber{},
tracer: tracer,
}
return controller, nil
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/jobcreator/jobcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/lilypad-tech/lilypad/pkg/web3"
"go.opentelemetry.io/otel/trace"
)

type JobCreatorMediationOptions struct {
Expand Down Expand Up @@ -39,6 +40,7 @@ type JobCreatorOptions struct {
Mediation JobCreatorMediationOptions
Offer JobCreatorOfferOptions
Web3 web3.Web3Options
Telemetry system.TelemetryOptions
}

type JobCreator struct {
Expand All @@ -50,8 +52,9 @@ type JobCreator struct {
func NewJobCreator(
options JobCreatorOptions,
web3SDK *web3.Web3SDK,
tracer trace.Tracer,
) (*JobCreator, error) {
controller, err := NewJobCreatorController(options, web3SDK)
controller, err := NewJobCreatorController(options, web3SDK, tracer)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/jobcreator/onchain_jobcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/lilypad-tech/lilypad/pkg/web3"
jobcreatorweb3 "github.com/lilypad-tech/lilypad/pkg/web3/bindings/jobcreator"
"go.opentelemetry.io/otel/trace"
)

const JOB_PRICE = 2
Expand All @@ -26,8 +27,9 @@ type OnChainJobCreator struct {
func NewOnChainJobCreator(
options JobCreatorOptions,
web3SDK *web3.Web3SDK,
tracer trace.Tracer,
) (*OnChainJobCreator, error) {
controller, err := NewJobCreatorController(options, web3SDK)
controller, err := NewJobCreatorController(options, web3SDK, tracer)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/jobcreator/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/system"
"github.com/lilypad-tech/lilypad/pkg/web3"
"go.opentelemetry.io/otel/trace/noop"
"go.opentelemetry.io/otel/trace"
)

type RunJobResults struct {
Expand All @@ -18,16 +18,16 @@ type RunJobResults struct {
func RunJob(
ctx *system.CommandContext,
options JobCreatorOptions,
tracer trace.Tracer,
eventSub JobOfferSubscriber,
) (*RunJobResults, error) {
noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.JobCreatorService))
web3SDK, err := web3.NewContractSDK(ctx.Ctx, options.Web3, noopTracer)
web3SDK, err := web3.NewContractSDK(ctx.Ctx, options.Web3, tracer)
if err != nil {
return nil, err
}

// create the job creator and start it's control loop
jobCreatorService, err := NewJobCreator(options, web3SDK)
jobCreatorService, err := NewJobCreator(options, web3SDK, tracer)
if err != nil {
return nil, err
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/options/job-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func NewJobCreatorOptions() jobcreator.JobCreatorOptions {
Offer: GetDefaultJobCreatorOfferOptions(),
Web3: GetDefaultWeb3Options(),
Mediation: GetDefaultJobCreatorMediationOptions(),
Telemetry: GetDefaultTelemetryOptions(),
}
options.Web3.Service = system.JobCreatorService
return options
Expand Down Expand Up @@ -62,6 +63,7 @@ func AddJobCreatorCliFlags(cmd *cobra.Command, options *jobcreator.JobCreatorOpt
AddJobCreatorMediationCliFlags(cmd, &options.Mediation)
AddWeb3CliFlags(cmd, &options.Web3)
AddJobCreatorOfferCliFlags(cmd, &options.Offer)
AddTelemetryCliFlags(cmd, &options.Telemetry)
}

func CheckJobCreatorOptions(options jobcreator.JobCreatorOptions) error {
Expand All @@ -77,6 +79,10 @@ func CheckJobCreatorOptions(options jobcreator.JobCreatorOptions) error {
if err != nil {
return err
}
err = CheckTelemetryOptions(options.Telemetry)
if err != nil {
return err
}

if options.Mediation.CheckResultsPercentage < 0 || options.Mediation.CheckResultsPercentage > 100 {
return fmt.Errorf("mediation-chance must be between 0 and 100")
Expand Down Expand Up @@ -119,6 +125,12 @@ func ProcessJobCreatorOptions(options jobcreator.JobCreatorOptions, args []strin
}
options.Offer.Target = newTargetOptions

newTelemetryOptions, err := ProcessTelemetryOptions(options.Telemetry, network)
if err != nil {
return options, err
}
options.Telemetry = newTelemetryOptions

return options, CheckJobCreatorOptions(options)
}

Expand All @@ -135,6 +147,12 @@ func ProcessOnChainJobCreatorOptions(options jobcreator.JobCreatorOptions, args
}
options.Offer.Services = newServicesOptions

newTelemetryOptions, err := ProcessTelemetryOptions(options.Telemetry, network)
if err != nil {
return options, err
}
options.Telemetry = newTelemetryOptions

err = CheckWeb3Options(options.Web3)
if err != nil {
return options, err
Expand All @@ -143,6 +161,10 @@ func ProcessOnChainJobCreatorOptions(options jobcreator.JobCreatorOptions, args
if err != nil {
return options, err
}
err = CheckTelemetryOptions(options.Telemetry)
if err != nil {
return options, err
}

options.Mediation.CheckResultsPercentage = 0

Expand Down
3 changes: 2 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func testStackWithOptions(
return nil, err
}

result, err := jobcreator.RunJob(commandCtx, jobCreatorOptions, func(evOffer data.JobOfferContainer) {
noopTracer := traceNoop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.DefaultService))
result, err := jobcreator.RunJob(commandCtx, jobCreatorOptions, noopTracer, func(evOffer data.JobOfferContainer) {

})
if err != nil {
Expand Down

0 comments on commit b467d0f

Please sign in to comment.