Skip to content

Commit

Permalink
fix events exporter config loading from symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
xxbbxb committed Apr 16, 2024
1 parent f22ecc9 commit 36fbef9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
28 changes: 17 additions & 11 deletions exporters/events/cmd/events-exporter/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,41 @@ func (reader *HeadReader) GetValidatorsIdentity(ctx context.Context, accU32 stri

func (reader *HeadReader) ReadKnownValidatorsFile(filename string) {
if filename != "" {

fi, err := os.Lstat(filename)
if err != nil {
reader.log.WithError(err).Errorf("loading known validators skipped %s", filename)
return
}
if fi.Mode()&os.ModeSymlink != 0 {
dir := filepath.Dir(filename)
filename, _ = os.Readlink(filename)
}
pth, err := filepath.Abs(filename)
if err != nil {
reader.log.WithError(err).Errorf("loading known validators skipped %s", pth)
return
filename = filepath.Join(dir, filename)
}

if r, err := os.Open(pth); err != nil {
reader.log.WithError(err).Errorf("loading known validators skipped %s", pth)
if r, err := os.Open(filename); err != nil {
reader.log.WithError(err).Errorf("loading known validators skipped %s", filename)
return
} else {
payload, err := io.ReadAll(r)
if err != nil {
reader.log.WithError(err).Errorf("loading known validators skipped %s", pth)
reader.log.WithError(err).Errorf("loading known validators skipped %s", filename)
return
}
registry := ValidatorsRegistry{}
var registry ValidatorsRegistry
if err := yaml.Unmarshal(payload, &registry); err != nil {
reader.log.WithError(err).Errorf("loading known validators skipped %s", pth)
reader.log.WithError(err).Errorf("loading known validators skipped %s", filename)
return
}
reader.registry = &registry
if reader.registry == nil {
reader.registry = &ValidatorsRegistry{
Validators: map[string]ValidatorRecord{},
}
}

for k, v := range registry.Validators {
reader.registry.Validators[k] = v
}
}
}
}
Expand Down
55 changes: 23 additions & 32 deletions exporters/events/cmd/events-exporter/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
)

type Config struct {
WSUrl string `json:"WS_ENDPOINT" envconfig:"WS_ENDPOINT" default:"wss://rpc.polkadot.io/"`
MetricsListen string `json:"LISTEN" envconfig:"LISTEN" default:":9150"`
LabelChainValue string `json:"LABEL_CHAIN" envconfig:"LABEL_CHAIN" default:""`
LogLevel string `json:"LOG_LEVEL" envconfig:"LOG_LEVEL" default:"info"`
NewHeadBy string `json:"NEW_HEAD_BY" envconfig:"NEW_HEAD_BY" default:"poll"`
ExposeIdentities bool `json:"EXPOSE_ID" envconfig:"EXPOSE_ID"`
KnownValidatorCfg string `json:"VALIDATORS_CFG" envconfig:"VALIDATORS_CFG" default:""`
WSUrl string `json:"WS_ENDPOINT" envconfig:"WS_ENDPOINT" default:"wss://rpc.polkadot.io/"`
MetricsListen string `json:"LISTEN" envconfig:"LISTEN" default:":9150"`
LabelChainValue string `json:"LABEL_CHAIN" envconfig:"LABEL_CHAIN" default:""`
LogLevel string `json:"LOG_LEVEL" envconfig:"LOG_LEVEL" default:"info"`
NewHeadBy string `json:"NEW_HEAD_BY" envconfig:"NEW_HEAD_BY" default:"poll"`
ExposeIdentities bool `json:"EXPOSE_ID" envconfig:"EXPOSE_ID"`
KnownValidatorCfg []string `json:"VALIDATORS_CFG" envconfig:"VALIDATORS_CFG" default:""`
}

/*
Expand Down Expand Up @@ -77,8 +77,9 @@ func NewHeadReader(l *logrus.Logger, cfg Config, ctx context.Context) (*HeadRead
log: l,
cfg: cfg,
}
if cfg.KnownValidatorCfg != "" {
r.ReadKnownValidatorsFile(cfg.KnownValidatorCfg)
for _, path := range cfg.KnownValidatorCfg {
l.Infof("loading config from %s", path)
r.ReadKnownValidatorsFile(path)
}

return &r, nil
Expand Down Expand Up @@ -266,46 +267,36 @@ func (reader *HeadReader) Read(ctx context.Context) error {
return fmt.Errorf("no new events during tick interval")
}
case <-ctx.Done():
return nil
return ctx.Err()
}

}
})
// follow head goroutine
g.Go(func() error {
if reader.cfg.NewHeadBy == "subscription" {
if err := reader.subscribeHeadHashes(ctx, headHashes); err != nil {
return err
} else {
return nil
}
return reader.subscribeHeadHashes(ctx, headHashes)
} else {
if err := reader.pollHeadHashes(ctx, headHashes); err != nil {
return err
} else {
return nil
}
return reader.pollHeadHashes(ctx, headHashes)
}
})
// handle input hashes
g.Go(func() error {
for {
ctx, cancel := context.WithTimeout(ctx, 120*time.Second)
defer cancel()
select {
case h := <-headHashes:
g.Go(func() error {
ctx, cancel := context.WithTimeout(ctx, 120*time.Second)
defer cancel()
if err := reader.ProcessBlockEvents(ctx, h); err != nil {
return err
}
if err := reader.ProcessBlockParaVotes(ctx, h); err != nil {
return err
}
return nil
})
if err := reader.ProcessBlockEvents(ctx, h); err != nil {
return err
}
if err := reader.ProcessBlockParaVotes(ctx, h); err != nil {
return err
}
case <-ctx.Done():
return nil
return ctx.Err()
}
cancel()
}
})
if err := g.Wait(); err != nil {
Expand Down

0 comments on commit 36fbef9

Please sign in to comment.