Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke committed Dec 20, 2024
1 parent 4fc469d commit 687e4da
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions internal/pdh/registry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ type Counter struct {

func NewCollector[T any](object string, _ []string) (*Collector, error) {
collector := &Collector{
object: object,
query: MapCounterToIndex(object),
object: object,
query: MapCounterToIndex(object),
nameIndexValue: -1,
counters: make(map[string]Counter),
}

var values [0]T
Expand All @@ -44,19 +46,21 @@ func NewCollector[T any](object string, _ []string) (*Collector, error) {
}

for _, f := range reflect.VisibleFields(valueType) {
counterName, ok := f.Tag.Lookup("perflib")
counterName, ok := f.Tag.Lookup("perfdata")
if !ok {
continue
}

var counter Counter
if counter, ok = collector.counters[counterName]; !ok {
counter = Counter{
Name: counterName,
Name: counterName,
FieldIndexSecondValue: -1,
FieldIndexValue: -1,
}
}

if strings.HasPrefix(counterName, ",secondvalue") {
if strings.HasSuffix(counterName, ",secondvalue") {
counterName = strings.TrimSuffix(counterName, ",secondvalue")

counter.FieldIndexSecondValue = f.Index[0]
Expand Down Expand Up @@ -125,7 +129,7 @@ func (c *Collector) Collect(data any) error {
instanceName = pdh.InstanceEmpty
}

if c.nameIndexValue != 0 {
if c.nameIndexValue != -1 {
elemValue.Field(c.nameIndexValue).SetString(instanceName)
}

Expand All @@ -142,23 +146,27 @@ func (c *Collector) Collect(data any) error {
continue
}

value := dv.Index(index).Field(counter.FieldIndexValue)

switch perfCounter.Def.CounterType {
case pdh.PERF_ELAPSED_TIME:
value.SetFloat(float64((perfCounter.Value - pdh.WindowsEpoch) / perfObject.Frequency))
dv.Index(index).
Field(counter.FieldIndexValue).
SetFloat(float64((perfCounter.Value - pdh.WindowsEpoch) / perfObject.Frequency))
case pdh.PERF_100NSEC_TIMER, pdh.PERF_PRECISION_100NS_TIMER:
value.SetFloat(float64(perfCounter.Value) * pdh.TicksToSecondScaleFactor)
case pdh.PERF_AVERAGE_BULK, pdh.PERF_RAW_FRACTION:
if counter.FieldIndexSecondValue != 0 {
dv.Index(index).
Field(counter.FieldIndexValue).
SetFloat(float64(perfCounter.Value) * pdh.TicksToSecondScaleFactor)
default:
if counter.FieldIndexSecondValue != -1 {
dv.Index(index).
Field(counter.FieldIndexSecondValue).
SetFloat(float64(perfCounter.SecondValue))
}

fallthrough
default:
value.SetFloat(float64(perfCounter.Value))
if counter.FieldIndexValue != -1 {
dv.Index(index).
Field(counter.FieldIndexValue).
SetFloat(float64(perfCounter.Value))
}
}
}
}
Expand Down

0 comments on commit 687e4da

Please sign in to comment.