util

package
v1.42.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EelFile                 = "mascot/eel.txt"
	EelConfigFile           = "config-eel/config.json"
	DefaultConfigFolder     = "config-handlers"
	Eel1MinStats            = "Eel.Stats.1Min"
	Eel5MinStats            = "Eel.Stats.5Min"
	Eel1hrStats             = "Eel.Stats.1hr"
	Eel24hrStats            = "Eel.Stats.24hr"
	EelTotalStats           = "Eel.TotalStats"
	EelPathWhiteList        = "Eel.PathWhiteList"
	EelDispatcher           = "Eel.Dispatcher"
	EelDuplicateChecker     = "Eel.DuplicateChecker"
	EelStartTime            = "StartTime"
	EelConfig               = "Eel.Settings"
	EelHandlerFactory       = "HandlerFactory"
	EelObserver             = "Observer"
	EelHttpClient           = "Eel.HttpClient"
	EelHttpTransport        = "Eel.HttpTransport"
	EelRequestHeader        = "Eel.Header"
	EelRequestQuery         = "Eel.Query"
	EelNamedTransformations = "Eel.NamedTransformations"
	EelHandlerConfig        = "Eel.HandlerConfig"
	EelTenantId             = "Eel.TenantId"
	EelPartnerId            = "Eel.PartnerId"
	EelCustomProperties     = "Eel.CustomProperties"
	EelRetryService         = "Eel.RetryService"
	EelErrors               = "Eel.Errors"
	EelSyncPath             = "Eel.SyncPath"
	EelTraceLogger          = "Eel.TraceLogger"
	EelCache                = "Eel.Cache"
	EelTenantIds            = "Eel.TenantIds"
	LogTenantId             = "gears.app.id"
	LogPartnerId            = "gears.partner.id"
)
View Source
const (
	M_Namespace = "Namespace"
	M_Metric    = "Metric"
	M_Unit      = "Unit"
	M_Dims      = "Dims"
	M_Val       = "Val"
)
View Source
const (
	L_NilLevel    = "nil" // log nothing, equivalent to /dev/null
	L_MetricLevel = "metric"
	L_ErrorLevel  = "error"
	L_WarnLevel   = "warn"
	L_InfoLevel   = "info"
	L_DebugLevel  = "debug"
)
View Source
const (
	// metric names
	HTTPRequestDuration = "http.request.duration"
	HTTPHandleDuration  = "http.handle.duration"

	MessageProcessDuration  = "message.process.duration"
	MessageResponseDuration = "message.response.duration"
	MessageLatency          = "message.message.latency"

	// span names
	HTTPHandle  = "http.handle"
	HTTPRequest = "http.request"

	MessageProcess = "message.process"

	// attribute keys
	HTTPHostKey       = string(semconv.HTTPHostKey)
	HTTPMethodKey     = string(semconv.HTTPMethodKey)
	HTTPRouteKey      = string(semconv.HTTPRouteKey)
	HTTPStatusCodeKey = string(semconv.HTTPStatusCodeKey)
	HTTPURLKey        = string(semconv.HTTPURLKey)

	TopicKey   = "topic"
	HandlerKey = "handler"
)

Variables

View Source
var (
	LogLevel     = L_InfoLevel
	Gctx         Context
	Mutex        = &sync.RWMutex{}
	Once         sync.Once
	BasePath     = ""
	ConfigPath   = ""
	HandlerPath  = ""
	HandlerPaths = []string{""}
	InstanceName = "localhost"
	EnvName      = "default"
	AppId        = "eel"
)
View Source
var (
	StatusQueueFull           = map[string]interface{}{"error": "queue full"}
	StatusInvalidJson         = map[string]interface{}{"error": "invalid json"}
	StatusEmptyBody           = map[string]interface{}{"error": "empty body"}
	StatusProcessed           = map[string]interface{}{"status": "processed"}
	StatusProcessedDummy      = map[string]interface{}{"status": "processed", "dummy": true}
	StatusDuplicateEliminated = map[string]interface{}{"status": "duplicate eliminated"}
	StatusRequestTooLarge     = map[string]interface{}{"error": "request too large"}
	StatusHttpPostRequired    = map[string]interface{}{"error": "http post required"}
	StatusUnknownTopic        = map[string]interface{}{"error": "unknown topic"}
	StatusAlreadySubscribed   = map[string]interface{}{"error": "already subscribed"}
	StatusNotEvenSubscribed   = map[string]interface{}{"error": "not even subscribed"}
	StatusNoWorkerPool        = map[string]interface{}{"error": "not worker pool"}

	HttpStatusTooManyRequests = 429
)

Functions

func AddError

func AddError(ctx Context, err error)

AddError adds error to list of errors in current transaction in current context for lib use

func AddLatencyLog

func AddLatencyLog(ctx Context, stats *ServiceStats, key string)

func ClearErrors

func ClearErrors(ctx Context)

ClearErrors clears any stale errors from current transacction in case lib user recycles contexts

func DeepEquals

func DeepEquals(a, b interface{}) bool

func End added in v1.42.0

func End(ctx Context, attrs map[string]string, err error)

End ends the existing span

func ExtractAppId added in v1.41.0

func ExtractAppId(tenant string, allowPartner bool) string

func ExtractPartnerId added in v1.41.0

func ExtractPartnerId(tenant string, allowPartner bool, defaultPartner string) string

func GetCustomProperties

func GetCustomProperties(ctx Context) map[string]interface{}

GetTenant gets tenant id from context if one was passed in as http header.

func GetErrors

func GetErrors(ctx Context) []error

GetErrors gets list of errors in current transaction from current context for lib use

func GetHttpClient

func GetHttpClient(ctx Context) *http.Client

func GetResponse

func GetResponse(ctx Context, errTmpl map[string]interface{}) []byte

func GetTenantId

func GetTenantId(ctx Context) string

GetTenant gets tenant id from context if one was passed in as http header.

func HitEndpoint

func HitEndpoint(ctx Context, url string, payload string, verb string, headers map[string]string, auth map[string]string) (string, int, error)

HitEndpoint helper method for posting payloads to endpoints. Supports other verbs, http headers and basic auth.

func InitGctx

func InitGctx(logLevel string)

func InitHttpTransport

func InitHttpTransport(ctx Context)

InitHttpTransport initializes http transport with some parameters from config.json.

func InitHttpTransportWithDial

func InitHttpTransportWithDial(ctx Context, dial func(network, addr string) (net.Conn, error))

func NewUUID

func NewUUID() (string, error)

func Record added in v1.42.0

func Record(ctx Context, metric string, attrs map[string]string, val int)

Record records a metric if given

func RegisterObserver added in v1.42.0

func RegisterObserver(ctx Context, o Observer)

RegisterObserver registers an observer implementation

func SetDefaultRetrier

func SetDefaultRetrier(retrier Retrier)

SetDefaultRetrier sets the default retrier to an external implementation

func ToFlatString

func ToFlatString(v interface{}) string

ToFlatString helper function to convert anything to a flat string representation. Complex types are converted to json.

func UpdateContext added in v1.42.0

func UpdateContext(ctx Context, conf *EelSettings, tenantId string, updateVal bool)

Types

type Context

type Context interface {
	SetId(id string)
	Id() string
	SubContext() Context
	AddValue(key interface{}, value interface{})
	AddLogValue(key interface{}, value interface{})
	AddConfigValue(key interface{}, value interface{})
	Value(key interface{}) interface{}
	LogValue(key interface{}) interface{}
	ConfigValue(key interface{}) interface{}
	Log() Logger
	DisableLogging()
	EnableLogging()
	HandlePanic()
	WrapPanicHttpHandler(func(http.ResponseWriter, *http.Request)) http.HandlerFunc
}

Context is the interface for a request context and logging.

func NewDefaultContext

func NewDefaultContext(level string) Context

NewDefaultContext creates a default context.

func Start added in v1.42.0

func Start(ctx Context, trace string, attrs map[string]string) Context

Start starts a new span

type DefaultContext

type DefaultContext struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

DefaultContext simple implementation of the Context interface.

func (*DefaultContext) AddConfigValue

func (c *DefaultContext) AddConfigValue(key interface{}, value interface{})

func (*DefaultContext) AddLogValue

func (c *DefaultContext) AddLogValue(key interface{}, value interface{})

func (*DefaultContext) AddValue

func (c *DefaultContext) AddValue(key interface{}, value interface{})

func (*DefaultContext) ConfigValue

func (c *DefaultContext) ConfigValue(key interface{}) interface{}

func (*DefaultContext) DisableLogging

func (c *DefaultContext) DisableLogging()

func (*DefaultContext) EnableLogging

func (c *DefaultContext) EnableLogging()

func (*DefaultContext) HandlePanic

func (c *DefaultContext) HandlePanic()

func (*DefaultContext) Id

func (c *DefaultContext) Id() string

func (*DefaultContext) Log

func (c *DefaultContext) Log() Logger

func (*DefaultContext) LogValue

func (c *DefaultContext) LogValue(key interface{}) interface{}

func (*DefaultContext) SetId

func (c *DefaultContext) SetId(id string)

func (*DefaultContext) SubContext

func (c *DefaultContext) SubContext() Context

func (*DefaultContext) Value

func (c *DefaultContext) Value(key interface{}) interface{}

func (*DefaultContext) WrapPanicHttpHandler

func (c *DefaultContext) WrapPanicHttpHandler(fn func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc

type DefaultLogWriter

type DefaultLogWriter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

DefaultLogWriter wraps the default stdout writer as a singelton (shared by all loggers)

func NewDefaultLogWriter

func NewDefaultLogWriter() *DefaultLogWriter

type DefaultLogger

type DefaultLogger struct {
	// contains filtered or unexported fields
}

DefaultLogger simple implementation of the Logger interface.

func NewDefaultLogger

func NewDefaultLogger(ctx *DefaultContext, level string) *DefaultLogger

NewDefaultLogger creates a new default logger. There is one logger instance per context instance.

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(args ...interface{})

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(args ...interface{})

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(args ...interface{})

func (*DefaultLogger) Metric

func (l *DefaultLogger) Metric(statKey interface{}, args ...interface{})

func (*DefaultLogger) RuntimeLogLoop

func (l *DefaultLogger) RuntimeLogLoop(interval time.Duration, iterations int)

func (*DefaultLogger) Warn

func (l *DefaultLogger) Warn(args ...interface{})

type DefaultRetrier

type DefaultRetrier struct {
}

func (*DefaultRetrier) Retry

func (*DefaultRetrier) Retry(ctx Context, url string, payload string, verb string, headers map[string]string, auth map[string]string, f func(ctx Context, url string, payload string, verb string, headers map[string]string, auth map[string]string) (string, int, error)) (string, int, error)

Retry implements retry logic with injected request function.

func (*DefaultRetrier) RetryEndpoint

func (d *DefaultRetrier) RetryEndpoint(ctx Context, url string, payload string, verb string, headers map[string]string, auth map[string]string) (string, int, error)

RetryEndpoint same as HitEndpoint but with local trivial implementation exponential backoff.

type DuplicateChecker

type DuplicateChecker interface {
	IsDuplicate(Context, []byte) bool
	GetTtl() int
}

DuplicateChecker simple interface for duplicate checker.

func NewLocalInMemoryDupChecker

func NewLocalInMemoryDupChecker(ttl int, size int) DuplicateChecker

NewLocalInMemoryDupChecker creates a simple local in-memory de-duplication cache with optional ttl support.

type EelDebugLogParams

type EelDebugLogParams struct {
	Lock           sync.RWMutex
	RefreshSec     int
	WhiteListTable string
	IdWhiteList    map[string]string
	IdPath         string
	LogParams      map[string]string
}

EelDebugLogParams struct is an optional debug white list and log param config in eel settings

func GetDebugLogParams

func GetDebugLogParams(ctx Context) *EelDebugLogParams

GetDebugLogParams is a helper function to obtain the global debug white list and log information.

type EelSettings

type EelSettings struct {
	Name                           string
	AppName                        string
	ElementsPublishEndpoint        string
	ElementsWebhookEndpoint        string
	ElementsAuth                   string
	EelWebhook                     string
	Brokers                        string
	FunctionalMonitoringPort       int
	Endpoint                       interface{}
	MaxAttempts                    int
	InitialDelay                   time.Duration
	InitialBackoff                 time.Duration
	Pad                            time.Duration
	BackoffMethod                  string
	Organization                   string
	EventTopics                    []string
	ActionTopics                   []string
	MaxMessageSize                 int64
	HttpTransactionHeader          string
	HttpDebugHeader                string
	HttpTenantHeader               string
	HttpPartnerHeader              string
	HttpTimeout                    time.Duration
	ResponseHeaderTimeout          time.Duration
	MaxIdleConnsPerHost            int
	CustomProperties               map[string]interface{}
	Misc                           map[string]interface{}
	LogParams                      map[string]string
	DebugLogParams                 *EelDebugLogParams
	TraceLogParams                 *EelTraceLogParams
	WorkerPoolSize                 map[string]int
	MessageQueueTimeout            int
	MessageQueueDepth              int
	TopicPath                      string
	LogStats                       bool
	SendCloudWatchMetrics          bool
	DuplicateTimeout               int
	CloseIdleConnectionIntervalSec int
	CloseIdleConnectionsStarted    bool
	RetryQueues                    []string
	RetryServiceAvailable          bool
	UseRetryQueue                  bool
	Version                        string
	HandlerConfigPath              string
	AllowPartner                   bool
	DefaultPartner                 string
}

EelSettings struct representing config.json master config file.

func GetConfig

func GetConfig(ctx Context) *EelSettings

GetConfig is a helper function to obtain the global config from the context.

func GetConfigFromFile

func GetConfigFromFile(ctx Context) *EelSettings

GetConfigFromFile loads config.json from disk and returns a pointer to a EelSettings struct.

type EelTraceLogParams

type EelTraceLogParams struct {
	Active      bool
	FileName    string
	LogIncoming bool
	LogOutgoing bool
	LogParams   map[string]string
}

EelTraceLogParams struct is an optional trace log config in eel settings to be activated for brief periods of time to capture a full trace of incoming or outgoing events

type LocalInMemoryDupChecker

type LocalInMemoryDupChecker struct {
	// contains filtered or unexported fields
}

func (*LocalInMemoryDupChecker) GetTtl

func (d *LocalInMemoryDupChecker) GetTtl() int

GetTtl gets ttl setting for cache.

func (*LocalInMemoryDupChecker) IsDuplicate

func (d *LocalInMemoryDupChecker) IsDuplicate(ctx Context, payload []byte) bool

IsDuplicate checks if payload was seen in past ttl ms.

type Logger

type Logger interface {
	Debug(args ...interface{})
	Info(args ...interface{})
	Error(args ...interface{})
	Warn(args ...interface{})
	Metric(statKey interface{}, args ...interface{})
	RuntimeLogLoop(interval time.Duration, iterations int)
}

Logger is the interface for logging.

type NetworkError

type NetworkError struct {
	Endpoint string
	Message  string
	Status   int
}

func (NetworkError) Error

func (e NetworkError) Error() string

type Observer added in v1.42.0

type Observer interface {
	Start(ctx Context, trace string, attrs map[string]string) Context
	End(ctx Context, attrs map[string]string, err error)
	Record(ctx Context, metric string, attrs map[string]string, val int)
}

Observer is the common interface for metrics and tracing

type ParseError

type ParseError struct {
	Message string
}

func (ParseError) Error

func (e ParseError) Error() string

type Retrier

type Retrier interface {
	RetryEndpoint(Context, string, string, string, map[string]string, map[string]string) (string, int, error)
	Retry(Context, string, string, string, map[string]string, map[string]string, func(Context, string, string, string, map[string]string, map[string]string) (string, int, error)) (string, int, error)
}

Retrier is the interface for retrying failed http requests.

func GetRetrier

func GetRetrier(ctx Context) Retrier

GetRetrier gets the Retrier to be used

type RuntimeError

type RuntimeError struct {
	Message  string
	Function string
	Params   []string
}

func (RuntimeError) Error

func (e RuntimeError) Error() string

type ServiceStats

type ServiceStats struct {
	InCount           uint64
	OutCount          uint64
	ErrorCount        uint64
	TotalTimeInternal uint64
	TotalTimeExternal uint64
	TotalBytesIn      uint64
	TotalBytesOut     uint64
}

func (*ServiceStats) Add

func (stats *ServiceStats) Add(src *ServiceStats) *ServiceStats

func (*ServiceStats) Clone

func (stats *ServiceStats) Clone() *ServiceStats

func (*ServiceStats) IncBytesIn

func (stats *ServiceStats) IncBytesIn(size int)

func (*ServiceStats) IncBytesOut

func (stats *ServiceStats) IncBytesOut(size int)

func (*ServiceStats) IncErrors

func (stats *ServiceStats) IncErrors()

func (*ServiceStats) IncInCount

func (stats *ServiceStats) IncInCount()

func (*ServiceStats) IncOutCount

func (stats *ServiceStats) IncOutCount()

func (*ServiceStats) IncTimeExternal

func (stats *ServiceStats) IncTimeExternal(nanos int64)

func (*ServiceStats) IncTimeInternal

func (stats *ServiceStats) IncTimeInternal(nanos int64)

func (*ServiceStats) Reset

func (stats *ServiceStats) Reset()

func (*ServiceStats) StatsLoop

func (stats *ServiceStats) StatsLoop(ctx Context, interval time.Duration, iterations int, label string, getWorkQueueFillLevel propFunc, getNumWorkersIdle propFunc, tenantId string)

StatsLoop logs some basic stats at pre-defined interval. If iterations is negative, the loop is endless. Otherwise the loop terminates after the specified number of iterations.

func (*ServiceStats) Subtract

func (stats *ServiceStats) Subtract(src *ServiceStats) *ServiceStats

type SyntaxError

type SyntaxError struct {
	Message  string
	Function string
	Params   []string
}

func (SyntaxError) Error

func (e SyntaxError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL