add pprof handler
Some checks failed
build container / build-container (push) Failing after 22s
run go test / test (push) Failing after 19s

This commit is contained in:
guochao 2025-03-03 00:26:29 +08:00
parent 629c095bbe
commit f9ff71f62a
6 changed files with 59 additions and 13 deletions

5
.gitignore vendored
View File

@ -1,4 +1,7 @@
.vscode .vscode
data data
__debug* __debug*
.env
.envrc

View File

@ -1,11 +1,12 @@
FROM docker.io/library/golang:1.24-alpine FROM docker.io/library/golang:1.24-alpine
ARG GOPROXY=direct ARG GOPROXY=direct
ARG TAGS=""
ARG ALPINE_MIRROR=https://mirrors.ustc.edu.cn ARG ALPINE_MIRROR=https://mirrors.ustc.edu.cn
WORKDIR /src WORKDIR /src
COPY go.mod go.sum ./ 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 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 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 FROM docker.io/library/alpine:3.21 AS runtime
COPY --from=0 /cache-proxy /bin/cache-proxy COPY --from=0 /cache-proxy /bin/cache-proxy

17
cmd/proxy/debug.go Normal file
View File

@ -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")
}

View File

@ -4,6 +4,7 @@ import (
"flag" "flag"
"log/slog" "log/slog"
"net/http" "net/http"
"net/http/pprof"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -22,6 +23,7 @@ var (
configFilePath = "config.yaml" configFilePath = "config.yaml"
logLevel = "info" logLevel = "info"
sentrydsn = "" sentrydsn = ""
pprofEnabled = false
) )
func init() { func init() {
@ -31,12 +33,8 @@ func init() {
if v, ok := os.LookupEnv("LOG_LEVEL"); ok { if v, ok := os.LookupEnv("LOG_LEVEL"); ok {
logLevel = v logLevel = v
} }
if v, ok := os.LookupEnv("SENTRY_DSN"); ok {
sentrydsn = v
}
flag.StringVar(&configFilePath, "config", configFilePath, "path to config file") flag.StringVar(&configFilePath, "config", configFilePath, "path to config file")
flag.StringVar(&logLevel, "log-level", logLevel, "log level. (trace, debug, info, warn, error)") 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) { func configFromFile(path string) (*cacheproxy.Config, error) {
@ -124,6 +122,14 @@ func main() {
mux.HandleFunc("GET /", server.HandleRequestWithCache) 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") slog.With("addr", ":8881").Info("serving app")
if err := http.ListenAndServe(":8881", middleware.Use(mux, if err := http.ListenAndServe(":8881", middleware.Use(mux,
recover.Recover(), recover.Recover(),

20
compose.release.yaml Normal file
View File

@ -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

View File

@ -1,12 +1,12 @@
services: services:
cache-proxy: 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: build:
args: args:
GOPROXY: ${GOPROXY:-direct} GOPROXY: ${GOPROXY:-direct}
ALPINE_MIRROR: ${ALPINE_MIRROR:-https://mirrors.ustc.edu.cn} ALPINE_MIRROR: ${ALPINE_MIRROR:-https://mirrors.ustc.edu.cn}
TAGS: pprof
pull_policy: always
restart: always restart: always
volumes: volumes:
@ -15,9 +15,8 @@ services:
ports: ports:
- 8881:8881 - 8881:8881
environment: environment:
- HTTPS_PROXY=http://100.64.0.23:7890 - SENTRY_DSN=${SENTRY_DSN}
- 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
- LOG_LEVEL=debug - LOG_LEVEL=debug
env_file:
- path: .env
required: false