mirror of
https://github.com/zitadel/zitadel.git
synced 2025-04-28 04:40:51 +00:00

# Which Problems Are Solved With current provided telemetry it's difficult to predict when a projection handler is under increased load until it's too late and causes downstream issues. Importantly, projection updating is in the critical path for many login flows and increased latency there can result in system downtime for users. # How the Problems Are Solved This PR adds three new prometheus-style metrics: 1. **projection_events_processed** (_labels: projection, success_) - This metric gives us a counter of the number of events processed per projection update run and whether they we're processed without error. A high number of events being processed can let us know how busy a particular projection handler is. 2. **projection_handle_timer** _(labels: projection)_ - This is the time it takes to process a projection update given a batch of events - time to take the current_states lock, query for new events, reduce, update_the projection, and update current_states. 3. **projection_state_latency** _(labels: projection)_ - This is the time from the last event processed in the current_states table for a given projection. It tells us how old was the last event you processed? Or, how far behind are you running for this projection? Higher latencies could mean high load or stalled projection handling. # Additional Changes I also had to initialize the global otel metrics provider (`metrics.M`) in the `setup` step additionally to `start` since projection handlers are initialized at setup. The initialization checks if a metrics provider is already set (in case of `start-from-setup` or `start-from-init` to prevent overwriting, which causes the otel metrics provider to stop working. # Additional Context ## Example Dashboards   --------- Co-authored-by: Silvan <27845747+adlerhurst@users.noreply.github.com> Co-authored-by: Livio Spring <livio.a@gmail.com> (cherry picked from commit c1535b7b4916abd415a29e034d266c4ea1dc3645)
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
package metrics
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
)
|
|
|
|
// MockMetrics implements the metrics.Metrics interface for testing
|
|
type MockMetrics struct {
|
|
mu sync.RWMutex
|
|
histogramValues map[string][]float64
|
|
counterValues map[string]int64
|
|
histogramLabels map[string][]map[string]attribute.Value
|
|
counterLabels map[string][]map[string]attribute.Value
|
|
}
|
|
|
|
var _ Metrics = new(MockMetrics)
|
|
|
|
// NewMockMetrics creates a new Metrics instance for testing
|
|
func NewMockMetrics() *MockMetrics {
|
|
return &MockMetrics{
|
|
histogramValues: make(map[string][]float64),
|
|
counterValues: make(map[string]int64),
|
|
histogramLabels: make(map[string][]map[string]attribute.Value),
|
|
counterLabels: make(map[string][]map[string]attribute.Value),
|
|
}
|
|
}
|
|
|
|
func (m *MockMetrics) GetExporter() http.Handler {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) GetMetricsProvider() metric.MeterProvider {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterCounter(name, description string) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) AddCount(ctx context.Context, name string, value int64, labels map[string]attribute.Value) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.counterValues[name] += value
|
|
m.counterLabels[name] = append(m.counterLabels[name], labels)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) AddHistogramMeasurement(ctx context.Context, name string, value float64, labels map[string]attribute.Value) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.histogramValues[name] = append(m.histogramValues[name], value)
|
|
m.histogramLabels[name] = append(m.histogramLabels[name], labels)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterUpDownSumObserver(name, description string, callbackFunc metric.Int64Callback) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterValueObserver(name, description string, callbackFunc metric.Int64Callback) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterHistogram(name, description, unit string, buckets []float64) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) GetHistogramValues(name string) []float64 {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.histogramValues[name]
|
|
}
|
|
|
|
func (m *MockMetrics) GetHistogramLabels(name string) []map[string]attribute.Value {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.histogramLabels[name]
|
|
}
|
|
|
|
func (m *MockMetrics) GetCounterValue(name string) int64 {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.counterValues[name]
|
|
}
|
|
|
|
func (m *MockMetrics) GetCounterLabels(name string) []map[string]attribute.Value {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.counterLabels[name]
|
|
}
|