-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing bytes field to backend response log (#494)
* Add missing bytes field to backend response log * (docs) remove unknown utf8 chars and reformat tables * (docs) add response.bytes field to backend log documentation * Add content-length fallback * rm obsolete key * more checks in test * handle timeout cancel select case * fmt * Fix missing token request within custom log context * Fix possible timer deadlock golang/go#27169 * cancel while reading results * fire collected backend logStack at the end endpoint handler has to many exits; even with panic recovers * Add changelog entry Co-authored-by: Alex Schneider <alex.schneider@avenga.com>
- Loading branch information
Marcel Ludwig
and
Alex Schneider
authored
May 9, 2022
1 parent
27bce19
commit 502ef91
Showing
15 changed files
with
398 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"sync/atomic" | ||
|
||
"github.com/avenga/couper/config/request" | ||
) | ||
|
||
var _ io.ReadCloser = &BytesCountReader{} | ||
|
||
type BytesCountReader struct { | ||
c context.Context | ||
n int64 | ||
r io.ReadCloser | ||
} | ||
|
||
// NewBytesCountReader just counts the raw read bytes from given response body for logging purposes. | ||
func NewBytesCountReader(beresp *http.Response) io.ReadCloser { | ||
return &BytesCountReader{ | ||
c: beresp.Request.Context(), | ||
r: beresp.Body, | ||
} | ||
} | ||
|
||
func (b *BytesCountReader) Read(p []byte) (n int, err error) { | ||
n, err = b.r.Read(p) | ||
b.n += int64(n) | ||
return n, err | ||
} | ||
|
||
func (b *BytesCountReader) Close() error { | ||
bytesPtr, ok := b.c.Value(request.BackendBytes).(*int64) | ||
if ok { | ||
atomic.StoreInt64(bytesPtr, b.n) | ||
} | ||
return b.r.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type entry struct { | ||
logEntry *logrus.Entry | ||
} | ||
|
||
func (e *entry) Level(lvl logrus.Level) { | ||
e.logEntry.Level = lvl | ||
} | ||
|
||
type Level interface { | ||
Level(level logrus.Level) | ||
} | ||
|
||
type Stack struct { | ||
entries []*entry | ||
mu sync.Mutex | ||
} | ||
|
||
const logStack = "logStack" | ||
|
||
func NewStack(ctx context.Context) (context.Context, *Stack) { | ||
s := &Stack{} | ||
return context.WithValue(ctx, logStack, s), s | ||
} | ||
|
||
func (s *Stack) Push(e *logrus.Entry) Level { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
item := &entry{logEntry: e} | ||
s.entries = append(s.entries, item) | ||
return item | ||
} | ||
|
||
func (s *Stack) Fire() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
for _, item := range s.entries { | ||
item.logEntry.Log(item.logEntry.Level) | ||
} | ||
} | ||
|
||
func FromContext(ctx context.Context) (*Stack, bool) { | ||
s, exist := ctx.Value(logStack).(*Stack) | ||
return s, exist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.