Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace strings.Replace with strings.ReplaceAll #11079

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions plugins/inputs/aerospike/aerospike.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (a *Aerospike) parseNodeInfo(acc telegraf.Accumulator, stats map[string]str
if len(parts) < 2 {
continue
}
key := strings.Replace(parts[0], "-", "_", -1)
key := strings.ReplaceAll(parts[0], "-", "_")
nFields[key] = parseAerospikeValue(key, parts[1])
}
acc.AddFields("aerospike_node", nFields, nTags, time.Now())
Expand Down Expand Up @@ -244,7 +244,7 @@ func (a *Aerospike) parseNamespaceInfo(acc telegraf.Accumulator, stats map[strin
if len(parts) < 2 {
continue
}
key := strings.Replace(parts[0], "-", "_", -1)
key := strings.ReplaceAll(parts[0], "-", "_")
nFields[key] = parseAerospikeValue(key, parts[1])
}
acc.AddFields("aerospike_namespace", nFields, nTags, time.Now())
Expand Down Expand Up @@ -311,7 +311,7 @@ func (a *Aerospike) parseSetInfo(acc telegraf.Accumulator, stats map[string]stri
continue
}

key := strings.Replace(pieces[0], "-", "_", -1)
key := strings.ReplaceAll(pieces[0], "-", "_")
nFields[key] = parseAerospikeValue(key, pieces[1])
}
acc.AddFields("aerospike_set", nFields, nTags, time.Now())
Expand Down Expand Up @@ -403,7 +403,7 @@ func (a *Aerospike) parseHistogram(acc telegraf.Accumulator, stats map[string]st
}
}

acc.AddFields(fmt.Sprintf("aerospike_histogram_%v", strings.Replace(histogramType, "-", "_", -1)), nFields, nTags, time.Now())
acc.AddFields(fmt.Sprintf("aerospike_histogram_%v", strings.ReplaceAll(histogramType, "-", "_")), nFields, nTags, time.Now())
}

func splitNamespaceSet(namespaceSet string) (namespace string, set string) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/aliyuncms/aliyuncms.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,14 @@ func formatField(metricName string, statistic string) string {
}

func formatMeasurement(project string) string {
project = strings.Replace(project, "/", "_", -1)
project = strings.ReplaceAll(project, "/", "_")
project = snakeCase(project)
return fmt.Sprintf("aliyuncms_%s", project)
}

func snakeCase(s string) string {
s = internal.SnakeCase(s)
s = strings.Replace(s, "__", "_", -1)
s = strings.ReplaceAll(s, "__", "_")
return s
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/amd_rocm_smi/amd_rocm_smi.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func genTagsFields(gpus map[string]GPU, system map[string]sysInfo) []metric {
setTagIfUsed(tags, "gpu_id", payload.GpuID)
setTagIfUsed(tags, "gpu_unique_id", payload.GpuUniqueID)

setIfUsed("int", fields, "driver_version", strings.Replace(system["system"].DriverVersion, ".", "", -1))
setIfUsed("int", fields, "driver_version", strings.ReplaceAll(system["system"].DriverVersion, ".", ""))
setIfUsed("int", fields, "fan_speed", payload.GpuFanSpeedPercentage)
setIfUsed("int64", fields, "memory_total", payload.GpuVRAMTotalMemory)
setIfUsed("int64", fields, "memory_used", payload.GpuVRAMTotalUsedMemory)
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/apache/apache.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (n *Apache) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {
line := sc.Text()
if strings.Contains(line, ":") {
parts := strings.SplitN(line, ":", 2)
key, part := strings.Replace(parts[0], " ", "", -1), strings.TrimSpace(parts[1])
key, part := strings.ReplaceAll(parts[0], " ", ""), strings.TrimSpace(parts[1])

switch key {
case "Scoreboard":
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/burrow/burrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// remap uri to json file, eg: /v3/kafka -> ./testdata/v3_kafka.json
func getResponseJSON(requestURI string) ([]byte, int) {
uri := strings.TrimLeft(requestURI, "/")
mappedFile := strings.Replace(uri, "/", "_", -1)
mappedFile := strings.ReplaceAll(uri, "/", "_")
jsonFile := fmt.Sprintf("./testdata/%s.json", mappedFile)

code := 200
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/ceph/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ func (c *Ceph) execute(command string) (string, error) {

// Ceph doesn't sanitize its output, and may return invalid JSON. Patch this
// up for them, as having some inaccurate data is better than none.
output = strings.Replace(output, "-inf", "0", -1)
output = strings.Replace(output, "inf", "0", -1)
output = strings.ReplaceAll(output, "-inf", "0")
output = strings.ReplaceAll(output, "inf", "0")

return output, nil
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/chrony/chrony.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func processChronycOutput(out string) (map[string]interface{}, map[string]string
if len(stats) < 2 {
return nil, nil, fmt.Errorf("unexpected output from chronyc, expected ':' in %s", out)
}
name := strings.ToLower(strings.Replace(strings.TrimSpace(stats[0]), " ", "_", -1))
name := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(stats[0]), " ", "_"))
// ignore reference time
if strings.Contains(name, "ref_time") {
continue
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/cisco_telemetry_mdt/cisco_telemetry_mdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *CiscoTelemetryMDT) Start(acc telegraf.Accumulator) error {
// Fill extra tags
c.extraTags = make(map[string]map[string]struct{})
for _, tag := range c.EmbeddedTags {
dir := strings.Replace(path.Dir(tag), "-", "_", -1)
dir := strings.ReplaceAll(path.Dir(tag), "-", "_")
if _, hasKey := c.extraTags[dir]; !hasKey {
c.extraTags[dir] = make(map[string]struct{})
}
Expand Down Expand Up @@ -441,7 +441,7 @@ func decodeTag(field *telemetry.TelemetryField) string {

// Recursively parse tag fields
func (c *CiscoTelemetryMDT) parseKeyField(tags map[string]string, field *telemetry.TelemetryField, prefix string) {
localname := strings.Replace(field.Name, "-", "_", -1)
localname := strings.ReplaceAll(field.Name, "-", "_")
name := localname
if len(localname) == 0 {
name = prefix
Expand Down Expand Up @@ -529,7 +529,7 @@ func (c *CiscoTelemetryMDT) parseClassAttributeField(grouper *metric.SeriesGroup

func (c *CiscoTelemetryMDT) parseContentField(grouper *metric.SeriesGrouper, field *telemetry.TelemetryField, prefix string,
encodingPath string, tags map[string]string, timestamp time.Time) {
name := strings.Replace(field.Name, "-", "_", -1)
name := strings.ReplaceAll(field.Name, "-", "_")

if (name == "modTs" || name == "createTs") && decodeValue(field) == "never" {
return
Expand All @@ -540,7 +540,7 @@ func (c *CiscoTelemetryMDT) parseContentField(grouper *metric.SeriesGrouper, fie
name = prefix + "/" + name
}

extraTags := c.extraTags[strings.Replace(encodingPath, "-", "_", -1)+"/"+name]
extraTags := c.extraTags[strings.ReplaceAll(encodingPath, "-", "_")+"/"+name]

if value := decodeValue(field); value != nil {
// Do alias lookup, to shorten measurement names
Expand Down Expand Up @@ -571,7 +571,7 @@ func (c *CiscoTelemetryMDT) parseContentField(grouper *metric.SeriesGrouper, fie
if len(extraTags) > 0 {
for _, subfield := range field.Fields {
if _, isExtraTag := extraTags[subfield.Name]; isExtraTag {
tags[name+"/"+strings.Replace(subfield.Name, "-", "_", -1)] = decodeTag(subfield)
tags[name+"/"+strings.ReplaceAll(subfield.Name, "-", "_")] = decodeTag(subfield)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ func New() *CloudWatch {
}

func sanitizeMeasurement(namespace string) string {
namespace = strings.Replace(namespace, "/", "_", -1)
namespace = strings.ReplaceAll(namespace, "/", "_")
namespace = snakeCase(namespace)
return "cloudwatch_" + namespace
}

func snakeCase(s string) string {
s = internal.SnakeCase(s)
s = strings.Replace(s, " ", "_", -1)
s = strings.Replace(s, "__", "_", -1)
s = strings.ReplaceAll(s, " ", "_")
s = strings.ReplaceAll(s, "__", "_")
return s
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/dcos/dcos.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type point struct {
func (d *DCOS) createPoints(m *Metrics) []*point {
points := make(map[string]*point)
for _, dp := range m.Datapoints {
fieldKey := strings.Replace(dp.Name, ".", "_", -1)
fieldKey := strings.ReplaceAll(dp.Name, ".", "_")

tags := dp.Tags
if tags == nil {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (ds *DiskStats) Gather(acc telegraf.Accumulator) error {
mountOpts := MountOptions(partitions[i].Opts)
tags := map[string]string{
"path": du.Path,
"device": strings.Replace(partitions[i].Device, "/dev/", "", -1),
"device": strings.ReplaceAll(partitions[i].Device, "/dev/", ""),
"fstype": du.Fstype,
"mode": mountOpts.Mode(),
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (d *Docker) gatherInfo(acc telegraf.Accumulator) error {
)

for _, rawData := range info.DriverStatus {
name := strings.ToLower(strings.Replace(rawData[0], " ", "_", -1))
name := strings.ToLower(strings.ReplaceAll(rawData[0], " ", "_"))
if name == "pool_name" {
poolName = rawData[1]
continue
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/elasticsearch_query/aggregation_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (aggregation *esAggregation) buildAggregationQuery() error {
measurement: aggregation.MeasurementName,
function: aggregation.MetricFunction,
field: k,
name: strings.Replace(k, ".", "_", -1) + "_" + aggregation.MetricFunction,
name: strings.ReplaceAll(k, ".", "_") + "_" + aggregation.MetricFunction,
},
isParent: true,
aggregation: agg,
Expand Down Expand Up @@ -185,7 +185,7 @@ func (aggregation *esAggregation) buildAggregationQuery() error {
measurement: aggregation.MeasurementName,
function: "terms",
field: term,
name: strings.Replace(term, ".", "_", -1),
name: strings.ReplaceAll(term, ".", "_"),
},
isParent: true,
aggregation: agg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,9 @@ func setupIntegrationTest() error {
logline := nginxlog{
IPaddress: parts[0],
Timestamp: time.Now().UTC(),
Method: strings.Replace(parts[5], `"`, "", -1),
Method: strings.ReplaceAll(parts[5], `"`, ""),
URI: parts[6],
Httpversion: strings.Replace(parts[7], `"`, "", -1),
Httpversion: strings.ReplaceAll(parts[7], `"`, ""),
Response: parts[8],
Size: float64(size),
ResponseTime: float64(responseTime),
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (c *GNMI) handleTelemetryField(update *gnmiLib.Update, tags map[string]stri
jsondata = val.JsonVal
}

name := strings.Replace(gpath, "-", "_", -1)
name := strings.ReplaceAll(gpath, "-", "_")
fields := make(map[string]interface{})
if value != nil {
fields[name] = value
Expand Down Expand Up @@ -462,7 +462,7 @@ func (c *GNMI) handlePath(gnmiPath *gnmiLib.Path, tags map[string]string, prefix

if tags != nil {
for key, val := range elem.Key {
key = strings.Replace(key, "-", "_", -1)
key = strings.ReplaceAll(key, "-", "_")

// Use short-form of key if possible
if _, exists := tags[key]; exists {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/intel_rdt/intel_rdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (r *IntelRDT) readData(ctx context.Context, args []string, processesPIDsAss

if r.UseSudo {
// run pqos with `/bin/sh -c "sudo /path/to/pqos ..."`
args = []string{"-c", fmt.Sprintf("sudo %s %s", r.PqosPath, strings.Replace(strings.Join(args, " "), ";", "\\;", -1))}
args = []string{"-c", fmt.Sprintf("sudo %s %s", r.PqosPath, strings.ReplaceAll(strings.Join(args, " "), ";", "\\;"))}
cmd = exec.Command("/bin/sh", args...)
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/ipmi_sensor/ipmi_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func trim(s string) string {
func transform(s string) string {
s = trim(s)
s = strings.ToLower(s)
return strings.Replace(s, " ", "_", -1)
return strings.ReplaceAll(s, " ", "_")
}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/jolokia2/common/point_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (pb *pointBuilder) formatFieldName(attribute, path string) string {
}

if path != "" {
fieldName = fieldName + fieldSeparator + strings.Replace(path, "/", fieldSeparator, -1)
fieldName = fieldName + fieldSeparator + strings.ReplaceAll(path, "/", fieldSeparator)
}

return fieldName
Expand Down Expand Up @@ -200,7 +200,7 @@ func (pb *pointBuilder) applySubstitutions(mbean string, fieldMap map[string]int
substitution := properties[subKey]

for fieldName, fieldValue := range fieldMap {
newFieldName := strings.Replace(fieldName, symbol, substitution, -1)
newFieldName := strings.ReplaceAll(fieldName, symbol, substitution)
if fieldName != newFieldName {
fieldMap[newFieldName] = fieldValue
delete(fieldMap, fieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func spitTagsNPath(xmlpath string) (string, map[string]string) {
// we must emit multiple tags
for _, kv := range strings.Split(sub[2], " and ") {
key := tagKey + strings.TrimSpace(strings.Split(kv, "=")[0])
tagValue := strings.Replace(strings.Split(kv, "=")[1], "'", "", -1)
tagValue := strings.ReplaceAll(strings.Split(kv, "=")[1], "'", "")
tags[key] = tagValue
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1875,8 +1875,8 @@ func (m *Mysql) parseValueByDatabaseTypeName(value sql.RawBytes, databaseTypeNam
func findThreadState(rawCommand, rawState string) string {
var (
// replace '_' symbol with space
command = strings.Replace(strings.ToLower(rawCommand), "_", " ", -1)
state = strings.Replace(strings.ToLower(rawState), "_", " ", -1)
command = strings.ReplaceAll(strings.ToLower(rawCommand), "_", " ")
state = strings.ReplaceAll(strings.ToLower(rawState), "_", " ")
)
// if the state is already valid, then return it
if _, ok := generalThreadStates[state]; ok {
Expand Down Expand Up @@ -1909,7 +1909,7 @@ func findThreadState(rawCommand, rawState string) string {

// newNamespace can be used to make a namespace
func newNamespace(words ...string) string {
return strings.Replace(strings.Join(words, "_"), " ", "_", -1)
return strings.ReplaceAll(strings.Join(words, "_"), " ", "_")
}

func copyTags(in map[string]string) map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/nsd/nsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *NSD) Gather(acc telegraf.Accumulator) error {
}
}
} else {
field := strings.Replace(stat, ".", "_", -1)
field := strings.ReplaceAll(stat, ".", "_")
fields[field] = fieldValue
}
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/openldap/openldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func dnToMetric(dn string, o *Openldap) string {
var metricParts []string

dn = strings.Trim(dn, " ")
dn = strings.Replace(dn, " ", "_", -1)
dn = strings.Replace(dn, "cn=", "", -1)
dn = strings.ReplaceAll(dn, " ", "_")
dn = strings.ReplaceAll(dn, "cn=", "")
dn = strings.ToLower(dn)
metricParts = strings.Split(dn, ",")
for i, j := 0, len(metricParts)-1; i < j; i, j = i+1, j-1 {
Expand All @@ -181,12 +181,12 @@ func dnToMetric(dn string, o *Openldap) string {
}

metricName := strings.Trim(dn, " ")
metricName = strings.Replace(metricName, " ", "_", -1)
metricName = strings.ReplaceAll(metricName, " ", "_")
metricName = strings.ToLower(metricName)
metricName = strings.TrimPrefix(metricName, "cn=")
metricName = strings.Replace(metricName, strings.ToLower("cn=Monitor"), "", -1)
metricName = strings.Replace(metricName, "cn=", "_", -1)
return strings.Replace(metricName, ",", "", -1)
metricName = strings.ReplaceAll(metricName, strings.ToLower("cn=Monitor"), "")
metricName = strings.ReplaceAll(metricName, "cn=", "_")
return strings.ReplaceAll(metricName, ",", "")
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/opensmtpd/opensmtpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *Opensmtpd) Gather(acc telegraf.Accumulator) error {
continue
}

field := strings.Replace(stat, ".", "_", -1)
field := strings.ReplaceAll(stat, ".", "_")

fields[field], err = strconv.ParseFloat(value, 64)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/phpfpm/phpfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func importMetric(r io.Reader, acc telegraf.Accumulator, addr string) {
}
fields := make(map[string]interface{})
for k, v := range stats[pool] {
fields[strings.Replace(k, " ", "_", -1)] = v
fields[strings.ReplaceAll(k, " ", "_")] = v
}
acc.AddFields("phpfpm", fields, tags)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/redis_sentinel/redis_sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func prepareFieldValues(fields map[string]string, typeMap map[string]configField
preparedFields := make(map[string]interface{})

for key, val := range fields {
key = strings.Replace(key, "-", "_", -1)
key = strings.ReplaceAll(key, "-", "_")

valType, ok := typeMap[key]
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/sensors/sensors.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *Sensors) parse(acc telegraf.Accumulator) error {

// snake converts string to snake case
func snake(input string) string {
return strings.ToLower(strings.Replace(strings.TrimSpace(input), " ", "_", -1))
return strings.ToLower(strings.ReplaceAll(strings.TrimSpace(input), " ", "_"))
}

func init() {
Expand Down
Loading