http: emit indented JSON in the metrics stream endpoint
To remove the need to decode and re-encode in the CLI
This commit is contained in:
parent
b5a2cbf369
commit
cf2e25c6bb
|
@ -181,7 +181,7 @@ func (s *HTTPHandlers) AgentMetricsStream(resp http.ResponseWriter, req *http.Re
|
|||
|
||||
flusher, ok := resp.(http.Flusher)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Streaming not supported")
|
||||
return nil, fmt.Errorf("streaming not supported")
|
||||
}
|
||||
|
||||
resp.WriteHeader(http.StatusOK)
|
||||
|
@ -196,6 +196,7 @@ func (s *HTTPHandlers) AgentMetricsStream(resp http.ResponseWriter, req *http.Re
|
|||
encoder: json.NewEncoder(resp),
|
||||
flusher: flusher,
|
||||
}
|
||||
enc.encoder.SetIndent("", " ")
|
||||
s.agent.baseDeps.MetricsHandler.Stream(req.Context(), enc)
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package debug
|
|||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
@ -529,17 +530,10 @@ func (c *cmd) captureMetrics(ctx context.Context, timestampDir string) error {
|
|||
}
|
||||
defer fh.Close()
|
||||
|
||||
decoder := json.NewDecoder(stream)
|
||||
encoder := json.NewEncoder(fh)
|
||||
// TODO: is More() correct here?
|
||||
for decoder.More() {
|
||||
var raw interface{}
|
||||
if err := decoder.Decode(&raw); err != nil {
|
||||
return fmt.Errorf("failed to decode metrics: %w", err)
|
||||
}
|
||||
if err := encoder.Encode(raw); err != nil {
|
||||
return fmt.Errorf("failed to write metrics to file: %w", err)
|
||||
}
|
||||
b := bufio.NewReader(stream)
|
||||
_, err = b.WriteTo(fh)
|
||||
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
|
||||
return fmt.Errorf("failed to copy metrics to file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue