-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
refactor: Use package local metrics #1605
Open
SuperQ
wants to merge
1
commit into
0xERR0R:main
Choose a base branch
from
SuperQ:local_metrics_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,23 +6,48 @@ | |
"errors" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/sirupsen/logrus" | ||
"time" | ||
|
||
"github.com/0xERR0R/blocky/cache/stringcache" | ||
"github.com/0xERR0R/blocky/config" | ||
"github.com/0xERR0R/blocky/evt" | ||
"github.com/0xERR0R/blocky/lists/parsers" | ||
"github.com/0xERR0R/blocky/log" | ||
"github.com/0xERR0R/blocky/metrics" | ||
|
||
"github.com/ThinkChaos/parcour" | ||
"github.com/ThinkChaos/parcour/jobgroup" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
groupProducersBufferCap = 1000 | ||
regexWarningThreshold = 500 | ||
) | ||
|
||
//nolint:gochecknoglobals | ||
var ( | ||
lastListGroupRefreshTimestamp = promauto.With(metrics.Reg).NewGauge( | ||
prometheus.GaugeOpts{ | ||
Name: "blocky_last_list_group_refresh_timestamp_seconds", | ||
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. Do we have the same definition in metrics_event_publisher.go? |
||
Help: "Timestamp of last list refresh", | ||
}, | ||
) | ||
denylistEntries = promauto.With(metrics.Reg).NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "blocky_denylist_cache_entries", | ||
Help: "Number of entries in the denylist cache", | ||
}, []string{"group"}, | ||
) | ||
allowlistEntries = promauto.With(metrics.Reg).NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "blocky_allowlist_cache_entries", | ||
Help: "Number of entries in the allowlist cache", | ||
}, []string{"group"}, | ||
) | ||
) | ||
|
||
// ListCacheType represents the type of cached list ENUM( | ||
// denylist // is a list with blocked domains | ||
// allowlist // is a list with allowlisted domains / IPs | ||
|
@@ -144,7 +169,7 @@ | |
|
||
count := b.groupedCache.ElementCount(group) | ||
|
||
evt.Bus().Publish(evt.BlockingCacheGroupChanged, b.listType, group, count) | ||
updateGroupMetrics(b.listType, group, count) | ||
|
||
logger().WithFields(logrus.Fields{ | ||
"group": group, | ||
|
@@ -277,3 +302,14 @@ | |
|
||
return nil | ||
} | ||
|
||
func updateGroupMetrics(listType ListCacheType, group string, count int) { | ||
lastListGroupRefreshTimestamp.Set(float64(time.Now().Unix())) | ||
|
||
switch listType { | ||
case ListCacheTypeDenylist: | ||
denylistEntries.WithLabelValues(group).Set(float64(count)) | ||
case ListCacheTypeAllowlist: | ||
allowlistEntries.WithLabelValues(group).Set(float64(count)) | ||
} | ||
} |
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
Oops, something went wrong.
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.
now we don't test if metrics counters are being incremented. Maybe we can try to read the counter value and assert it?
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.
Yes, that is something we could test for. But I find it not very useful. The Prometheus client
Inc()
type functions are basically a simple Go atomic increment. There's basically no way for it to go wrong in a way that testing would help with.