mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #130708 from fuweid/reduce-spans-in-writer
*: reduce tracing events during streaming JSON objects
This commit is contained in:
commit
c28e7ffe24
@ -200,6 +200,10 @@ type deferredResponseWriter struct {
|
|||||||
hasWritten bool
|
hasWritten bool
|
||||||
hw http.ResponseWriter
|
hw http.ResponseWriter
|
||||||
w io.Writer
|
w io.Writer
|
||||||
|
// totalBytes is the number of bytes written to `w` and does not include buffered bytes
|
||||||
|
totalBytes int
|
||||||
|
// lastWriteErr holds the error result (if any) of the last write attempt to `w`
|
||||||
|
lastWriteErr error
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
@ -242,26 +246,11 @@ func (w *deferredResponseWriter) Write(p []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *deferredResponseWriter) unbufferedWrite(p []byte) (n int, err error) {
|
func (w *deferredResponseWriter) unbufferedWrite(p []byte) (n int, err error) {
|
||||||
ctx := w.ctx
|
|
||||||
span := tracing.SpanFromContext(ctx)
|
|
||||||
// This Step usually wraps in-memory object serialization.
|
|
||||||
span.AddEvent("About to start writing response", attribute.Int("size", len(p)))
|
|
||||||
|
|
||||||
firstWrite := !w.hasWritten
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
w.totalBytes += n
|
||||||
span.AddEvent("Write call failed",
|
w.lastWriteErr = err
|
||||||
attribute.String("writer", fmt.Sprintf("%T", w.w)),
|
|
||||||
attribute.Int("size", len(p)),
|
|
||||||
attribute.Bool("firstWrite", firstWrite),
|
|
||||||
attribute.String("err", err.Error()))
|
|
||||||
} else {
|
|
||||||
span.AddEvent("Write call succeeded",
|
|
||||||
attribute.String("writer", fmt.Sprintf("%T", w.w)),
|
|
||||||
attribute.Int("size", len(p)),
|
|
||||||
attribute.Bool("firstWrite", firstWrite))
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if w.hasWritten {
|
if w.hasWritten {
|
||||||
return w.w.Write(p)
|
return w.w.Write(p)
|
||||||
}
|
}
|
||||||
@ -282,12 +271,35 @@ func (w *deferredResponseWriter) unbufferedWrite(p []byte) (n int, err error) {
|
|||||||
w.w = hw
|
w.w = hw
|
||||||
}
|
}
|
||||||
|
|
||||||
|
span := tracing.SpanFromContext(w.ctx)
|
||||||
|
span.AddEvent("About to start writing response",
|
||||||
|
attribute.String("writer", fmt.Sprintf("%T", w.w)),
|
||||||
|
attribute.Int("size", len(p)),
|
||||||
|
)
|
||||||
|
|
||||||
header.Set("Content-Type", w.mediaType)
|
header.Set("Content-Type", w.mediaType)
|
||||||
hw.WriteHeader(w.statusCode)
|
hw.WriteHeader(w.statusCode)
|
||||||
return w.w.Write(p)
|
return w.w.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *deferredResponseWriter) Close() (err error) {
|
func (w *deferredResponseWriter) Close() (err error) {
|
||||||
|
defer func() {
|
||||||
|
if !w.hasWritten {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
span := tracing.SpanFromContext(w.ctx)
|
||||||
|
|
||||||
|
if w.lastWriteErr != nil {
|
||||||
|
span.AddEvent("Write call failed",
|
||||||
|
attribute.Int("size", w.totalBytes),
|
||||||
|
attribute.String("err", w.lastWriteErr.Error()))
|
||||||
|
} else {
|
||||||
|
span.AddEvent("Write call succeeded",
|
||||||
|
attribute.Int("size", w.totalBytes))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if !w.hasWritten {
|
if !w.hasWritten {
|
||||||
if !w.hasBuffered {
|
if !w.hasBuffered {
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user