-
Notifications
You must be signed in to change notification settings - Fork 833
Feature/public controller metrics #731
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
| ) | ||
|
|
||
| const ( | ||
| summaryMaxAge = time.Second * 30 | ||
| summaryAgeBuckets uint32 = 3 | ||
| metricNamespace = "opensandbox-controller" | ||
| ) | ||
|
|
||
| var ( | ||
| allocatorScheduleDurationSummary = func() *prometheus.SummaryVec { | ||
| return prometheus.NewSummaryVec( | ||
| prometheus.SummaryOpts{ | ||
| Namespace: metricNamespace, | ||
| Subsystem: "allocator", | ||
| Name: "schedule_duration_ms", | ||
| MaxAge: summaryMaxAge, | ||
| AgeBuckets: summaryAgeBuckets, | ||
| Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001}, | ||
| }, | ||
| []string{"namespace", "pool_name", "success"}, | ||
| ) | ||
| }() | ||
| allocatorPersistAllocationStateDurationSummary = func() *prometheus.SummaryVec { | ||
| return prometheus.NewSummaryVec( | ||
| prometheus.SummaryOpts{ | ||
| Namespace: metricNamespace, | ||
| Subsystem: "allocator", | ||
| Name: "persist_alloc_state_duration_ms", | ||
| MaxAge: summaryMaxAge, | ||
| AgeBuckets: summaryAgeBuckets, | ||
| Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001}, | ||
| }, | ||
| []string{"namespace", "pool_name", "success"}, | ||
| ) | ||
| }() | ||
| allocatorSyncAllocResultDurationSummary = func() *prometheus.SummaryVec { | ||
| return prometheus.NewSummaryVec( | ||
| prometheus.SummaryOpts{ | ||
| Namespace: metricNamespace, | ||
| Subsystem: "allocator", | ||
| Name: "sync_alloc_result_duration_ms", | ||
| MaxAge: summaryMaxAge, | ||
| AgeBuckets: summaryAgeBuckets, | ||
| Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001}, | ||
| }, | ||
| []string{"namespace", "pool_name", "success"}, | ||
| ) | ||
| }() | ||
| allocatorSyncSingleAllocResultDurationSummary = func() *prometheus.SummaryVec { | ||
| return prometheus.NewSummaryVec( | ||
| prometheus.SummaryOpts{ | ||
| Namespace: metricNamespace, | ||
| Subsystem: "allocator", | ||
| Name: "sync_single_alloc_result_duration_ms", | ||
| MaxAge: summaryMaxAge, | ||
| AgeBuckets: summaryAgeBuckets, | ||
| Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.005, 0.99: 0.001}, | ||
| }, | ||
| []string{"namespace", "pool_name", "sandbox_name", "success"}, | ||
| ) | ||
| }() | ||
| ) | ||
|
|
||
| func init() { | ||
| metrics.Registry.MustRegister(allocatorScheduleDurationSummary) | ||
| metrics.Registry.MustRegister(allocatorPersistAllocationStateDurationSummary) | ||
| metrics.Registry.MustRegister(allocatorSyncAllocResultDurationSummary) | ||
| metrics.Registry.MustRegister(allocatorSyncSingleAllocResultDurationSummary) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ package logging | |
|
|
||
| import ( | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| zap2 "go.uber.org/zap" | ||
|
|
@@ -24,6 +25,11 @@ import ( | |
| "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
| ) | ||
|
|
||
| // localTimeEncoder encodes time in local timezone with nanosecond precision | ||
| func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { | ||
| enc.AppendString(t.Local().Format("2006-01-02T15:04:05.000000000Z07:00")) | ||
| } | ||
|
|
||
| // Options contains configuration for the logger | ||
| type Options struct { | ||
| // Development configures the logger to use a development config | ||
|
|
@@ -63,6 +69,22 @@ func DefaultOptions() Options { | |
| // NewLoggerWithZapOptions creates a logger using controller-runtime's zap options | ||
| // and adds file output support | ||
| func NewLoggerWithZapOptions(opts Options) logr.Logger { | ||
| // Create encoder config with nanosecond timestamp | ||
| encoderConfig := zapcore.EncoderConfig{ | ||
| MessageKey: "msg", | ||
| LevelKey: "level", | ||
| TimeKey: "ts", | ||
| NameKey: "logger", | ||
| CallerKey: "caller", | ||
| FunctionKey: zapcore.OmitKey, | ||
| StacktraceKey: "stacktrace", | ||
| LineEnding: zapcore.DefaultLineEnding, | ||
| EncodeLevel: zapcore.LowercaseLevelEncoder, | ||
| EncodeTime: localTimeEncoder, | ||
| EncodeDuration: zapcore.StringDurationEncoder, | ||
| EncodeCaller: zapcore.ShortCallerEncoder, | ||
| } | ||
|
|
||
| // Add AddCaller option to include file and line number in logs | ||
| if opts.ZapOptions.ZapOpts == nil { | ||
| opts.ZapOptions.ZapOpts = []zap2.Option{} | ||
|
|
@@ -71,7 +93,10 @@ func NewLoggerWithZapOptions(opts Options) logr.Logger { | |
|
|
||
| // If file output is not enabled, use the default zap logger | ||
| if !opts.EnableFileOutput { | ||
| return zap.New(zap.UseFlagOptions(&opts.ZapOptions)) | ||
| return zap.New( | ||
| zap.UseFlagOptions(&opts.ZapOptions), | ||
| zap.Encoder(zapcore.NewJSONEncoder(encoderConfig)), | ||
|
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.
Hard-coding a JSON encoder here overrides encoder-related options parsed from Useful? React with 👍 / 👎. |
||
| ) | ||
| } | ||
|
|
||
| // Create file writer with rotation | ||
|
|
@@ -93,6 +118,7 @@ func NewLoggerWithZapOptions(opts Options) logr.Logger { | |
| // Create logger with multi-writer | ||
| return zap.New( | ||
| zap.UseFlagOptions(&opts.ZapOptions), | ||
| zap.Encoder(zapcore.NewJSONEncoder(encoderConfig)), | ||
| zap.WriteTo(multiWriter), | ||
| ) | ||
| } | ||
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.
This metric uses
info.Sandbox.Nameas a label value, which creates a distinctSummarytime series for every sandbox ever seen. Since the controller never deletes label values, a cluster with normal sandbox churn will continuously grow in-memory metric state and scrape payload size over process lifetime, which can become a reliability issue in production. Keep this metric at bounded cardinality (e.g., namespace/pool/success only) and use logs for per-sandbox detail.Useful? React with 👍 / 👎.