Skip to content

Commit c3e95f9

Browse files
Fix python log config file (#822)
* Fix python log config file * fix ci * fix ci
1 parent 2eec4f0 commit c3e95f9

File tree

7 files changed

+319
-83
lines changed

7 files changed

+319
-83
lines changed

.ci/helm.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,6 @@ function ci::verify_log_topic() {
588588

589589
sleep "$timesleep"
590590
MESSAGE=$(kubectl exec -n ${NAMESPACE} ${CLUSTER}-pulsar-broker-0 -- bin/pulsar-client consume -n 1 -s "sub" --subscription-position Earliest "${logTopic}")
591-
echo "$MESSAGE"
592591
if [[ "$MESSAGE" == *"$message"* ]]; then
593592
return 0
594593
fi

.ci/tests/integration/cases/py-download-function/verify.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ if [ $? -ne 0 ]; then
4444
fi
4545

4646
if [ $USE_TLS == "false" ]; then
47-
verify_log_topic=$(ci::verify_log_topic persistent://public/default/py-function-logs-sidecar "python_instance_main.py: Starting Python instance with Namespace" 10 2>&1)
47+
verify_log_topic=$(ci::verify_log_topic persistent://public/default/py-function-logs-sidecar "Starting Python instance with Namespace" 10 2>&1)
4848
if [ $? -ne 0 ]; then
4949
echo "$verify_log_topic"
5050
kubectl delete -f "${BASE_DIR}"/.ci/tests/integration/cases/py-download-function/manifests.yaml > /dev/null 2>&1 || true

.ci/tests/integration/cases/python-log-format-json/manifests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ spec:
1111
replicas: 1
1212
maxReplicas: 1
1313
logTopic: persistent://public/default/py-function-logs
14+
logTopicAgent: sidecar
1415
input:
1516
topics:
1617
- persistent://public/default/input-python-log-format-json-topic

.ci/tests/integration/e2e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ setup:
2828
command: |
2929
helm repo add cowboysysop https://cowboysysop.github.io/charts/
3030
helm repo update
31-
helm install vpa cowboysysop/vertical-pod-autoscaler --version 6.0.3
31+
helm install vpa cowboysysop/vertical-pod-autoscaler --version 11.1.1
3232
wait:
3333
- namespace: default
3434
resource: pod

controllers/spec/common.go

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ type TLSConfig interface {
169169
GetMountPath() string
170170
}
171171

172+
type RollingCfg struct {
173+
Enabled bool
174+
Type string // "size" or "time"
175+
File string
176+
// size-based
177+
MaxBytes int
178+
Backups int
179+
// time-based
180+
When string // e.g. "D", "W0"
181+
Interval int
182+
}
183+
184+
type LogCfg struct {
185+
Level string // INFO/DEBUG/...
186+
Format string // "json" or "text"
187+
Handlers string // computed: "stream_handler[,rotating_file_handler|,timed_rotating_file_handler]"
188+
Rolling RollingCfg
189+
}
190+
172191
func IsManaged(object metav1.Object) bool {
173192
managed, exists := object.GetAnnotations()[AnnotationManaged]
174193
return !exists || managed != "false"
@@ -1104,88 +1123,89 @@ func generatePythonLogConfigCommand(name string, runtime *v1alpha1.PythonRuntime
11041123

11051124
func renderPythonInstanceLoggingINITemplate(name string, runtime *v1alpha1.PythonRuntime, agent v1alpha1.LogTopicAgent) (string, error) {
11061125
tmpl := template.Must(template.New("spec").Parse(pythonLoggingINITemplate))
1107-
var tpl bytes.Buffer
1108-
type logConfig struct {
1109-
RollingEnabled bool
1110-
Level string
1111-
Policy template.HTML
1112-
Handlers string
1113-
Format string
1126+
1127+
lc := LogCfg{
1128+
Level: "INFO",
1129+
Format: "text",
1130+
Rolling: RollingCfg{
1131+
Enabled: false,
1132+
Backups: 5,
1133+
},
11141134
}
1115-
lc := &logConfig{}
1116-
lc.Level = "INFO"
1117-
lc.Format = "text"
1118-
lc.Handlers = "stream_handler"
1135+
1136+
// level
11191137
if runtime.Log != nil && runtime.Log.Level != "" {
11201138
if level := parsePythonLogLevel(runtime); level != "" {
11211139
lc.Level = level
11221140
}
11231141
}
1124-
if runtime.Log != nil && runtime.Log.Format != nil {
1125-
lc.Format = string(*runtime.Log.Format)
1142+
// format
1143+
if runtime.Log != nil && runtime.Log.Format != nil && strings.ToLower(string(*runtime.Log.Format)) == "json" {
1144+
lc.Format = "json"
11261145
}
1146+
1147+
// default handler
1148+
lc.Handlers = "stream_handler"
1149+
1150+
// log file path
1151+
logFile := fmt.Sprintf("logs/functions/%s.log", name)
1152+
1153+
// rolling policy
11271154
if runtime.Log != nil && runtime.Log.RotatePolicy != nil {
1128-
lc.RollingEnabled = true
1129-
logFile := fmt.Sprintf("logs/functions/%s-${%s}.log", name, EnvShardID)
1155+
lc.Rolling.Enabled = true
11301156
switch *runtime.Log.RotatePolicy {
1157+
case v1alpha1.SizedPolicyWith10MB:
1158+
lc.Rolling.Type = "size"
1159+
lc.Rolling.File = logFile
1160+
lc.Rolling.MaxBytes = 10 * 1024 * 1024
1161+
lc.Handlers = "stream_handler,rotating_file_handler"
1162+
case v1alpha1.SizedPolicyWith50MB:
1163+
lc.Rolling.Type = "size"
1164+
lc.Rolling.File = logFile
1165+
lc.Rolling.MaxBytes = 50 * 1024 * 1024
1166+
lc.Handlers = "stream_handler,rotating_file_handler"
1167+
case v1alpha1.SizedPolicyWith100MB:
1168+
lc.Rolling.Type = "size"
1169+
lc.Rolling.File = logFile
1170+
lc.Rolling.MaxBytes = 100 * 1024 * 1024
1171+
lc.Handlers = "stream_handler,rotating_file_handler"
11311172
case v1alpha1.TimedPolicyWithDaily:
1173+
lc.Rolling.Type = "time"
1174+
lc.Rolling.File = logFile
1175+
lc.Rolling.When = "D"
1176+
lc.Rolling.Interval = 1
11321177
lc.Handlers = "stream_handler,timed_rotating_file_handler"
1133-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_timed_rotating_file_handler]
1134-
args=(\"%s\", 'D', 1, 5,)
1135-
class=handlers.TimedRotatingFileHandler
1136-
level=%s
1137-
formatter=formatter`, logFile, lc.Level))
11381178
case v1alpha1.TimedPolicyWithWeekly:
1179+
lc.Rolling.Type = "time"
1180+
lc.Rolling.File = logFile
1181+
lc.Rolling.When = "W0" // every monday
1182+
lc.Rolling.Interval = 1
11391183
lc.Handlers = "stream_handler,timed_rotating_file_handler"
1140-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_timed_rotating_file_handler]
1141-
args=(\"%s\", 'W0', 1, 5,)
1142-
class=handlers.TimedRotatingFileHandler
1143-
level=%s
1144-
formatter=formatter`, logFile, lc.Level))
11451184
case v1alpha1.TimedPolicyWithMonthly:
1185+
lc.Rolling.Type = "time"
1186+
lc.Rolling.File = logFile
1187+
lc.Rolling.When = "D" // day
1188+
lc.Rolling.Interval = 30
11461189
lc.Handlers = "stream_handler,timed_rotating_file_handler"
1147-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_timed_rotating_file_handler]
1148-
args=(\"%s\", 'D', 30, 5,)
1149-
class=handlers.TimedRotatingFileHandler
1150-
level=%s
1151-
formatter=formatter`, logFile, lc.Level))
1152-
case v1alpha1.SizedPolicyWith10MB:
1153-
lc.Handlers = "stream_handler,rotating_file_handler"
1154-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1155-
args=(\"%s\", 'a', 10485760, 5,)
1156-
class=handlers.RotatingFileHandler
1157-
level=%s
1158-
formatter=formatter`, logFile, lc.Level))
1159-
case v1alpha1.SizedPolicyWith50MB:
1160-
lc.Handlers = "handler_stream_handler,rotating_file_handler"
1161-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1162-
args=(%s, 'a', 52428800, 5,)
1163-
class=handlers.RotatingFileHandler
1164-
level=%s
1165-
formatter=formatter`, logFile, lc.Level))
1166-
case v1alpha1.SizedPolicyWith100MB:
1167-
lc.Handlers = "handler_stream_handler,rotating_file_handler"
1168-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1169-
args=(%s, 'a', 104857600, 5,)
1170-
class=handlers.RotatingFileHandler
1171-
level=%s
1172-
formatter=formatter`, logFile, lc.Level))
1173-
}
1174-
} else if agent == v1alpha1.SIDECAR { // sidecar mode needs the rotated log file
1175-
lc.RollingEnabled = true
1176-
logFile := fmt.Sprintf("logs/functions/%s-${%s}.log", name, EnvShardID)
1190+
}
1191+
} else if agent == v1alpha1.SIDECAR {
1192+
// sidecar mode enables rolling by default, using size policy with 10MB
1193+
lc.Rolling = RollingCfg{
1194+
Enabled: true,
1195+
Type: "size",
1196+
File: logFile,
1197+
MaxBytes: 10 * 1024 * 1024,
1198+
Backups: 5,
1199+
}
11771200
lc.Handlers = "stream_handler,rotating_file_handler"
1178-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1179-
args=(\"%s\", 'a', 10485760, 5,)
1180-
class=handlers.RotatingFileHandler
1181-
level=%s
1182-
formatter=formatter`, logFile, lc.Level))
11831201
}
1184-
if err := tmpl.Execute(&tpl, lc); err != nil {
1202+
1203+
var buf bytes.Buffer
1204+
if err := tmpl.Execute(&buf, &lc); err != nil {
11851205
log.Error(err, "failed to render python instance logging template")
11861206
return "", err
11871207
}
1188-
return tpl.String(), nil
1208+
return buf.String(), nil
11891209
}
11901210

11911211
func parseJavaLogLevel(runtime *v1alpha1.JavaRuntime) string {

0 commit comments

Comments
 (0)