internal/api: better multi-sites support; validate events against allowed domains
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Iff1ced4966b4d42cfd6dfefb0cfd97696a6a6964
This commit is contained in:
parent
16ace569a0
commit
18fe1a8234
10 changed files with 542 additions and 35 deletions
|
|
@ -43,6 +43,10 @@ func NewMetricsAggregator(pathRegistry *PathRegistry, eventRegistry *CustomEvent
|
|||
labels = append(labels, "referrer")
|
||||
}
|
||||
|
||||
if cfg.Site.Collect.Domain {
|
||||
labels = append(labels, "domain")
|
||||
}
|
||||
|
||||
pageviews := prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "web_pageviews_total",
|
||||
|
|
@ -143,7 +147,7 @@ func sanitizeLabel(label string) string {
|
|||
}
|
||||
|
||||
// Records a pageview with the configured dimensions
|
||||
func (m *MetricsAggregator) RecordPageview(path, country, device, referrer string) {
|
||||
func (m *MetricsAggregator) RecordPageview(path, country, device, referrer, domain string) {
|
||||
// Build label values in the same order as label names
|
||||
labels := prometheus.Labels{"path": sanitizeLabel(path)}
|
||||
|
||||
|
|
@ -159,6 +163,10 @@ func (m *MetricsAggregator) RecordPageview(path, country, device, referrer strin
|
|||
labels["referrer"] = sanitizeLabel(referrer)
|
||||
}
|
||||
|
||||
if m.cfg.Site.Collect.Domain {
|
||||
labels["domain"] = sanitizeLabel(domain)
|
||||
}
|
||||
|
||||
m.pageviews.With(labels).Inc()
|
||||
}
|
||||
|
||||
|
|
@ -193,7 +201,7 @@ func (m *MetricsAggregator) AddUnique(ip, userAgent string) {
|
|||
}
|
||||
|
||||
m.estimator.Add(ip, userAgent)
|
||||
// Note: Gauge is updated in background goroutine, not here
|
||||
// NOTE: Gauge is updated in background goroutine, not here
|
||||
}
|
||||
|
||||
// Registers all metrics with the provided Prometheus registry
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ func TestMetricsAggregator_RecordPageview(t *testing.T) {
|
|||
agg := NewMetricsAggregator(registry, NewCustomEventRegistry(100), cfg)
|
||||
|
||||
// Record pageview with all dimensions
|
||||
agg.RecordPageview("/home", "US", "desktop", "google.com")
|
||||
agg.RecordPageview("/home", "US", "desktop", "google.com", "")
|
||||
|
||||
// Verify metric was recorded
|
||||
expected := `
|
||||
|
|
@ -54,7 +54,7 @@ func TestMetricsAggregator_RecordPageview_MinimalDimensions(t *testing.T) {
|
|||
agg := NewMetricsAggregator(registry, NewCustomEventRegistry(100), cfg)
|
||||
|
||||
// Record pageview with only path
|
||||
agg.RecordPageview("/home", "", "", "")
|
||||
agg.RecordPageview("/home", "", "", "", "")
|
||||
|
||||
// Verify metric was recorded
|
||||
expected := `
|
||||
|
|
@ -176,7 +176,7 @@ func TestMetricsAggregator_MustRegister(t *testing.T) {
|
|||
agg.MustRegister(promRegistry)
|
||||
|
||||
// Record some metrics to ensure they show up
|
||||
agg.RecordPageview("/test", "", "", "")
|
||||
agg.RecordPageview("/test", "", "", "", "")
|
||||
agg.RecordPathOverflow()
|
||||
|
||||
// Verify metrics can be gathered
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue