Skip to content

Commit

Permalink
Add imgproxy.source_image_url and imgproxy.processing_options att…
Browse files Browse the repository at this point in the history
…ributes to New Relic, DataDog, and OpenTelemetry traces
  • Loading branch information
DarthSim committed Aug 23, 2024
1 parent e9d96b7 commit ba17ed8
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [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.

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
28 changes: 28 additions & 0 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 Down Expand Up @@ -418,6 +419,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

0 comments on commit ba17ed8

Please sign in to comment.