Skip to content

Commit

Permalink
(#2165) Store metrics in a per-watcher variable
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Apr 6, 2024
1 parent 7e4e40a commit 6bc1961
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions aagent/watchers/metricwatcher/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
type Metric struct {
Labels map[string]string `json:"labels"`
Metrics map[string]float64 `json:"metrics"`
Time int64 `json:"time"`
name string
machine string
seen int
Expand Down Expand Up @@ -262,6 +263,8 @@ func (w *Watcher) handleCheck(ctx context.Context, output []byte, err error) err
return w.FailureTransition()
}

metric.Time = time.Now().Unix()

for k, v := range w.properties.Labels {
metric.Labels[k] = v
}
Expand Down Expand Up @@ -297,7 +300,7 @@ func (w *Watcher) storeMetricAsData(metric *Metric) error {

w.Debugf("Storing metrics to machine data")

return w.machine.DataPut("metric", metric)
return w.machine.DataPut("metric", map[string]any{w.name: metric})
}

func (w *Watcher) publishToGraphite(ctx context.Context, metric *Metric) error {
Expand Down Expand Up @@ -342,8 +345,6 @@ func (w *Watcher) publishToGraphite(ctx context.Context, metric *Metric) error {
}
defer conn.Close()

now := time.Now().Unix()

// copy it so we can add stuff to it without impacting other parts
// TODO: use maps.Copy() later
m := make(map[string]float64)
Expand All @@ -359,7 +360,7 @@ func (w *Watcher) publishToGraphite(ctx context.Context, metric *Metric) error {
}

name := fmt.Sprintf("%s.%s", prefix, k)
_, err = conn.Write([]byte(fmt.Sprintf("%s %f %d\n", name, v, now)))
_, err = conn.Write([]byte(fmt.Sprintf("%s %f %d\n", name, v, metric.Time)))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion aagent/watchers/metricwatcher/metric_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2020-2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -199,6 +199,7 @@ var _ = Describe("MetricWatcher", func() {
"metrics": map[string]any{
"choria_runtime_seconds": 0.5,
},
"time": float64(0),
},
},
}))
Expand Down

0 comments on commit 6bc1961

Please sign in to comment.