Skip to content

Commit

Permalink
Replace godspeed with datadog-go
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekempf-vsco committed Mar 14, 2024
1 parent 0c6d225 commit 12591e5
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 123 deletions.
52 changes: 6 additions & 46 deletions cli/api/client.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"encoding/json"

"time"

"github.com/theckman/godspeed"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/vsco/dcdr/cli/api/ioutil2"
"github.com/vsco/dcdr/cli/api/stores"
"github.com/vsco/dcdr/cli/printer"
Expand Down Expand Up @@ -46,11 +44,11 @@ type ClientIFace interface {
type Client struct {
Store stores.IFace
Repo repo.IFace
Stats *godspeed.Godspeed
Stats statsd.ClientInterface
config *config.Config
}

func New(st stores.IFace, rp repo.IFace, cfg *config.Config, stats *godspeed.Godspeed) (c *Client) {
func New(st stores.IFace, rp repo.IFace, cfg *config.Config, stats statsd.ClientInterface) (c *Client) {
c = &Client{
Store: st,
Repo: rp,
Expand Down Expand Up @@ -143,15 +141,7 @@ func (c *Client) Set(ft *models.Feature) error {
return err
}

err = c.Store.Set(ft.ScopedKey(), bts)

if err != nil {
return err
}

err = c.SendStatEvent(ft, false)

return nil
return c.Store.Set(ft.ScopedKey(), bts)
}

func (c *Client) Get(key string, v interface{}) error {
Expand Down Expand Up @@ -196,15 +186,7 @@ func (c *Client) Delete(key string, scope string) error {
return KeyNotFoundError(key)
}

err = c.Store.Delete(key)

if err != nil {
return err
}

err = c.SendStatEvent(existing, true)

return err
return c.Store.Delete(key)
}

func (c *Client) Commit(ft *models.Feature, deleted bool) error {
Expand Down Expand Up @@ -348,28 +330,6 @@ func (c *Client) WriteOutputFile(kvb stores.KVBytes) {
printer.Logf("wrote changes to: %s", c.config.Watcher.OutputPath)
}

func (c *Client) SendStatEvent(f *models.Feature, delete bool) error {
if c.Stats == nil {
return nil
}

var text string
title := "Decider Change"

if delete {
text = fmt.Sprintf("deleted %s", f.ScopedKey())
} else {
text = fmt.Sprintf("set %s: %v", f.ScopedKey(), f.Value)
}

optionals := make(map[string]string)
optionals["alert_type"] = "info"
optionals["source_type_name"] = "dcdr"
tags := []string{"source_type:dcdr"}

return c.Stats.Event(title, text, optionals, tags)
}

// KVsToFeatures helper for unmarshalling `KVBytes` to a `FeatureMap`
func (c *Client) KVsToFeatureMap(kvb stores.KVBytes) (*models.FeatureMap, error) {
fm := models.EmptyFeatureMap()
Expand Down
28 changes: 0 additions & 28 deletions client/stats/godspeed/godspeed_statter.go

This file was deleted.

12 changes: 0 additions & 12 deletions client/stats/stats.go

This file was deleted.

18 changes: 9 additions & 9 deletions client/stats_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package client
import (
"strings"

"github.com/vsco/dcdr/client/stats"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/vsco/dcdr/config"
"github.com/vsco/dcdr/models"
)

// StatsClient delegates `Client` methods with metrics.
type StatsClient struct {
Client
stats stats.IFace
stats statsd.ClientInterface
}

// NewStatsClient creates a new client.
func NewStatsClient(cfg *config.Config, stats stats.IFace) (sc *StatsClient, err error) {
func NewStatsClient(cfg *config.Config, stats statsd.ClientInterface) (sc *StatsClient, err error) {
sc = &StatsClient{
stats: stats,
}
Expand All @@ -29,7 +29,7 @@ func NewStatsClient(cfg *config.Config, stats stats.IFace) (sc *StatsClient, err
}

// NewStatsClient creates a new client.
func NewStatsDefault(stats stats.IFace) (sc *StatsClient, err error) {
func NewStatsDefault(stats statsd.ClientInterface) (sc *StatsClient, err error) {
sc = &StatsClient{
stats: stats,
}
Expand Down Expand Up @@ -90,21 +90,21 @@ func (sc *StatsClient) Scopes() []string {
// Incr increments the formatted `statKey`.
func (sc *StatsClient) Incr(feature string, enabled bool, sampleRate float64) {
key := sc.statKey(feature, enabled)
sc.stats.Incr(key, sampleRate)
sc.stats.Incr(key, []string{}, sampleRate)
}

func (sc *StatsClient) statKey(feature string, enabled bool) string {
status := stats.Enabled
status := "enabled"

if enabled == false {
status = stats.Disabled
status = "disabled"
}

scopes := models.DefaultScope

if len(sc.Client.Scopes()) > 0 {
scopes = strings.Replace(strings.Join(sc.Client.Scopes(), stats.JoinWith), "/", stats.JoinWith, -1)
scopes = strings.Replace(strings.Join(sc.Client.Scopes(), "."), "/", ".", -1)
}

return strings.Join([]string{sc.config.Namespace, scopes, feature, status}, stats.JoinWith)
return strings.Join([]string{sc.config.Namespace, scopes, feature, status}, ".")
}
116 changes: 102 additions & 14 deletions client/stats_client_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,119 @@
package client

import (
"testing"

"strings"
"testing"
"time"

"github.com/DataDog/datadog-go/v5/statsd"
"github.com/stretchr/testify/assert"
"github.com/vsco/dcdr/client/stats"
"github.com/vsco/dcdr/config"
"github.com/vsco/dcdr/models"
)

type MockStatter struct {
Count map[string]int
count map[string]int
}

func (ms *MockStatter) Gauge(name string, value float64, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) GaugeWithTimestamp(name string, value float64, tags []string, rate float64, timestamp time.Time) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Count(name string, value int64, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) CountWithTimestamp(name string, value int64, tags []string, rate float64, timestamp time.Time) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Histogram(name string, value float64, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Distribution(name string, value float64, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Decr(name string, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Incr(name string, tags []string, rate float64) error {
//TODO implement me
ms.count[name]++
return nil
}

func (ms *MockStatter) Set(name string, value string, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Timing(name string, value time.Duration, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) TimeInMilliseconds(name string, value float64, tags []string, rate float64) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Event(e *statsd.Event) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) SimpleEvent(title, text string) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) ServiceCheck(sc *statsd.ServiceCheck) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) SimpleServiceCheck(name string, status statsd.ServiceCheckStatus) error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Close() error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Flush() error {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Incr(key string, sampleRate float64) {
ms.Count[key]++
func (ms *MockStatter) IsClosed() bool {
//TODO implement me
panic("implement me")
}

func (ms *MockStatter) Tags() []string {
return []string{"tag"}
func (ms *MockStatter) GetTelemetry() statsd.Telemetry {
//TODO implement me
panic("implement me")
}

func NewMockStatter() (ms *MockStatter) {
ms = &MockStatter{
Count: make(map[string]int),
count: make(map[string]int),
}

return
Expand All @@ -39,7 +127,7 @@ func TestStatsClientIsAvailable(t *testing.T) {

enabled := c.IsAvailable(ft)
key := c.statKey(ft, enabled)
assert.Equal(t, 1, ms.Count[key])
assert.Equal(t, 1, ms.count[key])
}

func TestStatsClientIsAvailableForID(t *testing.T) {
Expand All @@ -50,7 +138,7 @@ func TestStatsClientIsAvailableForID(t *testing.T) {

enabled := c.IsAvailableForID(ft, 1)
key := c.statKey(ft, enabled)
assert.Equal(t, 1, ms.Count[key])
assert.Equal(t, 1, ms.count[key])
}

func TestFormatKey(t *testing.T) {
Expand All @@ -62,14 +150,14 @@ func TestFormatKey(t *testing.T) {
c, err := NewStatsClient(cfg, ms)
assert.NoError(t, err)

expected := strings.Join([]string{cfg.Namespace, models.DefaultScope, ft, stats.Enabled}, stats.JoinWith)
expected := strings.Join([]string{cfg.Namespace, models.DefaultScope, ft, "enabled"}, ".")
assert.Equal(t, expected, c.statKey(ft, true))

c.scopes = []string{"a", "b/c"}

expected = strings.Join([]string{cfg.Namespace, "a.b.c", ft, stats.Enabled}, stats.JoinWith)
expected = strings.Join([]string{cfg.Namespace, "a.b.c", ft, "enabled"}, ".")
assert.Equal(t, expected, c.statKey(ft, true))

expected = strings.Join([]string{cfg.Namespace, "a.b.c", ft, stats.Disabled}, stats.JoinWith)
expected = strings.Join([]string{cfg.Namespace, "a.b.c", ft, "disabled"}, ".")
assert.Equal(t, expected, c.statKey(ft, false))
}
14 changes: 9 additions & 5 deletions dcdr.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"fmt"
"os"

"github.com/theckman/godspeed"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/vsco/dcdr/cli"
"github.com/vsco/dcdr/cli/api"
"github.com/vsco/dcdr/cli/api/resolver"
Expand All @@ -19,18 +20,21 @@ func main() {

rp := repo.New(cfg)

var gs *godspeed.Godspeed
var err error
var stats statsd.ClientInterface

if cfg.StatsEnabled() {
gs, err = godspeed.New(cfg.Stats.Host, cfg.Stats.Port, false)
var err error
stats, err = statsd.New(fmt.Sprintf("%s:%d", cfg.Stats.Host, cfg.Stats.Port))

if err != nil {
printer.SayErr("%v", err)
os.Exit(1)
}
} else {
stats = &statsd.NoOpClient{}
}

kv := api.New(store, rp, cfg, gs)
kv := api.New(store, rp, cfg, stats)
ctrl := controller.New(cfg, kv)

dcdr := cli.New(ctrl)
Expand Down
Loading

0 comments on commit 12591e5

Please sign in to comment.