-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvidia_mock.go
59 lines (47 loc) · 1.39 KB
/
nvidia_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"log/slog"
"math/rand/v2"
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"github.com/google/uuid"
)
type MockProducer struct {
deviceUuids []string
lastTime 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)
for range nDevices {
deviceUuids = append(deviceUuids, uuid.New().String())
}
return &MockProducer{
deviceUuids: deviceUuids,
lastTime: samplesFromTime,
}
}
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)
m := ms.AppendEmpty()
g := m.SetEmptyGauge()
now := time.Now()
m.SetName("gpu_utilization_percent")
for i, uuid := range p.deviceUuids {
lastTimeRounded := p.lastTime.Truncate(PERIOD).Add(PERIOD)
for lastTimeRounded.Before(now) {
dp := g.DataPoints().AppendEmpty()
dp.SetIntValue(int64(rand.IntN(100)))
dp.SetTimestamp(pcommon.NewTimestampFromTime(lastTimeRounded))
lastTimeRounded = lastTimeRounded.Add(PERIOD)
dp.Attributes().PutStr("UUID", uuid)
dp.Attributes().PutInt("index", int64(i))
}
}
p.lastTime = now
}
return nil
}