-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logs via a browser #2235
Merged
Merged
Logs via a browser #2235
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,6 +255,9 @@ func (s *StreamFrame) IsCleared() bool { | |
|
||
// StreamFramer is used to buffer and send frames as well as heartbeat. | ||
type StreamFramer struct { | ||
// plainTxt determines whether we frame or just send plain text data. | ||
plainTxt bool | ||
|
||
out io.WriteCloser | ||
enc *codec.Encoder | ||
encLock sync.Mutex | ||
|
@@ -281,8 +284,11 @@ type StreamFramer struct { | |
} | ||
|
||
// NewStreamFramer creates a new stream framer that will output StreamFrames to | ||
// the passed output. | ||
func NewStreamFramer(out io.WriteCloser, heartbeatRate, batchWindow time.Duration, frameSize int) *StreamFramer { | ||
// the passed output. If plainTxt is set we do not frame and just batch plain | ||
// text data. | ||
func NewStreamFramer(out io.WriteCloser, plainTxt bool, | ||
heartbeatRate, batchWindow time.Duration, frameSize int) *StreamFramer { | ||
|
||
// Create a JSON encoder | ||
enc := codec.NewEncoder(out, jsonHandle) | ||
|
||
|
@@ -291,6 +297,7 @@ func NewStreamFramer(out io.WriteCloser, heartbeatRate, batchWindow time.Duratio | |
flusher := time.NewTicker(batchWindow) | ||
|
||
return &StreamFramer{ | ||
plainTxt: plainTxt, | ||
out: out, | ||
enc: enc, | ||
frameSize: frameSize, | ||
|
@@ -390,6 +397,10 @@ OUTER: | |
func (s *StreamFramer) send(f *StreamFrame) error { | ||
s.encLock.Lock() | ||
defer s.encLock.Unlock() | ||
if s.plainTxt { | ||
_, err := io.Copy(s.out, bytes.NewReader(f.Data)) | ||
return err | ||
} | ||
return s.enc.Encode(f) | ||
} | ||
|
||
|
@@ -549,7 +560,7 @@ func (s *HTTPServer) Stream(resp http.ResponseWriter, req *http.Request) (interf | |
output := ioutils.NewWriteFlusher(resp) | ||
|
||
// Create the framer | ||
framer := NewStreamFramer(output, streamHeartbeatRate, streamBatchWindow, streamFrameSize) | ||
framer := NewStreamFramer(output, false, streamHeartbeatRate, streamBatchWindow, streamFrameSize) | ||
framer.Run() | ||
defer framer.Destroy() | ||
|
||
|
@@ -697,7 +708,7 @@ OUTER: | |
// applied. Defaults to "start". | ||
func (s *HTTPServer) Logs(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
var allocID, task, logType string | ||
var follow bool | ||
var plain, follow bool | ||
var err error | ||
|
||
q := req.URL.Query() | ||
|
@@ -710,8 +721,22 @@ func (s *HTTPServer) Logs(resp http.ResponseWriter, req *http.Request) (interfac | |
return nil, taskNotPresentErr | ||
} | ||
|
||
if follow, err = strconv.ParseBool(q.Get("follow")); err != nil { | ||
return nil, fmt.Errorf("Failed to parse follow field to boolean: %v", err) | ||
followStr := q.Get("follow") | ||
if followStr != "" { | ||
if follow, err = strconv.ParseBool(followStr); err != nil { | ||
return nil, fmt.Errorf("Failed to parse follow field to boolean: %v", err) | ||
} | ||
} | ||
|
||
plainStr := q.Get("plain") | ||
if plainStr != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
if plain, err = strconv.ParseBool(plainStr); err != nil { | ||
return nil, fmt.Errorf("Failed to parse plain field to boolean: %v", err) | ||
} | ||
|
||
if plain { | ||
follow = false | ||
} | ||
} | ||
|
||
logType = q.Get("type") | ||
|
@@ -747,15 +772,15 @@ func (s *HTTPServer) Logs(resp http.ResponseWriter, req *http.Request) (interfac | |
// Create an output that gets flushed on every write | ||
output := ioutils.NewWriteFlusher(resp) | ||
|
||
return nil, s.logs(follow, offset, origin, task, logType, fs, output) | ||
return nil, s.logs(follow, plain, offset, origin, task, logType, fs, output) | ||
} | ||
|
||
func (s *HTTPServer) logs(follow bool, offset int64, | ||
func (s *HTTPServer) logs(follow, plain bool, offset int64, | ||
origin, task, logType string, | ||
fs allocdir.AllocDirFS, output io.WriteCloser) error { | ||
|
||
// Create the framer | ||
framer := NewStreamFramer(output, streamHeartbeatRate, streamBatchWindow, streamFrameSize) | ||
framer := NewStreamFramer(output, plain, streamHeartbeatRate, streamBatchWindow, streamFrameSize) | ||
framer.Run() | ||
defer framer.Destroy() | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline the above statement since you are not using it outside the block.