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

Draft: Statistics monitoring #100

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

Conversation

Skantes
Copy link

@Skantes Skantes commented Mar 2, 2020

The aim of this pull request is to add statistics that can be monitored through prometheus as time-series metrics.

@Skantes Skantes force-pushed the monitoring branch 3 times, most recently from 052e44c to 14828b6 Compare March 3, 2020 10:05
@Skantes Skantes changed the title Statistics monitoring [WIP]Statistics monitoring Mar 3, 2020
@Skantes Skantes changed the title [WIP]Statistics monitoring Statistics monitoring Mar 16, 2020
@Skantes Skantes force-pushed the monitoring branch 2 times, most recently from 8c6c383 to 46575ae Compare March 26, 2020 23:05
for i, v := range values {
value, okValue := v.([]byte)
if !okValue {
return nil, errors.New("invalid type for file")
Copy link
Owner

Choose a reason for hiding this comment

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

Probably not the error you want to return.

return nil, err
}

files := make([]string, len(values))
Copy link
Owner

Choose a reason for hiding this comment

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

You probably want countries more than files.

return err
}
defer conn.Close()
conn.Do("SADD", "COUNTRIES", country)
Copy link
Owner

Choose a reason for hiding this comment

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

I would probably return the error to the calling function.

if clientInfo.Country != "" {
err = h.redis.AddCountry(clientInfo.Country)
}
if err != nil {
Copy link
Owner

Choose a reason for hiding this comment

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

Careful here with your scope. Since err = h.redis.AddCountry(clientInfo.Country) is within a totally different scope, you may end up checking a previous error that was already handled.

http/stats.go Outdated
if m.Name == "" {
return errUnknownMirror
}
if fileinfo.Path == "" {
return errEmptyFileError
}
if clientInfo.Country == "" {
return errUnknownCountry
Copy link
Owner

Choose a reason for hiding this comment

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

Nope. The country might be empty in various cases (in a unknown IP block for example). You can't assume that you'll always have a country in clientInfo.

metrics.Year, _ = redis.Int(stats[index+3], err)
output += statsToPrometheusFormat(metrics, "country", name)
index += 4
// Get all download count information from the previous redis querries
Copy link
Owner

Choose a reason for hiding this comment

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

typo

http/metrics.go Outdated
Comment on lines 284 to 390
func removeOccurence(slice []string, value string) []string {
for i, val := range slice {
if val == value {
if i < len(slice)-1 {
copy(slice[i:], slice[i+1:])
}
slice[len(slice)-1] = ""
return slice[:len(slice)-1]
}
}
return slice
}
Copy link
Owner

Choose a reason for hiding this comment

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

If the slice gets pretty big you can probably squeeze some CPU cycles by using a sorted slice during insertion and deletion instead of looping on the whole slice.

http/metrics.go Outdated
if i < len(slice)-1 {
copy(slice[i:], slice[i+1:])
}
slice[len(slice)-1] = ""
Copy link
Owner

Choose a reason for hiding this comment

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

This is not needed because of the resize below.

config/config.go Outdated
@@ -165,6 +168,9 @@ func ReloadConfig() error {
if c.RepositoryScanInterval < 0 {
c.RepositoryScanInterval = 0
}
if c.MetricsTopFilesRetention < 1 {
return fmt.Errorf("Invalid retention duration in days")
Copy link
Owner

Choose a reason for hiding this comment

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

What if you don't care about this feature? I would allow 0 and disable the feature entirely.

cli/commands.go Outdated
@@ -99,11 +99,14 @@ func (c *cli) CmdHelp() error {
help += fmt.Sprintf("CLI commands:\n")
for _, command := range [][]string{
{"add", "Add a new mirror"},
{"addMetric", "Add a tracked file to the metrics route"},
Copy link
Owner

Choose a reason for hiding this comment

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

meh. Can you add a metrics subcommand instead?

Copy link
Owner

@etix etix left a comment

Choose a reason for hiding this comment

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

About your last commit Metrics: Add auto tracked files mode:

This entire feature is awkward. If you want to track past and future files without manual intervention, modify your metrics add to take a pattern instead of a filename; or just track all files. But this feature will only lead to confusion and tears.

@jbkempf
Copy link
Collaborator

jbkempf commented Apr 8, 2024

Please rebase @Skantes

Add entries in the database to count downloads per country.
This is not accessible through the stats endpoint.
This is intended for a future metrics integration.
This adds a metrics route to monitoring metrics using prometheus.
It exports mirror and files stats, as well as the newly introduced
country downloads counter.
This adds a more detailed metrics category for files served by
mirrorbits.
These tracked files will detail their downloads per country, but due to
the increased number of fields required for this, only files selected
through the command line will be tracked.
This also adds command line arguments to add, delete, and list the
files to monitore closely.
Add export of current daily download stats for all files that have been
downloaded in the current day.
With this is also added a retention period for these stats as they can
be heavy for the database and should be removed after use.
When enabled, files will be automatically added to tracked files.
This is to return a 503 Service Unavailable error instead of a 404 Not
Found, a more helpfull error.
@Skantes Skantes changed the title Statistics monitoring Draft: Statistics monitoring Jun 8, 2024
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.

3 participants