From d5fb9270b61f578e41262d7ade26577f3b179f88 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 3 Mar 2023 13:51:10 -0500 Subject: [PATCH] more metrics infrastructure (#128) --- cmd/zrok/metrics.go | 39 ++++++++++++++++++++++++++++++++ controller/metrics/config.go | 13 +++++++++++ controller/metrics/fileSource.go | 33 +++++++++++++++++++++++---- etc/metrics.yml | 4 ++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 cmd/zrok/metrics.go create mode 100644 etc/metrics.yml diff --git a/cmd/zrok/metrics.go b/cmd/zrok/metrics.go new file mode 100644 index 00000000..445348d5 --- /dev/null +++ b/cmd/zrok/metrics.go @@ -0,0 +1,39 @@ +package main + +import ( + "github.com/michaelquigley/cf" + "github.com/openziti/zrok/controller/metrics" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(newMetricsCommand().cmd) +} + +type metricsCommand struct { + cmd *cobra.Command +} + +func newMetricsCommand() *metricsCommand { + cmd := &cobra.Command{ + Use: "metrics ", + Short: "Start a zrok metrics agent", + Args: cobra.ExactArgs(1), + } + command := &metricsCommand{cmd} + cmd.Run = command.run + return command +} + +func (cmd *metricsCommand) run(_ *cobra.Command, args []string) { + cfg, err := metrics.LoadConfig(args[0]) + if err != nil { + panic(err) + } + logrus.Infof(cf.Dump(cfg, metrics.GetCfOptions())) + + if err := metrics.Run(cfg); err != nil { + panic(err) + } +} diff --git a/controller/metrics/config.go b/controller/metrics/config.go index 471844de..ab38f941 100644 --- a/controller/metrics/config.go +++ b/controller/metrics/config.go @@ -1,5 +1,18 @@ package metrics +import ( + "github.com/michaelquigley/cf" + "github.com/pkg/errors" +) + type Config struct { Source interface{} } + +func LoadConfig(path string) (*Config, error) { + cfg := &Config{} + if err := cf.BindYaml(cfg, path, GetCfOptions()); err != nil { + return nil, errors.Wrapf(err, "error loading config from '%v'", path) + } + return cfg, nil +} diff --git a/controller/metrics/fileSource.go b/controller/metrics/fileSource.go index 97bbfed4..c545bd12 100644 --- a/controller/metrics/fileSource.go +++ b/controller/metrics/fileSource.go @@ -1,17 +1,42 @@ package metrics -import "github.com/michaelquigley/cf" +import ( + "github.com/michaelquigley/cf" + "github.com/pkg/errors" + "os" +) type FileSourceConfig struct { Path string } func loadFileSourceConfig(v interface{}, opts *cf.Options) (interface{}, error) { - return nil, nil + if submap, ok := v.(map[string]interface{}); ok { + cfg := &FileSourceConfig{} + if err := cf.Bind(cfg, submap, cf.DefaultOptions()); err != nil { + return nil, err + } + return &fileSource{cfg}, nil + } + return nil, errors.New("invalid config structure for 'file' source") } -type fileSource struct{} +type fileSource struct { + cfg *FileSourceConfig +} func (s *fileSource) Start() (chan struct{}, error) { - return nil, nil + f, err := os.Open(s.cfg.Path) + if err != nil { + return nil, errors.Wrapf(err, "error opening '%v'", s.cfg.Path) + } + ch := make(chan struct{}) + go func() { + f.Close() + close(ch) + }() + return ch, nil +} + +func (s *fileSource) Stop() { } diff --git a/etc/metrics.yml b/etc/metrics.yml new file mode 100644 index 00000000..b0fc6308 --- /dev/null +++ b/etc/metrics.yml @@ -0,0 +1,4 @@ +source: + type: file + path: /tmp/fabric-usage.log + \ No newline at end of file