From 5a0bec8866738f8fc7e80b287d7d06b1b4245892 Mon Sep 17 00:00:00 2001 From: alexandru topliceanu Date: Wed, 5 Jan 2022 18:41:10 +0000 Subject: [PATCH] update configuration to expose parsing raw feeds from the rdd --- pkg/monitoring/config/config.go | 4 +- pkg/monitoring/config/defaults.go | 8 +-- pkg/monitoring/config/feeds.go | 81 ++++++++++++++----------------- pkg/monitoring/config/types.go | 7 +-- 4 files changed, 46 insertions(+), 54 deletions(-) diff --git a/pkg/monitoring/config/config.go b/pkg/monitoring/config/config.go index a004b4e6c..ecaf37da0 100644 --- a/pkg/monitoring/config/config.go +++ b/pkg/monitoring/config/config.go @@ -104,14 +104,14 @@ func parseEnvVars(cfg *Config) error { if err != nil { return fmt.Errorf("failed to parse env var FEEDS_RDD_READ_TIMEOUT, see https://pkg.go.dev/time#ParseDuration: %w", err) } - cfg.Feeds.RddReadTimeout = readTimeout + cfg.Feeds.RDDReadTimeout = readTimeout } if value, isPresent := os.LookupEnv("FEEDS_RDD_POLL_INTERVAL"); isPresent { pollInterval, err := time.ParseDuration(value) if err != nil { return fmt.Errorf("failed to parse env var FEEDS_RDD_POLL_INTERVAL, see https://pkg.go.dev/time#ParseDuration: %w", err) } - cfg.Feeds.RddPollInterval = pollInterval + cfg.Feeds.RDDPollInterval = pollInterval } if value, isPresent := os.LookupEnv("HTTP_ADDRESS"); isPresent { diff --git a/pkg/monitoring/config/defaults.go b/pkg/monitoring/config/defaults.go index 978af5e5e..a56eb1cde 100644 --- a/pkg/monitoring/config/defaults.go +++ b/pkg/monitoring/config/defaults.go @@ -9,10 +9,10 @@ func applyDefaults(cfg *Config) { if cfg.Solana.PollInterval == 0 { cfg.Solana.PollInterval = 5 * time.Second } - if cfg.Feeds.RddReadTimeout == 0 { - cfg.Feeds.RddReadTimeout = 1 * time.Second + if cfg.Feeds.RDDReadTimeout == 0 { + cfg.Feeds.RDDReadTimeout = 1 * time.Second } - if cfg.Feeds.RddPollInterval == 0 { - cfg.Feeds.RddPollInterval = 10 * time.Second + if cfg.Feeds.RDDPollInterval == 0 { + cfg.Feeds.RDDPollInterval = 10 * time.Second } } diff --git a/pkg/monitoring/config/feeds.go b/pkg/monitoring/config/feeds.go index 024961d30..267e54ff8 100644 --- a/pkg/monitoring/config/feeds.go +++ b/pkg/monitoring/config/feeds.go @@ -1,74 +1,65 @@ package config import ( - "context" "encoding/json" "fmt" - "net/http" "os" "github.com/gagliardetto/solana-go" ) func populateFeeds(cfg *Config) error { - feeds := []jsonFeedConfig{} - if cfg.Feeds.URL != "" { - rddCtx, cancel := context.WithTimeout(context.Background(), cfg.Feeds.RddReadTimeout) - defer cancel() - readFeedsReq, err := http.NewRequestWithContext(rddCtx, http.MethodGet, cfg.Feeds.URL, nil) - if err != nil { - return fmt.Errorf("unable to build a request to the RDD URL '%s': %w", cfg.Feeds.URL, err) - } - httpClient := &http.Client{} - res, err := httpClient.Do(readFeedsReq) - if err != nil { - return fmt.Errorf("unable to fetch RDD data from URL '%s': %w", cfg.Feeds.URL, err) - } - defer res.Body.Close() - decoder := json.NewDecoder(res.Body) - if err := decoder.Decode(&feeds); err != nil { - return fmt.Errorf("unable to unmarshal feeds config from RDD URL '%s': %w", cfg.Feeds.URL, err) - } - } else if cfg.Feeds.FilePath != "" { - contents, err := os.ReadFile(cfg.Feeds.FilePath) - if err != nil { - return fmt.Errorf("unable to read feeds file '%s': %w", cfg.Feeds.FilePath, err) - } - if err = json.Unmarshal(contents, &feeds); err != nil { - return fmt.Errorf("unable to unmarshal feeds config from file '%s': %w", cfg.Feeds.FilePath, err) - } + if cfg.Feeds.FilePath == "" { + return nil + } + contents, err := os.ReadFile(cfg.Feeds.FilePath) + if err != nil { + return fmt.Errorf("unable to read feeds file '%s': %w", cfg.Feeds.FilePath, err) + } + rawFeeds := []RawFeedConfig{} + if err = json.Unmarshal(contents, &rawFeeds); err != nil { + return fmt.Errorf("unable to unmarshal feeds config from file '%s': %w", cfg.Feeds.FilePath, err) + } + feeds, err := NewFeeds(rawFeeds) + if err != nil { + return err } + cfg.Feeds.Feeds = feeds + return nil +} - cfg.Feeds.Feeds = make([]Feed, len(feeds)) - for i, feed := range feeds { - contractAddress, err := solana.PublicKeyFromBase58(feed.ContractAddressBase58) +func NewFeeds(rawFeeds []RawFeedConfig) ([]Feed, error) { + feeds := make([]Feed, len(rawFeeds)) + for i, rawFeed := range rawFeeds { + contractAddress, err := solana.PublicKeyFromBase58(rawFeed.ContractAddressBase58) if err != nil { - return fmt.Errorf("failed to parse program id '%s' from JSON at index i=%d: %w", feed.ContractAddressBase58, i, err) + return nil, fmt.Errorf("failed to parse program id '%s' from JSON at index i=%d: %w", rawFeed.ContractAddressBase58, i, err) } - transmissionsAccount, err := solana.PublicKeyFromBase58(feed.TransmissionsAccountBase58) + transmissionsAccount, err := solana.PublicKeyFromBase58(rawFeed.TransmissionsAccountBase58) if err != nil { - return fmt.Errorf("failed to parse transmission account '%s' from JSON at index i=%d: %w", feed.TransmissionsAccountBase58, i, err) + return nil, fmt.Errorf("failed to parse transmission account '%s' from JSON at index i=%d: %w", rawFeed.TransmissionsAccountBase58, i, err) } - stateAccount, err := solana.PublicKeyFromBase58(feed.StateAccountBase58) + stateAccount, err := solana.PublicKeyFromBase58(rawFeed.StateAccountBase58) if err != nil { - return fmt.Errorf("failed to parse state account '%s' from JSON at index i=%d: %w", feed.StateAccountBase58, i, err) + return nil, fmt.Errorf("failed to parse state account '%s' from JSON at index i=%d: %w", rawFeed.StateAccountBase58, i, err) } - cfg.Feeds.Feeds[i] = Feed{ - feed.FeedName, - feed.FeedPath, - feed.Symbol, - feed.Heartbeat, - feed.ContractType, - feed.ContractStatus, + feeds[i] = Feed{ + rawFeed.FeedName, + rawFeed.FeedPath, + rawFeed.Symbol, + rawFeed.Heartbeat, + rawFeed.ContractType, + rawFeed.ContractStatus, contractAddress, transmissionsAccount, stateAccount, } } - return nil + return feeds, nil } -type jsonFeedConfig struct { +// RawFeedConfig should only be used for deserializing responses from the RDD. +type RawFeedConfig struct { FeedName string `json:"name,omitempty"` FeedPath string `json:"path,omitempty"` Symbol string `json:"symbol,omitempty"` diff --git a/pkg/monitoring/config/types.go b/pkg/monitoring/config/types.go index 5ed925166..e06a5a634 100644 --- a/pkg/monitoring/config/types.go +++ b/pkg/monitoring/config/types.go @@ -47,11 +47,12 @@ type SchemaRegistry struct { } type Feeds struct { - URL string + // If URL is set, the RDD tracker will start and override any feed configs extracted from FilePath! FilePath string + URL string + RDDReadTimeout time.Duration + RDDPollInterval time.Duration Feeds []Feed - RddReadTimeout time.Duration - RddPollInterval time.Duration } type Feed struct {