config

package
v0.0.0-...-e098d20 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package config is responsible for loading the configuration from various sources, e.g., environment variables, configuration files and user input. It also accepts dynamic settings from the collector server.

In order to add a new configuration item, you need to:

  • add a field to the Config struct and assign the corresponding env variable name and the default value via struct tags.
  • add validation code to method `Config.validate()` (optional).
  • add a method to retrieve the config value and a wrapper for the default global variable `conf` (see wrappers.go).

Index

Constants

View Source
const (
	// MaxSampleRate is the maximum sample rate we can have
	MaxSampleRate = 1000000
	// MinSampleRate is the minimum sample rate we can have
	MinSampleRate = 0
)

Variables

View Source
var (
	ErrUnsupportedFormat = errors.New("unsupported format")
	ErrFileTooLarge      = errors.New("file size exceeds limit")
	ErrInvalidServiceKey = errors.New(fullTextInvalidServiceKey)
)

Errors

View Source
var (
	ErrTFInvalidType     = errors.New("invalid Type")
	ErrTFInvalidTracing  = errors.New("invalid Tracing")
	ErrTFInvalidRegExExt = errors.New("must set either RegEx or Extensions, but not both")
)

TransactionFilter unmarshal errors

View Source
var (
	// IsValidServiceKey verifies if the service key is a valid one.
	// A valid service key is something like 'service_token:service_name'.
	// The service_token should be of 64 characters long and the size of
	// service_name is larger than 0 but up to 255 characters.
	IsValidServiceKey = regexp.MustCompile(validServiceKeyPattern).MatchString

	// ReplaceSpacesWith replaces all the spaces with valid characters (hyphen)
	ReplaceSpacesWith = regexp.MustCompile(spacesPattern).ReplaceAllString

	// RemoveInvalidChars remove invalid characters
	RemoveInvalidChars = regexp.MustCompile(invalidCharacters).ReplaceAllString
)
View Source
var DebugLevel = conf.GetDebugLevel

DebugLevel is a wrapper to the method of the global config

View Source
var GetCollector = conf.GetCollector

GetCollector is a wrapper to the method of the global config

View Source
var GetCollectorUDP = conf.GetCollectorUDP

GetCollectorUDP is a wrapper to the method of the global config

View Source
var GetDelta = conf.GetDelta
View Source
var GetDisabled = conf.GetDisabled

GetDisabled is a wrapper to the method of the global config

View Source
var GetEc2MetadataTimeout = conf.GetEc2MetadataTimeout

GetEc2MetadataTimeout is a wrapper to the method of the global config

View Source
var GetHostAlias = conf.GetHostAlias

GetHostAlias is a wrapper to the method of the global config

View Source
var GetPrecision = conf.GetPrecision

GetPrecision is a wrapper to the method of the global config

View Source
var GetPrependDomain = conf.GetPrependDomain

GetPrependDomain is a wrapper to the method of the global config

View Source
var GetProxy = conf.GetProxy

GetProxy is a wrapper to the method of the global config

View Source
var GetProxyCertPath = conf.GetProxyCertPath

GetProxyCertPath is a wrapper to the method of the global config

View Source
var GetReportQueryString = conf.GetReportQueryString
View Source
var GetReporterType = conf.GetReporterType

GetReporterType is a wrapper to the method of the global config

View Source
var GetRuntimeMetrics = conf.GetRuntimeMetrics

GetRuntimeMetrics is a wrapper to the method of the global config

View Source
var GetSQLSanitize = conf.GetSQLSanitize

GetSQLSanitize is a wrapper to method GetSQLSanitize of the global variable config.

View Source
var GetSampleRate = conf.GetSampleRate

GetSampleRate is a wrapper to the method of the global config

View Source
var GetServiceKey = conf.GetServiceKey

GetServiceKey is a wrapper to the method of the global config

View Source
var GetTokenBucketCap = conf.GetTokenBucketCap
View Source
var GetTokenBucketRate = conf.GetTokenBucketRate
View Source
var GetTracingMode = conf.GetTracingMode

GetTracingMode is a wrapper to the method of the global config

View Source
var GetTransactionFiltering = conf.GetTransactionFiltering

GetTransactionFiltering is a wrapper to the method of the global config

View Source
var GetTransactionName = conf.GetTransactionName
View Source
var GetTriggerTrace = conf.GetTriggerTrace

GetTriggerTrace is a wrapper to the method of the global config

View Source
var GetTrustedPath = conf.GetTrustedPath

GetTrustedPath is a wrapper to the method of the global config

View Source
var Load = conf.Load

Load reads the customized configurations

View Source
var ReporterOpts = conf.GetReporter

ReporterOpts is a wrapper to the method of the global config

View Source
var SamplingConfigured = conf.SamplingConfigured

SamplingConfigured is a wrapper to the method of the global config

Functions

func InvalidEnv

func InvalidEnv(env string, val string) string

InvalidEnv returns a string indicating invalid environment variables

func IsValidEc2MetadataTimeout

func IsValidEc2MetadataTimeout(t int) bool

IsValidEc2MetadataTimeout checks if the timeout is within the designated range

func IsValidFile

func IsValidFile(file string) bool

IsValidFile checks if the string represents a valid file.

func IsValidHost

func IsValidHost(host string) bool

IsValidHost verifies if the host is in a valid format

func IsValidHostnameAlias

func IsValidHostnameAlias(a string) bool

IsValidHostnameAlias checks if the alias is valid

func IsValidReporterType

func IsValidReporterType(t string) bool

IsValidReporterType checks if the reporter type is valid.

func IsValidSampleRate

func IsValidSampleRate(rate int) bool

IsValidSampleRate checks if the rate is valid

func IsValidTokenBucketCap

func IsValidTokenBucketCap(cap float64) bool

func IsValidTokenBucketRate

func IsValidTokenBucketRate(rate float64) bool

func IsValidTracingMode

func IsValidTracingMode(m TracingMode) bool

IsValidTracingMode checks if the mode is valid

func MaskServiceKey

func MaskServiceKey(validKey string) string

MaskServiceKey masks the middle part of the token and returns the masked service key. For example: key: "ae38315f6116585d64d82ec2455aa3ec61e02fee25d286f74ace9e4fea189217:go" masked:"ae38********************************************************9217:go"

func MissingEnv

func MissingEnv(env string) string

MissingEnv returns a string indicating missing environment variables

func ToInteger

func ToInteger(i string) int

ToInteger converts a string to an integer

func ToServiceKey

func ToServiceKey(s string) string

ToServiceKey converts a string to a service key. The argument should be a valid service key string.

It doesn't touch the service key but does the following to the original service name: - convert all characters to lowercase - convert spaces to hyphens - remove invalid characters ( [^a-z0-9.:_-])

Types

type Config

type Config struct {
	sync.RWMutex `yaml:"-"`

	// Collector defines the host and port of the AppOptics collector
	Collector string `yaml:"Collector,omitempty" env:"APPOPTICS_COLLECTOR" default:"collector.appoptics.com:443"`

	// ServiceKey defines the service key and service name
	ServiceKey string `yaml:"ServiceKey,omitempty" env:"APPOPTICS_SERVICE_KEY"`

	// The file path of the cert file for gRPC connection
	TrustedPath string `yaml:"TrustedPath,omitempty" env:"APPOPTICS_TRUSTEDPATH"`

	// The host and port of the UDP collector
	CollectorUDP string `yaml:"CollectorUDP,omitempty" env:"APPOPTICS_COLLECTOR_UDP"`

	// The reporter type, ssl or udp
	ReporterType string `yaml:"ReporterType,omitempty" env:"APPOPTICS_REPORTER" default:"ssl"`

	Sampling *SamplingConfig `yaml:"Sampling,omitempty"`

	// Whether the domain should be prepended to the transaction name.
	PrependDomain bool `yaml:"PrependDomain,omitempty" env:"APPOPTICS_PREPEND_DOMAIN"`

	// The alias of the hostname
	HostAlias string `yaml:"HostAlias,omitempty" env:"APPOPTICS_HOSTNAME_ALIAS"`

	// The precision of the histogram
	Precision int `yaml:"Precision,omitempty" env:"APPOPTICS_HISTOGRAM_PRECISION" default:"2"`

	// The SQL sanitization level
	SQLSanitize int `yaml:"SQLSanitize,omitempty" env:"APPOPTICS_SQL_SANITIZE" default:"0"`

	// The reporter options
	ReporterProperties *ReporterOptions `yaml:"ReporterProperties,omitempty"`

	// The transaction filtering config
	TransactionSettings []TransactionFilter `yaml:"TransactionSettings,omitempty"`

	Disabled bool `yaml:"Disabled,omitempty" env:"APPOPTICS_DISABLED"`

	// EC2 metadata retrieval timeout in milliseconds
	Ec2MetadataTimeout int `yaml:"Ec2MetadataTimeout,omitempty" env:"APPOPTICS_EC2_METADATA_TIMEOUT" default:"1000"`

	// The default log level. It should follow the level defined in log.DefaultLevel
	DebugLevel string `yaml:"DebugLevel,omitempty" env:"APPOPTICS_DEBUG_LEVEL" default:"warn"`

	// The flag for trigger trace. It's enabled by default.
	TriggerTrace bool `yaml:"TriggerTrace" env:"APPOPTICS_TRIGGER_TRACE" default:"true"`

	// Url of the HTTP/HTTPS proxy in the format of "scheme://<username>:<password>@<host>:<port>"
	Proxy string `yaml:"Proxy,omitempty" env:"APPOPTICS_PROXY"`
	// Cert path for the HTTP/HTTPS proxy
	ProxyCertPath string `yaml:"ProxyCertPath" env:"APPOPTICS_PROXY_CERT_PATH"`
	// Report runtime metrics or not
	RuntimeMetrics bool `yaml:"RuntimeMetrics" env:"APPOPTICS_RUNTIME_METRICS" default:"true"`
	// ReportQueryString indicates if the query string should be reported as part of the URL
	ReportQueryString bool    `yaml:"ReportQueryString" env:"APPOPTICS_REPORT_QUERY_STRING" default:"true"`
	TokenBucketCap    float64 `yaml:"TokenBucketCap" env:"APPOPTICS_TOKEN_BUCKET_CAPACITY" default:"8"`
	TokenBucketRate   float64 `yaml:"TokenBucketRate" env:"APPOPTICS_TOKEN_BUCKET_RATE" default:"0.17"`
	// The user-defined transaction name. It's only available in the AWS Lambda environment.
	TransactionName string `yaml:"TransactionName" env:"APPOPTICS_TRANSACTION_NAME"`
}

Config is the struct to define the agent configuration. The configuration options in this struct (excluding those from ReporterOptions) are not intended for dynamically updating.

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig initializes a Config object and override default values with options provided as arguments. It may print errors if there are invalid values in the configuration file or the environment variables.

If there is a fatal error (e.g., invalid config file), it will return a config object with default values.

func (*Config) GetCollector

func (c *Config) GetCollector() string

GetCollector returns the collector address

func (*Config) GetCollectorUDP

func (c *Config) GetCollectorUDP() string

GetCollectorUDP returns the UDP collector host

func (*Config) GetDebugLevel

func (c *Config) GetDebugLevel() string

GetDebugLevel returns the global logging level. Note that it may return an empty string

func (*Config) GetDelta

func (c *Config) GetDelta() *Delta

func (*Config) GetDisabled

func (c *Config) GetDisabled() bool

GetDisabled returns if the agent is disabled

func (*Config) GetEc2MetadataTimeout

func (c *Config) GetEc2MetadataTimeout() int

GetEc2MetadataTimeout returns the EC2 metadata retrieval timeout in milliseconds

func (*Config) GetHostAlias

func (c *Config) GetHostAlias() string

GetHostAlias returns the host alias

func (*Config) GetPrecision

func (c *Config) GetPrecision() int

GetPrecision returns the histogram precision

func (*Config) GetPrependDomain

func (c *Config) GetPrependDomain() bool

GetPrependDomain returns the prepend domain config

func (*Config) GetProxy

func (c *Config) GetProxy() string

GetProxy returns the HTTP/HTTPS proxy url

func (*Config) GetProxyCertPath

func (c *Config) GetProxyCertPath() string

GetProxyCertPath returns the proxy's certificate path

func (*Config) GetReportQueryString

func (c *Config) GetReportQueryString() bool

GetReportQueryString returns the ReportQueryString flag

func (*Config) GetReporter

func (c *Config) GetReporter() *ReporterOptions

GetReporter returns the reporter options struct

func (*Config) GetReporterType

func (c *Config) GetReporterType() string

GetReporterType returns the reporter type

func (*Config) GetRuntimeMetrics

func (c *Config) GetRuntimeMetrics() bool

GetRuntimeMetrics returns the runtime metrics flag

func (*Config) GetSQLSanitize

func (c *Config) GetSQLSanitize() int

GetSQLSanitize returns the SQL sanitization level.

The meaning of each level: 0 - disable SQL sanitizing (the default). 1 - enable SQL sanitizing and attempt to automatically determine which quoting form to use. 2 - enable SQL sanitizing and force dropping double quoted characters. 4 - enable SQL sanitizing and force retaining double quoted character.

func (*Config) GetSampleRate

func (c *Config) GetSampleRate() int

GetSampleRate returns the local sample rate

func (*Config) GetServiceKey

func (c *Config) GetServiceKey() string

GetServiceKey returns the service key

func (*Config) GetTokenBucketCap

func (c *Config) GetTokenBucketCap() float64

GetTokenBucketCap returns the token bucket capacity

func (*Config) GetTokenBucketRate

func (c *Config) GetTokenBucketRate() float64

GetTokenBucketRate returns the token bucket rate

func (*Config) GetTracingMode

func (c *Config) GetTracingMode() TracingMode

GetTracingMode returns the local tracing mode

func (*Config) GetTransactionFiltering

func (c *Config) GetTransactionFiltering() []TransactionFilter

GetTransactionFiltering returns the transaction filtering config

func (*Config) GetTransactionName

func (c *Config) GetTransactionName() string

GetTransactionName returns the user-defined transaction name. It's only available in the AWS Lambda environment.

func (*Config) GetTriggerTrace

func (c *Config) GetTriggerTrace() bool

GetTriggerTrace returns the trigger trace configuration

func (*Config) GetTrustedPath

func (c *Config) GetTrustedPath() string

GetTrustedPath returns the file path of the cert file

func (*Config) Load

func (c *Config) Load(opts ...Option) *Config

Load reads configuration from config file and environment variables.

func (*Config) SamplingConfigured

func (c *Config) SamplingConfigured() bool

SamplingConfigured returns if tracing mode or sampling rate is configured

type Delta

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

Delta defines the overall delta of two Config objects

func (*Delta) String

func (d *Delta) String() string

type DeltaItem

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

DeltaItem defines a delta item of two Config objects

type FilterType

type FilterType string

FilterType defines the type of the transaction filter

const (
	// URL based filter
	URL FilterType = "url"
)

type Option

type Option func(c *Config)

Option is a function type that accepts a Config pointer and applies the configuration option it defines.

func WithCollector

func WithCollector(collector string) Option

WithCollector defines a Config option for collector address.

func WithServiceKey

func WithServiceKey(key string) Option

WithServiceKey defines a Config option for the service key.

type ReporterOptions

type ReporterOptions struct {
	// Events flush interval in seconds
	EventFlushInterval int64 `yaml:"EventFlushInterval,omitempty" env:"APPOPTICS_EVENTS_FLUSH_INTERVAL" default:"2"`

	// The maximum bytes per RPC request
	MaxReqBytes int64 `yaml:"MaxReqBytes,omitempty" env:"APPOPTICS_MAX_REQUEST_BYTES" default:"2048000"`

	// Metrics flush interval in seconds
	MetricFlushInterval int64 `yaml:"MetricFlushInterval,omitempty" default:"30"`

	// GetSettings interval in seconds
	GetSettingsInterval int64 `yaml:"GetSettingsInterval,omitempty" default:"30"`

	// Settings timeout interval in seconds
	SettingsTimeoutInterval int64 `yaml:"SettingsTimeoutInterval,omitempty" default:"10"`

	// Ping interval in seconds
	PingInterval int64 `yaml:"PingInterval,omitempty" default:"20"`

	// Retry backoff initial delay
	RetryDelayInitial int64 `yaml:"RetryDelayInitial,omitempty" default:"500"`

	// Maximum retry delay
	RetryDelayMax int `yaml:"RetryDelayMax,omitempty" default:"60"`

	// Maximum redirect times
	RedirectMax int `yaml:"RedirectMax,omitempty" default:"20"`

	// The threshold of retries before debug printing
	RetryLogThreshold int `yaml:"RetryLogThreshold,omitempty" default:"10"`

	// The maximum retries
	MaxRetries int `yaml:"MaxRetries,omitempty" default:"20"`
}

ReporterOptions defines the options of a reporter. The fields of it must be accessed through atomic operators

func (*ReporterOptions) GetEventFlushInterval

func (r *ReporterOptions) GetEventFlushInterval() int64

GetEventFlushInterval returns the current event flush interval

func (*ReporterOptions) GetMaxReqBytes

func (r *ReporterOptions) GetMaxReqBytes() int64

GetMaxReqBytes returns the maximum RPC request size

func (*ReporterOptions) SetEventFlushInterval

func (r *ReporterOptions) SetEventFlushInterval(i int64)

SetEventFlushInterval sets the event flush interval to i

func (*ReporterOptions) SetMaxReqBytes

func (r *ReporterOptions) SetMaxReqBytes(i int64)

SetMaxReqBytes sets the maximum bytes of the PRC request body to i

type SamplingConfig

type SamplingConfig struct {
	// The tracing mode
	TracingMode TracingMode `yaml:"TracingMode,omitempty" env:"APPOPTICS_TRACING_MODE" default:"enabled"`

	// The sample rate
	SampleRate int `yaml:"SampleRate,omitempty" env:"APPOPTICS_SAMPLE_RATE" default:"1000000"`
	// contains filtered or unexported fields
}

SamplingConfig defines the configuration options for the sampling decision

func (*SamplingConfig) Configured

func (s *SamplingConfig) Configured() bool

Configured returns if either the tracing mode or the sampling rate has been configured

func (*SamplingConfig) ResetSampleRate

func (s *SamplingConfig) ResetSampleRate()

ResetSampleRate resets the sample rate to its default value and clear the flag.

func (*SamplingConfig) ResetTracingMode

func (s *SamplingConfig) ResetTracingMode()

ResetTracingMode resets the tracing mode to its default value and clear the flag.

func (*SamplingConfig) SetSampleRate

func (s *SamplingConfig) SetSampleRate(rate int)

SetSampleRate assigns the sample rate and set the corresponding flag. Note: Do not change the method name as it (`Set`+Field name) is used in method `loadEnvsInternal` to assign the values loaded from env variables dynamically.

func (*SamplingConfig) SetTracingMode

func (s *SamplingConfig) SetTracingMode(mode TracingMode)

SetTracingMode assigns the tracing mode and set the corresponding flag. Note: Do not change the method name as it (`Set`+Field name) is used in method `loadEnvsInternal` to assign the values loaded from env variables dynamically.

func (*SamplingConfig) UnmarshalYAML

func (s *SamplingConfig) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is the customized unmarshal method for SamplingConfig

type TracingMode

type TracingMode string

TracingMode defines the tracing mode which is either `enabled` or `disabled`

const (
	// EnabledTracingMode means tracing is enabled
	EnabledTracingMode TracingMode = "enabled"
	// DisabledTracingMode means tracing is disabled
	DisabledTracingMode TracingMode = "disabled"

	UnknownTracingMode TracingMode = "unknown"
)

func NormalizeTracingMode

func NormalizeTracingMode(m TracingMode) TracingMode

NormalizeTracingMode converts an old-style tracing mode (always/never) to a new-style tracing mode (enabled/disabled).

type TransactionFilter

type TransactionFilter struct {
	Type       FilterType  `yaml:"Type"`
	RegEx      string      `yaml:"RegEx,omitempty"`
	Extensions []string    `yaml:"Extensions,omitempty"`
	Tracing    TracingMode `yaml:"Tracing"`
}

TransactionFilter defines the transaction filtering based on a filter type.

func (*TransactionFilter) UnmarshalYAML

func (f *TransactionFilter) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is the customized unmarshal method for TransactionFilter

Jump to

Keyboard shortcuts

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