Skip to content

Refactor strings.NewReader into bytes to avoid allocations #659

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

Merged
merged 1 commit into from
Jun 1, 2025
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
4 changes: 2 additions & 2 deletions pkg/logging/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func CreateLoggerDBFile(dbfile string) (*LoggerDB, error) {
// Initialize backend
backend, err := backend.CreateDBManagerFile(dbfile)
if err != nil {
return nil, fmt.Errorf("Failed to create backend - %w", err)
return nil, fmt.Errorf("failed to create backend - %w", err)
}
return CreateLoggerDB(backend)
}
Expand All @@ -71,7 +71,7 @@ func CreateLoggerDBConfig(dbConfig backend.JSONConfigurationDB) (*LoggerDB, erro
// Initialize backend
backend, err := backend.CreateDBManager(dbConfig)
if err != nil {
return nil, fmt.Errorf("Failed to create backend - %w", err)
return nil, fmt.Errorf("failed to create backend - %w", err)
}
return CreateLoggerDB(backend)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/logging/elastic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logging

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (logE *LoggerElastic) Send(logType string, data []byte, environment, uuid s
}
req := esapi.IndexRequest{
Index: logE.IndexName(),
Body: strings.NewReader(string(jsonEvent)),
Body: bytes.NewReader(jsonEvent),
Refresh: "true",
}
res, err := req.Do(context.Background(), logE.Client)
Expand Down
4 changes: 2 additions & 2 deletions pkg/logging/graylog.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package logging

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

"github.com/jmpsec/osctrl/pkg/config"
Expand Down Expand Up @@ -136,7 +136,7 @@ func (logGL *LoggerGraylog) Send(logType string, data []byte, environment, uuid
if err != nil {
log.Err(err).Msg("error marshaling data")
}
jsonParam := strings.NewReader(string(jsonMessage))
jsonParam := bytes.NewReader(jsonMessage)
if debug {
log.Debug().Msgf("Sending %d bytes to Graylog for %s - %s", len(data), environment, uuid)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/logging/logstash.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package logging

import (
"bytes"
"fmt"
"net"
"strings"

"github.com/jmpsec/osctrl/pkg/config"
"github.com/jmpsec/osctrl/pkg/settings"
Expand Down Expand Up @@ -100,7 +100,7 @@ func (logLS *LoggerLogstash) SendHTTP(logType string, data []byte, environment,
if debug {
log.Debug().Msgf("Send %s via Logstash HTTP", logType)
}
jsonData := strings.NewReader(string(data))
jsonData := bytes.NewReader(data)
if debug {
log.Debug().Msgf("Sending %d bytes to Logstash HTTP for %s - %s", len(data), environment, uuid)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/logging/splunk.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package logging

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

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