Skip to content

Commit

Permalink
Merge branch 'main' into rt-save-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 5, 2023
2 parents 24c115c + 4961d21 commit f07f134
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 89 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ Usage: server
- open-source for use by all under the [GPLv3](LICENSE) license
- also available under a flexible commercial license from [Interline](mailto:[email protected])


8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,6 @@ github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231122224424-d95957e76b4e h1:y24HWm0yVrlwE9a8oM+vKLDDW5F/zco/lFB+qX9Fx3I=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231122224424-d95957e76b4e/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231130233906-2adb3bfd1d44 h1:kRFPzcrs2SihxsTu+hOVTCRIICw6p+A2HlR9LzvXX6w=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231130233906-2adb3bfd1d44/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201022717-a7acfe4abe4b h1:2O4SNVGuxBCQDwxC1oyiJSTWmfLytQJwO9ZokE1n5Yg=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201022717-a7acfe4abe4b/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d h1:0Tfw/JoAkRvDQvuJUhhJALVTl16zTvMd3dGUPecPJUw=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7 h1:rwkKzYzl05Q4TM++L9RIJbXPjIvURoMN5vEr8dvUV/Q=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202021452-9af1eb605dd8 h1:2TYv/zFgZgjUU5uoNlu7chu+laGVwadHG3xA7/WnOcE=
Expand Down
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.
File renamed without changes.
File renamed without changes.
36 changes: 35 additions & 1 deletion internal/meters/meters.go → meters/meters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meters
import (
"context"
"net/http"
"os"
"time"

"github.com/interline-io/transitland-server/auth/authn"
Expand Down Expand Up @@ -34,8 +35,11 @@ func WithMeter(apiMeter MeterProvider, meterName string, meterValue float64, dim
ctx := r.Context()
ctxMeter := apiMeter.NewMeter(authn.ForContext(ctx))
r = r.WithContext(context.WithValue(ctx, meterCtxKey, ctxMeter))
if err := ctxMeter.Meter(meterName, meterValue, dims); err != nil {
http.Error(w, "429", http.StatusTooManyRequests)
return
}
next.ServeHTTP(w, r)
ctxMeter.Meter(meterName, meterValue, dims)
})
}
}
Expand Down Expand Up @@ -72,3 +76,33 @@ func dimsContainedIn(checkDims Dimensions, eventDims Dimensions) bool {
}
return true
}

//////

type Config struct {
EnableMetering bool
EnableRateLimits bool
MeteringProvider string
MeteringAmberfloConfig string
}

func GetProvider(cfg Config) (MeterProvider, error) {
var meterProvider MeterProvider
meterProvider = NewDefaultMeterProvider()
if cfg.MeteringProvider == "amberflo" {
a := NewAmberfloMeterProvider(os.Getenv("AMBERFLO_APIKEY"), 30*time.Second, 100)
if cfg.MeteringAmberfloConfig != "" {
if err := a.LoadConfig(cfg.MeteringAmberfloConfig); err != nil {
return nil, err
}
}
meterProvider = a
}
if cfg.EnableRateLimits {
mp := NewLimitMeterProvider(meterProvider)
mp.Enabled = true
// mp.DefaultLimits = append(mp.DefaultLimits, meters.UserMeterLimit{Limit: 10, Period: "monthly", MeterName: "rest"})
meterProvider = mp
}
return meterProvider, nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions internal/metrics/metrics.go → metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ type MetricProvider interface {
NewJobMetric(queue string) JobMetric
MetricsHandler() http.Handler
}

type Config struct {
EnableMetrics bool
MetricsProvider string
}

func GetProvider(cfg Config) (MetricProvider, error) {
var metricProvider MetricProvider
metricProvider = NewDefaultMetric()
if cfg.MetricsProvider == "prometheus" {
metricProvider = NewPromMetrics()
}
return metricProvider, nil
}
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
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// log request and duration
func loggingMiddleware(longQueryDuration int) func(http.Handler) http.Handler {
func LoggingMiddleware(longQueryDuration int) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
Loading

0 comments on commit f07f134

Please sign in to comment.