Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix events exporter config loading from symlinks #36

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 26 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,39 @@ func (reader *HeadReader) Read(ctx context.Context) error {
return fmt.Errorf("no new events during tick interval")
}
case <-ctx.Done():
return nil
reader.EventsRate = 0
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 {
callCtx, 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(callCtx, h); err != nil {
return err
}
if err := reader.ProcessBlockParaVotes(callCtx, h); err != nil {
return err
}
case <-ctx.Done():
return nil
return ctx.Err()
case <-callCtx.Done():
return callCtx.Err()
}
cancel()
}
})
if err := g.Wait(); err != nil {
Expand Down