1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00

Refactor tracing

This commit is contained in:
Svyatoslav Kryukov 2021-03-23 12:36:08 +03:00 committed by Sergey Alexandrovich
parent 08b70d8b91
commit 5ba463749c
8 changed files with 72 additions and 69 deletions

View File

@ -65,11 +65,9 @@ func (p *bufPool) calibrateAndClean() {
runtime.GC() runtime.GC()
} }
if prometheusEnabled {
setPrometheusBufferDefaultSize(p.name, p.defaultSize) setPrometheusBufferDefaultSize(p.name, p.defaultSize)
setPrometheusBufferMaxSize(p.name, p.maxSize) setPrometheusBufferMaxSize(p.name, p.maxSize)
} }
}
func (p *bufPool) Get(size int) *bytes.Buffer { func (p *bufPool) Get(size int) *bytes.Buffer {
p.mutex.Lock() p.mutex.Lock()
@ -143,7 +141,7 @@ func (p *bufPool) Put(buf *bytes.Buffer) {
if b == nil { if b == nil {
p.buffers[i] = buf p.buffers[i] = buf
if prometheusEnabled && buf.Cap() > 0 { if buf.Cap() > 0 {
observePrometheusBufferSize(p.name, buf.Cap()) observePrometheusBufferSize(p.name, buf.Cap())
} }

View File

@ -214,14 +214,8 @@ func downloadImage(imageURL string) (*imageData, error) {
func downloadImageCtx(ctx context.Context) (context.Context, context.CancelFunc, error) { func downloadImageCtx(ctx context.Context) (context.Context, context.CancelFunc, error) {
imageURL := getImageURL(ctx) imageURL := getImageURL(ctx)
if newRelicEnabled { defer startNewRelicSegment(ctx, "Downloading image")()
newRelicCancel := startNewRelicSegment(ctx, "Downloading image")
defer newRelicCancel()
}
if prometheusEnabled {
defer startPrometheusDuration(prometheusDownloadDuration)() defer startPrometheusDuration(prometheusDownloadDuration)()
}
imgdata, err := downloadImage(imageURL) imgdata, err := downloadImage(imageURL)
if err != nil { if err != nil {

View File

@ -73,11 +73,9 @@ func run() error {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
if prometheusEnabled {
if err := startPrometheusServer(cancel); err != nil { if err := startPrometheusServer(cancel); err != nil {
return err return err
} }
}
s, err := startServer(cancel) s, err := startServer(cancel)
if err != nil { if err != nil {

View File

@ -9,12 +9,14 @@ import (
"github.com/newrelic/go-agent/v3/newrelic" "github.com/newrelic/go-agent/v3/newrelic"
) )
const (
newRelicTransactionCtxKey = ctxKey("newRelicTransaction")
)
var ( var (
newRelicEnabled = false newRelicEnabled = false
newRelicApp *newrelic.Application newRelicApp *newrelic.Application
newRelicTransactionCtxKey = ctxKey("newRelicTransaction")
) )
func initNewrelic() error { func initNewrelic() error {
@ -44,6 +46,10 @@ func initNewrelic() error {
} }
func startNewRelicTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) { func startNewRelicTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
if !newRelicEnabled {
return ctx, func() {}, rw
}
txn := newRelicApp.StartTransaction("request") txn := newRelicApp.StartTransaction("request")
txn.SetWebRequestHTTP(r) txn.SetWebRequestHTTP(r)
newRw := txn.SetWebResponse(rw) newRw := txn.SetWebResponse(rw)
@ -52,17 +58,24 @@ func startNewRelicTransaction(ctx context.Context, rw http.ResponseWriter, r *ht
} }
func startNewRelicSegment(ctx context.Context, name string) context.CancelFunc { func startNewRelicSegment(ctx context.Context, name string) context.CancelFunc {
if !newRelicEnabled {
return func() {}
}
txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction) txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction)
segment := txn.StartSegment(name) segment := txn.StartSegment(name)
return func() { segment.End() } return func() { segment.End() }
} }
func sendErrorToNewRelic(ctx context.Context, err error) { func sendErrorToNewRelic(ctx context.Context, err error) {
if newRelicEnabled {
txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction) txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction)
txn.NoticeError(err) txn.NoticeError(err)
} }
}
func sendTimeoutToNewRelic(ctx context.Context, d time.Duration) { func sendTimeoutToNewRelic(ctx context.Context, d time.Duration) {
if newRelicEnabled {
txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction) txn := ctx.Value(newRelicTransactionCtxKey).(*newrelic.Transaction)
txn.NoticeError(newrelic.Error{ txn.NoticeError(newrelic.Error{
Message: "Timeout", Message: "Timeout",
@ -72,3 +85,4 @@ func sendTimeoutToNewRelic(ctx context.Context, d time.Duration) {
}, },
}) })
} }
}

View File

@ -762,14 +762,8 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
if newRelicEnabled { defer startNewRelicSegment(ctx, "Processing image")()
newRelicCancel := startNewRelicSegment(ctx, "Processing image")
defer newRelicCancel()
}
if prometheusEnabled {
defer startPrometheusDuration(prometheusProcessingDuration)() defer startPrometheusDuration(prometheusProcessingDuration)()
}
defer vipsCleanup() defer vipsCleanup()

View File

@ -121,16 +121,12 @@ func respondWithNotModified(ctx context.Context, reqID string, r *http.Request,
func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) { func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
if newRelicEnabled {
var newRelicCancel context.CancelFunc var newRelicCancel context.CancelFunc
ctx, newRelicCancel, rw = startNewRelicTransaction(ctx, rw, r) ctx, newRelicCancel, rw = startNewRelicTransaction(ctx, rw, r)
defer newRelicCancel() defer newRelicCancel()
}
if prometheusEnabled { incrementPrometheusRequestsTotal()
prometheusRequestsTotal.Inc()
defer startPrometheusDuration(prometheusRequestDuration)() defer startPrometheusDuration(prometheusRequestDuration)()
}
select { select {
case processingSem <- struct{}{}: case processingSem <- struct{}{}:
@ -150,12 +146,8 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
ctx, downloadcancel, err := downloadImageCtx(ctx) ctx, downloadcancel, err := downloadImageCtx(ctx)
defer downloadcancel() defer downloadcancel()
if err != nil { if err != nil {
if newRelicEnabled {
sendErrorToNewRelic(ctx, err) sendErrorToNewRelic(ctx, err)
}
if prometheusEnabled {
incrementPrometheusErrorsTotal("download") incrementPrometheusErrorsTotal("download")
}
if fallbackImage == nil { if fallbackImage == nil {
panic(err) panic(err)
@ -202,12 +194,8 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
imageData, processcancel, err := processImage(ctx) imageData, processcancel, err := processImage(ctx)
defer processcancel() defer processcancel()
if err != nil { if err != nil {
if newRelicEnabled {
sendErrorToNewRelic(ctx, err) sendErrorToNewRelic(ctx, err)
}
if prometheusEnabled {
incrementPrometheusErrorsTotal("processing") incrementPrometheusErrorsTotal("processing")
}
panic(err) panic(err)
} }

View File

@ -116,6 +116,10 @@ func initPrometheus() {
} }
func startPrometheusServer(cancel context.CancelFunc) error { func startPrometheusServer(cancel context.CancelFunc) error {
if !prometheusEnabled {
return nil
}
s := http.Server{Handler: promhttp.Handler()} s := http.Server{Handler: promhttp.Handler()}
l, err := listenReuseport("tcp", conf.PrometheusBind) l, err := listenReuseport("tcp", conf.PrometheusBind)
@ -135,6 +139,10 @@ func startPrometheusServer(cancel context.CancelFunc) error {
} }
func startPrometheusDuration(m prometheus.Histogram) func() { func startPrometheusDuration(m prometheus.Histogram) func() {
if !prometheusEnabled {
return func() {}
}
t := time.Now() t := time.Now()
return func() { return func() {
m.Observe(time.Since(t).Seconds()) m.Observe(time.Since(t).Seconds())
@ -142,17 +150,31 @@ func startPrometheusDuration(m prometheus.Histogram) func() {
} }
func incrementPrometheusErrorsTotal(t string) { func incrementPrometheusErrorsTotal(t string) {
if prometheusEnabled {
prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc() prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc()
} }
}
func incrementPrometheusRequestsTotal() {
if prometheusEnabled {
prometheusRequestsTotal.Inc()
}
}
func observePrometheusBufferSize(t string, size int) { func observePrometheusBufferSize(t string, size int) {
if prometheusEnabled {
prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size)) prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
} }
}
func setPrometheusBufferDefaultSize(t string, size int) { func setPrometheusBufferDefaultSize(t string, size int) {
if prometheusEnabled {
prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size)) prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
} }
}
func setPrometheusBufferMaxSize(t string, size int) { func setPrometheusBufferMaxSize(t string, size int) {
if prometheusEnabled {
prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size)) prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
} }
}

View File

@ -25,13 +25,8 @@ func checkTimeout(ctx context.Context) {
panic(newError(499, fmt.Sprintf("Request was cancelled after %v", d), "Cancelled")) panic(newError(499, fmt.Sprintf("Request was cancelled after %v", d), "Cancelled"))
} }
if newRelicEnabled {
sendTimeoutToNewRelic(ctx, d) sendTimeoutToNewRelic(ctx, d)
}
if prometheusEnabled {
incrementPrometheusErrorsTotal("timeout") incrementPrometheusErrorsTotal("timeout")
}
panic(newError(503, fmt.Sprintf("Timeout after %v", d), "Timeout")) panic(newError(503, fmt.Sprintf("Timeout after %v", d), "Timeout"))
default: default: