kit

module
v0.0.0-...-bbd7fc3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: BSD-3-Clause

README

Kit

Components for building excellent, production ready services

Config

Configruation should have sensible defaults and be opinionated about settings for different environments. The main switches should be based on your APP_ENVIRONMENT, namely switching between development and production for now.

Environment Flags

By default, we will be in development mode (APP_ENVIRONMENT=development). By simply switching to APP_ENVIRONMENT=production many production level defaults will be set to their correct values without having to update individual config (ie: logging will be structured/fluentd/stackdriver formatted, telemetry enabled, error reporting on etc)

Metrics, Tracing, Observability

To enable telemetry, ensure TELEMETRY_ENABLED is set to true. This will enable tracing, metrics, and error alerting which default to being enabled. Each can be individually toggled by setting one/each of

  • TRACING_ENABLED
  • METRICS_ENABLE
  • ERROR_ALERTING_ENABLED

to false.

To set a go service collecting telemetry, the following block should be initialized where main is called (ie: where you launch your server)

    sd, err := telemetry.NewExporter()

	if err != nil {
		panic(err)
	}

	defer sd.Flush()

	// Start exporting traces
	tracing.RegisterExporter(sd)

	// Register metrics views for stackdriver HTTP stats
	err = wrapper.RegisterDefaultHTTPViews()
	if err != nil {
		panic(err)
	}

	// Start metrics exporter
	err = sd.StartMetricsExporter()
	if err != nil {
		panic(err)
	}

    defer sd.StopMetricsExporter()

Then, if you want default http metrics and tracing capture, wrap your mux router/handler in wrapper.WrapMux


import(
	"net/http"

	"github.com/whiteblock/kit/telemetry/metrics/http/handlers/wrapper"

	"github.com/gorilla/mux"

)

http.ListenAndServe(":8080", wrapper.WrapMux(r))

And sensible tracing and http metrics will be calculated. For examples of setting up custom metrics, a basic RED example has been set up in telemetry/metrics/http/

Error Alerting

Error Alerting will allow unhandled panics to be reported back to stackdriver error reporting

ERROR_ALERTING_ENABLED defaults to true and is activated when telemetry is enabled

Logging

We want to use dynamic formatter configuration, so deployed environments use structured logging, but local developers can still use human readable text formatters.

To allow configuration of your logrus formatter from a variety of kit config switches, in your go service:

import(
	"github.com/whiteblock/kit/logging"

	log "github.com/sirupsen/logrus"
)

log.SetFormatter(logging.Formatter())

This will default to logrus.TextFormatter but when in production (APP_ENVIRONMENT=production) automatically switch to the stackdriver/fluentd structured formatter

Jump to

Keyboard shortcuts

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