Skip to content

Commit

Permalink
expose metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Feb 19, 2024
1 parent 6c487fe commit b8954c4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ func main() {

fog.SetupLogs()
fog.PrintConfig()
go catchSignal(fog)

if err := fog.Start(); err != nil {
log.Fatalf("Starting Fog Failed: %v", err)
}
}

func catchSignal(fog *fog.Config) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
// Wait here for a signal to shut down.
Expand Down
5 changes: 2 additions & 3 deletions pkg/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ func (f *FileBuffer) Flush(opts FlusOpts) {
}

func (f *FileBuffer) flush(opts FlusOpts) {
defer IncFiles()

f.mu.Lock()
defer f.mu.Unlock()

Expand Down Expand Up @@ -146,5 +144,6 @@ func (f *FileBuffer) flush(opts FlusOpts) {
}

AddBytes(float64(size))
f.Printf("%s (%s) %d bytes (%d lines) to '%s'", word, opts.Type, size, f.writes, f.Path)
IncFiles()
f.Printf("%s (%s) %d bytes (%d writes) to '%s'", word, opts.Type, size, f.writes, f.Path)
}
2 changes: 1 addition & 1 deletion pkg/fog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Config) PrintConfig() {
c.Printf("=> Fogwillow Starting, pid: %d", os.Getpid())
c.Printf("=> Listen Address: %s", c.ListenAddr)
c.Printf("=> Output Path: %s", c.OutputPath)
c.Printf("=> Intervals; Flush/Group: %s", c.FlushInterval, c.GroupInterval)
c.Printf("=> Intervals; Flush/Group: %s/%s", c.FlushInterval, c.GroupInterval)
c.Printf("=> Buffers; UDP/Packet/Chan: %d/%d/%d", c.BufferUDP, c.BufferPacket, c.BufferChan)
c.Printf("=> Threads; Listen/Process: %d/%d", c.Listeners, c.Processors)

Expand Down
27 changes: 26 additions & 1 deletion pkg/fog/start.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package fog

import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"time"

"github.com/Notifiarr/fogwillow/pkg/buf"
"github.com/Notifiarr/fogwillow/pkg/willow"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golift.io/cnfg"
"golift.io/cnfgfile"
)
Expand Down Expand Up @@ -40,6 +44,7 @@ type Config struct {
sock *net.UDPConn
willow *willow.Willow
metrics *Metrics
httpSrv *http.Server
}

// LoadConfigFile does what its name implies.
Expand Down Expand Up @@ -82,6 +87,22 @@ func (c *Config) Start() error {
go c.packetListener(i + 1)
}

smx := http.NewServeMux()
c.httpSrv = &http.Server{
Handler: smx,
Addr: c.ListenAddr,
ReadTimeout: time.Second,
ReadHeaderTimeout: time.Second,
WriteTimeout: time.Second,
IdleTimeout: 20 * time.Second,
}

smx.Handle("/metrics", promhttp.Handler())
err := c.httpSrv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}

return nil
}

Expand All @@ -108,8 +129,8 @@ func (c *Config) setup() {
c.packets = make(chan *packet, c.BufferChan)
c.metrics = getMetrics(c)
c.Config.Expires = c.metrics.Expires.Inc
buf.AddBytes = c.metrics.Bytes.Add
buf.IncFiles = c.metrics.Files.Inc
buf.AddBytes = c.metrics.Bytes.Add
c.willow = willow.NeWillow(c.Config)
}

Expand All @@ -131,6 +152,10 @@ func (c *Config) setupSocket() error {
}

func (c *Config) Shutdown() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

c.httpSrv.Shutdown(ctx)
c.sock.Close()
close(c.packets)
c.willow.Stop()
Expand Down

0 comments on commit b8954c4

Please sign in to comment.