Skip to content

Commit

Permalink
fix: handle json numbers as both float and strings
Browse files Browse the repository at this point in the history
Earlier versions exposed some metrics as floats, now they are strings.
Use json.Number to convert all possible variants.

Fixes #11
  • Loading branch information
bluecmd committed Jul 27, 2024
1 parent bb3a5f0 commit a9c56df
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
84 changes: 61 additions & 23 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ func handleTemperature(m *metricRegistry, j string, groups []string) error {
name := groups[0]
val := struct {
Temperature struct {
Instant float64
Instant json.Number
}
}{}

if err := json.Unmarshal([]byte(j), &val); err != nil {
return fmt.Errorf("failed to parse temperature metric: %v", err)
}
log.V(2).Infof("New temperature metric for %q: %+v", name, val)
m.temperature.With(prometheus.Labels{"device": name}).Set(float64(val.Temperature.Instant))
t, err := val.Temperature.Instant.Float64()
if err != nil {
return err
}
m.temperature.With(prometheus.Labels{"device": name}).Set(t)
return nil
}

Expand Down Expand Up @@ -226,15 +230,19 @@ func handleCPUUtilization(m *metricRegistry, j string, groups []string) error {
name := groups[0]
val := struct {
State struct {
Instant float64
Instant json.Number
}
}{}

if err := json.Unmarshal([]byte(j), &val); err != nil {
return fmt.Errorf("failed to parse cpu utilization metric: %v", err)
}
log.V(2).Infof("New CPU utilization metric for %q: %+v", name, val)
m.cpuUtilization.With(prometheus.Labels{"device": name}).Set(float64(val.State.Instant) / 100.0)
cpu, err := val.State.Instant.Float64()
if err != nil {
return err
}
m.cpuUtilization.With(prometheus.Labels{"device": name}).Set(cpu / 100.0)
return nil
}

Expand Down Expand Up @@ -289,24 +297,42 @@ func handleGeneralLaser(m *metricRegistry, j string, groups []string) error {
labels = prometheus.Labels{"device": name, "index": index}
}
val := struct {
InputPower struct {
Instant float64
InputPower *struct {
Instant json.Number
} `json:"input-power"`
LaserBiasCurrent struct {
Instant float64
LaserBiasCurrent *struct {
Instant json.Number
} `json:"laser-bias-current"`
OutputPower struct {
Instant float64
OutputPower *struct {
Instant json.Number
} `json:"output-power"`
}{}

if err := json.Unmarshal([]byte(j), &val); err != nil {
return fmt.Errorf("failed to parse general laser metric: %v", err)
}
log.V(2).Infof("New general laser metric for %v, %+v", labels, val)
m.laserInputPower.With(labels).Set(val.InputPower.Instant)
m.laserBiasCurrent.With(labels).Set(val.LaserBiasCurrent.Instant / 1000.0)
m.laserOutputPower.With(labels).Set(val.OutputPower.Instant)
if val.InputPower != nil {
v, err := val.InputPower.Instant.Float64()
if err != nil {
return fmt.Errorf("input-power: %w", err)
}
m.laserInputPower.With(labels).Set(v)
}
if val.LaserBiasCurrent != nil {
v, err := val.LaserBiasCurrent.Instant.Float64()
if err != nil {
return fmt.Errorf("laser-bias-current: %w", err)
}
m.laserBiasCurrent.With(labels).Set(v / 1000.0)
}
if val.OutputPower != nil {
v, err := val.OutputPower.Instant.Float64()
if err != nil {
return fmt.Errorf("output-power: %w", err)
}
m.laserOutputPower.With(labels).Set(v)
}
return nil
}

Expand All @@ -315,29 +341,41 @@ func handleTerminalLaser(m *metricRegistry, j string, groups []string) error {
labels := prometheus.Labels{"device": name}
val := struct {
ChromaticDispersion struct {
Instant float64
Instant json.Number
} `json:"chromatic-dispersion"`
PolarizationDependentLoss struct {
Instant float64
Instant json.Number
} `json:"polarization-dependent-loss"`
PolarizationModeDispersion struct {
Instant float64
Instant json.Number
} `json:"polarization-mode-dispersion"`
LaserFrequencyOffset string `json:"laser-freq-offset"`
LaserFrequencyOffset json.Number `json:"laser-freq-offset"`
}{}

if err := json.Unmarshal([]byte(j), &val); err != nil {
return fmt.Errorf("failed to parse terminal laser metric: %v", err)
}
log.V(2).Infof("New terminal laser metric for %v, %+v", labels, val)

freqOff, err := strconv.Atoi(val.LaserFrequencyOffset)
freqOff, err := val.LaserFrequencyOffset.Float64()
if err != nil {
return err
return fmt.Errorf("laser-freq-offset: %w", err)
}
cd, err := val.ChromaticDispersion.Instant.Float64()
if err != nil {
return fmt.Errorf("chromatic-dispersion: %w", err)
}
pdl, err := val.PolarizationDependentLoss.Instant.Float64()
if err != nil {
return fmt.Errorf("polarization-dependent-loss: %w", err)
}
pmd, err := val.PolarizationModeDispersion.Instant.Float64()
if err != nil {
return fmt.Errorf("polarization-mode-dispersion: %w", err)
}
m.laserChromaticDispersion.With(labels).Set(val.ChromaticDispersion.Instant)
m.laserPolarizationDependetLoss.With(labels).Set(val.PolarizationDependentLoss.Instant)
m.laserPolarizationModeDispersion.With(labels).Set(val.PolarizationModeDispersion.Instant)
m.laserFrequencyOffset.With(labels).Set(float64(freqOff) * 1000 * 1000)
m.laserChromaticDispersion.With(labels).Set(cd)
m.laserPolarizationDependetLoss.With(labels).Set(pdl)
m.laserPolarizationModeDispersion.With(labels).Set(pmd)
m.laserFrequencyOffset.With(labels).Set(freqOff * 1000 * 1000)
return nil
}
6 changes: 1 addition & 5 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,18 @@ dc908_memory_utilized_bytes{device="MCU-1-41"} 9.08222464e+08
# TYPE dc908_laser_bias_current_amepere gauge
dc908_laser_bias_current_amepere{device="OCH-1-1-L1",index=""} 0.18869999999999998
dc908_laser_bias_current_amepere{device="OCH-1-1-L2",index=""} 0.2263
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C1",index=""} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C1",index="1"} 0.0555
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C1",index="2"} 0.055
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C1",index="3"} 0.055
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C1",index="4"} 0.0555
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C2",index=""} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C2",index="1"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C2",index="2"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C2",index="3"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C2",index="4"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C3",index=""} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C3",index="1"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C3",index="2"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C3",index="3"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C3",index="4"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C4",index=""} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C4",index="1"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C4",index="2"} 0
dc908_laser_bias_current_amepere{device="TRANSCEIVER-1-1-C4",index="3"} 0
Expand Down Expand Up @@ -173,7 +169,7 @@ dc908_temperature_celsius{device="TRANSCEIVER-1-1-L2"} 50

WalkNotification(m.GetUpdate(), func(name string, _ *time.Time, j string) {
if err := mr.Update(name, j); err != nil {
t.Errorf("metric update: err %v", err)
t.Errorf("metric update: err %v for name %q, json:\n%s", err, name, j)
}
}, nil)

Expand Down
2 changes: 1 addition & 1 deletion testdata/mcu.textpb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ update: <
>
>
val: <
json_ietf_val: "{\"memory\":{\"utilized\":\"908222464\"},\"temperature\":{\"instant\":34.7,\"interval\":\"900000000000\",\"max\":34.8,\"min\":34.3}}"
json_ietf_val: "{\"memory\":{\"utilized\":\"908222464\"},\"temperature\":{\"instant\":34.7,\"interval\":\"900000000000\",\"max\":\"34.8\",\"min\":\"34.3\"}}"
>
>
>
Expand Down

0 comments on commit a9c56df

Please sign in to comment.