Skip to content

Commit

Permalink
x-pack/filebeat/input/websocket: fix logging
Browse files Browse the repository at this point in the history
The body was previously not being logged since an io.ReadCloser is not JSON
serialisable type.
  • Loading branch information
efd6 committed Jul 9, 2024
1 parent 88d646a commit 65fdf7b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion x-pack/filebeat/input/websocket/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
package websocket

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"reflect"
"time"

"github.com/google/cel-go/cel"
"github.com/gorilla/websocket"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/types/known/structpb"

v2 "github.com/elastic/beats/v7/filebeat/input/v2"
Expand Down Expand Up @@ -109,7 +112,15 @@ func (i input) run(env v2.Context, src *source, cursor map[string]interface{}, p
headers := formHeader(cfg)
c, resp, err := websocket.DefaultDialer.DialContext(ctx, url, headers)
if resp != nil && resp.Body != nil {
log.Debugw("websocket connection response", "body", resp.Body)
var buf bytes.Buffer
if log.Core().Enabled(zapcore.DebugLevel) {
const limit = 1e4
io.CopyN(&buf, resp.Body, limit)

Check failure on line 118 in x-pack/filebeat/input/websocket/input.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value of `io.CopyN` is not checked (errcheck)
}
if n, _ := io.Copy(io.Discard, resp.Body); n != 0 && buf.Len() != 0 {
buf.WriteString("... truncated")
}
log.Debugw("websocket connection response", "body", &buf)
resp.Body.Close()
}
if err != nil {
Expand Down

0 comments on commit 65fdf7b

Please sign in to comment.