Skip to content

Commit

Permalink
chore: Add web3 sdk tracers (#384)
Browse files Browse the repository at this point in the history
* refactor: Replace tracerProvider with tracer on controller

* chore: Add tracer to web3 SDK

* refactor: Add configureTelemetry helper

* chore: Add noop tracers
  • Loading branch information
bgins authored Sep 24, 2024
1 parent 9a4bbb6 commit a4ddd54
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 47 deletions.
4 changes: 3 additions & 1 deletion cmd/lilypad/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/system"
"github.com/lilypad-tech/lilypad/pkg/web3"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel/trace/noop"
)

func newJobCreatorCmd() *cobra.Command {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/lilypad/mediator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/lilypad/pow_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
26 changes: 9 additions & 17 deletions cmd/lilypad/resource-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/lilypad/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/lilypad/utils.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 3 additions & 1 deletion pkg/jobcreator/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
37 changes: 18 additions & 19 deletions pkg/resourceprovider/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)),
Expand Down
5 changes: 3 additions & 2 deletions pkg/resourceprovider/resourceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/web3/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions pkg/web3/sdk_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -44,15 +48,15 @@ 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
}

return sdk, nil
}


func TestGetBalance(t *testing.T) {
sdk, err := CreateTestWeb3SDK()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit a4ddd54

Please sign in to comment.