Skip to content

Commit

Permalink
Add metrics parsing from config file and report metrics on runner
Browse files Browse the repository at this point in the history
Metrics will be configured in files and not through the CLI to
avoid unnecessarily expanding the CLI options.

Flush metrics on shutdown and configure dogstatsd reporting interval
Add no-op provider
Add documentation
Add telemetry config tests
Change instruments helper to accept nil meter
  • Loading branch information
findkim committed May 5, 2020
1 parent edf364d commit e7a3bb4
Show file tree
Hide file tree
Showing 15 changed files with 1,358 additions and 33 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,32 @@ template {
min = "2s"
max = "10s"
}
}
# These are example configurations for monitoring Consul Template metrics.
# This block is an HCL mapping to [OpenCensus](https://opencensus.io/) configurations for
# various exporters. Telemetry configuration is only supported in
# configuration files and not as CLI flags. Only one metric provider can
# be used at a given time.
telemetry {
stdout {
reporting_interval = "60s"
pretty_print = false
do_not_print_time = false
}
dogstatsd {
// address describes the destination for exporting dogstatsd data.
// e.g., udp://host:port tcp://host:port unix:///socket/path
address = "udp://127.0.0.1:8125"
reporting_interval = "60s"
}
prometheus {
reporting_interval = "60s"
port = 8888
}
}
}
```

Note that not all fields are required. If you are not retrieving secrets from
Expand Down
8 changes: 8 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/consul-template/logging"
"github.com/hashicorp/consul-template/manager"
"github.com/hashicorp/consul-template/signals"
"github.com/hashicorp/consul-template/telemetry"
"github.com/hashicorp/consul-template/version"
)

Expand Down Expand Up @@ -102,6 +103,13 @@ func (cli *CLI) Run(args []string) int {
return ExitCodeOK
}

// Initialize telemetry
tel, err := telemetry.Init(config.Telemetry)
if err != nil {
return logError(err, ExitCodeConfigError)
}
defer tel.Stop()

// Initial runner
runner, err := manager.NewRunner(config, dry)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Config struct {
// Syslog is the configuration for syslog.
Syslog *SyslogConfig `mapstructure:"syslog"`

// Telemetry is the configuration for collecting and emitting telemetry.
Telemetry *TelemetryConfig `mapstructure:"telemetry"`

// Templates is the list of templates.
Templates *TemplateConfigs `mapstructure:"template"`

Expand Down Expand Up @@ -135,6 +138,10 @@ func (c *Config) Copy() *Config {
o.Syslog = c.Syslog.Copy()
}

if c.Telemetry != nil {
o.Telemetry = c.Telemetry.Copy()
}

if c.Templates != nil {
o.Templates = c.Templates.Copy()
}
Expand Down Expand Up @@ -210,6 +217,10 @@ func (c *Config) Merge(o *Config) *Config {
r.Syslog = r.Syslog.Merge(o.Syslog)
}

if o.Telemetry != nil {
r.Telemetry = r.Telemetry.Merge(o.Telemetry)
}

if o.Templates != nil {
r.Templates = r.Templates.Merge(o.Templates)
}
Expand Down Expand Up @@ -256,6 +267,10 @@ func Parse(s string) (*Config, error) {
"exec.env",
"ssl",
"syslog",
"telemetry",
"telemetry.stdout",
"telemetry.dogstatsd",
"telemetry.prometheus",
"vault",
"vault.retry",
"vault.ssl",
Expand Down Expand Up @@ -413,6 +428,7 @@ func (c *Config) GoString() string {
"PidFile:%s, "+
"ReloadSignal:%s, "+
"Syslog:%#v, "+
"Telemetry:%#v, "+
"Templates:%#v, "+
"Vault:%#v, "+
"Wait:%#v,"+
Expand All @@ -429,6 +445,7 @@ func (c *Config) GoString() string {
StringGoString(c.PidFile),
SignalGoString(c.ReloadSignal),
c.Syslog,
c.Telemetry.GoString(),
c.Templates,
c.Vault,
c.Wait,
Expand Down Expand Up @@ -467,6 +484,7 @@ func DefaultConfig() *Config {
DefaultDelims: DefaultDefaultDelims(),
Exec: DefaultExecConfig(),
Syslog: DefaultSyslogConfig(),
Telemetry: DefaultTelemetryConfig(),
Templates: DefaultTemplateConfigs(),
Vault: DefaultVaultConfig(),
Wait: DefaultWaitConfig(),
Expand Down Expand Up @@ -529,6 +547,11 @@ func (c *Config) Finalize() {
}
c.Syslog.Finalize()

if c.Telemetry == nil {
c.Telemetry = DefaultTelemetryConfig()
}
c.Telemetry.Finalize()

if c.Templates == nil {
c.Templates = DefaultTemplateConfigs()
}
Expand Down
94 changes: 94 additions & 0 deletions config/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func BoolVal(b *bool) bool {
return *b
}

// BoolCopy returns a copy of the boolean pointer
func BoolCopy(b *bool) *bool {
if b == nil {
return nil
}

return Bool(*b)
}

// BoolGoString returns the value of the boolean for printing in a string.
func BoolGoString(b *bool) string {
if b == nil {
Expand Down Expand Up @@ -53,6 +62,15 @@ func FileModeVal(o *os.FileMode) os.FileMode {
return *o
}

// FileModeCopy returns a copy of the os.FireMode
func FileModeCopy(o *os.FileMode) *os.FileMode {
if o == nil {
return nil
}

return FileMode(*o)
}

// FileModeGoString returns the value of the os.FileMode for printing in a
// string.
func FileModeGoString(o *os.FileMode) string {
Expand Down Expand Up @@ -85,6 +103,15 @@ func IntVal(i *int) int {
return *i
}

// IntCopy returns a copy of the int pointer
func IntCopy(i *int) *int {
if i == nil {
return nil
}

return Int(*i)
}

// IntGoString returns the value of the int for printing in a string.
func IntGoString(i *int) string {
if i == nil {
Expand All @@ -102,6 +129,46 @@ func IntPresent(i *int) bool {
return *i != 0
}

// Uint returns a pointer to the given uint.
func Uint(i uint) *uint {
return &i
}

// UintVal returns the value of the uint at the pointer, or 0 if the pointer is
// nil.
func UintVal(i *uint) uint {
if i == nil {
return 0
}
return *i
}

// UintCopy returns a copy of the uint pointer
func UintCopy(i *uint) *uint {
if i == nil {
return nil
}

return Uint(*i)
}

// UintGoString returns the value of the uint for printing in a string.
func UintGoString(i *uint) string {
if i == nil {
return "(*uint)(nil)"
}
return fmt.Sprintf("%d", *i)
}

// UintPresent returns a boolean indicating if the pointer is nil, or if the
// pointer is pointing to the zero value.
func UintPresent(i *uint) bool {
if i == nil {
return false
}
return *i != 0
}

// Signal returns a pointer to the given os.Signal.
func Signal(s os.Signal) *os.Signal {
return &s
Expand All @@ -116,6 +183,15 @@ func SignalVal(s *os.Signal) os.Signal {
return *s
}

// SignalCopy returns a copy of the os.Signal
func SignalCopy(s *os.Signal) *os.Signal {
if s == nil {
return nil
}

return Signal(*s)
}

// SignalGoString returns the value of the os.Signal for printing in a string.
func SignalGoString(s *os.Signal) string {
if s == nil {
Expand Down Expand Up @@ -149,6 +225,15 @@ func StringVal(s *string) string {
return *s
}

// StringCopy returns a copy of the string pointer
func StringCopy(s *string) *string {
if s == nil {
return nil
}

return String(*s)
}

// StringGoString returns the value of the string for printing in a string.
func StringGoString(s *string) string {
if s == nil {
Expand Down Expand Up @@ -179,6 +264,15 @@ func TimeDurationVal(t *time.Duration) time.Duration {
return *t
}

// TimeDurationCopy returns a copy of the time.Duration pointer
func TimeDurationCopy(t *time.Duration) *time.Duration {
if t == nil {
return nil
}

return TimeDuration(*t)
}

// TimeDurationGoString returns the value of the time.Duration for printing in a
// string.
func TimeDurationGoString(t *time.Duration) string {
Expand Down
Loading

0 comments on commit e7a3bb4

Please sign in to comment.