Skip to content

Commit

Permalink
[WIP] Move meters/metrics out of internal
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 4, 2023
1 parent 4a9ad00 commit c20730a
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 35 deletions.
24 changes: 12 additions & 12 deletions internal/meters/amberflo.go → meters/amberflo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"github.com/xtgo/uuid"
)

type Amberflo struct {
type AmberfloMeterProvider struct {
apikey string
interval time.Duration
client *metering.Metering
usageClient *metering.UsageClient
cfgs map[string]amberFloConfig
}

func NewAmberflo(apikey string, interval time.Duration, batchSize int) *Amberflo {
func NewAmberfloMeterProvider(apikey string, interval time.Duration, batchSize int) *AmberfloMeterProvider {
afLog := &amberfloLogger{logger: log.Logger}
meteringClient := metering.NewMeteringClient(
apikey,
Expand All @@ -32,7 +32,7 @@ func NewAmberflo(apikey string, interval time.Duration, batchSize int) *Amberflo
apikey,
metering.WithCustomLogger(afLog),
)
return &Amberflo{
return &AmberfloMeterProvider{
apikey: apikey,
interval: interval,
client: meteringClient,
Expand All @@ -48,7 +48,7 @@ type amberFloConfig struct {
Dimensions Dimensions `json:"dimensions,omitempty"`
}

func (m *Amberflo) LoadConfig(path string) error {
func (m *AmberfloMeterProvider) LoadConfig(path string) error {
cfgs := map[string]amberFloConfig{}
data, err := ioutil.ReadFile(path)
if err != nil {
Expand All @@ -61,24 +61,24 @@ func (m *Amberflo) LoadConfig(path string) error {
return nil
}

func (m *Amberflo) NewMeter(user MeterUser) ApiMeter {
func (m *AmberfloMeterProvider) NewMeter(user MeterUser) ApiMeter {
return &amberFloMeter{
user: user,
mp: m,
}
}

func (m *Amberflo) Close() error {
func (m *AmberfloMeterProvider) Close() error {
return m.client.Shutdown()
}

func (m *Amberflo) Flush() error {
func (m *AmberfloMeterProvider) Flush() error {
// metering.Flush() // in API docs but not in library
time.Sleep(m.interval)
return nil
}

func (m *Amberflo) getValue(user MeterUser, meterName string, startTime time.Time, endTime time.Time, checkDims Dimensions) (float64, bool) {
func (m *AmberfloMeterProvider) getValue(user MeterUser, meterName string, startTime time.Time, endTime time.Time, checkDims Dimensions) (float64, bool) {
cfg, ok := m.getcfg(meterName)
if !ok {
return 0, false
Expand Down Expand Up @@ -138,7 +138,7 @@ func (m *Amberflo) getValue(user MeterUser, meterName string, startTime time.Tim
return total, true
}

func (m *Amberflo) sendMeter(user MeterUser, meterName string, value float64, extraDimensions Dimensions) error {
func (m *AmberfloMeterProvider) sendMeter(user MeterUser, meterName string, value float64, extraDimensions Dimensions) error {
cfg, ok := m.getcfg(meterName)
if !ok {
return nil
Expand Down Expand Up @@ -167,7 +167,7 @@ func (m *Amberflo) sendMeter(user MeterUser, meterName string, value float64, ex
})
}

func (m *Amberflo) getCustomerID(cfg amberFloConfig, user MeterUser) (string, bool) {
func (m *AmberfloMeterProvider) getCustomerID(cfg amberFloConfig, user MeterUser) (string, bool) {
customerId := cfg.DefaultUser
if user != nil {
eidKey := cfg.ExternalIDKey
Expand All @@ -184,7 +184,7 @@ func (m *Amberflo) getCustomerID(cfg amberFloConfig, user MeterUser) (string, bo
return customerId, customerId != ""
}

func (m *Amberflo) getcfg(meterName string) (amberFloConfig, bool) {
func (m *AmberfloMeterProvider) getcfg(meterName string) (amberFloConfig, bool) {
cfg, ok := m.cfgs[meterName]
if !ok {
cfg = amberFloConfig{
Expand All @@ -203,7 +203,7 @@ func (m *Amberflo) getcfg(meterName string) (amberFloConfig, bool) {
type amberFloMeter struct {
user MeterUser
addDims []eventAddDim
mp *Amberflo
mp *AmberfloMeterProvider
}

func (m *amberFloMeter) Meter(meterName string, value float64, extraDimensions Dimensions) error {
Expand Down
4 changes: 2 additions & 2 deletions internal/meters/amberflo_test.go → meters/amberflo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAmberfloMeter(t *testing.T) {
testMeter(t, mp, testConfig)
}

func getTestAmberfloMeter() (*Amberflo, testMeterConfig, error) {
func getTestAmberfloMeter() (*AmberfloMeterProvider, testMeterConfig, error) {
checkKeys := []string{
"TL_TEST_AMBERFLO_APIKEY",
"TL_TEST_AMBERFLO_METER1",
Expand Down Expand Up @@ -50,7 +50,7 @@ func getTestAmberfloMeter() (*Amberflo, testMeterConfig, error) {
data: map[string]string{eidKey: os.Getenv("TL_TEST_AMBERFLO_USER3")},
},
}
mp := NewAmberflo(os.Getenv("TL_TEST_AMBERFLO_APIKEY"), 1*time.Second, 1)
mp := NewAmberfloMeterProvider(os.Getenv("TL_TEST_AMBERFLO_APIKEY"), 1*time.Second, 1)
mp.cfgs[testConfig.testMeter1] = amberFloConfig{Name: testConfig.testMeter1, ExternalIDKey: eidKey}
mp.cfgs[testConfig.testMeter2] = amberFloConfig{Name: testConfig.testMeter2, ExternalIDKey: eidKey}
return mp, testConfig, nil
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions internal/meters/limit.go → meters/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func init() {

type LimitMeterProvider struct {
Enabled bool
DefaultLimits []UserMeterLimit
DefaultLimits []userMeterLimit
MeterProvider
}

Expand All @@ -42,9 +42,9 @@ type LimitMeter struct {
ApiMeter
}

func (c *LimitMeter) GetLimits(meterName string, checkDims Dimensions) []UserMeterLimit {
func (c *LimitMeter) GetLimits(meterName string, checkDims Dimensions) []userMeterLimit {
// The limit matches the event dimensions if all of the LIMIT dimensions are contained in event
var lims []UserMeterLimit
var lims []userMeterLimit
for _, userLimit := range parseGkUserLimits(c.userData) {
if userLimit.MeterName == meterName && dimsContainedIn(userLimit.Dims, checkDims) {
lims = append(lims, userLimit)
Expand Down Expand Up @@ -74,15 +74,15 @@ func (c *LimitMeter) Meter(meterName string, value float64, extraDimensions Dime
return c.ApiMeter.Meter(meterName, value, extraDimensions)
}

type UserMeterLimit struct {
type userMeterLimit struct {
User string
MeterName string
Dims Dimensions
Period string
Limit float64
}

func (lim *UserMeterLimit) Span() (time.Time, time.Time) {
func (lim *userMeterLimit) Span() (time.Time, time.Time) {
now := time.Now().In(time.UTC)
d1 := now
d2 := now
Expand All @@ -107,11 +107,11 @@ func (lim *UserMeterLimit) Span() (time.Time, time.Time) {
return d1, d2
}

func parseGkUserLimits(v string) []UserMeterLimit {
var lims []UserMeterLimit
func parseGkUserLimits(v string) []userMeterLimit {
var lims []userMeterLimit
for _, productLimit := range gjson.Get(v, "product_limits").Map() {
for _, plim := range productLimit.Array() {
lim := UserMeterLimit{
lim := userMeterLimit{
MeterName: plim.Get("amberflo_meter").String(),
Limit: plim.Get("limit_value").Float(),
Period: plim.Get("time_period").String(),
Expand Down
10 changes: 5 additions & 5 deletions internal/meters/limit_test.go → meters/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestLimitMeter(t *testing.T) {
mp := NewDefaultMeterProvider()
cmp := NewLimitMeterProvider(mp)
cmp.Enabled = true
cmp.DefaultLimits = []UserMeterLimit{lim}
cmp.DefaultLimits = []userMeterLimit{lim}
testLimitMeter(t,
cmp,
lim.MeterName,
Expand All @@ -42,7 +42,7 @@ func TestLimitMeter_Amberflo(t *testing.T) {
t.Run("", func(t *testing.T) {
cmp := NewLimitMeterProvider(mp)
cmp.Enabled = true
cmp.DefaultLimits = []UserMeterLimit{lim}
cmp.DefaultLimits = []userMeterLimit{lim}
testLimitMeter(t,
cmp,
lim.MeterName,
Expand Down Expand Up @@ -94,9 +94,9 @@ func TestLimitMeter_Gatekeeper(t *testing.T) {
}
}

func testLims(meterName string) []UserMeterLimit {
func testLims(meterName string) []userMeterLimit {
testKey := 1 // time.Now().In(time.UTC).Unix()
lims := []UserMeterLimit{
lims := []userMeterLimit{
// foo tests
{
MeterName: meterName,
Expand Down Expand Up @@ -139,7 +139,7 @@ func testLims(meterName string) []UserMeterLimit {
return lims
}

func testLimitMeter(t *testing.T, cmp *LimitMeterProvider, meterName string, user testUser, lim UserMeterLimit) {
func testLimitMeter(t *testing.T, cmp *LimitMeterProvider, meterName string, user testUser, lim userMeterLimit) {
incr := 1.0
m := cmp.NewMeter(user)
startTime, endTime := lim.Span()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/meters/meters_test.go → meters/meters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type testMeterConfig struct {
}

func testMeter(t *testing.T, mp MeterProvider, cfg testMeterConfig) {
d1, d2 := (&UserMeterLimit{Period: "hourly"}).Span()
d1, d2 := (&userMeterLimit{Period: "hourly"}).Span()
t.Run("Meter", func(t *testing.T) {
m := mp.NewMeter(cfg.user1)
v, _ := m.GetValue(cfg.testMeter1, d1, d2, nil)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion server/gql/query_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"

"github.com/interline-io/transitland-server/auth/authz"
"github.com/interline-io/transitland-server/internal/meters"
"github.com/interline-io/transitland-server/meters"
"github.com/interline-io/transitland-server/model"
)

Expand Down
2 changes: 1 addition & 1 deletion server/rest/feed_version_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/interline-io/transitland-lib/dmfr/store"
"github.com/interline-io/transitland-lib/tl"
"github.com/interline-io/transitland-lib/tl/request"
"github.com/interline-io/transitland-server/internal/meters"
"github.com/interline-io/transitland-server/internal/util"
"github.com/interline-io/transitland-server/meters"
"github.com/tidwall/gjson"
)

Expand Down
2 changes: 1 addition & 1 deletion server/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/interline-io/transitland-lib/log"
"github.com/interline-io/transitland-server/auth/ancheck"
"github.com/interline-io/transitland-server/config"
"github.com/interline-io/transitland-server/internal/meters"
"github.com/interline-io/transitland-server/internal/util"
"github.com/interline-io/transitland-server/meters"
"github.com/interline-io/transitland-server/model"
)

Expand Down
8 changes: 4 additions & 4 deletions server/server_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
"github.com/interline-io/transitland-server/finders/rtfinder"
"github.com/interline-io/transitland-server/internal/dbutil"
"github.com/interline-io/transitland-server/internal/jobs"
"github.com/interline-io/transitland-server/internal/meters"
"github.com/interline-io/transitland-server/internal/metrics"
"github.com/interline-io/transitland-server/internal/playground"
"github.com/interline-io/transitland-server/meters"
"github.com/interline-io/transitland-server/metrics"
"github.com/interline-io/transitland-server/model"
"github.com/interline-io/transitland-server/server/gql"
"github.com/interline-io/transitland-server/server/rest"
Expand Down Expand Up @@ -238,7 +238,7 @@ func (cmd *Command) Run() error {
meterProvider = meters.NewDefaultMeterProvider()
if cmd.metersConfig.EnableMetering {
if cmd.metersConfig.MeteringProvider == "amberflo" {
a := meters.NewAmberflo(os.Getenv("AMBERFLO_APIKEY"), 30*time.Second, 100)
a := meters.NewAmberfloMeterProvider(os.Getenv("AMBERFLO_APIKEY"), 30*time.Second, 100)
if cmd.metersConfig.MeteringAmberfloConfig != "" {
if err := a.LoadConfig(cmd.metersConfig.MeteringAmberfloConfig); err != nil {
return err
Expand All @@ -249,7 +249,7 @@ func (cmd *Command) Run() error {
if cmd.EnableRateLimits {
mp := meters.NewLimitMeterProvider(meterProvider)
mp.Enabled = true
// mp.DefaultLimits = append(mp.DefaultLimits, meters.UserMeterLimit{Limit: 10, Period: "monthly", MeterName: "rest"})
// mp.DefaultLimits = append(mp.DefaultLimits, meters.userMeterLimit{Limit: 10, Period: "monthly", MeterName: "rest"})
meterProvider = mp
}
defer meterProvider.Close()
Expand Down

0 comments on commit c20730a

Please sign in to comment.