more metrics infrastructure (#128)

This commit is contained in:
Michael Quigley 2023-03-03 13:51:10 -05:00
parent 56606d9b01
commit d5fb9270b6
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 85 additions and 4 deletions

39
cmd/zrok/metrics.go Normal file
View File

@ -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 <configPath>",
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)
}
}

View File

@ -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
}

View File

@ -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() {
}

4
etc/metrics.yml Normal file
View File

@ -0,0 +1,4 @@
source:
type: file
path: /tmp/fabric-usage.log