Skip to content

Commit

Permalink
nvidia_mock: Track last time per device
Browse files Browse the repository at this point in the history
We only tracked one lastTime and it caused only one device to ever get metrics generated for and the other two were empty.
  • Loading branch information
metalmatze committed Feb 21, 2025
1 parent 1a333b4 commit 6dbea28
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/NVIDIA/go-nvml v0.12.4-0
github.com/alecthomas/kong v1.5.1
github.com/google/uuid v1.6.0
github.com/oklog/run v1.1.0
github.com/open-telemetry/otel-arrow v0.31.0
github.com/parca-dev/parca-agent v0.35.0
github.com/prometheus/client_golang v1.20.5
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
Expand Down
32 changes: 16 additions & 16 deletions nvidia_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ package main
import (
"hash/fnv"
"log/slog"
"maps"
"slices"
"time"

"github.com/google/uuid"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/google/uuid"
)

type MockProducer struct {
deviceUuids []string
lastTime time.Time
deviceLastTime map[string]time.Time
}

// NewNvidiaMockProducer creates a Producer that generates random data to send.
func NewNvidiaMockProducer(nDevices int, samplesFromTime time.Time) *MockProducer {
deviceUuids := make([]string, 0, nDevices)
deviceLastTime := make(map[string]time.Time, nDevices)
for range nDevices {
deviceUuids = append(deviceUuids, uuid.New().String())
id := uuid.New().String()
deviceLastTime[id] = samplesFromTime
}

return &MockProducer{
deviceUuids: deviceUuids,
lastTime: samplesFromTime,
}
return &MockProducer{deviceLastTime: deviceLastTime}
}

const PERIOD = time.Second / 6

func (p *MockProducer) Produce(ms pmetric.MetricSlice) error {
for i, uuid := range p.deviceUuids {
slog.Debug("Collecting metrics for device", "uuid", uuid, "index", i)
deviceIDs := slices.Sorted(maps.Keys(p.deviceLastTime)) // Maps are unsorted, so we get its keys and sort

for i, id := range deviceIDs {
slog.Info("Collecting metrics for device", "uuid", id, "index", i)

m := ms.AppendEmpty()
g := m.SetEmptyGauge()
Expand All @@ -43,10 +43,10 @@ func (p *MockProducer) Produce(ms pmetric.MetricSlice) error {

// Create jitter based on the uuid so metrics values don't overlap.
h := fnv.New32a()
_, _ = h.Write([]byte(uuid))
_, _ = h.Write([]byte(id))
jitter := int64(h.Sum32() % 100)

lastTimeRounded := p.lastTime.Truncate(PERIOD).Add(PERIOD)
lastTimeRounded := p.deviceLastTime[id].Truncate(PERIOD).Add(PERIOD)

for lastTimeRounded.Before(now) {
// This will make the value go up and down between 0 and 100 based on the timestamp's seconds.
Expand All @@ -56,14 +56,14 @@ func (p *MockProducer) Produce(ms pmetric.MetricSlice) error {
}

dp := g.DataPoints().AppendEmpty()
dp.Attributes().PutStr("UUID", uuid)
dp.Attributes().PutStr("UUID", id)
dp.Attributes().PutInt("index", int64(i))
dp.SetTimestamp(pcommon.NewTimestampFromTime(lastTimeRounded))
dp.SetIntValue(v)

lastTimeRounded = lastTimeRounded.Add(PERIOD)
}
p.lastTime = now
p.deviceLastTime[id] = now
}

return nil
Expand Down

0 comments on commit 6dbea28

Please sign in to comment.