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

Add profile. #53

Merged
merged 10 commits into from
Aug 18, 2021
Merged

Add profile. #53

merged 10 commits into from
Aug 18, 2021

Conversation

bwplotka
Copy link
Owner

Signed-off-by: Bartlomiej Plotka bwplotka@gmail.com

Copy link
Collaborator

@saswatamcode saswatamcode left a comment

Choose a reason for hiding this comment

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

Looks amazing! 💫 Just some suggestions.

go.mod Outdated
@@ -7,6 +7,7 @@ require (
github.com/antchfx/xmlquery v1.3.4 // indirect
github.com/efficientgo/tools/core v0.0.0-20210609125236-d73259166f20
github.com/efficientgo/tools/extkingpin v0.0.0-20210609125236-d73259166f20
github.com/felixge/fgprof v0.9.1 // indirect
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will need to run go mod tidy as linter fails here. 🙂

main.go Outdated
if err := os.MkdirAll(filepath.Join(dir, "now"), os.ModePerm); err != nil {
return nil, err
}
f, err := os.OpenFile(filepath.Join(dir, "now", "fgprof.pprof"), os.O_TRUNC|os.O_WRONLY, os.ModePerm)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
f, err := os.OpenFile(filepath.Join(dir, "now", "fgprof.pprof"), os.O_TRUNC|os.O_WRONLY, os.ModePerm)
f, err := os.OpenFile(filepath.Join(dir, "now", "fgprof.pprof"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)

This would create a new file if none exists, which is why there was error before. 🙂

Copy link
Collaborator

@saswatamcode saswatamcode Jun 25, 2021

Choose a reason for hiding this comment

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

profile001
Screenshot 2021-06-25 at 10 49 36 AM
https://share.polarsignals.com/a3b7db6/ Works with Thanos! 🙂

bwplotka and others added 2 commits July 8, 2021 11:24
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner Author

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice, some suggestions only. Thanks for helping here!

main.go Outdated
@@ -34,6 +42,10 @@ const (
logFormatCLILog = "clilog"
)

type mdoxMetrics struct {
Copy link
Owner Author

Choose a reason for hiding this comment

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

do we need extra struct?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was kind of keeping the struct there since I was facing issues using just reg variable(metrics not registering due to how CLI operates). Will try out something else and see if I can remove.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've kept it for the Print() function now. 🙂

main.go Outdated
@@ -66,14 +78,28 @@ func main() {
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use.").
Default(logFormatCLILog).Enum(logFormatLogfmt, logFormatJson, logFormatCLILog)
// Profiling and metrics.
profilesPath := app.Flag("debug.profiles", "Path to which CPU and heap profiles are saved").Hidden().String()
metrics := app.Flag("metrics", "Enable metrics and view them at https://localhost:9091/metrics").Hidden().Bool()
Copy link
Owner Author

Choose a reason for hiding this comment

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

Why not printing at the end? It's changing behaviour if we suddenly need to block the whole CLI process 🤔

Copy link
Owner Author

Choose a reason for hiding this comment

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

Someday we could talk about things like this like https://groups.google.com/g/prometheus-developers/c/FPe0LsTfo2E

Copy link
Owner Author

Choose a reason for hiding this comment

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

Right I think we have to print it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Done!

Copy link
Owner Author

Choose a reason for hiding this comment

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

What about something simple: Always put that to the file,?

main.go Outdated
}

func snapshotProfiles(dir string) (func() error, error) {
// TODO: now -> date
Copy link
Owner Author

Choose a reason for hiding this comment

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

?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Missed this, will add it in. 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

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

Done!

@@ -286,6 +360,9 @@ func (v *validator) visit(filepath string, dest string, lineNumbers string) {
v.destFutures[k] = &futureResult{cases: 1, resultFn: func() error { return nil }}
matches := remoteLinkPrefixRe.FindAllStringIndex(dest, 1)
if matches == nil {
if v.l != nil {
Copy link
Owner Author

Choose a reason for hiding this comment

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

What about doing it always and just never register/print metrics? (it will be much less distracting)

Copy link
Collaborator

Choose a reason for hiding this comment

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

So no --metrics flag and it would always be enabled then?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Now it does this always but doesn't register if flag not provided! 🙂

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
main.go Outdated
enc := expfmt.NewEncoder(os.Stdout, expfmt.FmtProtoText)

for _, mf := range mfs {
if err := enc.Encode(mf); err != nil {
Copy link
Owner Author

Choose a reason for hiding this comment

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

We might want to make sure time is encoded too. By default there is no time encoded but in order to walk through those files at some point and load into block, we want timestamps

Copy link
Collaborator

@saswatamcode saswatamcode Aug 2, 2021

Choose a reason for hiding this comment

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

Yes, I've thought of a few ways of adding timestamps! The server is removed and metrics are written to a file now with timestamps but all timestamps are same as it's only called once. Will try to implement something better!

main.go Outdated
@@ -66,14 +78,28 @@ func main() {
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use.").
Default(logFormatCLILog).Enum(logFormatLogfmt, logFormatJson, logFormatCLILog)
// Profiling and metrics.
profilesPath := app.Flag("debug.profiles", "Path to which CPU and heap profiles are saved").Hidden().String()
metrics := app.Flag("metrics", "Enable metrics and view them at https://localhost:9091/metrics").Hidden().Bool()
Copy link
Owner Author

Choose a reason for hiding this comment

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

What about something simple: Always put that to the file,?

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner Author

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

LGTM, just minor nits! 💪🏽

@@ -257,6 +281,9 @@ func format(ctx context.Context, logger log.Logger, files []string, diffs *Diffs
spin.Message(fn + "...")
}
errs.Add(func() error {
start_time := time.Now()
Copy link
Owner Author

Choose a reason for hiding this comment

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

Suggested change
start_time := time.Now()
startTime := time.Now()

@@ -288,6 +315,9 @@ func format(ctx context.Context, logger log.Logger, files []string, diffs *Diffs
if err != nil {
return errors.Wrapf(err, "write %v", fn)
}
time_taken := time.Since(start_time)
Copy link
Owner Author

Choose a reason for hiding this comment

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

ditto

}

// IsFormatted tries to formats given markdown files and return Diff if files are not formatted.
// If diff is empty it means all files are formatted.
func IsFormatted(ctx context.Context, logger log.Logger, files []string, opts ...Option) (diffs Diffs, err error) {
func IsFormatted(ctx context.Context, logger log.Logger, files []string, reg *prometheus.Registry, opts ...Option) (diffs Diffs, err error) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Let's use option - metrics are only used rarely potentially (not by default)

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner Author

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Let's go! Amazing, work and good example for everyone else. Let's write about it / blog / talk 🤩

main.go Outdated
@@ -66,14 +79,33 @@ func main() {
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use.").
Default(logFormatCLILog).Enum(logFormatLogfmt, logFormatJson, logFormatCLILog)
// Profiling and metrics.
profilesPath := app.Flag("debug.profiles", "Path to which CPU and heap profiles are saved").Hidden().String()
Copy link
Owner Author

Choose a reason for hiding this comment

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

Suggested change
profilesPath := app.Flag("debug.profiles", "Path to which CPU and heap profiles are saved").Hidden().String()
profilesPath := app.Flag("profiles.path", "Path to directory where CPU and heap profiles will be saved; If empty, no profiling will be enabled. ").ExistingDir()

Copy link
Collaborator

Choose a reason for hiding this comment

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

Done!

main.go Outdated
@@ -66,14 +79,33 @@ func main() {
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use.").
Default(logFormatCLILog).Enum(logFormatLogfmt, logFormatJson, logFormatCLILog)
// Profiling and metrics.
profilesPath := app.Flag("debug.profiles", "Path to which CPU and heap profiles are saved").Hidden().String()
metrics := app.Flag("metrics", "Path to which metrics are saved in OpenMetrics format").Hidden().String()
Copy link
Owner Author

Choose a reason for hiding this comment

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

Ditto for above changes (:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Done!

main.go Outdated
}, nil
}

func (m *mdoxMetrics) Print() error {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Are we printing here? (::

Copy link
Owner Author

Choose a reason for hiding this comment

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

I don't think you need struct.. just Dump(r prometheus.Registry, dir string) error would be more cleaner, WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Done!


for _, mf := range mfs {
for _, metric := range mf.Metric {
unixTime := now.Unix()
Copy link
Owner Author

Choose a reason for hiding this comment

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

Can we cache now, so we have consistent timestamp across all series?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we do this already! now is defined when constructing dir name and is used across all metrics. So it's the same across all series.

Copy link
Owner Author

Choose a reason for hiding this comment

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

all good!

return err
}
}
if _, err = expfmt.FinalizeOpenMetrics(f); err != nil {
Copy link
Owner Author

Choose a reason for hiding this comment

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

For future: Let's document how one can use those and import to Prometheus 🤗

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure!

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Owner Author

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice! Let's go 💪🏽

bwplotka and others added 2 commits August 17, 2021 11:45
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
@bwplotka bwplotka merged commit d8446f1 into main Aug 18, 2021
@bwplotka bwplotka deleted the profile branch August 18, 2021 12:20
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