Skip to content

Commit

Permalink
Move metrics tracking from processor to manipulator
Browse files Browse the repository at this point in the history
  • Loading branch information
albertusdev committed Jul 10, 2019
1 parent f079880 commit e1c6479
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
14 changes: 0 additions & 14 deletions pkg/processor/native/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ import (
"github.com/anthonynsimon/bild/clone"
"github.com/anthonynsimon/bild/parallel"
"github.com/anthonynsimon/bild/transform"
"github.com/gojek/darkroom/pkg/metrics"
"github.com/gojek/darkroom/pkg/processor"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"time"
)

const (
pngType = "png"
jpgType = "jpeg"

watermarkDurationKey = "watermarkDuration"
decodeDurationKey = "decodeDuration"
encodeDurationKey = "encodeDuration"
)

// BildProcessor uses bild library to process images using native Golang image.Image interface
Expand Down Expand Up @@ -66,7 +60,6 @@ func (bp *BildProcessor) Watermark(base []byte, overlay []byte, opacity uint8) (
return nil, err
}

t := time.Now()
ratio := float64(overlayImg.Bounds().Dy()) / float64(overlayImg.Bounds().Dx())
dWidth := float64(baseImg.Bounds().Dx()) / 2.0

Expand All @@ -83,7 +76,6 @@ func (bp *BildProcessor) Watermark(base []byte, overlay []byte, opacity uint8) (

// Performing overlay
draw.DrawMask(baseImg.(draw.Image), overlayImg.Bounds().Add(offset), overlayImg, image.ZP, mask, image.ZP, draw.Over)
metrics.Update(metrics.UpdateOption{Name: watermarkDurationKey, Type: metrics.Duration, Duration: time.Since(t)})

return bp.Encode(baseImg, f)
}
Expand All @@ -109,16 +101,11 @@ func (bp *BildProcessor) GrayScale(img image.Image) image.Image {
}

func (bp *BildProcessor) Decode(data []byte) (image.Image, string, error) {
t := time.Now()
img, f, err := image.Decode(bytes.NewReader(data))
if err == nil {
metrics.Update(metrics.UpdateOption{Name: decodeDurationKey, Type: metrics.Duration, Duration: time.Since(t)})
}
return img, f, err
}

func (bp *BildProcessor) Encode(img image.Image, format string) ([]byte, error) {
t := time.Now()
if format == pngType && isOpaque(img) {
format = jpgType
}
Expand All @@ -130,7 +117,6 @@ func (bp *BildProcessor) Encode(img image.Image, format string) ([]byte, error)
} else {
err = jpeg.Encode(buff, img, nil)
}
metrics.Update(metrics.UpdateOption{Name: encodeDurationKey, Type: metrics.Duration, Duration: time.Since(t)})
return buff.Bytes(), err
}

Expand Down
19 changes: 15 additions & 4 deletions pkg/service/manipulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const (
blackHexCode = "000000"

cropDurationKey = "cropDuration"
resizeDurationKey = "resizeDuration"
decodeDurationKey = "decodeDuration"
encodeDurationKey = "encodeDuration"
grayScaleDurationKey = "grayScaleDuration"
resizeDurationKey = "resizeDuration"
)

// Manipulator interface sets the contract on the implementation for common processing support in darkroom
Expand Down Expand Up @@ -48,22 +50,31 @@ type ProcessSpec struct {
func (m *manipulator) Process(spec ProcessSpec) ([]byte, error) {
params := spec.Params
var err error
t := time.Now()
data, f, err := m.processor.Decode(spec.ImageData)
if err != nil {
return nil, err
}
trackDuration(decodeDurationKey, t, spec)
if params[fit] == crop {
t := time.Now()
t = time.Now()
data = m.processor.Crop(data, CleanInt(params[width]), CleanInt(params[height]), GetCropPoint(params[crop]))
trackDuration(cropDurationKey, t, spec)
} else if len(params[fit]) == 0 && (CleanInt(params[width]) != 0 || CleanInt(params[height]) != 0) {
t := time.Now()
t = time.Now()
data = m.processor.Resize(data, CleanInt(params[width]), CleanInt(params[height]))
trackDuration(resizeDurationKey, t, spec)
}
if params[mono] == blackHexCode {
t := time.Now()
t = time.Now()
data = m.processor.GrayScale(data)
trackDuration(grayScaleDurationKey, t, spec)
}
t = time.Now()
src, err := m.processor.Encode(data, f)
if err == nil {
trackDuration(encodeDurationKey, t, spec)
}
return src, err
}

Expand Down

0 comments on commit e1c6479

Please sign in to comment.