From 0de6fc0aa66e4e59a1ca3b9cfa00534a080947a5 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Tue, 1 Oct 2019 15:18:37 +0600 Subject: [PATCH] IMGPROXY_REPORT_DOWNLOADING_ERRORS config --- CHANGELOG.md | 3 ++- config.go | 4 ++++ docs/configuration.md | 7 ++++--- download.go | 6 +++--- errors.go | 4 ++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e4f55c..f4f1fcd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ - Reimplemented and more errors-tolerant image size parsing; - TIFF and BMP support; - Using Application Default Credentials when `IMGPROXY_USE_GCS` is set to `true` but `IMGPROXY_GCS_KEY` is not set. - **Note:** If you use `IMGPROXY_GCS_KEY`, it's recommended to set `IMGPROXY_USE_GCS` to `true` since it may be required by future versions to enable GCS support. + **Note:** If you use `IMGPROXY_GCS_KEY`, it's recommended to set `IMGPROXY_USE_GCS` to `true` since it may be required by future versions to enable GCS support; +- Setting `IMGPROXY_REPORT_DOWNLOADING_ERRORS` to `false` disables reporting of downloading errors. ## v2.5.0 diff --git a/config.go b/config.go index 7c405560..8b5c15cc 100644 --- a/config.go +++ b/config.go @@ -204,6 +204,8 @@ type config struct { SentryEnvironment string SentryRelease string + ReportDownloadingErrors bool + FreeMemoryInterval int DownloadBufferSize int GZipBufferSize int @@ -230,6 +232,7 @@ var conf = config{ HoneybadgerEnv: "production", SentryEnvironment: "production", SentryRelease: fmt.Sprintf("imgproxy/%s", version), + ReportDownloadingErrors: true, FreeMemoryInterval: 10, BufferPoolCalibrationThreshold: 1024, } @@ -336,6 +339,7 @@ func configure() { strEnvConfig(&conf.SentryDSN, "IMGPROXY_SENTRY_DSN") strEnvConfig(&conf.SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT") strEnvConfig(&conf.SentryRelease, "IMGPROXY_SENTRY_RELEASE") + boolEnvConfig(&conf.ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS") intEnvConfig(&conf.FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL") intEnvConfig(&conf.DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE") diff --git a/docs/configuration.md b/docs/configuration.md index 2157e284..4ab18d15 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -187,10 +187,11 @@ imgproxy can report occurred errors to Bugsnag, Honeybadger and Sentry: * `IMGPROXY_BUGSNAG_KEY`: Bugsnag API key. When provided, enables error reporting to Bugsnag; * `IMGPROXY_BUGSNAG_STAGE`: Bugsnag stage to report to. Default: `production`; * `IMGPROXY_HONEYBADGER_KEY`: Honeybadger API key. When provided, enables error reporting to Honeybadger; -* `IMGPROXY_HONEYBADGER_ENV`: Honeybadger env to report to. Default: `production`. +* `IMGPROXY_HONEYBADGER_ENV`: Honeybadger env to report to. Default: `production`; * `IMGPROXY_SENTRY_DSN`: Sentry project DSN. When provided, enables error reporting to Sentry; -* `IMGPROXY_SENTRY_ENVIRONMENT`: Sentry environment to report to. Default: `production`. -* `IMGPROXY_SENTRY_RELEASE`: Sentry release to report to. Default: `imgproxy/{imgproxy version}`. +* `IMGPROXY_SENTRY_ENVIRONMENT`: Sentry environment to report to. Default: `production`; +* `IMGPROXY_SENTRY_RELEASE`: Sentry release to report to. Default: `imgproxy/{imgproxy version}`; +* `IMGPROXY_REPORT_DOWNLOADING_ERRORS`: when `true`, imgproxy will report downloading errors. Default: `true`. ## Log diff --git a/download.go b/download.go index f11754dc..5713cd13 100644 --- a/download.go +++ b/download.go @@ -151,20 +151,20 @@ func readAndCheckImage(r io.Reader, contentLength int) (*imageData, error) { func requestImage(imageURL string) (*http.Response, error) { req, err := http.NewRequest("GET", imageURL, nil) if err != nil { - return nil, newError(404, err.Error(), msgSourceImageIsUnreachable).MarkAsUnexpected() + return nil, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors) } req.Header.Set("User-Agent", conf.UserAgent) res, err := downloadClient.Do(req) if err != nil { - return res, newError(404, err.Error(), msgSourceImageIsUnreachable).MarkAsUnexpected() + return res, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors) } if res.StatusCode != 200 { body, _ := ioutil.ReadAll(res.Body) msg := fmt.Sprintf("Can't download image; Status: %d; %s", res.StatusCode, string(body)) - return res, newError(404, msg, msgSourceImageIsUnreachable).MarkAsUnexpected() + return res, newError(404, msg, msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors) } return res, nil diff --git a/errors.go b/errors.go index d2f8f79a..d99ecf54 100644 --- a/errors.go +++ b/errors.go @@ -31,8 +31,8 @@ func (e *imgproxyError) StackTrace() []uintptr { return e.stack } -func (e *imgproxyError) MarkAsUnexpected() *imgproxyError { - e.Unexpected = true +func (e *imgproxyError) SetUnexpected(u bool) *imgproxyError { + e.Unexpected = u return e }