mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-09 19:52:30 +02:00
Move metrics.StartRequest to middleware && Fix Datadog
This commit is contained in:
@@ -4,6 +4,9 @@
|
|||||||
### Added
|
### Added
|
||||||
- (pro) Add `video_meta` to the `/info` response.
|
- (pro) Add `video_meta` to the `/info` response.
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
- Fix Datadog support.
|
||||||
|
|
||||||
## [3.1.3] - 2021-12-17
|
## [3.1.3] - 2021-12-17
|
||||||
### Fix
|
### Fix
|
||||||
- Fix ETag checking when S3 is used.
|
- Fix ETag checking when S3 is used.
|
||||||
|
@@ -34,6 +34,8 @@ func Init() {
|
|||||||
tracer.WithServiceVersion(version.Version()),
|
tracer.WithServiceVersion(version.Version()),
|
||||||
tracer.WithLogger(dataDogLogger{}),
|
tracer.WithLogger(dataDogLogger{}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
enabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func Stop() {
|
func Stop() {
|
||||||
@@ -42,6 +44,10 @@ func Stop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Enabled() bool {
|
||||||
|
return enabled
|
||||||
|
}
|
||||||
|
|
||||||
func StartRootSpan(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
|
func StartRootSpan(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
|
||||||
if !enabled {
|
if !enabled {
|
||||||
return ctx, func() {}, rw
|
return ctx, func() {}, rw
|
||||||
|
@@ -26,6 +26,12 @@ func Stop() {
|
|||||||
datadog.Stop()
|
datadog.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Enabled() bool {
|
||||||
|
return prometheus.Enabled() ||
|
||||||
|
newrelic.Enabled() ||
|
||||||
|
datadog.Enabled()
|
||||||
|
}
|
||||||
|
|
||||||
func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
|
func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
|
||||||
promCancel := prometheus.StartRequest()
|
promCancel := prometheus.StartRequest()
|
||||||
ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
|
ctx, nrCancel, rw := newrelic.StartTransaction(ctx, rw, r)
|
||||||
|
@@ -44,6 +44,10 @@ func Init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Enabled() bool {
|
||||||
|
return enabled
|
||||||
|
}
|
||||||
|
|
||||||
func StartTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
|
func StartTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
|
||||||
if !enabled {
|
if !enabled {
|
||||||
return ctx, func() {}, rw
|
return ctx, func() {}, rw
|
||||||
|
@@ -95,6 +95,10 @@ func Init() {
|
|||||||
enabled = true
|
enabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Enabled() bool {
|
||||||
|
return enabled
|
||||||
|
}
|
||||||
|
|
||||||
func StartServer(cancel context.CancelFunc) error {
|
func StartServer(cancel context.CancelFunc) error {
|
||||||
if !enabled {
|
if !enabled {
|
||||||
return nil
|
return nil
|
||||||
|
@@ -137,8 +137,7 @@ func respondWithNotModified(reqID string, r *http.Request, rw http.ResponseWrite
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
|
func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
|
||||||
ctx, metricsCancel, rw := metrics.StartRequest(r.Context(), rw, r)
|
ctx := r.Context()
|
||||||
defer metricsCancel()
|
|
||||||
|
|
||||||
path := r.RequestURI
|
path := r.RequestURI
|
||||||
if queryStart := strings.IndexByte(path, '?'); queryStart >= 0 {
|
if queryStart := strings.IndexByte(path, '?'); queryStart >= 0 {
|
||||||
|
16
server.go
16
server.go
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/imgproxy/imgproxy/v3/config"
|
"github.com/imgproxy/imgproxy/v3/config"
|
||||||
"github.com/imgproxy/imgproxy/v3/errorreport"
|
"github.com/imgproxy/imgproxy/v3/errorreport"
|
||||||
"github.com/imgproxy/imgproxy/v3/ierrors"
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
||||||
|
"github.com/imgproxy/imgproxy/v3/metrics"
|
||||||
"github.com/imgproxy/imgproxy/v3/reuseport"
|
"github.com/imgproxy/imgproxy/v3/reuseport"
|
||||||
"github.com/imgproxy/imgproxy/v3/router"
|
"github.com/imgproxy/imgproxy/v3/router"
|
||||||
)
|
)
|
||||||
@@ -29,7 +30,7 @@ func buildRouter() *router.Router {
|
|||||||
r.GET("/", handleLanding, true)
|
r.GET("/", handleLanding, true)
|
||||||
r.GET("/health", handleHealth, true)
|
r.GET("/health", handleHealth, true)
|
||||||
r.GET("/favicon.ico", handleFavicon, true)
|
r.GET("/favicon.ico", handleFavicon, true)
|
||||||
r.GET("/", withCORS(withPanicHandler(withSecret(handleProcessing))), false)
|
r.GET("/", withMetrics(withPanicHandler(withCORS(withSecret(handleProcessing)))), false)
|
||||||
r.HEAD("/", withCORS(handleHead), false)
|
r.HEAD("/", withCORS(handleHead), false)
|
||||||
r.OPTIONS("/", withCORS(handleHead), false)
|
r.OPTIONS("/", withCORS(handleHead), false)
|
||||||
|
|
||||||
@@ -75,6 +76,19 @@ func shutdownServer(s *http.Server) {
|
|||||||
s.Shutdown(ctx)
|
s.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withMetrics(h router.RouteHandler) router.RouteHandler {
|
||||||
|
if !metrics.Enabled() {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx, metricsCancel, rw := metrics.StartRequest(r.Context(), rw, r)
|
||||||
|
defer metricsCancel()
|
||||||
|
|
||||||
|
h(reqID, rw, r.WithContext(ctx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func withCORS(h router.RouteHandler) router.RouteHandler {
|
func withCORS(h router.RouteHandler) router.RouteHandler {
|
||||||
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
|
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
|
||||||
if len(config.AllowOrigin) > 0 {
|
if len(config.AllowOrigin) > 0 {
|
||||||
|
Reference in New Issue
Block a user