Skip to content

Commit 40ab984

Browse files
dilyevskyclaude
andcommitted
[cli] improve runtime and reverse proxy logging
Initialize structured logger for runtime components with stderr output. Route reverse proxy error logs through slog, downgrading noisy client disconnect errors (EOF, broken pipe, context canceled) to debug level. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0848501 commit 40ab984

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

pkg/cmd/run/cmd.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
configv1alpha1 "github.com/apoxy-dev/apoxy/api/config/v1alpha1"
1010
"github.com/apoxy-dev/apoxy/config"
11+
"github.com/apoxy-dev/apoxy/pkg/log"
1112
)
1213

1314
var runCmd = &cobra.Command{
@@ -36,6 +37,9 @@ Components are defined under runtime.components in the config. Example:
3637
if err != nil {
3738
return fmt.Errorf("failed to load config: %w", err)
3839
}
40+
if err := initRuntimeLogger(cfg); err != nil {
41+
return fmt.Errorf("failed to initialize runtime logger: %w", err)
42+
}
3943
if cfg.Runtime == nil || len(cfg.Runtime.Components) == 0 {
4044
return fmt.Errorf("no runtime components configured\n\n"+
4145
"Add a runtime section to your config (%s):\n\n"+
@@ -104,6 +108,16 @@ func Cmd() *cobra.Command {
104108
return runCmd
105109
}
106110

111+
func initRuntimeLogger(cfg *configv1alpha1.Config) error {
112+
opts := []log.Option{log.WithStderrOnly()}
113+
if config.Verbose || cfg.Verbose {
114+
opts = append(opts, log.WithDevMode(), log.WithLevel(log.DebugLevel))
115+
} else {
116+
opts = append(opts, log.WithLevel(log.InfoLevel))
117+
}
118+
return log.Init(opts...)
119+
}
120+
107121
func resolveKubeMirrorConfig(in *configv1alpha1.KubeMirrorConfig) *configv1alpha1.KubeMirrorConfig {
108122
out := in.DeepCopy()
109123
if out.Mirror == "" {

pkg/kube-controller/apiserviceproxy/cloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func resolveBaseAPIProxyHost(apiHost string) string {
115115

116116
func newCloudReverseProxy(remote *url.URL) *httputil.ReverseProxy {
117117
proxy := httputil.NewSingleHostReverseProxy(remote)
118+
proxy.ErrorLog = newReverseProxyErrorLogger()
118119
originalDirector := proxy.Director
119120
proxy.Director = func(req *http.Request) {
120121
originalDirector(req)

pkg/kube-controller/apiserviceproxy/kubeconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (p *APIServiceProxy) configureKubeconfigProxy(ctx context.Context) error {
3737
req.Host = hostURL.Host
3838
},
3939
Transport: transport,
40+
ErrorLog: newReverseProxyErrorLogger(),
4041
}
4142

4243
cert, _, _, caBundle, err := generateServingCertificate(p.opts.ServiceName, p.opts.Namespace)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package apiserviceproxy
2+
3+
import (
4+
"log"
5+
"log/slog"
6+
"strings"
7+
)
8+
9+
type reverseProxyErrorLogWriter struct{}
10+
11+
func (reverseProxyErrorLogWriter) Write(p []byte) (int, error) {
12+
msg := strings.TrimSpace(string(p))
13+
switch {
14+
case strings.Contains(msg, "httputil: ReverseProxy read error during body copy: unexpected EOF"),
15+
strings.Contains(msg, "httputil: ReverseProxy read error during body copy: context canceled"),
16+
strings.Contains(msg, "httputil: ReverseProxy read error during body copy: write: broken pipe"),
17+
strings.Contains(msg, "httputil: ReverseProxy read error during body copy: broken pipe"):
18+
slog.Debug("reverse proxy client disconnected", slog.String("message", msg))
19+
default:
20+
slog.Warn("reverse proxy internal error", slog.String("message", msg))
21+
}
22+
return len(p), nil
23+
}
24+
25+
func newReverseProxyErrorLogger() *log.Logger {
26+
return log.New(reverseProxyErrorLogWriter{}, "", 0)
27+
}

0 commit comments

Comments
 (0)