Skip to content

Commit

Permalink
Merge pull request #177 from checkr/zz/add-dd-metrics
Browse files Browse the repository at this point in the history
Add dd metrics
  • Loading branch information
zhouzhuojie authored Oct 9, 2018
2 parents 93674ef + ce16e68 commit 8c5b3a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/entity/flag_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package entity

import (
"encoding/json"
"fmt"

"github.com/checkr/flagr/pkg/config"
"github.com/checkr/flagr/pkg/util"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -66,4 +69,21 @@ func SaveFlagSnapshot(db *gorm.DB, flagID uint, updatedBy string) {
if err := tx.Commit().Error; err != nil {
tx.Rollback()
}

logFlagSnapshotUpdate(flagID, updatedBy)
}

var logFlagSnapshotUpdate = func(flagID uint, updatedBy string) {
if config.Global.StatsdClient == nil {
return
}

config.Global.StatsdClient.Incr(
"flag.snapshot.updated",
[]string{
fmt.Sprintf("FlagID:%d", flagID),
fmt.Sprintf("UpdatedBy:%s", util.SafeStringWithDefault(updatedBy, "null")),
},
float64(1),
)
}
21 changes: 21 additions & 0 deletions pkg/handler/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,34 @@ var logEvalResult = func(r *models.EvalResult, dataRecordsEnabled bool) {
rateLimitPerFlagConsoleLogging(r)
}

logEvalResultToDatadog(r)

if !config.Config.RecorderEnabled || !dataRecordsEnabled {
return
}
rec := GetDataRecorder()
rec.AsyncRecord(r)
}

var logEvalResultToDatadog = func(r *models.EvalResult) {
if config.Global.StatsdClient == nil {
return
}

config.Global.StatsdClient.Incr(
"evaluation",
[]string{
fmt.Sprintf("EntityType:%s", util.SafeStringWithDefault(r.EvalContext.EntityType, "null")),
fmt.Sprintf("FlagID:%d", util.SafeUint(r.FlagID)),
fmt.Sprintf("FlagSnapshotID:%d", util.SafeUint(r.FlagSnapshotID)),
fmt.Sprintf("SegmentID:%d", util.SafeUint(r.SegmentID)),
fmt.Sprintf("VariantID:%d", util.SafeUint(r.VariantID)),
fmt.Sprintf("VariantKey:%s", util.SafeStringWithDefault(r.VariantKey, "null")),
},
float64(1),
)
}

var evalSegment = func(
flagID uint,
evalContext models.EvalContext,
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func NewSecureRandomKey() string {
return randomKeyPrefix + uniuri.NewLenChars(uniuri.StdLen, randomKeyCharset)
}

// SafeStringWithDefault parse an interface to string
// and set it to default value if it's empty
func SafeStringWithDefault(s interface{}, deft string) (ret string) {
ret = SafeString(s)
if ret == "" {
ret = deft
}
return ret
}

// SafeString safely cast to string
func SafeString(s interface{}) (ret string) {
return cast.ToString(s)
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func TestSafeString(t *testing.T) {
assert.Equal(t, SafeString(ptr), "")
}

func TestSafeStringWithDefault(t *testing.T) {
assert.Equal(t, SafeStringWithDefault("123", ""), "123")
assert.Equal(t, SafeStringWithDefault(StringPtr("123"), ""), "123")
assert.Equal(t, SafeStringWithDefault(123, ""), "123")
assert.Equal(t, SafeStringWithDefault(nil, ""), "")

assert.Equal(t, SafeStringWithDefault("123", "<nil>"), "123")
assert.Equal(t, SafeStringWithDefault(StringPtr("123"), "<nil>"), "123")
assert.Equal(t, SafeStringWithDefault(123, "<nil>"), "123")
assert.Equal(t, SafeStringWithDefault(nil, "<nil>"), "<nil>")
}

func TestSafeUint(t *testing.T) {
assert.Equal(t, SafeUint(nil), uint(0))
assert.Equal(t, SafeUint("123"), uint(123))
Expand Down

0 comments on commit 8c5b3a5

Please sign in to comment.