Skip to content

Commit

Permalink
feat: refactor reporters configuration (#47)
Browse files Browse the repository at this point in the history
* feat: refactor reporters configuration

* feat: validate reporters and chains names uniqueness

* chore: pass reporter name to Telegram

* chore: rename TelegramReporter -> Reporter

* chore: pass type to reporter
  • Loading branch information
freak12techno committed Feb 19, 2024
1 parent 1629d04 commit 901a1fa
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 83 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ linters:
- deadcode
- depguard
- interfacebloat
- perfsprint
17 changes: 7 additions & 10 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ level = "info"
# solutions like ELK. Defaults to false.
json = false

# Telegram reporter configuration. See README.md for more details
[telegram]
# Telegram bot token.
token = "xxx:yyy"
# Chat ID to send reports to.
chat = 12345
# A list of user IDs that are allowed to contact the bot. The bot won't respond to others
# if this list is not empty. Strongly recommended to not leave it out, as otherwise
# anyone would be able to use your bot.
admins = [67890]
# Reporters configuration.
[[reporters]]
# Reporter type. Currently, the only supported type is "telegram", which is the default.
type = "telegram"
# Telegram config configuration. Required if the type is "telegram".
# See README.md for more details.
telegram-config = { token = "xxx:yyy", chat = 12345, admins = [67890] }

# Per-chain configuration. There can be multiple chains.
[[chains]]
Expand Down
18 changes: 14 additions & 4 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
metricsPkg "main/pkg/metrics"
nodesManagerPkg "main/pkg/nodes_manager"
reportersPkg "main/pkg/reporters"
"main/pkg/reporters/telegram"

"github.com/rs/zerolog"
)
Expand All @@ -41,8 +40,16 @@ func NewApp(config *config.AppConfig, version string) *App {

nodesManager := nodesManagerPkg.NewNodesManager(logger, config, metricsManager)

reporters := []reportersPkg.Reporter{
telegram.NewTelegramReporter(config, logger, nodesManager, aliasManager, version),
reporters := make([]reportersPkg.Reporter, len(config.Reporters))
for index, reporterConfig := range config.Reporters {
reporters[index] = reportersPkg.GetReporter(
reporterConfig,
config,
logger,
nodesManager,
aliasManager,
version,
)
}

dataFetchers := make(map[string]*data_fetcher.DataFetcher, len(config.Chains))
Expand Down Expand Up @@ -76,7 +83,10 @@ func (a *App) Start() {
reporter.Init()
a.MetricsManager.LogReporterEnabled(reporter.Name(), reporter.Enabled())
if reporter.Enabled() {
a.Logger.Info().Str("name", reporter.Name()).Msg("Init reporter")
a.Logger.Info().
Str("name", reporter.Name()).
Str("type", reporter.Type()).
Msg("Init reporter")
}
}

Expand Down
40 changes: 15 additions & 25 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@ func (c Chains) FindByName(name string) *types.Chain {
return nil
}

type AppConfig struct {
Path string
AliasesPath string
TelegramConfig TelegramConfig
LogConfig LogConfig
Chains Chains
Metrics MetricsConfig
Timezone *time.Location
}
type Reporters []*types.Reporter

type TelegramConfig struct {
Chat int64
Token string
Admins []int64
type AppConfig struct {
Path string
AliasesPath string
LogConfig LogConfig
Chains Chains
Reporters Reporters
Metrics MetricsConfig
Timezone *time.Location
}

type LogConfig struct {
Expand Down Expand Up @@ -84,11 +80,6 @@ func FromTomlConfig(c *tomlConfig.TomlConfig, path string) *AppConfig {
return &AppConfig{
Path: path,
AliasesPath: c.AliasesPath,
TelegramConfig: TelegramConfig{
Chat: c.TelegramConfig.Chat,
Token: c.TelegramConfig.Token,
Admins: c.TelegramConfig.Admins,
},
LogConfig: LogConfig{
LogLevel: c.LogConfig.LogLevel,
JSONOutput: c.LogConfig.JSONOutput.Bool,
Expand All @@ -100,18 +91,16 @@ func FromTomlConfig(c *tomlConfig.TomlConfig, path string) *AppConfig {
Chains: utils.Map(c.Chains, func(c *tomlConfig.Chain) *types.Chain {
return c.ToAppConfigChain()
}),
Reporters: utils.Map(c.Reporters, func(r *tomlConfig.Reporter) *types.Reporter {
return r.ToAppConfigReporter()
}),
Timezone: timezone,
}
}

func (c *AppConfig) ToTomlConfig() *tomlConfig.TomlConfig {
return &tomlConfig.TomlConfig{
AliasesPath: c.AliasesPath,
TelegramConfig: tomlConfig.TelegramConfig{
Chat: c.TelegramConfig.Chat,
Token: c.TelegramConfig.Token,
Admins: c.TelegramConfig.Admins,
},
LogConfig: tomlConfig.LogConfig{
LogLevel: c.LogConfig.LogLevel,
JSONOutput: null.BoolFrom(c.LogConfig.JSONOutput),
Expand All @@ -120,8 +109,9 @@ func (c *AppConfig) ToTomlConfig() *tomlConfig.TomlConfig {
ListenAddr: c.Metrics.ListenAddr,
Enabled: null.BoolFrom(c.Metrics.Enabled),
},
Chains: utils.Map(c.Chains, tomlConfig.FromAppConfigChain),
Timezone: c.Timezone.String(),
Chains: utils.Map(c.Chains, tomlConfig.FromAppConfigChain),
Reporters: utils.Map(c.Reporters, tomlConfig.FromAppConfigReporter),
Timezone: c.Timezone.String(),
}
}

Expand Down
27 changes: 24 additions & 3 deletions pkg/config/toml_config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ func (c *Chain) Validate() error {

for index, q := range c.Queries {
if _, err := query.New(q); err != nil {
return fmt.Errorf("Error in query %d: %s", index, err)
return fmt.Errorf("error in query %d: %s", index, err)
}
}

for index, filter := range c.Filters {
if _, err := query.New(filter); err != nil {
return fmt.Errorf("Error in filter %d: %s", index, err)
return fmt.Errorf("error in filter %d: %s", index, err)
}
}

for index, denom := range c.Denoms {
if err := denom.Validate(); err != nil {
return fmt.Errorf("Error in denom %d: %s", index, err)
return fmt.Errorf("error in denom %d: %s", index, err)
}
}

Expand Down Expand Up @@ -152,3 +152,24 @@ func FromAppConfigChain(c *types.Chain) *Chain {
}

type Chains []*Chain

func (chains Chains) Validate() error {
for index, chain := range chains {
if err := chain.Validate(); err != nil {
return fmt.Errorf("error in chain %d: %s", index, err)
}
}

// checking names uniqueness
names := map[string]bool{}

for _, chain := range chains {
if _, ok := names[chain.Name]; ok {
return fmt.Errorf("duplicate chain name: %s", chain.Name)
}

names[chain.Name] = true
}

return nil
}
103 changes: 103 additions & 0 deletions pkg/config/toml_config/reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package toml_config

import (
"errors"
"fmt"
"main/pkg/config/types"
"main/pkg/constants"
"main/pkg/utils"
"strings"
)

type TelegramConfig struct {
Chat int64 `toml:"chat"`
Token string `toml:"token"`
Admins []int64 `toml:"admins"`
}

type Reporter struct {
Name string `toml:"name"`
Type string `default:"telegram" toml:"type"`

TelegramConfig *TelegramConfig `toml:"telegram-config"`
}

func (reporter *Reporter) Validate() error {
if reporter.Name == "" {
return errors.New("reporter name not provided")
}

reporterTypes := constants.GetReporterTypes()
if !utils.Contains(reporterTypes, reporter.Type) {
return fmt.Errorf(
"expected type to be one of %s, but got %s",
strings.Join(reporterTypes, ", "),
reporter.Type,
)
}

if reporter.Type == constants.ReporterTypeTelegram && reporter.TelegramConfig == nil {
return errors.New("missing telegram-config for Telegram reporter")
}

return nil
}

type Reporters []*Reporter

func (reporters Reporters) Validate() error {
for index, reporter := range reporters {
if err := reporter.Validate(); err != nil {
return fmt.Errorf("error in reporter %d: %s", index, err)
}
}

// checking names uniqueness
names := map[string]bool{}

for _, reporter := range reporters {
if _, ok := names[reporter.Name]; ok {
return fmt.Errorf("duplicate reporter name: %s", reporter.Name)
}

names[reporter.Name] = true
}

return nil
}

func FromAppConfigReporter(reporter *types.Reporter) *Reporter {
var telegramConfig *TelegramConfig

if reporter.TelegramConfig != nil {
telegramConfig = &TelegramConfig{
Chat: reporter.TelegramConfig.Chat,
Token: reporter.TelegramConfig.Token,
Admins: reporter.TelegramConfig.Admins,
}
}

return &Reporter{
Name: reporter.Name,
Type: reporter.Type,
TelegramConfig: telegramConfig,
}
}

func (reporter *Reporter) ToAppConfigReporter() *types.Reporter {
var telegramConfig *types.TelegramConfig

if reporter.TelegramConfig != nil {
telegramConfig = &types.TelegramConfig{
Chat: reporter.TelegramConfig.Chat,
Token: reporter.TelegramConfig.Token,
Admins: reporter.TelegramConfig.Admins,
}
}

return &types.Reporter{
Name: reporter.Name,
Type: reporter.Type,
TelegramConfig: telegramConfig,
}
}
27 changes: 12 additions & 15 deletions pkg/config/toml_config/toml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ import (
)

type TomlConfig struct {
AliasesPath string `toml:"aliases"`
TelegramConfig TelegramConfig `toml:"telegram"`
LogConfig LogConfig `toml:"log"`
MetricsConfig MetricsConfig `toml:"metrics"`
Chains Chains `toml:"chains"`
Timezone string `default:"Etc/GMT" toml:"timezone"`
}
AliasesPath string `toml:"aliases"`
LogConfig LogConfig `toml:"log"`
MetricsConfig MetricsConfig `toml:"metrics"`
Chains Chains `toml:"chains"`
Timezone string `default:"Etc/GMT" toml:"timezone"`

type TelegramConfig struct {
Chat int64 `toml:"chat"`
Token string `toml:"token"`
Admins []int64 `toml:"admins"`
Reporters Reporters `toml:"reporters"`
}

type LogConfig struct {
Expand All @@ -36,10 +31,12 @@ func (c *TomlConfig) Validate() error {
return fmt.Errorf("error parsing timezone: %s", err)
}

for index, chain := range c.Chains {
if err := chain.Validate(); err != nil {
return fmt.Errorf("error in chain %d: %s", index, err)
}
if err := c.Chains.Validate(); err != nil {
return fmt.Errorf("error in chains: %s", err)
}

if err := c.Reporters.Validate(); err != nil {
return fmt.Errorf("error in reporters: %s", err)
}

return nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/config/types/reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

type TelegramConfig struct {
Chat int64
Token string
Admins []int64
}

type Reporter struct {
Name string
Type string

TelegramConfig *TelegramConfig
}
8 changes: 8 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ package constants

const (
PrometheusMetricsPrefix = "cosmos_transactions_bot_"

ReporterTypeTelegram string = "telegram"
)

func GetReporterTypes() []string {
return []string{
ReporterTypeTelegram,
}
}
Loading

0 comments on commit 901a1fa

Please sign in to comment.