Skip to content

Commit

Permalink
refactor: consistently use one uuid library (#40161)
Browse files Browse the repository at this point in the history
* refactor: consistently use one uuid library

the codebase is using a mix of github.com/google/uuid and github.com/gofrs/uuid
Update the codebase to use github.com/gofrs/uuid consistently and bump to the
current major version.

* lint: fix linting issues

* refactor: move to gofrs/uuid

* lint: add gomodguard setting

* lint: fix more linter issues

* lint: more linting errors

* lint: completely remove gofrs/uuid v4

* lint: more linting errors

* lint: more linting errors

* lint: fix more linting errors

* fix: replace uuid.MustParse with uuid.Must(uuid.FromString())

* refactor: update uuid.Parse to uuid.FromString

* lint: fix linting errors

* lint: fix linting errors

* docs: fix nolint comments

* lint: fix linting errors

* docs: update nolint comments

* test: update uuid parsing err

* lint: fix linter issues

* lint: fix linter issues

* lint: run go mod tidy

* lint: regenerate notice

* build: regenerate notice

* build: regenerate notice

* refactor: cleanup double uuid conversion

* test: gracefully log the error
  • Loading branch information
kruskall authored Sep 9, 2024
1 parent 9008e8d commit cd51941
Show file tree
Hide file tree
Showing 88 changed files with 340 additions and 349 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ linters-settings:
- errors
- fmt
reason: "This package is deprecated, use `fmt.Errorf` with `%w` instead"
- github.com/google/uuid:
# Recommended modules that should be used instead. (Optional)
recommendations:
- github.com/gofrs/uuid/v5
reason: "Use one uuid library consistently across the codebase"

gosimple:
# Select the Go version to target. The default is '1.13'.
Expand Down
110 changes: 40 additions & 70 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17497,12 +17497,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/gofrs/uuid
Version: v4.4.0+incompatible
Dependency : github.com/gofrs/uuid/v5
Version: v5.2.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/gofrs/uuid@v4.4.0+incompatible/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/gofrs/uuid/v5@v5.2.0/LICENSE:

Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>

Expand Down Expand Up @@ -18556,43 +18556,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/google/uuid
Version: v1.6.0
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/google/uuid@v1.6.0/LICENSE:

Copyright (c) 2009,2014 Google Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/googleapis/gax-go/v2
Version: v2.13.0
Expand Down Expand Up @@ -40751,36 +40714,6 @@ Contents of probable licence file $GOMODCACHE/github.com/godror/knownpb@v0.1.0/L
limitations under the License.


--------------------------------------------------------------------------------
Dependency : github.com/gofrs/uuid/v5
Version: v5.2.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/gofrs/uuid/v5@v5.2.0/LICENSE:

Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/golang-jwt/jwt/v4
Version: v4.5.0
Expand Down Expand Up @@ -42984,6 +42917,43 @@ Contents of probable licence file $GOMODCACHE/github.com/google/shlex@v0.0.0-201
limitations under the License.


--------------------------------------------------------------------------------
Dependency : github.com/google/uuid
Version: v1.6.0
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/google/uuid@v1.6.0/LICENSE:

Copyright (c) 2009,2014 Google Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/googleapis/enterprise-certificate-proxy
Version: v0.3.2
Expand Down
2 changes: 1 addition & 1 deletion filebeat/fileset/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"sync"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"
"github.com/mitchellh/hashstructure"

"github.com/elastic/beats/v7/libbeat/beat"
Expand Down
2 changes: 1 addition & 1 deletion filebeat/harvester/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package harvester

import (
"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"
)

// Harvester contains all methods which must be supported by each harvester
Expand Down
2 changes: 1 addition & 1 deletion filebeat/harvester/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"sync"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"

"github.com/elastic/elastic-agent-libs/logp"
)
Expand Down
33 changes: 16 additions & 17 deletions filebeat/input/log/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"sync"
"time"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"
"golang.org/x/text/transform"

"github.com/elastic/beats/v7/libbeat/beat"
Expand Down Expand Up @@ -191,15 +191,15 @@ func (h *Harvester) ID() uuid.UUID {
func (h *Harvester) Setup() error {
err := h.open()
if err != nil {
return fmt.Errorf("Harvester setup failed. Unexpected file opening error: %s", err)
return fmt.Errorf("Harvester setup failed. Unexpected file opening error: %w", err)
}

h.reader, err = h.newLogFileReader()
if err != nil {
if h.source != nil {
h.source.Close()
}
return fmt.Errorf("Harvester setup failed. Unexpected encoding line reader error: %s", err)
return fmt.Errorf("Harvester setup failed. Unexpected encoding line reader error: %w", err)
}

h.metrics = newHarvesterProgressMetrics(h.id.String())
Expand Down Expand Up @@ -325,20 +325,20 @@ func (h *Harvester) Run() error {

message, err := h.reader.Next()
if err != nil {
switch err {
case ErrFileTruncate:
switch {
case errors.Is(err, ErrFileTruncate):
logger.Info("File was truncated. Begin reading file from offset 0.")
h.state.Offset = 0
filesTruncated.Add(1)
case ErrRemoved:
case errors.Is(err, ErrRemoved):
logger.Info("File was removed. Closing because close_removed is enabled.")
case ErrRenamed:
case errors.Is(err, ErrRenamed):
logger.Info("File was renamed. Closing because close_renamed is enabled.")
case ErrClosed:
case errors.Is(err, ErrClosed):
logger.Info("Reader was closed. Closing.")
case io.EOF:
case errors.Is(err, io.EOF):
logger.Info("End of file reached. Closing because close_eof is enabled.")
case ErrInactive:
case errors.Is(err, ErrInactive):
logger.Infof("File is inactive. Closing because close_inactive of %v reached.", h.config.CloseInactive)
default:
logger.Errorf("Read line error: %v", err)
Expand Down Expand Up @@ -519,15 +519,15 @@ func (h *Harvester) shouldExportLine(line string) bool {
func (h *Harvester) openFile() error {
fi, err := os.Stat(h.state.Source)
if err != nil {
return fmt.Errorf("failed to stat source file %s: %v", h.state.Source, err)
return fmt.Errorf("failed to stat source file %s: %w", h.state.Source, err)
}
if fi.Mode()&os.ModeNamedPipe != 0 {
return fmt.Errorf("failed to open file %s, named pipes are not supported", h.state.Source)
}

f, err := file_helper.ReadOpen(h.state.Source)
if err != nil {
return fmt.Errorf("Failed opening %s: %s", h.state.Source, err)
return fmt.Errorf("Failed opening %s: %w", h.state.Source, err)
}

harvesterOpenFiles.Add(1)
Expand All @@ -549,7 +549,7 @@ func (h *Harvester) validateFile(f *os.File) error {

info, err := f.Stat()
if err != nil {
return fmt.Errorf("Failed getting stats for file %s: %s", h.state.Source, err)
return fmt.Errorf("Failed getting stats for file %s: %w", h.state.Source, err)
}

if !info.Mode().IsRegular() {
Expand All @@ -563,8 +563,7 @@ func (h *Harvester) validateFile(f *os.File) error {

h.encoding, err = h.encodingFactory(f)
if err != nil {

if err == transform.ErrShortSrc {
if errors.Is(err, transform.ErrShortSrc) {
logger.Infof("Initialising encoding for '%v' failed due to file being too short", f)
} else {
logger.Errorf("Initialising encoding for '%v' failed: %v", f, err)
Expand All @@ -588,12 +587,12 @@ func (h *Harvester) initFileOffset(file *os.File) (int64, error) {
// continue from last known offset
if h.state.Offset > 0 {
h.logger.Debugf("Set previous offset: %d ", h.state.Offset)
return file.Seek(h.state.Offset, os.SEEK_SET)
return file.Seek(h.state.Offset, io.SeekStart)
}

// get offset from file in case of encoding factory was required to read some data.
h.logger.Debug("Setting offset to: 0")
return file.Seek(0, os.SEEK_CUR)
return file.Seek(0, io.SeekCurrent)
}

// getState returns an updated copy of the harvester state
Expand Down
2 changes: 1 addition & 1 deletion filebeat/input/log/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"sync"
"time"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"

"github.com/elastic/beats/v7/filebeat/channel"
"github.com/elastic/beats/v7/filebeat/harvester"
Expand Down
26 changes: 19 additions & 7 deletions filebeat/input/redis/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
"time"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"
rd "github.com/gomodule/redigo/redis"

"github.com/elastic/beats/v7/libbeat/beat"
Expand Down Expand Up @@ -82,22 +82,26 @@ func (h *Harvester) Run() error {
default:
}
// Writes Slowlog get and slowlog reset both to the buffer so they are executed together
h.conn.Send("SLOWLOG", "GET")
h.conn.Send("SLOWLOG", "RESET")
if err := h.conn.Send("SLOWLOG", "GET"); err != nil {
return fmt.Errorf("error sending slowlog get: %w", err)
}
if err := h.conn.Send("SLOWLOG", "RESET"); err != nil {
return fmt.Errorf("error sending slowlog reset: %w", err)
}

// Flush the buffer to execute both commands and receive the reply from SLOWLOG GET
h.conn.Flush()

// Receives first reply from redis which is the one from GET
logs, err := rd.Values(h.conn.Receive())
if err != nil {
return fmt.Errorf("error receiving slowlog data: %s", err)
return fmt.Errorf("error receiving slowlog data: %w", err)
}

// Read reply from RESET
_, err = h.conn.Receive()
if err != nil {
return fmt.Errorf("error receiving reset data: %s", err)
return fmt.Errorf("error receiving reset data: %w", err)
}

for _, item := range logs {
Expand All @@ -115,7 +119,11 @@ func (h *Harvester) Run() error {

var log log
var args []string
rd.Scan(entry, &log.id, &log.timestamp, &log.duration, &args)
_, err = rd.Scan(entry, &log.id, &log.timestamp, &log.duration, &args)
if err != nil {
logp.Err("Error scanning slowlog entry: %s", err)
continue
}

// This splits up the args into cmd, key, args.
argsLen := len(args)
Expand Down Expand Up @@ -144,7 +152,7 @@ func (h *Harvester) Run() error {
slowlogEntry["args"] = log.args
}

h.forwarder.Send(beat.Event{
err = h.forwarder.Send(beat.Event{
Timestamp: time.Unix(log.timestamp, 0).UTC(),
Fields: mapstr.M{
"message": strings.Join(args, " "),
Expand All @@ -156,6 +164,10 @@ func (h *Harvester) Run() error {
},
},
})
if err != nil {
logp.Err("Error sending beat event: %s", err)
continue
}
}
return nil
}
Expand Down
Loading

0 comments on commit cd51941

Please sign in to comment.