Skip to content

Commit

Permalink
Reset reporter for gauges in tally (#3)
Browse files Browse the repository at this point in the history
* fix: closer

* fix: create new reporter with each tick
  • Loading branch information
rbarabas authored Sep 24, 2024
1 parent a8c8218 commit b3c42e3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
Binary file added fly-exporter
Binary file not shown.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ package main
import (
"sync"

"github.com/tigrisdata/fly-exporter/reporter"
"github.com/tigrisdata/fly-exporter/provider"
)

func main() {
var wg sync.WaitGroup

// Create reporter to serve up prometheus metrics
m := reporter.NewReporter()
m := provider.NewMetricProvider()
defer m.Close()
go m.ServeHttp()
wg.Add(1)
Expand Down
49 changes: 49 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package provider

import (
"log/slog"
"time"

"github.com/tigrisdata/fly-exporter/reporter"
)

type MetricProvider struct {
interval time.Duration
reporter *reporter.Reporter
}

func NewMetricProvider() MetricProvider {
mp := MetricProvider{
interval: 10 * time.Second,
reporter: reporter.NewReporter(),
}
return mp
}

func (mp *MetricProvider) Collect() {
ticker := time.NewTicker(mp.interval)

if err := mp.reporter.CollectOnce(); err != nil {
slog.Error("failed to collect metrics", "error", err)
}

for range ticker.C {
reporter := reporter.NewReporter()
if err := reporter.CollectOnce(); err != nil {
slog.Error("failed to collect metric data:", "error", err)
}
time.Sleep(1 * time.Second)
oldReporter := mp.reporter
mp.reporter = reporter
oldReporter.Close()
}
defer ticker.Stop()
}

func (mp *MetricProvider) ServeHttp() {
mp.reporter.ServeHttp()
}

func (mp *MetricProvider) Close() {
mp.reporter.Close()
}
34 changes: 16 additions & 18 deletions reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
pclient "github.com/m3db/prometheus_client_golang/prometheus"
"github.com/uber-go/tally"
"github.com/uber-go/tally/prometheus"
promreporter "github.com/uber-go/tally/prometheus"
)

type Reporter struct {
closer io.Closer
groups []Collectable
interval time.Duration
reporter prometheus.Reporter
scoped
}
Expand All @@ -59,19 +59,27 @@ func NewReporter() *Reporter {
reg := pclient.NewRegistry()

r := Reporter{
interval: 10 * time.Second,
reporter: prometheus.NewReporter(prometheus.Options{Registerer: reg}),
reporter: promreporter.NewReporter(
promreporter.Options{Registerer: reg},
),
}

// Initialize base scopes
r.scopes = make(map[string]tally.Scope)
r.scopes["root"], r.closer = tally.NewRootScope(tally.ScopeOptions{
// Create root scope
scope, closer := tally.NewRootScope(tally.ScopeOptions{
Tags: GetBaseTags(),
CachedReporter: r.reporter,
Separator: prometheus.DefaultSeparator,
Separator: promreporter.DefaultSeparator,
OmitCardinalityMetrics: true,
}, 1*time.Second)
defer closer.Close()

// Add root scope to reporter
r.scopes["root"] = scope

// Add prometheus prefix for root scope
r.AddScope(r.scopes["root"], "fly", "fly")

// Add prometheus prefix once for machine scope, collected below
r.AddScope(r.scopes["fly"], "machine", "machine")

// Collect metric groups
Expand Down Expand Up @@ -107,7 +115,7 @@ func (r *Reporter) getReport() (*models.Report, error) {
return &report, nil
}

func (r *Reporter) collectOnce() error {
func (r *Reporter) CollectOnce() error {
report, err := r.getReport()
if err != nil {
return err
Expand All @@ -128,16 +136,6 @@ func (r *Reporter) Close() {
slog.Error("failed to close server:", "error", r.closer.Close())
}

func (r *Reporter) Collect() {
ticker := time.NewTicker(r.interval)
for range ticker.C {
if err := r.collectOnce(); err != nil {
slog.Error("failed to collect metric data:", "error", err)
}
}
defer ticker.Stop()
}

func (r *Reporter) ServeHttp() {
port := os.Getenv("FLY_EXPORTER_PORT")
if port == "" {
Expand Down

0 comments on commit b3c42e3

Please sign in to comment.