Skip to content

Commit

Permalink
Merge pull request #1 from adalberht/master
Browse files Browse the repository at this point in the history
Add new metrics tracking in manipulator (size and extension)
  • Loading branch information
Albertus Angga Raharja committed Jul 2, 2019
2 parents 38ee973 + 8ce5b8f commit 2e47c1c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
19 changes: 19 additions & 0 deletions pkg/metrics/utils.go
@@ -0,0 +1,19 @@
package metrics

// GetImageSizeCluster takes in byte array and return the size cluster for tracking purpose
func GetImageSizeCluster(imageData []byte) string {
switch sz := len(imageData); {
case sz <= 128*1024:
return "<=128KB"
case sz <= 256*1024:
return "<=256KB"
case sz <= 512*1024:
return "<=512KB"
case sz <= 1024*1024:
return "<=1MB"
case sz <= 2048*1024:
return "<=2MB"
default:
return ">2MB"
}
}
15 changes: 15 additions & 0 deletions pkg/metrics/utils_test.go
@@ -0,0 +1,15 @@
package metrics

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetImageSizeCluster(t *testing.T) {
assert.Equal(t, "<=128KB", GetImageSizeCluster(make([]byte, 128*1024)), "<=500KB")
assert.Equal(t, "<=256KB", GetImageSizeCluster(make([]byte, 256*1024)), "<=500KB")
assert.Equal(t, "<=512KB", GetImageSizeCluster(make([]byte, 512*1024)), "<=500KB")
assert.Equal(t, "<=1MB", GetImageSizeCluster(make([]byte, 1024*1024)), "<=500KB")
assert.Equal(t, "<=2MB", GetImageSizeCluster(make([]byte, 2048*1024)), "<=500KB")
assert.Equal(t, ">2MB", GetImageSizeCluster(make([]byte, 2049*1024)), "<=500KB")
}
26 changes: 16 additions & 10 deletions pkg/service/manipulator.go
@@ -1,9 +1,12 @@
package service

import (
"fmt"
"github.com/gojek/darkroom/pkg/metrics"
"github.com/gojek/darkroom/pkg/processor"
"net/http"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -33,11 +36,11 @@ type manipulator struct {
// ProcessSpec defines the specification for a image manipulation job
type ProcessSpec struct {
// Scope defines a scope for the image manipulation job, it can be used for logging/mertrics collection purposes
Scope string
Scope string
// ImageData holds the actual image contents to processed
ImageData []byte
// Params hold the key-value pairs for the processing job and tells the manipulator what to do with the image
Params map[string]string
Params map[string]string
}

// Process takes ProcessSpec as an argument and returns []byte, error
Expand All @@ -50,20 +53,20 @@ func (m *manipulator) Process(spec ProcessSpec) ([]byte, error) {
t := time.Now()
data, err = m.processor.Crop(data, CleanInt(params[width]), CleanInt(params[height]), GetCropPoint(params[crop]))
if err == nil {
trackDuration(cropDurationKey, t, spec.Scope)
trackDuration(cropDurationKey, t, spec)
}
} else if len(params[fit]) == 0 && (CleanInt(params[width]) != 0 || CleanInt(params[height]) != 0) {
t := time.Now()
data, err = m.processor.Resize(data, CleanInt(params[width]), CleanInt(params[height]))
if err == nil {
trackDuration(resizeDurationKey, t, spec.Scope)
trackDuration(resizeDurationKey, t, spec)
}
}
if params[mono] == blackHexCode {
t := time.Now()
data, err = m.processor.GrayScale(data)
if err == nil {
trackDuration(grayScaleDurationKey, t, spec.Scope)
trackDuration(grayScaleDurationKey, t, spec)
}
}
return data, err
Expand Down Expand Up @@ -102,13 +105,16 @@ func GetCropPoint(input string) processor.CropPoint {
}
}

func trackDuration(name string, start time.Time, scope string) {
metrics.Update(metrics.UpdateOption{
Name: name,
func trackDuration(name string, start time.Time, spec ProcessSpec) *metrics.UpdateOption {
ext := strings.Split(http.DetectContentType(spec.ImageData), "/")[1]
updateOption := metrics.UpdateOption{
Name: fmt.Sprintf("%s.%s.%s", name, metrics.GetImageSizeCluster(spec.ImageData), ext),
Type: metrics.Duration,
Duration: time.Since(start),
Scope: scope,
})
Scope: spec.Scope,
}
metrics.Update(updateOption)
return &updateOption
}

// NewManipulator takes in a Processor interface and returns a new manipulator
Expand Down
20 changes: 20 additions & 0 deletions pkg/service/manipulator_test.go
@@ -1,11 +1,14 @@
package service

import (
"fmt"
"github.com/gojek/darkroom/pkg/processor"
"github.com/gojek/darkroom/pkg/processor/native"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io/ioutil"
"testing"
"time"
)

func TestNewManipulator(t *testing.T) {
Expand Down Expand Up @@ -77,6 +80,23 @@ func TestCleanInt(t *testing.T) {
assert.Equal(t, 0, CleanInt("-234"))
}

func Test_trackDuration(t *testing.T) {
imageData, err := ioutil.ReadFile("../processor/native/_testdata/test.png")
if err != nil {
panic(err)
}

updateOption := trackDuration(cropDurationKey, time.Now(), ProcessSpec{
ImageData: imageData,
})
assert.Equal(t, fmt.Sprintf("%s.%s.%s", cropDurationKey, "<=128KB", "png"), updateOption.Name)

updateOption = trackDuration(cropDurationKey, time.Now(), ProcessSpec{
ImageData: make([]byte, 10, 10),
})
assert.Equal(t, fmt.Sprintf("%s.%s.%s", cropDurationKey, "<=128KB", "octet-stream"), updateOption.Name)
}

type mockProcessor struct {
mock.Mock
}
Expand Down

0 comments on commit 2e47c1c

Please sign in to comment.