Add nomad monitor export command (#26178)

* Add MonitorExport command and handlers
* Implement autocomplete
* Require nomad in serviceName
* Fix race in StreamReader.Read
* Add and use framer.Flush() to coordinate function exit
* Add LogFile to client/Server config and read NomadLogPath in rpcHandler instead of HTTPServer
* Parameterize StreamFixed stream size
This commit is contained in:
tehut
2025-08-01 10:26:59 -07:00
committed by GitHub
parent 6f81222ec8
commit d709accaf5
31 changed files with 2354 additions and 180 deletions

View File

@@ -302,8 +302,20 @@ func (a *Agent) Host(serverID, nodeID string, q *QueryOptions) (*HostDataRespons
// Monitor returns a channel which will receive streaming logs from the agent
// Providing a non-nil stopCh can be used to close the connection and stop log streaming
func (a *Agent) Monitor(stopCh <-chan struct{}, q *QueryOptions) (<-chan *StreamFrame, <-chan error) {
frames, errCh := a.monitorHelper(stopCh, q, "/v1/agent/monitor")
return frames, errCh
}
// MonitorExport returns a channel which will receive streaming logs from the agent
// Providing a non-nil stopCh can be used to close the connection and stop log streaming
func (a *Agent) MonitorExport(stopCh <-chan struct{}, q *QueryOptions) (<-chan *StreamFrame, <-chan error) {
frames, errCh := a.monitorHelper(stopCh, q, "/v1/agent/monitor/export")
return frames, errCh
}
func (a *Agent) monitorHelper(stopCh <-chan struct{}, q *QueryOptions, path string) (chan *StreamFrame, chan error) {
errCh := make(chan error, 1)
r, err := a.client.newRequest("GET", "/v1/agent/monitor")
r, err := a.client.newRequest("GET", path)
if err != nil {
errCh <- err
return nil, errCh

View File

@@ -389,12 +389,23 @@ func (f *FrameReader) Read(p []byte) (n int, err error) {
case <-unblock:
return 0, nil
case err := <-f.errCh:
return 0, err
// check for race with f.frames before returning error
select {
case frame, ok := <-f.frames:
if !ok {
return 0, io.EOF
}
f.frame = frame
// Store the total offset into the file
f.byteOffset = int(f.frame.Offset)
default:
return 0, err
}
case <-f.cancelCh:
return 0, io.EOF
}
}
// Copy the data out of the frame and update our offset
n = copy(p, f.frame.Data[f.frameOffset:])
f.frameOffset += n