@@ -64,7 +64,38 @@ func configmap(ctx context.Context, pgadmin *v1beta1.PGAdmin,
6464
6565 // TODO(tjmoore4): Populate configuration details.
6666 initialize .Map (& configmap .Data )
67- configSettings , err := generateConfig (ctx , pgadmin )
67+ var (
68+ logRetention bool
69+ maxBackupRetentionNumber = 1
70+ // One day in minutes for pgadmin rotation
71+ pgAdminRetentionPeriod = 24 * 60
72+ // Daily rotation for gunicorn rotation
73+ gunicornRetentionPeriod = "D"
74+ )
75+ // If OTel logs feature gate is enabled, we want to change the pgAdmin/gunicorn logging
76+ if feature .Enabled (ctx , feature .OpenTelemetryLogs ) && pgadmin .Spec .Instrumentation != nil {
77+ logRetention = true
78+
79+ // If the user has set a retention period, we will use those values for log rotation,
80+ // which is otherwise managed by python.
81+ if pgadmin .Spec .Instrumentation .Logs != nil &&
82+ pgadmin .Spec .Instrumentation .Logs .RetentionPeriod != nil {
83+
84+ retentionNumber , period := collector .ParseDurationForLogrotate (pgadmin .Spec .Instrumentation .Logs .RetentionPeriod .AsDuration ())
85+ // `LOG_ROTATION_MAX_LOG_FILES`` in pgadmin refers to the already rotated logs.
86+ // `backupCount` for gunicorn is similar.
87+ // Our retention unit is for total number of log files, so subtract 1 to account
88+ // for the currently-used log file.
89+ maxBackupRetentionNumber = retentionNumber - 1
90+ if period == "hourly" {
91+ // If the period is hourly, set the pgadmin
92+ // and gunicorn retention periods to hourly.
93+ pgAdminRetentionPeriod = 60
94+ gunicornRetentionPeriod = "H"
95+ }
96+ }
97+ }
98+ configSettings , err := generateConfig (pgadmin , logRetention , maxBackupRetentionNumber , pgAdminRetentionPeriod )
6899 if err == nil {
69100 configmap .Data [settingsConfigMapKey ] = configSettings
70101 }
@@ -74,7 +105,8 @@ func configmap(ctx context.Context, pgadmin *v1beta1.PGAdmin,
74105 configmap .Data [settingsClusterMapKey ] = clusterSettings
75106 }
76107
77- gunicornSettings , gunicornLoggingSettings , err := generateGunicornConfig (ctx , pgadmin )
108+ gunicornSettings , gunicornLoggingSettings , err := generateGunicornConfig (pgadmin ,
109+ logRetention , maxBackupRetentionNumber , gunicornRetentionPeriod )
78110 if err == nil {
79111 configmap .Data [gunicornConfigKey ] = gunicornSettings
80112 configmap .Data [gunicornLoggingConfigKey ] = gunicornLoggingSettings
@@ -84,7 +116,9 @@ func configmap(ctx context.Context, pgadmin *v1beta1.PGAdmin,
84116}
85117
86118// generateConfigs generates the config settings for the pgAdmin and gunicorn
87- func generateConfig (ctx context.Context , pgadmin * v1beta1.PGAdmin ) (string , error ) {
119+ func generateConfig (pgadmin * v1beta1.PGAdmin ,
120+ logRetention bool , maxBackupRetentionNumber , pgAdminRetentionPeriod int ) (
121+ string , error ) {
88122 settings := map [string ]any {
89123 // Bind to all IPv4 addresses by default. "0.0.0.0" here represents INADDR_ANY.
90124 // - https://flask.palletsprojects.com/en/2.2.x/api/#flask.Flask.run
@@ -107,33 +141,7 @@ func generateConfig(ctx context.Context, pgadmin *v1beta1.PGAdmin) (string, erro
107141 settings ["DATA_DIR" ] = dataMountPath
108142 settings ["LOG_FILE" ] = LogFileAbsolutePath
109143
110- // If OTel logs feature gate is enabled, we want to change the pgAdmin/gunicorn logging
111- if feature .Enabled (ctx , feature .OpenTelemetryLogs ) && pgadmin .Spec .Instrumentation != nil {
112-
113- var (
114- maxBackupRetentionNumber = 1
115- // One day in minutes for pgadmin rotation
116- pgAdminRetentionPeriod = 24 * 60
117- )
118-
119- // If the user has set a retention period, we will use those values for log rotation,
120- // which is otherwise managed by python.
121- if pgadmin .Spec .Instrumentation .Logs != nil &&
122- pgadmin .Spec .Instrumentation .Logs .RetentionPeriod != nil {
123-
124- retentionNumber , period := collector .ParseDurationForLogrotate (pgadmin .Spec .Instrumentation .Logs .RetentionPeriod .AsDuration ())
125- // `LOG_ROTATION_MAX_LOG_FILES`` in pgadmin refers to the already rotated logs.
126- // `backupCount` for gunicorn is similar.
127- // Our retention unit is for total number of log files, so subtract 1 to account
128- // for the currently-used log file.
129- maxBackupRetentionNumber = retentionNumber - 1
130- if period == "hourly" {
131- // If the period is hourly, set the pgadmin
132- // and gunicorn retention periods to hourly.
133- pgAdminRetentionPeriod = 60
134- }
135- }
136-
144+ if logRetention {
137145 settings ["LOG_ROTATION_AGE" ] = pgAdminRetentionPeriod
138146 settings ["LOG_ROTATION_MAX_LOG_FILES" ] = maxBackupRetentionNumber
139147 settings ["JSON_LOGGER" ] = true
@@ -229,9 +237,9 @@ func generateClusterConfig(
229237
230238// generateGunicornConfig generates the config settings for the gunicorn server
231239// - https://docs.gunicorn.org/en/latest/settings.html
232- func generateGunicornConfig (ctx context. Context , pgadmin * v1beta1.PGAdmin ) (
233- string , string , error ,
234- ) {
240+ func generateGunicornConfig (pgadmin * v1beta1.PGAdmin ,
241+ logRetention bool , maxBackupRetentionNumber int , gunicornRetentionPeriod string ,
242+ ) ( string , string , error ) {
235243 settings := map [string ]any {
236244 // Bind to all IPv4 addresses and set 25 threads by default.
237245 // - https://docs.gunicorn.org/en/latest/settings.html#bind
@@ -266,33 +274,8 @@ func generateGunicornConfig(ctx context.Context, pgadmin *v1beta1.PGAdmin) (
266274 // Gunicorn logging dict settings
267275 logSettings := map [string ]any {}
268276
269- // If OTel logs feature gate is enabled, we want to change the pgAdmin/gunicorn logging
270- if feature .Enabled (ctx , feature .OpenTelemetryLogs ) &&
271- pgadmin .Spec .Instrumentation != nil {
272-
273- var (
274- maxBackupRetentionNumber = "1"
275- // Daily rotation for gunicorn rotation
276- gunicornRetentionPeriod = "D"
277- )
278-
279- // If the user has set a retention period, we will use those values for log rotation,
280- // which is otherwise managed by python.
281- if pgadmin .Spec .Instrumentation .Logs != nil &&
282- pgadmin .Spec .Instrumentation .Logs .RetentionPeriod != nil {
283-
284- retentionNumber , period := collector .ParseDurationForLogrotate (pgadmin .Spec .Instrumentation .Logs .RetentionPeriod .AsDuration ())
285- // `LOG_ROTATION_MAX_LOG_FILES`` in pgadmin refers to the already rotated logs.
286- // `backupCount` for gunicorn is similar.
287- // Our retention unit is for total number of log files, so subtract 1 to account
288- // for the currently-used log file.
289- maxBackupRetentionNumber = strconv .Itoa (retentionNumber - 1 )
290- if period == "hourly" {
291- // If the period is hourly, set the pgadmin
292- // and gunicorn retention periods to hourly.
293- gunicornRetentionPeriod = "H"
294- }
295- }
277+ // If OTel logs feature gate is enabled, we want to change the gunicorn logging
278+ if logRetention {
296279
297280 // Gunicorn uses the Python logging package, which sets the following attributes:
298281 // https://docs.python.org/3/library/logging.html#logrecord-attributes.
0 commit comments