owl

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2018 License: MIT Imports: 14 Imported by: 0

README

owl logo

Build Status

Features

  • Write (colored) logs on standard/error outputs
  • Also write logs as JSON lines on disk
  • Automatically send logged errors to Sentry
  • Send metrics to Wavefront via StatsD proxy
  • Send metrics to Wavefront via Wavefront proxy
  • Send messages to Slack channel(s)

Installation for your own project

Local development

git clone git@github.com:algolia/owl.git $GOPATH/src/github.com/algolia/owl

How to use?

Logging and monitoring should be easy to use in any project to avoid friction. With this goal in mind, owl was written in a way it should be really straightforward to use out of the box:

  1. Generate a minimal owl.Configuration object (either in your code Go code or unmarshal it from a JSON file.
  2. Pass it to the owl.Init function at the very beginning of your main and check its error return value to make sure the configuration was valid.
  3. Put a defer owl.Stop() right after to make sure everything will clean up by itself when your program will terminate.

To prevent you from logging/sending metrics from your code when testing without changing your own code or the owl configuration, some features are only enabled if specific OWL_USE_* environment variables are set.

Examples

Log messages and errors

owlConfig := owl.Configuration{
	AppName: "my-project",
	Logger: &owl.LoggerConfiguration{
		DisplayLogs: true,
		LogFilePath: "logs.json",
		UseColors:   true,
	},
}

err := owl.Init(owlConfig)
defer owl.Stop()
if err != nil {
	fmt.Println(err)
}

owl.Info("this is an info")
owl.Warning("this is a warning %s", "message")
err = owl.Error("this is an error")
owl.Info("this error is displayed on the standard output: %s", err)

Log errors to Sentry

To let you enable/disable Sentry easily without changing the configuration, the OWL_USE_SENTRY environment variable must to be set in order to send errors to Sentry.

You should find the Sentry DSN token here if you're logged to Sentry website with your own account.

owlConfig := owl.Configuration{
	AppName: "my-project",
	Logger: &owl.LoggerConfiguration{
		SentryDsn: "https://*****@sentry.io/124548",
	},
}

err := owl.Init(owlConfig)
defer owl.Stop()
if err != nil {
	fmt.Println(err)
}

owl.Info("this is an info message: it won't be logged to Sentry")
owl.Error("this is an error: it will be recorded by Sentry")

Log metrics to Wavefront

To let you enable/disable Metric logging easily without changing the configuration, the OWL_USE_METRIC environment variable must to be set in order to metrics to Wavefront.

The following code shows how to configure owl and send few metrics to Wavefront when your program runs, sending the metrics via the StatsD proxy running. If you'd like to run it on a Kubernetes cluster instead, replace the StatsdUrl configuration parameter with WavefrontUrl and set it with the Wavefront proxy URL instead (usually localhost:2878). In this case, you can use the exact same Metric* functions but also pass extra maps of tags with *WithTags function variants.

owlConfig := owl.Configuration{
	AppName: "my-project",
	Metric: &owl.MetricConfiguration{
		StatsdUrl: "127.0.0.1:8125",
	},
}

err := owl.Init(owlConfig)
defer owl.Stop()
if err != nil {
	fmt.Println(err)
}

t := owl.NewMetricTimer()

owl.MetricIncByOne("my_counter")
owl.MetricGauge("temperature", int64(30))

t.Stop("time_spent.main_function")

Send message to a Slack channel

To let you enable/disable Slack logging easily without changing the configuration, the OWL_USE_SLACK environment variable must to be set in order to send messages to Slack.

You should generate a Slack token from here if you're logged to Slack with your own account.

owlConfig := owl.Configuration{
	AppName: "my-project",
	Slack: &owl.SlackConfiguration{
		Token: "your-slack-token",
	},
}

err := owl.Init(owlConfig)
defer owl.Stop()
if err != nil {
	fmt.Println(err)
}

owl.Slack("general", "This message will be seen in the *general* Slack")
owl.Slack("specific-channel", "And you can still use %s %s", "format", "strings")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GitTag string

Functions

func Error

func Error(format string, args ...interface{}) error

func Info

func Info(format string, args ...interface{})

func Init

func Init(c Configuration) error

func MetricDuration

func MetricDuration(stat string, delta time.Duration)

func MetricDurationWithTags

func MetricDurationWithTags(stat string, delta time.Duration, tags map[string]string)

func MetricGauge

func MetricGauge(stat string, value int64)

func MetricGaugeWithTags

func MetricGaugeWithTags(stat string, value int64, tags map[string]string)

func MetricInc

func MetricInc(stat string, value int64)

func MetricIncByOne

func MetricIncByOne(stat string)

func MetricIncByOneWithTags

func MetricIncByOneWithTags(stat string, tags map[string]string)

func MetricIncWithTags

func MetricIncWithTags(stat string, value int64, tags map[string]string)

func Slack

func Slack(channel string, format string, args ...interface{})

func Stop

func Stop()

func Warning

func Warning(format string, args ...interface{})

Types

type Configuration

type Configuration struct {
	AppName string               `json:"app_name"`
	Logger  *LoggerConfiguration `json:"logger"`
	Metric  *MetricConfiguration `json:"metric"`
	Slack   *SlackConfiguration  `json:"slack"`
}

type LoggerConfiguration

type LoggerConfiguration struct {
	DisplayLogs bool   `json:"display_logs"`
	LogFilePath string `json:"log_file_path"`
	Logger      string `json:"logger"`
	SentryDsn   string `json:"sentry_dsn"`
	UseColors   bool   `json:"use_colors"`
}

type MetricConfiguration

type MetricConfiguration struct {
	StatsdUrl    string `json:"statsd_url"`
	WavefrontUrl string `json:"wavefront_url"`
}

type MetricTimer

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

func NewMetricTimer

func NewMetricTimer() *MetricTimer

func (*MetricTimer) Stop

func (t *MetricTimer) Stop(stat string)

func (*MetricTimer) StopWithTags

func (t *MetricTimer) StopWithTags(stat string, tags map[string]string)

type SlackConfiguration

type SlackConfiguration struct {
	Token string `json:"token"`
}

Jump to

Keyboard shortcuts

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