diff --git a/cmd/lilypad/jobcreator.go b/cmd/lilypad/jobcreator.go index 44689e36..1affab81 100644 --- a/cmd/lilypad/jobcreator.go +++ b/cmd/lilypad/jobcreator.go @@ -6,6 +6,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/system" "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/spf13/cobra" + "go.opentelemetry.io/otel/trace/noop" ) func newJobCreatorCmd() *cobra.Command { @@ -36,7 +37,8 @@ func runJobCreator(cmd *cobra.Command, options jobcreator.JobCreatorOptions) err commandCtx := system.NewCommandContext(cmd) defer commandCtx.Cleanup() - web3SDK, err := web3.NewContractSDK(options.Web3) + noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.JobCreatorService)) + web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, noopTracer) if err != nil { return err } diff --git a/cmd/lilypad/mediator.go b/cmd/lilypad/mediator.go index 9790c386..08cc7e92 100644 --- a/cmd/lilypad/mediator.go +++ b/cmd/lilypad/mediator.go @@ -8,6 +8,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/rs/zerolog/log" "github.com/spf13/cobra" + "go.opentelemetry.io/otel/trace/noop" ) func newMediatorCmd() *cobra.Command { @@ -38,7 +39,8 @@ func runMediator(cmd *cobra.Command, options mediator.MediatorOptions) error { commandCtx := system.NewCommandContext(cmd) defer commandCtx.Cleanup() - web3SDK, err := web3.NewContractSDK(options.Web3) + noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.MediatorService)) + web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, noopTracer) if err != nil { return err } diff --git a/cmd/lilypad/pow_signal.go b/cmd/lilypad/pow_signal.go index 9af12075..22f18727 100644 --- a/cmd/lilypad/pow_signal.go +++ b/cmd/lilypad/pow_signal.go @@ -7,6 +7,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/rs/zerolog/log" "github.com/spf13/cobra" + "go.opentelemetry.io/otel/trace/noop" ) func newPowSignalCmd() *cobra.Command { @@ -42,7 +43,8 @@ func runPowSignal(cmd *cobra.Command, options options.PowSignalOptions) error { commandCtx := system.NewCommandContext(cmd) defer commandCtx.Cleanup() - web3SDK, err := web3.NewContractSDK(options.Web3) + noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.DefaultService)) + web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, noopTracer) if err != nil { log.Error().Err(err).Msg("Failed to initialize Web3 SDK") return err diff --git a/cmd/lilypad/resource-provider.go b/cmd/lilypad/resource-provider.go index 49b67647..e7f36765 100644 --- a/cmd/lilypad/resource-provider.go +++ b/cmd/lilypad/resource-provider.go @@ -39,7 +39,14 @@ func runResourceProvider(cmd *cobra.Command, options resourceprovider.ResourcePr commandCtx := system.NewCommandContext(cmd) defer commandCtx.Cleanup() - web3SDK, err := web3.NewContractSDK(options.Web3) + telemetry, err := configureTelemetry(commandCtx.Ctx, system.ResourceProviderService, network, options.Telemetry, options.Web3) + if err != nil { + fmt.Printf("failed to setup opentelemetry: %s", err) + } + commandCtx.Cm.RegisterCallbackWithContext(telemetry.Shutdown) + tracer := telemetry.TracerProvider.Tracer(system.GetOTelServiceName(system.ResourceProviderService)) + + web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, tracer) if err != nil { return err } @@ -55,22 +62,7 @@ func runResourceProvider(cmd *cobra.Command, options resourceprovider.ResourcePr return err } - tc := system.TelemetryConfig{ - TelemetryURL: options.Telemetry.URL, - TelemetryToken: options.Telemetry.Token, - Enabled: !options.Telemetry.Disable, - Service: system.ResourceProviderService, - Network: network, - Address: web3SDK.GetAddress().String(), - GPU: system.GetGPUInfo(), - } - telemetry, err := system.SetupOTelSDK(commandCtx.Ctx, tc) - if err != nil { - fmt.Printf("failed to setup opentelemetry: %s", err) - } - commandCtx.Cm.RegisterCallbackWithContext(telemetry.Shutdown) - - resourceProviderService, err := resourceprovider.NewResourceProvider(options, web3SDK, executor, telemetry) + resourceProviderService, err := resourceprovider.NewResourceProvider(options, web3SDK, executor, tracer) if err != nil { return err } diff --git a/cmd/lilypad/solver.go b/cmd/lilypad/solver.go index c18fe232..ebe8e4cd 100644 --- a/cmd/lilypad/solver.go +++ b/cmd/lilypad/solver.go @@ -7,6 +7,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/system" "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/spf13/cobra" + "go.opentelemetry.io/otel/trace/noop" ) func newSolverCmd() *cobra.Command { @@ -37,7 +38,8 @@ func runSolver(cmd *cobra.Command, options solver.SolverOptions) error { commandCtx := system.NewCommandContext(cmd) defer commandCtx.Cleanup() - web3SDK, err := web3.NewContractSDK(options.Web3) + noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.SolverService)) + web3SDK, err := web3.NewContractSDK(commandCtx.Ctx, options.Web3, noopTracer) if err != nil { return err } diff --git a/cmd/lilypad/utils.go b/cmd/lilypad/utils.go index 79450d99..6cca39e5 100644 --- a/cmd/lilypad/utils.go +++ b/cmd/lilypad/utils.go @@ -1,10 +1,13 @@ package lilypad import ( + "context" "os" "strconv" "strings" + "github.com/lilypad-tech/lilypad/pkg/system" + "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/spf13/cobra" ) @@ -34,6 +37,35 @@ func getDefaultServeOptionInt(envName string, defaultValue int) int { return defaultValue } +/* +Telemetry +*/ +func configureTelemetry(ctx context.Context, + service system.Service, + network string, + options system.TelemetryOptions, + web3Options web3.Web3Options, +) (*system.Telemetry, error) { + privateKey, err := web3.ParsePrivateKey(web3Options.PrivateKey) + if err != nil { + return nil, err + } + address := web3.GetAddress(privateKey) + + tc := system.TelemetryConfig{ + TelemetryURL: options.URL, + TelemetryToken: options.Token, + Enabled: !options.Disable, + Service: service, + Network: network, + Address: address.String(), + GPU: system.GetGPUInfo(), + } + telemetry, err := system.SetupOTelSDK(ctx, tc) + + return &telemetry, err +} + /* useful tools */ diff --git a/pkg/jobcreator/run.go b/pkg/jobcreator/run.go index 03795571..3e2920fe 100644 --- a/pkg/jobcreator/run.go +++ b/pkg/jobcreator/run.go @@ -7,6 +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" ) type RunJobResults struct { @@ -19,7 +20,8 @@ func RunJob( options JobCreatorOptions, eventSub JobOfferSubscriber, ) (*RunJobResults, error) { - web3SDK, err := web3.NewContractSDK(options.Web3) + noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.JobCreatorService)) + web3SDK, err := web3.NewContractSDK(ctx.Ctx, options.Web3, noopTracer) if err != nil { return nil, err } diff --git a/pkg/resourceprovider/controller.go b/pkg/resourceprovider/controller.go index 53d73d0b..e21ddcc4 100644 --- a/pkg/resourceprovider/controller.go +++ b/pkg/resourceprovider/controller.go @@ -21,14 +21,14 @@ import ( ) type ResourceProviderController struct { - solverClient *solver.SolverClient - options ResourceProviderOptions - web3SDK *web3.Web3SDK - web3Events *web3.EventChannels - loop *system.ControlLoop - log *system.ServiceLogger - tracerProvider trace.TracerProvider - executor executor.Executor + solverClient *solver.SolverClient + options ResourceProviderOptions + web3SDK *web3.Web3SDK + web3Events *web3.EventChannels + loop *system.ControlLoop + log *system.ServiceLogger + tracer trace.Tracer + executor executor.Executor // keep track of which jobs are running // this is because no remote state will change // whilst we are actually running a job @@ -46,7 +46,7 @@ func NewResourceProviderController( options ResourceProviderOptions, web3SDK *web3.Web3SDK, executor executor.Executor, - telemetry system.Telemetry, + tracer trace.Tracer, ) (*ResourceProviderController, error) { // we know the address of the solver but what is it's url? solverUrl, err := web3SDK.GetSolverUrl(options.Offers.Services.Solver) @@ -66,14 +66,14 @@ func NewResourceProviderController( } controller := &ResourceProviderController{ - solverClient: solverClient, - options: options, - web3SDK: web3SDK, - web3Events: web3.NewEventChannels(), - log: system.NewServiceLogger(system.ResourceProviderService), - tracerProvider: telemetry.TracerProvider, - executor: executor, - runningJobs: map[string]bool{}, + solverClient: solverClient, + options: options, + web3SDK: web3SDK, + web3Events: web3.NewEventChannels(), + log: system.NewServiceLogger(system.ResourceProviderService), + tracer: tracer, + executor: executor, + runningJobs: map[string]bool{}, } return controller, nil } @@ -415,8 +415,7 @@ func (controller *ResourceProviderController) runJob(ctx context.Context, deal d controller.log.Info("deal ID", deal.Deal.ID) // Start run job trace - service := system.ResourceProviderService - ctx, span := controller.tracerProvider.Tracer(system.GetOTelServiceName(service)).Start(ctx, "run_job", + ctx, span := controller.tracer.Start(ctx, "run_job", trace.WithAttributes(attribute.String("deal.id", deal.ID)), trace.WithAttributes(attribute.String("deal.job_creator", deal.JobCreator)), trace.WithAttributes(attribute.String("deal.resource_provider", deal.ResourceProvider)), diff --git a/pkg/resourceprovider/resourceprovider.go b/pkg/resourceprovider/resourceprovider.go index 049139a5..0e269474 100644 --- a/pkg/resourceprovider/resourceprovider.go +++ b/pkg/resourceprovider/resourceprovider.go @@ -19,6 +19,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/lilypad-tech/lilypad/pkg/web3/bindings/pow" "github.com/rs/zerolog/log" + "go.opentelemetry.io/otel/trace" ) // this configures the resource offers we will keep track of @@ -82,9 +83,9 @@ func NewResourceProvider( options ResourceProviderOptions, web3SDK *web3.Web3SDK, executor executor.Executor, - telemetry system.Telemetry, + tracer trace.Tracer, ) (*ResourceProvider, error) { - controller, err := NewResourceProviderController(options, web3SDK, executor, telemetry) + controller, err := NewResourceProviderController(options, web3SDK, executor, tracer) if err != nil { return nil, err } diff --git a/pkg/web3/sdk.go b/pkg/web3/sdk.go index 7eb67e9b..06924a4a 100644 --- a/pkg/web3/sdk.go +++ b/pkg/web3/sdk.go @@ -21,6 +21,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/web3/bindings/token" "github.com/lilypad-tech/lilypad/pkg/web3/bindings/users" "github.com/rs/zerolog/log" + "go.opentelemetry.io/otel/trace" ) // these are the go-binding wrappers for the various deployed contracts @@ -191,7 +192,7 @@ func NewContracts( }, nil } -func NewContractSDK(options Web3Options) (*Web3SDK, error) { +func NewContractSDK(ctx context.Context, options Web3Options, tracer trace.Tracer) (*Web3SDK, error) { displayOpts := options displayOpts.PrivateKey = "*********" log.Debug().Msgf("NewContractSDK: %+v", displayOpts) diff --git a/pkg/web3/sdk_test.go b/pkg/web3/sdk_test.go index c63aadf8..c577a755 100644 --- a/pkg/web3/sdk_test.go +++ b/pkg/web3/sdk_test.go @@ -1,13 +1,17 @@ package web3_test import ( + "context" "errors" "log" "os" "testing" + "github.com/BurntSushi/toml" "github.com/lilypad-tech/lilypad/pkg/options" + "github.com/lilypad-tech/lilypad/pkg/system" "github.com/lilypad-tech/lilypad/pkg/web3" + "go.opentelemetry.io/otel/trace/noop" ) func getConfig() (*options.Config, error) { @@ -44,7 +48,8 @@ func CreateTestWeb3SDK() (*web3.Web3SDK, error) { JobCreatorAddress: config.Web3.JobCreatorAddress, } - sdk, err := web3.NewContractSDK(options) + noopTracer := noop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.DefaultService)) + sdk, err := web3.NewContractSDK(context.Background(), options, noopTracer) if err != nil { return nil, err } @@ -52,7 +57,6 @@ func CreateTestWeb3SDK() (*web3.Web3SDK, error) { return sdk, nil } - func TestGetBalance(t *testing.T) { sdk, err := CreateTestWeb3SDK() if err != nil { diff --git a/test/integration_test.go b/test/integration_test.go index ee3bb025..5aa79f3f 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -16,6 +16,7 @@ import ( "github.com/lilypad-tech/lilypad/pkg/system" "github.com/lilypad-tech/lilypad/pkg/web3" "github.com/stretchr/testify/assert" + traceNoop "go.opentelemetry.io/otel/trace/noop" ) type testOptions struct { @@ -38,7 +39,8 @@ func getMediator( return nil, err } - web3SDK, err := web3.NewContractSDK(mediatorOptions.Web3) + noopTracer := traceNoop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.MediatorService)) + web3SDK, err := web3.NewContractSDK(systemContext.Ctx, mediatorOptions.Web3, noopTracer) if err != nil { return nil, err }