zrok/controller/metrics/fileSource.go

131 lines
3.0 KiB
Go
Raw Normal View History

2023-03-03 19:31:57 +01:00
package metrics
2023-03-03 19:51:10 +01:00
import (
"encoding/binary"
2023-03-03 19:51:10 +01:00
"github.com/michaelquigley/cf"
"github.com/nxadm/tail"
"github.com/openziti/zrok/controller/env"
2023-03-03 19:51:10 +01:00
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
2023-03-03 19:51:10 +01:00
"os"
)
2023-03-03 19:31:57 +01:00
func init() {
2023-03-15 21:14:06 +01:00
env.GetCfOptions().AddFlexibleSetter("fileSource", loadFileSourceConfig)
}
2023-03-03 19:31:57 +01:00
type FileSourceConfig struct {
2023-03-15 21:14:06 +01:00
Path string
PointerPath string
2023-03-03 19:31:57 +01:00
}
2023-03-06 21:51:23 +01:00
func loadFileSourceConfig(v interface{}, _ *cf.Options) (interface{}, error) {
2023-03-03 19:51:10 +01:00
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: cfg}, nil
2023-03-03 19:51:10 +01:00
}
2023-03-15 21:14:06 +01:00
return nil, errors.New("invalid config structure for 'fileSource'")
2023-03-03 19:31:57 +01:00
}
2023-03-03 19:51:10 +01:00
type fileSource struct {
2023-03-15 21:14:06 +01:00
cfg *FileSourceConfig
ptrF *os.File
t *tail.Tail
2023-03-03 19:51:10 +01:00
}
2023-03-03 19:31:57 +01:00
2023-03-15 21:14:06 +01:00
func (s *fileSource) Start(events chan ZitiEventJson) (join chan struct{}, err error) {
2023-03-03 19:51:10 +01:00
f, err := os.Open(s.cfg.Path)
if err != nil {
return nil, errors.Wrapf(err, "error opening '%v'", s.cfg.Path)
}
_ = f.Close()
2023-03-15 21:14:06 +01:00
s.ptrF, err = os.OpenFile(s.pointerPath(), os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
2023-03-15 21:14:06 +01:00
return nil, errors.Wrapf(err, "error opening pointer '%v'", s.pointerPath())
}
2023-03-15 21:14:06 +01:00
ptr, err := s.readPtr()
if err != nil {
logrus.Errorf("error reading pointer: %v", err)
}
2023-03-15 21:14:06 +01:00
logrus.Infof("retrieved stored position pointer at '%d'", ptr)
join = make(chan struct{})
2023-03-03 19:51:10 +01:00
go func() {
2023-03-15 21:14:06 +01:00
s.tail(ptr, events)
close(join)
2023-03-03 19:51:10 +01:00
}()
return join, nil
2023-03-03 19:51:10 +01:00
}
func (s *fileSource) Stop() {
if err := s.t.Stop(); err != nil {
logrus.Error(err)
}
2023-03-03 19:31:57 +01:00
}
2023-03-15 21:14:06 +01:00
func (s *fileSource) tail(ptr int64, events chan ZitiEventJson) {
logrus.Info("started")
defer logrus.Info("stopped")
var err error
s.t, err = tail.TailFile(s.cfg.Path, tail.Config{
ReOpen: true,
Follow: true,
2023-03-15 21:14:06 +01:00
Location: &tail.SeekInfo{Offset: ptr},
})
if err != nil {
2023-03-15 21:14:06 +01:00
logrus.Errorf("error starting tail: %v", err)
return
}
2023-03-15 21:14:06 +01:00
for event := range s.t.Lines {
events <- ZitiEventJson(event.Text)
if err := s.writePtr(event.SeekInfo.Offset); err != nil {
logrus.Error(err)
}
}
}
func (s *fileSource) pointerPath() string {
if s.cfg.PointerPath == "" {
return s.cfg.Path + ".ptr"
} else {
return s.cfg.PointerPath
}
}
func (s *fileSource) readPtr() (int64, error) {
ptr := int64(0)
buf := make([]byte, 8)
if n, err := s.ptrF.Seek(0, 0); err == nil && n == 0 {
if n, err := s.ptrF.Read(buf); err == nil && n == 8 {
ptr = int64(binary.LittleEndian.Uint64(buf))
return ptr, nil
} else {
2023-03-15 21:14:06 +01:00
return 0, errors.Wrapf(err, "error reading pointer (%d): %v", n, err)
}
2023-03-15 21:14:06 +01:00
} else {
return 0, errors.Wrapf(err, "error seeking pointer (%d): %v", n, err)
}
}
2023-03-15 21:14:06 +01:00
func (s *fileSource) writePtr(ptr int64) error {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(ptr))
if n, err := s.ptrF.Seek(0, 0); err == nil && n == 0 {
if n, err := s.ptrF.Write(buf); err != nil || n != 8 {
return errors.Wrapf(err, "error writing pointer (%d): %v", n, err)
}
} else {
2023-03-15 21:14:06 +01:00
return errors.Wrapf(err, "error seeking pointer (%d): %v", n, err)
}
2023-03-15 21:14:06 +01:00
return nil
}