-
Notifications
You must be signed in to change notification settings - Fork 1
grafana metrics #560
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
Merged
Merged
grafana metrics #560
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7e7246a
grafana metrics
evgensheff 872a56f
grafana metrics
evgensheff 92c00be
Merge branch 'main' into vultisig/487-grafana-metrics
evgensheff 3cdba23
AI commits fix
evgensheff c42431c
disabled validation for endpoints with signature checks
evgensheff d5bc357
fix AI comment
evgensheff 6e25eca
Merge branch 'main' into vultisig/487-grafana-metrics
evgensheff 3a41ae1
fix AI comments
evgensheff 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| var ( | ||
| appstoreInstallationsTotal = prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "installations_total", | ||
| Help: "Current number of installations per plugin", | ||
| }, | ||
| []string{"plugin_id"}, | ||
| ) | ||
|
|
||
| appstorePoliciesTotal = prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "policies_total", | ||
| Help: "Current number of active policies per plugin", | ||
| }, | ||
| []string{"plugin_id"}, | ||
| ) | ||
|
|
||
| appstoreFeesEarnedTotal = prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "fees_earned_total", | ||
| Help: "Total fees earned per plugin in smallest unit", | ||
| }, | ||
| []string{"plugin_id"}, | ||
| ) | ||
|
|
||
| appstoreInstallationsGrandTotal = prometheus.NewGauge( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "installations_grand_total", | ||
| Help: "Total installations across all plugins", | ||
| }, | ||
| ) | ||
|
|
||
| appstorePoliciesGrandTotal = prometheus.NewGauge( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "policies_grand_total", | ||
| Help: "Total active policies across all plugins", | ||
| }, | ||
| ) | ||
|
|
||
| appstoreFeesGrandTotal = prometheus.NewGauge( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "fees_grand_total", | ||
| Help: "Total fees earned across all plugins in smallest unit", | ||
| }, | ||
| ) | ||
|
|
||
| appstoreCollectorLastUpdateTimestamp = prometheus.NewGauge( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: "verifier", | ||
| Subsystem: "appstore", | ||
| Name: "collector_last_update_timestamp", | ||
| Help: "Unix timestamp of last successful metrics update", | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| type AppStoreMetrics struct{} | ||
|
|
||
| func NewAppStoreMetrics() *AppStoreMetrics { | ||
| return &AppStoreMetrics{} | ||
| } | ||
|
|
||
| func (a *AppStoreMetrics) UpdateInstallations(data map[string]int64) { | ||
| appstoreInstallationsTotal.Reset() | ||
|
|
||
| var grandTotal int64 | ||
| for pluginID, count := range data { | ||
| appstoreInstallationsTotal.WithLabelValues(pluginID).Set(float64(count)) | ||
| grandTotal += count | ||
| } | ||
|
|
||
| appstoreInstallationsGrandTotal.Set(float64(grandTotal)) | ||
| } | ||
|
|
||
| func (a *AppStoreMetrics) UpdatePolicies(data map[string]int64) { | ||
| appstorePoliciesTotal.Reset() | ||
|
|
||
| var grandTotal int64 | ||
| for pluginID, count := range data { | ||
| appstorePoliciesTotal.WithLabelValues(pluginID).Set(float64(count)) | ||
| grandTotal += count | ||
| } | ||
|
|
||
| appstorePoliciesGrandTotal.Set(float64(grandTotal)) | ||
| } | ||
|
|
||
| func (a *AppStoreMetrics) UpdateFees(data map[string]int64) { | ||
| appstoreFeesEarnedTotal.Reset() | ||
|
|
||
| var grandTotal int64 | ||
| for pluginID, total := range data { | ||
| appstoreFeesEarnedTotal.WithLabelValues(pluginID).Set(float64(total)) | ||
| grandTotal += total | ||
| } | ||
|
|
||
| appstoreFeesGrandTotal.Set(float64(grandTotal)) | ||
| } | ||
|
|
||
| func (a *AppStoreMetrics) UpdateTimestamp() { | ||
| appstoreCollectorLastUpdateTimestamp.Set(float64(time.Now().Unix())) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| type DatabaseQuerier interface { | ||
| GetInstallationsByPlugin(ctx context.Context) (map[string]int64, error) | ||
| GetPoliciesByPlugin(ctx context.Context) (map[string]int64, error) | ||
| GetFeesByPlugin(ctx context.Context) (map[string]int64, error) | ||
| } | ||
|
|
||
| type AppStoreCollector struct { | ||
| db DatabaseQuerier | ||
| metrics *AppStoreMetrics | ||
| logger *logrus.Logger | ||
| interval time.Duration | ||
| stopCh chan struct{} | ||
| doneCh chan struct{} | ||
| stopOnce sync.Once | ||
| } | ||
|
|
||
| func NewAppStoreCollector(db DatabaseQuerier, metrics *AppStoreMetrics, logger *logrus.Logger, interval time.Duration) *AppStoreCollector { | ||
| return &AppStoreCollector{ | ||
| db: db, | ||
| metrics: metrics, | ||
| logger: logger, | ||
| interval: interval, | ||
| stopCh: make(chan struct{}), | ||
| doneCh: make(chan struct{}), | ||
| } | ||
| } | ||
|
|
||
| func (c *AppStoreCollector) Start() { | ||
| c.logger.Info("Starting App Store metrics collector") | ||
|
|
||
| go func() { | ||
| defer close(c.doneCh) | ||
|
|
||
| ticker := time.NewTicker(c.interval) | ||
| defer ticker.Stop() | ||
|
|
||
| c.collect() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ticker.C: | ||
| c.collect() | ||
| case <-c.stopCh: | ||
| c.logger.Info("Stopping App Store metrics collector") | ||
| return | ||
| } | ||
| } | ||
| }() | ||
evgensheff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (c *AppStoreCollector) Stop() { | ||
| c.stopOnce.Do(func() { | ||
| close(c.stopCh) | ||
| <-c.doneCh | ||
| }) | ||
| } | ||
|
|
||
| func (c *AppStoreCollector) collect() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), c.interval*9/10) | ||
| defer cancel() | ||
|
|
||
| installations, err := c.db.GetInstallationsByPlugin(ctx) | ||
| if err != nil { | ||
| c.logger.Errorf("Failed to collect installations: %v", err) | ||
| } else { | ||
| c.metrics.UpdateInstallations(installations) | ||
| c.metrics.UpdateTimestamp() | ||
| } | ||
|
|
||
| policies, err := c.db.GetPoliciesByPlugin(ctx) | ||
| if err != nil { | ||
| c.logger.Errorf("Failed to collect policies: %v", err) | ||
| } else { | ||
| c.metrics.UpdatePolicies(policies) | ||
| c.metrics.UpdateTimestamp() | ||
| } | ||
|
|
||
| fees, err := c.db.GetFeesByPlugin(ctx) | ||
| if err != nil { | ||
| c.logger.Errorf("Failed to collect fees: %v", err) | ||
| } else { | ||
| c.metrics.UpdateFees(fees) | ||
| c.metrics.UpdateTimestamp() | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.