watchman

package module
v0.0.0-...-224f3cd Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: Apache-2.0 Imports: 6 Imported by: 24

README

Watchman

Watchman is an opinionated StatsD client used for publishing metrics.

Installation

To install and configure watchman, use the following snippet:

package main

import "github.com/renderedtext/go-watchman"

func main() {
  statsdHost := "0.0.0.0"
  statdPort := 8125

  // by convention, this is <service-name>.<environment>
  metricNamespace := "example-service.prod"

  err := watchman.Configure(statsdHost, statdPort, metricNamespace)
  if err != nil {
    panic(err)
  }
}
Optional filtering

If you need to filter your metrics based on some runtime environment variable, you can use the following snippet:

package main
import "github.com/renderedtext/go-watchman"
func main() {
  statsdHost := "0.0.0.0"
  statdPort := 8125
  // by convention, this is <service-name>.<environment>
  metricNamespace := "example-service.prod"
  doFilter := strconv.ParseBool(os.Getenv("DO_FILTER"))
  err := watchman.ConfigureWithOptions(watchman.Options{
		Host:                  statsdHost,
		Port:                  statsdPort,
    MetricPrefix:          metricNamespace,
    MetricsChannel:        watchman.InternalOnly,
		ConnectionAttempts:    5,
		ConnectionAttemptWait: 2 * time.Second,
	})
  if err != nil {
    panic(err)
  }
}

This flag MetricsChannel can be set to one of the following InternalOnly, ExternalOnly or All If you want ExternalOnly metrics to be passed through you must use external client. e.g.

watchman.External().Submit("user.count", 12)
Metrics Backends

Metrics backend can be set with BackendType option, default is BackendGraphite and it will send metrics in the form:

tagged.{metricPrefix}.{[tags]}.metricName

the other available option is BackendCloudwatch that tags metrics in Datadog's style:

{metricPrefix}.{name}|{metricType}|#{[tags]}

with BackendCloudwatch option set you must send metrics in key-value pairs, if there is uneven number of elements in tags array library will panic.

Submitting gauges

To submit a simple gauge value use watchman.Submit. For example, if you want to submit that you have 12 users in the database, you would use:

watchman.Submit("user.count", 12)

Now, let's suppose that you want to measure the number of users per group, where each group has a name. You would use:

for _, group := range groups {
	watchman.SubmitWithTags("user.count", []string{group.Name}, group.UserCount())
}

Notice that for the above example we used watchman.SubmitWithTags. Tags are the ideal way to submit variable identifiers like group name.

Here is rule of thumb:

//
// BAD, don't do this.
//
// It will create a dedicated metric for each group and
// overload the backend system.
//
watchman.Submit(fmt.Sprintf("users.%s.count", group.Name), group.UserCount())

//
// GOOD, do this.
//
// Tags are a cheap and won't cause problems in the database. Use tags for
// variable data.
//
watchman.SubmitWithTags("user.count", []string{group.Name}, group.UserCount())

Submitting benchmarks

To measure the execution speed of functions, use watchman.Benchmark or watchman.BenchmarkWithTags.

For example, if you have a RegisterUser function:

func RegisterUser(name, email string) (*models.User, error) {
  if err := Validate(name, email) {
    return nil, err
  }

  return models.CreateUser(name, email)
}

To measure how it performs, use:

func RegisterUser(name, email string) (*models.User, error) {
  defer watchman.Benchmark(Time.Now(), "http.register.user.duration")

  if err := Validate(name, email) {
    return nil, err
  }

  return models.CreateUser(name, email)
}

Counting events

To count events use one of:

  • watchman.Increment
  • watchman.IncrementBy
  • watchman.IncrementWithTags

Examples:

watchman.Increment("profile-page.visits")
watchman.IncrementBy("users.added", len(users))
watchman.IncrementByWithTags("users.added.to.group", []string{group.Name}, len(group.UserCount()))

License

This software is licensed under the Apache 2.0 license.

Documentation

Index

Constants

View Source
const (
	InternalOnly = iota
	ExternalOnly
	All
)
View Source
const (
	BackendGraphite = iota
	BackendCloudwatch
)

Variables

This section is empty.

Functions

func Benchmark

func Benchmark(start time.Time, name string) error

func BenchmarkWithTags

func BenchmarkWithTags(start time.Time, name string, tags []string) error

func Configure

func Configure(host string, port string, metricPrefix string) error

func ConfigureWithOptions

func ConfigureWithOptions(options Options) error

func External

func External() externalClientConvenience

func Increment

func Increment(name string) error

func IncrementBy

func IncrementBy(name string, value int) error

func IncrementByWithTags

func IncrementByWithTags(name string, value int, tags []string) error

func IncrementWithTags

func IncrementWithTags(name string, tags []string) error

func Submit

func Submit(name string, value int) error

func SubmitWithTags

func SubmitWithTags(name string, tags []string, value int) error

func TimingWithTags

func TimingWithTags(name string, tags []string, value int64) error

Types

type BackendType

type BackendType uint8

type Client

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

func (*Client) BenchmarkWithTags

func (c *Client) BenchmarkWithTags(start time.Time, name string, tags []string) error

func (*Client) FormatMetricNameWithTags

func (c *Client) FormatMetricNameWithTags(name string, tags []string) (string, error)

func (*Client) IncrementByWithTags

func (c *Client) IncrementByWithTags(name string, value int, tags []string) error

func (*Client) IncrementWithTags

func (c *Client) IncrementWithTags(name string, tags []string) error

func (*Client) SubmitWithTags

func (c *Client) SubmitWithTags(name string, tags []string, value int) error

func (*Client) TimingWithTags

func (c *Client) TimingWithTags(name string, tags []string, value int64) error

type ClientI

type ClientI interface {
	TimingWithTags(name string, tags []string, value int64) error
	BenchmarkWithTags(start time.Time, name string, tags []string) error
	IncrementWithTags(name string, tags []string) error
	IncrementByWithTags(name string, value int, tags []string) error
	SubmitWithTags(name string, tags []string, value int) error
}

type MetricsChannel

type MetricsChannel uint8

type Options

type Options struct {
	Host                  string
	Port                  string
	MetricPrefix          string
	MetricsChannel        MetricsChannel
	BackendType           BackendType
	ConnectionAttempts    int
	ConnectionAttemptWait time.Duration
}

Jump to

Keyboard shortcuts

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