Skip to content
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

WIP: Add diagnostic session service #1578

Merged
merged 1 commit into from
Oct 10, 2017
Merged

WIP: Add diagnostic session service #1578

merged 1 commit into from
Oct 10, 2017

Conversation

desa
Copy link
Contributor

@desa desa commented Sep 20, 2017

No description provided.

@desa desa force-pushed the md-log-api branch 3 times, most recently from 7fc5a7f to 013f733 Compare September 21, 2017 18:16
@ghost ghost assigned desa Sep 26, 2017
@ghost ghost added the in progress label Sep 26, 2017
@desa
Copy link
Contributor Author

desa commented Sep 28, 2017

@nathanielc I still need to add a few tests, but for the time it should be reviewable at this point.

Copy link
Contributor

@nathanielc nathanielc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

I have made a few comments on how to deal with the fact that this streams forever.

@@ -659,6 +660,39 @@ func (c *Client) Do(req *http.Request, result interface{}, codes ...int) (*http.
return resp, nil
}

func (c *Client) Logs(w io.Writer, q map[string]string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really isn't too useful since it never returns, unless there is an error.

We should add a context.Context argument so users can cancel the request if they like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

if len(args) != 1 {
return errors.New("must provide task ID.")
}
err := cli.Logs(os.Stdout, map[string]string{"task": args[0]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we setup a signal handler here to cancel the context and cleanly as possible exit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. fixed.

@@ -2246,3 +2258,48 @@ func doBackup(args []string) error {
}
return nil
}

func watchUsage() {
var u = `Usage: kapacitor watch <output file>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think writing to Stdout is fine, no need for an output file. Also allowing optional list of key value pairs after the task id would be nice. That way you could filter down by node or whatever within a task easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo :) fixed.

}
m[pair[0]] = pair[1]
}
err := cli.Logs(os.Stdout, m)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here I think we want to catch signals and cancel the context.

server/server.go Outdated
@@ -421,6 +423,14 @@ func (s *Server) appendTaskStoreService() {
s.AppendService("task_store", srv)
}

func (s *Server) appendSessionService() {
// TODO: add diagnostic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The irony :)

Method: "GET",
Pattern: sessionsPath,
HandlerFunc: s.handleSessions,
NoGzip: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here explaining why these flag are set would be helpful, since it took us quite a while to figure out why they are needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

}

func (s *SessionService) Close() error {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In close you should remove the routes from the HTTPD service.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

defer kv.mu.Unlock()

flusher, ok := w.(http.Flusher)
var wf WriteFlusher = &noopWriteFlusher{w: w}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could put this creation of the noopWriteFlusher in the else block. It would save an allocation :)

flusher, ok := w.(http.Flusher)
var wf WriteFlusher = &noopWriteFlusher{w: w}
if ok {
wf = &httpWriteFlusher{w: w, f: flusher}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need the type httpWriteFlusher, as w is already a writer and you have asserted that its a flusher, so you can now just type assert it to be a WriteFlusher interface.

wf = w.(WriteFlusher)

Actually you can skip the direct flusher check and just check it its a WriteFlusher to begin with.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


Examples:

$ kapacitor logs mytask
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples are wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

done = true
}()

if err := cli.Logs(ctx, os.Stdout, m); err != nil && !done {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a race on the done variable.

}

func doLogs(args []string) error {
m := map[string]string{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a helper function that both doLogs and doWatch use so that this logic is not duplicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

done = true
}()

if err := cli.Logs(ctx, os.Stdout, m); err != nil && !done {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again same race on done.

@desa
Copy link
Contributor Author

desa commented Sep 29, 2017

@nathanielc made the changes you suggested.

@desa
Copy link
Contributor Author

desa commented Oct 2, 2017

@nathanielc made changes you suggested

@desa
Copy link
Contributor Author

desa commented Oct 5, 2017

@nathanielc anything else here that needs to be done?

ctx, cancel := context.WithCancel(context.Background())
done := false
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@desa desa force-pushed the md-log-api branch 2 times, most recently from 27a6512 to a71dc4f Compare October 6, 2017 15:40
@desa
Copy link
Contributor Author

desa commented Oct 6, 2017

@nathanielc Just rebased. Should be good to merge on your signal.

@nathanielc
Copy link
Contributor

nathanielc commented Oct 6, 2017

@desa The server shutsdown if an invalid TICkscript is provided. We need the soft behavior, i.e. keep running if at all possible. A typo is a script should not cause the server to shutdown.

We can still fail to start if there is an invalid script, but should not shutdown if it fails to reload.

@desa
Copy link
Contributor Author

desa commented Oct 6, 2017

@nathanielc Fixed. Removed the logic for hard errors previously, but forgot to ignore the error it returned.

@desa
Copy link
Contributor Author

desa commented Oct 6, 2017

whoops just realized that I think the previous comment applies to the other PR, or does

The server shutsdown if an invalid TICkscript is provided. We need the soft behavior, i.e. keep running if at all possible. A typo is a script should not cause the server to shutdown.

apply to the logging stuff?

@nathanielc
Copy link
Contributor

Oops, yeah I commented on the wrong one :-P

@nathanielc
Copy link
Contributor

@desa Now actually testing this PR...

How can we make it easy to get logs for all levels greater than info?

@nathanielc
Copy link
Contributor

Testing this out I realized we haven't updated the bash completion scripts for the new CLI options.

@nathanielc
Copy link
Contributor

I know this isn't directly related but why is starting a task warn?

ts=2017-10-06T11:06:53.850-06:00 lvl=warn msg="starting task" service=kapacitor task_master=main task=log

Looking around I see quite a few things are warns. In general I don't think we need warns. In fact I'd be fine if we removed it entirely. Maybe we should open up a separate PR for remove warns?

@desa desa closed this Oct 6, 2017
@ghost ghost removed the in progress label Oct 6, 2017
@desa desa reopened this Oct 6, 2017
@ghost ghost added the in progress label Oct 6, 2017
@desa
Copy link
Contributor Author

desa commented Oct 6, 2017

I know this isn't directly related but why is starting a task warn?

I think this is an issue with the sessions logger. I fixed it. It should be debug.

Maybe we should open up a separate PR for remove warns?

I'll add another PR for removing warns in general.

@nathanielc added completions.

@desa
Copy link
Contributor Author

desa commented Oct 6, 2017

@nathanielc just rebased. I think things should be good to go now. (hopefully :))

@desa
Copy link
Contributor Author

desa commented Oct 9, 2017

@nathanielc fixed issue with kapacitor logs lvl=error+ not correctly logging levels.

@desa
Copy link
Contributor Author

desa commented Oct 9, 2017

going to add some tests related to the log level setting

@desa
Copy link
Contributor Author

desa commented Oct 10, 2017

@nathanielc added test for issue you uncovered

Rename constants

Add pruning of sessions

Add changelog entry

Close channel on service close

Restructure everything into a single package

Fix tests

SQUASH: saving state

Get logs via the API

Switch to streaming http

Add JSON logging

Add table test template for json logger

Add tests for JSON logs

Add todo comment

Fixup todo comment

Add diagnostic session changes

Add kapacitor CLI commands for logs

Change dao to sessions

Reorganize code

Reorganize

Add context to Log function

Fix kapacitor subcommand logic

Address issues from PR

Add diagnostic to session service

Remove unreachable code

Remove deadlock introduced by using diagnostic

Remove TODOs

Add test for content type

Fix race on watch/logs subcommand

And pull out similar logic

Fix notify signals

Fix bad usage of warn

Expose loglevels with + in kv pairs

Update bash-completion

Make logLevelFromName case insensitive

Add tests for handleSessions http hander
@desa desa merged commit 953362a into master Oct 10, 2017
@desa desa deleted the md-log-api branch October 10, 2017 15:24
@ghost ghost removed the in progress label Oct 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants