From f9ff71f62a3de6d6b3789b7e437bee9849e95b05 Mon Sep 17 00:00:00 2001 From: guochao Date: Mon, 3 Mar 2025 00:26:29 +0800 Subject: [PATCH] add pprof handler --- .gitignore | 5 ++++- Dockerfile | 3 ++- cmd/proxy/debug.go | 17 +++++++++++++++++ cmd/proxy/main.go | 14 ++++++++++---- compose.release.yaml | 20 ++++++++++++++++++++ compose.yaml | 13 ++++++------- 6 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 cmd/proxy/debug.go create mode 100644 compose.release.yaml diff --git a/.gitignore b/.gitignore index 1c0c469..098cf6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .vscode data -__debug* \ No newline at end of file +__debug* + +.env +.envrc \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e87c8df..3ac2ca4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ FROM docker.io/library/golang:1.24-alpine ARG GOPROXY=direct +ARG TAGS="" ARG ALPINE_MIRROR=https://mirrors.ustc.edu.cn WORKDIR /src COPY go.mod go.sum ./ RUN sed -i "s,https://dl-cdn.alpinelinux.org,${ALPINE_MIRROR}," /etc/apk/repositories; apk add git && env GOPROXY=${GOPROXY:-direct} go mod download ADD . /src -RUN go build -o /cache-proxy ./cmd/proxy +RUN go build -o /cache-proxy -tags "${TAGS}" ./cmd/proxy FROM docker.io/library/alpine:3.21 AS runtime COPY --from=0 /cache-proxy /bin/cache-proxy diff --git a/cmd/proxy/debug.go b/cmd/proxy/debug.go new file mode 100644 index 0000000..ae7d32c --- /dev/null +++ b/cmd/proxy/debug.go @@ -0,0 +1,17 @@ +//go:build pprof + +package main + +import ( + "flag" + "os" +) + +func init() { + flag.BoolVar(&pprofEnabled, "pprof", true, "") + + if v, ok := os.LookupEnv("SENTRY_DSN"); ok { + sentrydsn = v + } + flag.StringVar(&sentrydsn, "sentry", sentrydsn, "sentry dsn to report errors") +} diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 0829abc..33d451f 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -4,6 +4,7 @@ import ( "flag" "log/slog" "net/http" + "net/http/pprof" "os" "path/filepath" "time" @@ -22,6 +23,7 @@ var ( configFilePath = "config.yaml" logLevel = "info" sentrydsn = "" + pprofEnabled = false ) func init() { @@ -31,12 +33,8 @@ func init() { if v, ok := os.LookupEnv("LOG_LEVEL"); ok { logLevel = v } - if v, ok := os.LookupEnv("SENTRY_DSN"); ok { - sentrydsn = v - } flag.StringVar(&configFilePath, "config", configFilePath, "path to config file") flag.StringVar(&logLevel, "log-level", logLevel, "log level. (trace, debug, info, warn, error)") - flag.StringVar(&sentrydsn, "sentry", sentrydsn, "sentry dsn to report errors") } func configFromFile(path string) (*cacheproxy.Config, error) { @@ -124,6 +122,14 @@ func main() { mux.HandleFunc("GET /", server.HandleRequestWithCache) + if pprofEnabled { + mux.HandleFunc("GET /debug/pprof/", pprof.Index) + mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) + mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) + } + slog.With("addr", ":8881").Info("serving app") if err := http.ListenAndServe(":8881", middleware.Use(mux, recover.Recover(), diff --git a/compose.release.yaml b/compose.release.yaml new file mode 100644 index 0000000..db6d90c --- /dev/null +++ b/compose.release.yaml @@ -0,0 +1,20 @@ +services: + cache-proxy: + image: ${REPOSITORY:-git.jeffthecoder.xyz}/${OWNER:-public}/cache-proxy:${VERSION:-latest} + build: + args: + GOPROXY: ${GOPROXY:-direct} + ALPINE_MIRROR: ${ALPINE_MIRROR:-https://mirrors.ustc.edu.cn} + + restart: always + + volumes: + - ./config.yaml:/config.yaml:ro + - ./data:/data + ports: + - 8881:8881 + environment: + - LOG_LEVEL=info + env_file: + - path: .env + required: false \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index ddad148..168353b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,12 +1,12 @@ services: cache-proxy: - image: 100.64.0.2:3000/guochao/cache-proxy:${VERSION:-latest} + image: ${REPOSITORY:-git.jeffthecoder.xyz}/${OWNER:-public}/cache-proxy:${VERSION:-latest} build: args: GOPROXY: ${GOPROXY:-direct} ALPINE_MIRROR: ${ALPINE_MIRROR:-https://mirrors.ustc.edu.cn} + TAGS: pprof - pull_policy: always restart: always volumes: @@ -15,9 +15,8 @@ services: ports: - 8881:8881 environment: - - HTTPS_PROXY=http://100.64.0.23:7890 - - HTTP_PROXY=http://100.64.0.23:7890 - - ALL_PROXY=http://100.64.0.23:7890 - - NO_PROXY=localhost,local,cn,*.local,*.cn,127.0.0.1,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,100.64.0.0/16 - - SENTRY_DSN=http://3b7336602c77427d96318074cd86ee04@100.64.0.2:8000/2 + - SENTRY_DSN=${SENTRY_DSN} - LOG_LEVEL=debug + env_file: + - path: .env + required: false \ No newline at end of file