diff --git a/internal/meters/amberflo.go b/meters/amberflo.go similarity index 86% rename from internal/meters/amberflo.go rename to meters/amberflo.go index c5848574..cdbf14ea 100644 --- a/internal/meters/amberflo.go +++ b/meters/amberflo.go @@ -12,7 +12,7 @@ import ( "github.com/xtgo/uuid" ) -type Amberflo struct { +type AmberfloMeterProvider struct { apikey string interval time.Duration client *metering.Metering @@ -20,7 +20,7 @@ type Amberflo struct { 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, @@ -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, @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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{ @@ -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 { diff --git a/internal/meters/amberflo_test.go b/meters/amberflo_test.go similarity index 89% rename from internal/meters/amberflo_test.go rename to meters/amberflo_test.go index 6f69737d..1e7b4cda 100644 --- a/internal/meters/amberflo_test.go +++ b/meters/amberflo_test.go @@ -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", @@ -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 diff --git a/internal/meters/default.go b/meters/default.go similarity index 100% rename from internal/meters/default.go rename to meters/default.go diff --git a/internal/meters/default_test.go b/meters/default_test.go similarity index 100% rename from internal/meters/default_test.go rename to meters/default_test.go diff --git a/internal/meters/limit.go b/meters/limit.go similarity index 92% rename from internal/meters/limit.go rename to meters/limit.go index c3dd91c1..973cb8a4 100644 --- a/internal/meters/limit.go +++ b/meters/limit.go @@ -15,7 +15,7 @@ func init() { type LimitMeterProvider struct { Enabled bool - DefaultLimits []UserMeterLimit + DefaultLimits []userMeterLimit MeterProvider } @@ -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) @@ -74,7 +74,7 @@ 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 @@ -82,7 +82,7 @@ type UserMeterLimit struct { 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 @@ -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(), diff --git a/internal/meters/limit_test.go b/meters/limit_test.go similarity index 94% rename from internal/meters/limit_test.go rename to meters/limit_test.go index efa33a34..77475f3d 100644 --- a/internal/meters/limit_test.go +++ b/meters/limit_test.go @@ -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, @@ -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, @@ -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, @@ -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() diff --git a/internal/meters/meters.go b/meters/meters.go similarity index 100% rename from internal/meters/meters.go rename to meters/meters.go diff --git a/internal/meters/meters_test.go b/meters/meters_test.go similarity index 98% rename from internal/meters/meters_test.go rename to meters/meters_test.go index 96ae6799..1d74f22f 100644 --- a/internal/meters/meters_test.go +++ b/meters/meters_test.go @@ -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) diff --git a/internal/metrics/default.go b/metrics/default.go similarity index 100% rename from internal/metrics/default.go rename to metrics/default.go diff --git a/internal/metrics/http.go b/metrics/http.go similarity index 100% rename from internal/metrics/http.go rename to metrics/http.go diff --git a/internal/metrics/jobs.go b/metrics/jobs.go similarity index 100% rename from internal/metrics/jobs.go rename to metrics/jobs.go diff --git a/internal/metrics/metrics.go b/metrics/metrics.go similarity index 100% rename from internal/metrics/metrics.go rename to metrics/metrics.go diff --git a/internal/metrics/prom.go b/metrics/prom.go similarity index 100% rename from internal/metrics/prom.go rename to metrics/prom.go diff --git a/server/gql/query_resolver.go b/server/gql/query_resolver.go index aed09109..1247cd26 100644 --- a/server/gql/query_resolver.go +++ b/server/gql/query_resolver.go @@ -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" ) diff --git a/server/rest/feed_version_download.go b/server/rest/feed_version_download.go index bf5fdc91..fedf8725 100644 --- a/server/rest/feed_version_download.go +++ b/server/rest/feed_version_download.go @@ -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" ) diff --git a/server/rest/rest.go b/server/rest/rest.go index 8cbee863..d13f3e10 100644 --- a/server/rest/rest.go +++ b/server/rest/rest.go @@ -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" ) diff --git a/server/server_cmd.go b/server/server_cmd.go index fa80054c..58de7a59 100644 --- a/server/server_cmd.go +++ b/server/server_cmd.go @@ -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" @@ -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 @@ -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()