-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from porters-xyz/poktscan-metrics-endpoint
Poktscan metrics endpoint
- Loading branch information
Showing
8 changed files
with
151 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,115 @@ | ||
package common | ||
|
||
import ( | ||
log "log/slog" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
log "log/slog" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// store config as constants for now | ||
const ( | ||
SHUTDOWN_DELAY string = "SHUTDOWN_DELAY" | ||
JOB_BUFFER_SIZE = "JOB_BUFFER_SIZE" | ||
NUM_WORKERS = "NUM_WORKERS" | ||
PROXY_TO = "PROXY_TO" | ||
HOST = "HOST" // host to add subdomains to | ||
PORT = "PORT" | ||
DATABASE_URL = "DATABASE_URL" | ||
REDIS_URL = "REDIS_URL" | ||
REDIS_ADDR = "REDIS_ADDR" | ||
REDIS_USER = "REDIS_USER" | ||
REDIS_PASSWORD = "REDIS_PASSWORD" | ||
INSTRUMENT_ENABLED = "ENABLE_INSTRUMENT" | ||
LOG_LEVEL = "LOG_LEVEL" | ||
SHUTDOWN_DELAY string = "SHUTDOWN_DELAY" | ||
JOB_BUFFER_SIZE = "JOB_BUFFER_SIZE" | ||
NUM_WORKERS = "NUM_WORKERS" | ||
PROXY_TO = "PROXY_TO" | ||
HOST = "HOST" // host to add subdomains to | ||
PORT = "PORT" | ||
DATABASE_URL = "DATABASE_URL" | ||
REDIS_URL = "REDIS_URL" | ||
REDIS_ADDR = "REDIS_ADDR" | ||
REDIS_USER = "REDIS_USER" | ||
REDIS_PASSWORD = "REDIS_PASSWORD" | ||
INSTRUMENT_ENABLED = "ENABLE_INSTRUMENT" | ||
LOG_LEVEL = "LOG_LEVEL" | ||
LOG_HTTP_RESPONSE = "LOG_HTTP_RESPONSE" | ||
) | ||
|
||
// This may evolve to include config outside env, or use .env file for | ||
// convenience | ||
type Config struct { | ||
defaults map[string]string | ||
loglevel *log.LevelVar | ||
defaults map[string]string | ||
loglevel *log.LevelVar | ||
} | ||
|
||
var config *Config | ||
var configMutex sync.Once | ||
|
||
func setupConfig() *Config { | ||
configMutex.Do(func() { | ||
config = &Config{ | ||
defaults: make(map[string]string), | ||
loglevel: &log.LevelVar{}, | ||
} | ||
config.defaults[SHUTDOWN_DELAY] = "5" | ||
config.defaults[JOB_BUFFER_SIZE] = "50" | ||
config.defaults[NUM_WORKERS] = "10" | ||
config.defaults[HOST] = "localhost" | ||
config.defaults[PORT] = "9000" | ||
config.defaults[INSTRUMENT_ENABLED] = "false" | ||
configMutex.Do(func() { | ||
config = &Config{ | ||
defaults: make(map[string]string), | ||
loglevel: &log.LevelVar{}, | ||
} | ||
config.defaults[SHUTDOWN_DELAY] = "5" | ||
config.defaults[JOB_BUFFER_SIZE] = "50" | ||
config.defaults[NUM_WORKERS] = "10" | ||
config.defaults[HOST] = "localhost" | ||
config.defaults[PORT] = "9000" | ||
config.defaults[INSTRUMENT_ENABLED] = "false" | ||
config.defaults[LOG_HTTP_RESPONSE] = "true" | ||
|
||
level := parseLogLevel(os.Getenv(LOG_LEVEL)) | ||
config.SetLogLevel(level) | ||
}) | ||
return config | ||
level := parseLogLevel(os.Getenv(LOG_LEVEL)) | ||
config.SetLogLevel(level) | ||
}) | ||
return config | ||
} | ||
|
||
func GetConfig(key string) string { | ||
config := setupConfig() | ||
value, ok := os.LookupEnv(key) | ||
if ok { | ||
return value | ||
} else { | ||
defaultval, ok := config.defaults[key] | ||
if ok { | ||
return defaultval | ||
} else { | ||
log.Warn("config not set, no default", "key", key) | ||
return "" | ||
} | ||
} | ||
config := setupConfig() | ||
value, ok := os.LookupEnv(key) | ||
if ok { | ||
return value | ||
} else { | ||
defaultval, ok := config.defaults[key] | ||
if ok { | ||
return defaultval | ||
} else { | ||
log.Warn("config not set, no default", "key", key) | ||
return "" | ||
} | ||
} | ||
} | ||
|
||
func GetConfigInt(key string) int { | ||
configval := GetConfig(key) | ||
intval, err := strconv.Atoi(configval) | ||
if err != nil { | ||
log.Error("Error parsing config", "err", err) | ||
intval = -1 | ||
} | ||
return intval | ||
configval := GetConfig(key) | ||
intval, err := strconv.Atoi(configval) | ||
if err != nil { | ||
log.Error("Error parsing config", "err", err) | ||
intval = -1 | ||
} | ||
return intval | ||
} | ||
|
||
func Enabled(key string) bool { | ||
configval := GetConfig(key) | ||
boolval, err := strconv.ParseBool(configval) | ||
if err != nil { | ||
boolval = false | ||
} | ||
return boolval | ||
configval := GetConfig(key) | ||
boolval, err := strconv.ParseBool(configval) | ||
if err != nil { | ||
boolval = false | ||
} | ||
return boolval | ||
} | ||
|
||
func GetLogLevel() log.Level { | ||
configval := GetConfig(LOG_LEVEL) | ||
return parseLogLevel(configval) | ||
configval := GetConfig(LOG_LEVEL) | ||
return parseLogLevel(configval) | ||
} | ||
|
||
func parseLogLevel(level string) log.Level { | ||
if strings.EqualFold(level, "ERROR") { | ||
return log.LevelError | ||
} else if strings.EqualFold(level, "WARN") { | ||
return log.LevelWarn | ||
} else if strings.EqualFold(level, "DEBUG") { | ||
return log.LevelDebug | ||
} else { | ||
return log.LevelInfo | ||
} | ||
if strings.EqualFold(level, "ERROR") { | ||
return log.LevelError | ||
} else if strings.EqualFold(level, "WARN") { | ||
return log.LevelWarn | ||
} else if strings.EqualFold(level, "DEBUG") { | ||
return log.LevelDebug | ||
} else { | ||
return log.LevelInfo | ||
} | ||
} | ||
|
||
func (c *Config) SetLogLevel(level log.Level) { | ||
c.loglevel.Set(level) | ||
logger := log.New(log.NewTextHandler(os.Stdout, &log.HandlerOptions{Level: c.loglevel})) | ||
log.SetDefault(logger) | ||
c.loglevel.Set(level) | ||
logger := log.New(log.NewTextHandler(os.Stdout, &log.HandlerOptions{Level: c.loglevel})) | ||
log.SetDefault(logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"os" | ||
|
||
"porters/plugins" | ||
"porters/proxy" | ||
"porters/plugins" | ||
"porters/proxy" | ||
) | ||
|
||
// command line runner | ||
func main() { | ||
|
||
arg := os.Args[1] | ||
if arg == "gateway" { | ||
arg := os.Args[1] | ||
if arg == "gateway" { | ||
|
||
// currently registering plugins via main | ||
proxy.Register(&plugins.ApiKeyAuth{"X-API"}) | ||
proxy.Register(&plugins.BalanceTracker{}) | ||
proxy.Register(&plugins.LeakyBucketPlugin{"APP"}) | ||
proxy.Register(&plugins.ProductFilter{}) | ||
proxy.Register(&plugins.UserAgentFilter{}) | ||
proxy.Register(&plugins.AllowedOriginFilter{}) | ||
proxy.Register(proxy.NewReconciler(300)) // seconds | ||
// currently registering plugins via main | ||
proxy.Register(&plugins.ApiKeyAuth{"X-API"}) | ||
proxy.Register(&plugins.BalanceTracker{}) | ||
proxy.Register(&plugins.LeakyBucketPlugin{"APP"}) | ||
proxy.Register(&plugins.ProductFilter{}) | ||
proxy.Register(&plugins.UserAgentFilter{}) | ||
proxy.Register(&plugins.AllowedOriginFilter{}) | ||
proxy.Register(proxy.NewReconciler(300)) // seconds | ||
|
||
gateway() | ||
} | ||
gateway() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.