Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from imgproxy:master #432

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

## [Unreleased]
# Add
- Add `imgproxy.source_image_url` and `imgproxy.processing_options` attributes to New Relic, DataDog, and OpenTelemetry traces.
- (pro) Add [monochrome](https://docs.imgproxy.net/latest/usage/processing#monochrome) processing option.
- (pro) Add [duotone](https://docs.imgproxy.net/latest/usage/processing#duotone) processing option.

# Change
- Properly set the `net.host.name` and `http.url` tags in OpenTelemetry traces.

# Fix
- Fix handling `#` symbols in `local://`, `s3://`, `gcs://`, `abs://`, and `swift://` URLs.
- Fix `IMGPROXY_FALLBACK_IMAGE_HTTP_CODE` value check. Allow `0` value.
Expand Down
10 changes: 10 additions & 0 deletions metrics/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ func StartRootSpan(ctx context.Context, rw http.ResponseWriter, r *http.Request)
return context.WithValue(ctx, spanCtxKey{}, span), cancel, newRw
}

func SetMetadata(ctx context.Context, key string, value any) {
if !enabled {
return
}

if rootSpan, ok := ctx.Value(spanCtxKey{}).(tracer.Span); ok {
rootSpan.SetTag(key, value)
}
}

func StartSpan(ctx context.Context, name string) context.CancelFunc {
if !enabled {
return func() {}
Expand Down
24 changes: 24 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package metrics

import (
"context"
"fmt"
"net/http"

"github.com/imgproxy/imgproxy/v3/metrics/cloudwatch"
"github.com/imgproxy/imgproxy/v3/metrics/datadog"
"github.com/imgproxy/imgproxy/v3/metrics/newrelic"
"github.com/imgproxy/imgproxy/v3/metrics/otel"
"github.com/imgproxy/imgproxy/v3/metrics/prometheus"
"github.com/imgproxy/imgproxy/v3/structdiff"
)

func Init() error {
Expand Down Expand Up @@ -62,6 +64,28 @@ func StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request)
return ctx, cancel, rw
}

func setMetadata(ctx context.Context, key string, value any) {
newrelic.SetMetadata(ctx, key, value)
datadog.SetMetadata(ctx, key, value)
otel.SetMetadata(ctx, key, value)
}

func SetMetadata(ctx context.Context, key string, value any) {
type diffable interface {
Diff() structdiff.Entries
}

if diff, ok := value.(diffable); ok {
m := diff.Diff().Flatten()
for k, v := range m {
setMetadata(ctx, fmt.Sprintf("%s.%s", key, k), v)
}
return
}

setMetadata(ctx, key, value)
}

func StartQueueSegment(ctx context.Context) context.CancelFunc {
promCancel := prometheus.StartQueueSegment()
nrCancel := newrelic.StartSegment(ctx, "Queue")
Expand Down
23 changes: 23 additions & 0 deletions metrics/newrelic/newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"net/http"
"reflect"
"regexp"
"sync"
"time"
Expand Down Expand Up @@ -131,6 +132,28 @@ func StartTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Reque
return context.WithValue(ctx, transactionCtxKey{}, txn), cancel, newRw
}

func SetMetadata(ctx context.Context, key string, value interface{}) {
if !enabled {
return
}

if txn, ok := ctx.Value(transactionCtxKey{}).(*newrelic.Transaction); ok {
rv := reflect.ValueOf(value)
switch {
case rv.Kind() == reflect.String || rv.Kind() == reflect.Bool:
txn.AddAttribute(key, value)
case rv.CanInt():
txn.AddAttribute(key, rv.Int())
case rv.CanUint():
txn.AddAttribute(key, rv.Uint())
case rv.CanFloat():
txn.AddAttribute(key, rv.Float())
default:
txn.AddAttribute(key, fmt.Sprintf("%v", value))
}
}
}

func StartSegment(ctx context.Context, name string) context.CancelFunc {
if !enabled {
return func() {}
Expand Down
40 changes: 37 additions & 3 deletions metrics/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"os"
"reflect"
"runtime"
"strconv"
"strings"
Expand All @@ -34,8 +35,8 @@ import (
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/semconv/v1.17.0/httpconv"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/semconv/v1.20.0/httpconv"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/credentials"

Expand Down Expand Up @@ -396,10 +397,16 @@ func StartRootSpan(ctx context.Context, rw http.ResponseWriter, r *http.Request)
ctx = propagator.Extract(ctx, propagation.HeaderCarrier(r.Header))
}

server := r.Host
if len(server) == 0 {
server = "imgproxy"
}

ctx, span := tracer.Start(
ctx, "/request",
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(httpconv.ServerRequest("imgproxy", r)...),
trace.WithAttributes(httpconv.ServerRequest(server, r)...),
trace.WithAttributes(semconv.HTTPURL(r.RequestURI)),
)
ctx = context.WithValue(ctx, hasSpanCtxKey{}, struct{}{})

Expand All @@ -418,6 +425,33 @@ func StartRootSpan(ctx context.Context, rw http.ResponseWriter, r *http.Request)
return ctx, cancel, newRw
}

func SetMetadata(ctx context.Context, key string, value interface{}) {
if !enabled {
return
}

span := trace.SpanFromContext(ctx)

rv := reflect.ValueOf(value)

switch {
case rv.Kind() == reflect.String:
span.SetAttributes(attribute.String(key, value.(string)))
case rv.Kind() == reflect.Bool:
span.SetAttributes(attribute.Bool(key, value.(bool)))
case rv.CanInt():
span.SetAttributes(attribute.Int64(key, rv.Int()))
case rv.CanUint():
span.SetAttributes(attribute.Int64(key, int64(rv.Uint())))
case rv.CanFloat():
span.SetAttributes(attribute.Float64(key, rv.Float()))
default:
// Theoretically, we can also cover slices and arrays here,
// but it's pretty complex and not really needed for now
span.SetAttributes(attribute.String(key, fmt.Sprintf("%v", value)))
}
}

func StartSpan(ctx context.Context, name string) context.CancelFunc {
if !enabled {
return func() {}
Expand Down
3 changes: 3 additions & 0 deletions processing_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
errorreport.SetMetadata(r, "Source Image URL", imageURL)
errorreport.SetMetadata(r, "Processing Options", po)

metrics.SetMetadata(ctx, "imgproxy.source_image_url", imageURL)
metrics.SetMetadata(ctx, "imgproxy.processing_options", po)

err = security.VerifySourceURL(imageURL)
checkErr(ctx, "security", err)

Expand Down
21 changes: 21 additions & 0 deletions structdiff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ func (d Entries) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}

func (d Entries) flatten(m map[string]interface{}, prefix string) {
for _, e := range d {
key := e.Name
if len(prefix) > 0 {
key = prefix + "." + key
}

if dd, ok := e.Value.(Entries); ok {
dd.flatten(m, key)
} else {
m[key] = e.Value
}
}
}

func (d Entries) Flatten() map[string]interface{} {
m := make(map[string]interface{})
d.flatten(m, "")
return m
}

func Diff(a, b interface{}) Entries {
valA := reflect.Indirect(reflect.ValueOf(a))
valB := reflect.Indirect(reflect.ValueOf(b))
Expand Down
Loading