Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add log support for sentry/syslog/rsyslog #112

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/fabiocicerchia/go-proxy-cache/cache/engine"
"github.com/fabiocicerchia/go-proxy-cache/logger"
"github.com/fabiocicerchia/go-proxy-cache/utils"
"github.com/fabiocicerchia/go-proxy-cache/utils/slice"
)
Expand Down Expand Up @@ -136,7 +137,7 @@ func (c Object) handleMetadata(domainID string, targetURL url.URL, expiration ti
// StoreFullPage - Stores the whole page response in cache.
func (c Object) StoreFullPage(expiration time.Duration) (bool, error) {
if !c.IsStatusAllowed() || !c.IsMethodAllowed() || expiration < 1 {
log.WithFields(log.Fields{
logger.GetGlobal().WithFields(log.Fields{
"ReqID": c.ReqID,
}).Debugf(
"Not allowed to be stored. Status: %v - Method: %v - Expiration: %v",
Expand Down Expand Up @@ -195,7 +196,7 @@ func (c *Object) RetrieveFullPage() error {
}

key := StorageKey(c.CurrentURIObject, meta)
log.WithFields(log.Fields{
logger.GetGlobal().WithFields(log.Fields{
"ReqID": c.ReqID,
}).Debugf("StorageKey: %s", key)

Expand Down
18 changes: 9 additions & 9 deletions cache/engine/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (rdb *RedisClient) unlock(key string) error {

// PurgeAll - Purges all the existing keys on a DB.
func (rdb *RedisClient) PurgeAll() (bool, error) {
_, err := circuitbreaker.CB(rdb.Name).Execute(func() (interface{}, error) {
_, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(func() (interface{}, error) {
err := rdb.Client.FlushDB(ctx).Err()
return nil, err
})
Expand All @@ -101,7 +101,7 @@ func (rdb *RedisClient) PurgeAll() (bool, error) {

// Ping - Tests the connection.
func (rdb *RedisClient) Ping() bool {
_, err := circuitbreaker.CB(rdb.Name).Execute(func() (interface{}, error) {
_, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(func() (interface{}, error) {
err := rdb.Client.Ping(ctx).Err()
return nil, err
})
Expand All @@ -111,7 +111,7 @@ func (rdb *RedisClient) Ping() bool {

// Set - Sets a key, with certain value, with TTL for expiring (soft and hard eviction).
func (rdb *RedisClient) Set(key string, value string, expiration time.Duration) (bool, error) {
_, err := circuitbreaker.CB(rdb.Name).Execute(rdb.doSet(key, value, expiration))
_, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(rdb.doSet(key, value, expiration))

return err == nil, err
}
Expand All @@ -134,7 +134,7 @@ func (rdb *RedisClient) doSet(key string, value string, expiration time.Duration

// Get - Gets a key.
func (rdb *RedisClient) Get(key string) (string, error) {
value, err := circuitbreaker.CB(rdb.Name).Execute(func() (interface{}, error) {
value, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(func() (interface{}, error) {
value, err := rdb.Client.Get(ctx, key).Result()
if value == "" && err != nil && err.Error() == "redis: nil" {
return value, nil
Expand All @@ -155,7 +155,7 @@ func (rdb *RedisClient) Del(key string) error {

// DelWildcard - Removes the matching keys based on a pattern.
func (rdb *RedisClient) DelWildcard(key string) (int, error) {
k, err := circuitbreaker.CB(rdb.Name).Execute(func() (interface{}, error) {
k, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(func() (interface{}, error) {
keys, err := rdb.Client.Keys(ctx, key).Result()
return keys, err
})
Expand All @@ -175,7 +175,7 @@ func (rdb *RedisClient) deleteKeys(keyID string, keys []string) (int, error) {
return 0, nil
}

_, errDel := circuitbreaker.CB(rdb.Name).Execute(rdb.doDeleteKeys(keyID, keys))
_, errDel := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(rdb.doDeleteKeys(keyID, keys))

return l, errDel
}
Expand All @@ -198,7 +198,7 @@ func (rdb *RedisClient) doDeleteKeys(keyID string, keys []string) func() (interf

// List - Returns the values in a list.
func (rdb *RedisClient) List(key string) ([]string, error) {
value, err := circuitbreaker.CB(rdb.Name).Execute(func() (interface{}, error) {
value, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(func() (interface{}, error) {
value, err := rdb.Client.LRange(ctx, key, 0, -1).Result()
return value, err
})
Expand All @@ -212,7 +212,7 @@ func (rdb *RedisClient) List(key string) ([]string, error) {

// Push - Append values to a list.
func (rdb *RedisClient) Push(key string, values []string) error {
_, err := circuitbreaker.CB(rdb.Name).Execute(rdb.doPushKey(key, values))
_, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(rdb.doPushKey(key, values))

return err
}
Expand All @@ -235,7 +235,7 @@ func (rdb *RedisClient) doPushKey(key string, values []string) func() (interface

// Expire - Sets a TTL on a key (hard eviction only).
func (rdb *RedisClient) Expire(key string, expiration time.Duration) error {
_, err := circuitbreaker.CB(rdb.Name).Execute(func() (interface{}, error) {
_, err := circuitbreaker.CB(rdb.Name, rdb.logger).Execute(func() (interface{}, error) {
err := rdb.Client.Expire(ctx, key, expiration).Err()
return nil, err
})
Expand Down
31 changes: 16 additions & 15 deletions cache/engine/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/fabiocicerchia/go-proxy-cache/cache/engine/client"
"github.com/fabiocicerchia/go-proxy-cache/config"
"github.com/fabiocicerchia/go-proxy-cache/logger"
"github.com/fabiocicerchia/go-proxy-cache/utils"
circuit_breaker "github.com/fabiocicerchia/go-proxy-cache/utils/circuit-breaker"
)
Expand Down Expand Up @@ -56,27 +57,27 @@ func TestCircuitBreakerWithPingTimeout(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

assert.Equal(t, "closed", circuit_breaker.CB(redisConnName).State().String())
assert.Equal(t, "closed", circuit_breaker.CB(redisConnName, logger.GetGlobal()).State().String())

val := rdb.Ping()
assert.True(t, val)
assert.Equal(t, "closed", circuit_breaker.CB(redisConnName).State().String())
assert.Equal(t, "closed", circuit_breaker.CB(redisConnName, logger.GetGlobal()).State().String())

_ = rdb.Close()

val = rdb.Ping()
assert.False(t, val)
assert.Equal(t, "half-open", circuit_breaker.CB(redisConnName).State().String())
assert.Equal(t, "half-open", circuit_breaker.CB(redisConnName, logger.GetGlobal()).State().String())

rdb = client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

val = rdb.Ping()
assert.True(t, val)
assert.Equal(t, "closed", circuit_breaker.CB(redisConnName).State().String())
assert.Equal(t, "closed", circuit_breaker.CB(redisConnName, logger.GetGlobal()).State().String())
}

func TestClose(t *testing.T) {
Expand All @@ -96,7 +97,7 @@ func TestClose(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -124,7 +125,7 @@ func TestSetGet(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -154,7 +155,7 @@ func TestSetGetWithExpiration(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -186,7 +187,7 @@ func TestDel(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -223,7 +224,7 @@ func TestExpire(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -259,7 +260,7 @@ func TestPushList(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -288,7 +289,7 @@ func TestDelWildcardNoMatch(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -344,7 +345,7 @@ func TestDelWildcard(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -400,7 +401,7 @@ func TestPurgeAll(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down Expand Up @@ -456,7 +457,7 @@ func TestEncodeDecode(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker)
circuit_breaker.InitCircuitBreaker(redisConnName, cfg.CircuitBreaker, logger.GetGlobal())

rdb := client.Connect(redisConnName, cfg.Cache, log.StandardLogger())

Expand Down
3 changes: 2 additions & 1 deletion cache/engine/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/fabiocicerchia/go-proxy-cache/cache/engine/client"
"github.com/fabiocicerchia/go-proxy-cache/config"
"github.com/fabiocicerchia/go-proxy-cache/logger"
)

var rdb map[string]*client.RedisClient
Expand All @@ -24,7 +25,7 @@ func GetConn(connName string) *client.RedisClient {
return conn
}

log.Errorf("Missing redis connection for %s", connName)
logger.GetGlobal().Errorf("Missing redis connection for %s", connName)

return nil
}
Expand Down
45 changes: 32 additions & 13 deletions config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ server:
# Values: http, https, ws, wss.
scheme: ~
# Load Balancing Algorithm to be used when present multiple endpoints.
# Allowed formats: ip-hash, least-connections, random, round-robin (default).
# Default: round-robin
# Allowed formats: ip-hash, least-connections, random, round-robin.
balancing_algorithm: round-robin
# List of IPs/Hostnames to be used as load balanced backend servers.
# They'll be selected using the chosen algorithm (or round-robin).
Expand Down Expand Up @@ -252,19 +253,37 @@ log:
# Ref: https://golang.org/src/time/format.go
time_format: "2006/01/02 15:04:05"
# Available variables:
# - $host = request host
# - $remote_addr = request remote addr
# - $remote_user = -
# - $time_local = local time with format of `time_format`
# - $protocol = protocol
# - $request_method = method
# - $request = full URI
# - $status = status code
# - $body_bytes_sent = number of bytes in content body
# - $http_referer = referer
# - $http_user_agent = user agent
# - $cached_status = cache flag
# - $host = request host
# - $remote_addr = request remote addr
# - $remote_user = -
# - $time_local = local time with format of `time_format`
# - $protocol = protocol
# - $request_method = method
# - $request = full URI
# - $status = status code
# - $body_bytes_sent = number of bytes in content body
# - $http_referer = referer
# - $http_user_agent = user agent
# - $cached_status = cache flag
# - $cached_status_label = cached flag label (HIT/MISS/STALE)
format: $host - $remote_addr - $remote_user $protocol $request_method "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $cached_status
# Sentry DSN
# All errors will be tracked and sent to Sentry.
# Logged levels: warning, error, fatal, panic.
# Default (empty)
sentry_dsn: ~
# Syslog Protocol
# If no protocol and no endpoint is specified, all the logs will be sent to
# the local syslog.
# Default (empty)
syslog_protocol: ~
# Syslog Endpoint
# If no protocol and no endpoint is specified, all the logs will be sent to
# the local syslog.
# All errors will be tracked and sent to syslog.
# Logged levels: warning, error, critical, alert, emergency.
# Default (empty)
syslog_endpoint: ~

# --- TRACING
tracing:
Expand Down
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (c *Configuration) CopyOverWith(overrides Configuration, file *string) {
c.copyOverWithUpstream(overrides.Server)
c.copyOverWithCache(overrides.Cache)
c.copyOverWithTracing(overrides.Tracing)
c.copyOverWithLog(overrides.Log)
}

// --- SERVER.
Expand Down Expand Up @@ -205,6 +206,13 @@ func (c *Configuration) copyOverWithTracing(overrides Tracing) {
c.Tracing.Enabled = utils.Coalesce(overrides.Enabled, c.Tracing.Enabled).(bool)
}

// --- LOG.
func (c *Configuration) copyOverWithLog(overrides Log) {
c.Log.SentryDsn = utils.Coalesce(overrides.SentryDsn, c.Log.SentryDsn).(string)
c.Log.SyslogProtocol = utils.Coalesce(overrides.SyslogProtocol, c.Log.SyslogProtocol).(string)
c.Log.SyslogEndpoint = utils.Coalesce(overrides.SyslogEndpoint, c.Log.SyslogEndpoint).(string)
}

// Print - Shows the current configuration.
func Print() {
obfuscatedConfig := Configuration{}
Expand Down
7 changes: 5 additions & 2 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ type Cache struct {

// Log - Defines the config for the logs.
type Log struct {
TimeFormat string `yaml:"time_format"`
Format string `yaml:"format"`
TimeFormat string `yaml:"time_format"`
Format string `yaml:"format"`
SentryDsn string `yaml:"sentry_dsn" envconfig:"SENTRY_DSN"`
SyslogProtocol string `yaml:"syslog_protocol" envconfig:"SYSLOG_PROTOCOL"`
SyslogEndpoint string `yaml:"syslog_endpoint" envconfig:"SYSLOG_ENDPOINT"`
}

// Tracing - Defines the config for the OpenTelemetry tracing.
Expand Down
Loading