Skip to content

Commit 8433d1a

Browse files
authored
Merge pull request #659 from jmpsec/bytes-newreader-refactor
Refactor `strings.NewReader` into bytes to avoid allocations
2 parents 5f3d72b + aeb5f8d commit 8433d1a

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

pkg/logging/db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func CreateLoggerDBFile(dbfile string) (*LoggerDB, error) {
6161
// Initialize backend
6262
backend, err := backend.CreateDBManagerFile(dbfile)
6363
if err != nil {
64-
return nil, fmt.Errorf("Failed to create backend - %w", err)
64+
return nil, fmt.Errorf("failed to create backend - %w", err)
6565
}
6666
return CreateLoggerDB(backend)
6767
}
@@ -71,7 +71,7 @@ func CreateLoggerDBConfig(dbConfig backend.JSONConfigurationDB) (*LoggerDB, erro
7171
// Initialize backend
7272
backend, err := backend.CreateDBManager(dbConfig)
7373
if err != nil {
74-
return nil, fmt.Errorf("Failed to create backend - %w", err)
74+
return nil, fmt.Errorf("failed to create backend - %w", err)
7575
}
7676
return CreateLoggerDB(backend)
7777
}

pkg/logging/elastic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logging
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -126,7 +127,7 @@ func (logE *LoggerElastic) Send(logType string, data []byte, environment, uuid s
126127
}
127128
req := esapi.IndexRequest{
128129
Index: logE.IndexName(),
129-
Body: strings.NewReader(string(jsonEvent)),
130+
Body: bytes.NewReader(jsonEvent),
130131
Refresh: "true",
131132
}
132133
res, err := req.Do(context.Background(), logE.Client)

pkg/logging/graylog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package logging
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
6-
"strings"
77
"time"
88

99
"github.com/jmpsec/osctrl/pkg/config"
@@ -136,7 +136,7 @@ func (logGL *LoggerGraylog) Send(logType string, data []byte, environment, uuid
136136
if err != nil {
137137
log.Err(err).Msg("error marshaling data")
138138
}
139-
jsonParam := strings.NewReader(string(jsonMessage))
139+
jsonParam := bytes.NewReader(jsonMessage)
140140
if debug {
141141
log.Debug().Msgf("Sending %d bytes to Graylog for %s - %s", len(data), environment, uuid)
142142
}

pkg/logging/logstash.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package logging
22

33
import (
4+
"bytes"
45
"fmt"
56
"net"
6-
"strings"
77

88
"github.com/jmpsec/osctrl/pkg/config"
99
"github.com/jmpsec/osctrl/pkg/settings"
@@ -100,7 +100,7 @@ func (logLS *LoggerLogstash) SendHTTP(logType string, data []byte, environment,
100100
if debug {
101101
log.Debug().Msgf("Send %s via Logstash HTTP", logType)
102102
}
103-
jsonData := strings.NewReader(string(data))
103+
jsonData := bytes.NewReader(data)
104104
if debug {
105105
log.Debug().Msgf("Sending %d bytes to Logstash HTTP for %s - %s", len(data), environment, uuid)
106106
}

pkg/logging/splunk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package logging
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
6-
"strings"
77
"time"
88

99
"github.com/jmpsec/osctrl/pkg/config"
@@ -134,7 +134,7 @@ func (logSP *LoggerSplunk) Send(logType string, data []byte, environment, uuid s
134134
if err != nil {
135135
log.Err(err).Msgf("Error parsing data")
136136
}
137-
jsonParam := strings.NewReader(string(jsonEvents))
137+
jsonParam := bytes.NewReader(jsonEvents)
138138
if debug {
139139
log.Debug().Msgf("Sending %d bytes to Splunk for %s - %s", len(data), environment, uuid)
140140
}

0 commit comments

Comments
 (0)