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

default tar filename has extra dash in custom metric mode #31

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

karishmalalani
Copy link

Issue fixed extra dash has been removed

karishma.lalani added 2 commits September 13, 2024 15:45
Issue fixed extra dash has been removed
# Conflicts:
#	promdump/promdump.go
Copy link

@ionthegeek ionthegeek left a comment

Choose a reason for hiding this comment

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

This seems unnecessarily complex. There is a bug in handling the case where compression is enabled for a custom metric promdump.

Feedback inline.

Comment on lines 34 to 38
ywclient "github.com/yugabyte/platform-go-client"

"github.com/dsnet/compress/bzip2"
"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"

Choose a reason for hiding this comment

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

Avoid making spurious changes like this.

Copy link
Author

Choose a reason for hiding this comment

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

This time I commit with v1

Choose a reason for hiding this comment

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

Please revert this change and also the change above that moves the ywclient line.

Comment on lines 645 to 661
var prefix string

// Define a regular expression to match the node_prefix key-value pair in the metric string
re := regexp.MustCompile(`node_prefix="([^"]+)"`)

// Try to extract node_prefix from the metric
if *metric != "" {
matches := re.FindStringSubmatch(*metric)
if len(matches) > 1 {
prefix = matches[1] // Use the captured node_prefix value
}
}

// If node_prefix is not found in metric, use nodePrefix flag
if prefix == "" && *nodePrefix != "" {
prefix = *nodePrefix
}
Copy link

@ionthegeek ionthegeek Sep 16, 2024

Choose a reason for hiding this comment

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

We don't need to be this clever. Just test if *nodePrefix == "" to figure out whether a node prefix has been provided or not. We don't need to handle the case where somebody has put node_prefix into the custom metric.

Copy link
Author

Choose a reason for hiding this comment

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

As per your suggestion, the code now only looks for the flag "nodePrefix" and does not find "nodePrefix" for other cases.

Comment on lines 663 to 666
// If neither is available, use a default prefix
if prefix == "" {
return fmt.Sprintf("promdump-%s.tar", time.Now().Format("20060102-150405"))
}

Choose a reason for hiding this comment

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

This doesn't handle the case where a custom metric is specified and compression is enabled.

You'll probably want to switch to a string builder kind of approach where you put the filename together in pieces by appending, then return the assembled string at the end of the func. Concatenate promdump- with the node prefix (if provided), the timestamp string, and the appropriate extension(s).

Copy link
Author

Choose a reason for hiding this comment

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

I did not use string builder but change logic which do similar like string builder

karishma.lalani added 4 commits October 4, 2024 09:38
Make changes as per feedback

now code give default name for custom metric without extra '-'
Changes were made as per feedback
Copy link

@ionthegeek ionthegeek left a comment

Choose a reason for hiding this comment

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

Needs some changes. Mostly janitorial, but one major one.

Comment on lines 652 to 672
// Start with the default prefix "promdump"
filename = "promdump"

// Check if nodePrefix is provided and append it to the filename if not empty
if nodePrefix != nil && *nodePrefix != "" {
filename = fmt.Sprintf("%s-%s", filename, *nodePrefix)

}
// Append the timestamp
filename = fmt.Sprintf("%s-%s", filename, time.Now().Format("20060102-150405"))
if tarCompression != nil {
switch *tarCompression {
case "gzip":
return filename + ".tar.gz"
case "bzip2":
return filename + ".tar.bz2"
}
}

return fmt.Sprintf("promdump-%s-%s.tar", *nodePrefix, now().Format("20060102-150405"))
// Default to ".tar" if no compression type is specified
return filename + ".tar"

Choose a reason for hiding this comment

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

This approach results in a bunch of memory copying, which is inefficient. Because strings are immutable, we have to allocate a new string object every time we append or concatenate strings in this way.

Use strings.Builder instead:
https://pkg.go.dev/strings#example-Builder

var filename strings.Builder

filename.WriteString("promdump")

// Check if nodePrefix is provided and append it to the filename if not empty
if *nodePrefix != "" {
  fmt.Fprintf(&filename, "-%s", *nodePrefix)
}

// Append the timestamp
fmt.Fprintf(&filename, "-%s", time.Now().Format("20060102-150405"))

...

return filename.String()

filename = "promdump"

// Check if nodePrefix is provided and append it to the filename if not empty
if nodePrefix != nil && *nodePrefix != "" {

Choose a reason for hiding this comment

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

The nodePrefix != nil portion of this conditional is spurious. This condition will always be true since it's a pointer to a (possibly nil) string. Simplify this to if *nodePrefix != "" {.

Comment on lines 34 to 38
ywclient "github.com/yugabyte/platform-go-client"

"github.com/dsnet/compress/bzip2"
"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"

Choose a reason for hiding this comment

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

Please revert this change and also the change above that moves the ywclient line.

Comment on lines +1421 to +1428
if *out != "" {

_, err := cleanFiles(*out, customMetricCount, false)
if err != nil {
log.Printf("Error cleaning files : %v", err)
}

}

Choose a reason for hiding this comment

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

Please keep changes to one issue per PR.

As per feedback generateDefaultTarFilename() use strings.builder
Copy link

@ionthegeek ionthegeek left a comment

Choose a reason for hiding this comment

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

Avoid making spurious changes like moving libraries around in the import section. Use git diff to review what's changed and if you see something that your IDE or code formatter has changed for no reason, revert that change before submitting.

The handling of file extensions could still be improved. I've provided a specific change in the comments.

There is a section added for cleaning custom metric files. Split this out into a separate issue and PR.

Comment on lines +663 to +675
// file extension based on compression type
if tarCompression != nil {
switch *tarCompression {
case "gzip":
return filename.String() + ".tar.gz"
case "bzip2":
return filename.String() + ".tar.bz2"
}
}

// Default to ".tar" if no compression type is specified
return filename.String() + ".tar"

Choose a reason for hiding this comment

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

This is section is still doing unnecessary memory copying. If you're returning StringBuilder + something, you're probably doing an unnecessary memory copy.

The program flow could be made clearer by appending the extensions like this:

filename.WriteString(".tar")

if tarCompression != nil {
  switch *tarCompression {
  case "gzip":
    filename.WriteString(".gz")
  case "bzip2":
    filename.WriteString(".bz2")
  }
}

return filename.String()

@@ -15,7 +15,6 @@ import (
"errors"
"flag"
"fmt"
ywclient "github.com/yugabyte/platform-go-client"

Choose a reason for hiding this comment

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

Revert this removal

Comment on lines +34 to +35
"github.com/yugabyte/platform-go-client"

Choose a reason for hiding this comment

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

Revert this addition

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.

promdump: default tar filename has extra dash in custom metric mode
2 participants