Skip to content

Commit

Permalink
elevate the fix for 'None' string literals into a general rule
Browse files Browse the repository at this point in the history
This is very safe to do in the general case since ""None"" is very
obviously a JSON syntax error, and having it here next to where the
problem is caused is clearer than having a special override in a
completely different place.
  • Loading branch information
majewsky committed Oct 24, 2023
1 parent e5c142d commit c1c4d1e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
9 changes: 1 addition & 8 deletions internal/collector/recon/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package recon

import (
"bytes"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -527,12 +526,6 @@ func (t *ShardingTask) UpdateMetrics() (map[string]int, error) {
}

for hostname, dataBytes := range outputPerHost {
if !json.Valid(dataBytes) {
//Replace instances of breaking null string errors with a 0.
//Workaround for unmarshalling, and consistent with eventual metric export value. (0: no error/1: error)
dataBytes = bytes.ReplaceAll(dataBytes, []byte(`"None"`), []byte(`0`))
}

var data struct {
ShardingStats ShardingStats `json:"sharding_stats"`
}
Expand Down Expand Up @@ -593,7 +586,7 @@ func (t *ShardingTask) UpdateMetrics() (map[string]int, error) {
t.containerShardingInProgressActive.With(l).Set(float64(shardingProcess.Active))
t.containerShardingInProgressCleaved.With(l).Set(float64(shardingProcess.Cleaved))
t.containerShardingInProgressCreated.With(l).Set(float64(shardingProcess.Created))
if shardingProcess.Error != `0` {
if shardingProcess.Error != "None" {
t.containerShardingInProgressError.With(l).Set(float64(1))
} else {
t.containerShardingInProgressError.With(l).Set(float64(0))
Expand Down
5 changes: 4 additions & 1 deletion internal/collector/recon/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ func splitOutputPerHost(output []byte, cmdArgs []string) (map[string][]byte, err

logg.Debug("output from command 'swift-recon %s': %s: %s", util.CmdArgsToStr(cmdArgs), hostname, string(data))

// sanitize JSON
// convert Python literals to JSON (best effort)
data = bytes.ReplaceAll(data, []byte(`u'`), []byte(`'`))
data = bytes.ReplaceAll(data, []byte(`'`), []byte(`"`))
data = bytes.ReplaceAll(data, []byte(`True`), []byte(`true`))
data = bytes.ReplaceAll(data, []byte(`False`), []byte(`false`))
data = bytes.ReplaceAll(data, []byte(`None`), []byte(`"None"`))
data = bytes.ReplaceAll(data, []byte(`""None""`), []byte(`"None"`))
//^ We sometimes observe strings with the value "None".
//The None -> "None" replacement introduces double quoting there which we need to compensate for.

//In the event that object store issues add escape characters to swift-recon output, strip these chracters to continue.
data = []byte(stripEscapeChars(string(data)))
Expand Down

0 comments on commit c1c4d1e

Please sign in to comment.