From d720bc48d3a74a83c2050291536f2b3c12086803 Mon Sep 17 00:00:00 2001 From: hc-github-team-nomad-core <82989552+hc-github-team-nomad-core@users.noreply.github.com> Date: Mon, 16 Jan 2023 05:15:52 -0500 Subject: [PATCH] Return buffered text from log endpoint if decoding fails (#15558) (#15796) To see why I think this is a good change lets look at why I am making it My disk was full, which means GC was happening agressively. So by the time I called the logging endpoint from the SDK, the logs were GC'd The error I was getting before was: ``` invalid character 'i' in literal false (expecting 'l') ``` Now the error I get is: ``` failed to decode log endpoint response as JSON: "failed to list entries: open /tmp/nomad.data.4219353875/alloc/f11fee50-2b66-a7a2-d3ec-8442cb3d557a/alloc/logs: no such file or directory" ``` Still not super descriptive but much more debugable Co-authored-by: Benjamin Buzbee --- .changelog/15558.txt | 3 +++ api/fs.go | 10 +++++++++- api/go.mod | 4 +++- api/go.sum | 8 ++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changelog/15558.txt diff --git a/.changelog/15558.txt b/.changelog/15558.txt new file mode 100644 index 000000000000..eb6ae7dad082 --- /dev/null +++ b/.changelog/15558.txt @@ -0,0 +1,3 @@ +```release-note:improvement +api: improved error returned from AllocFS.Logs when response is not JSON +``` diff --git a/api/fs.go b/api/fs.go index 8b63a388c9d5..91baca3aa40e 100644 --- a/api/fs.go +++ b/api/fs.go @@ -4,10 +4,13 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "net" "strconv" "sync" "time" + + "github.com/hashicorp/go-multierror" ) const ( @@ -288,7 +291,12 @@ func (a *AllocFS) Logs(alloc *Allocation, follow bool, task, logType, origin str if err == io.EOF || err == io.ErrClosedPipe { close(frames) } else { - errCh <- err + buf, err2 := ioutil.ReadAll(dec.Buffered()) + if err2 != nil { + errCh <- fmt.Errorf("failed to decode and failed to read buffered data: %w", multierror.Append(err, err2)) + } else { + errCh <- fmt.Errorf("failed to decode log endpoint response as JSON: %q", buf) + } } return } diff --git a/api/go.mod b/api/go.mod index f86693d37ca1..fe9aa230f0d0 100644 --- a/api/go.mod +++ b/api/go.mod @@ -7,6 +7,7 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/hashicorp/cronexpr v1.1.1 github.com/hashicorp/go-cleanhttp v0.5.2 + github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-rootcerts v1.0.2 github.com/mitchellh/go-testing-interface v1.14.1 github.com/mitchellh/mapstructure v1.5.0 @@ -14,7 +15,8 @@ require ( ) require ( - github.com/google/go-cmp v0.5.8 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/hashicorp/errwrap v1.0.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/stretchr/testify v1.8.1 // indirect ) diff --git a/api/go.sum b/api/go.sum index ecf26e26c4e0..394a8107a5de 100644 --- a/api/go.sum +++ b/api/go.sum @@ -3,14 +3,18 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/cronexpr v1.1.1 h1:NJZDd87hGXjoZBdvyCF9mX4DCq5Wy7+A/w+A7q0wn6c= github.com/hashicorp/cronexpr v1.1.1/go.mod h1:P4wA0KBl9C5q2hABiMO7cp6jcIg96CDh1Efb3g1PWA4= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc= github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=