util

package
v0.0.0-...-7652ffb Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2017 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

span is a very simple implementation of ideas presented in Google's Dapper paper (http://research.google.com/pubs/pub36356.html). The basic idea is that every request has a Span object associated with it which allows for registering subspans (used for timing subroutines), attributes (arbitrary metadata) and counts (incrementers ala StatsD). At the end of a request, this span is turned into JSON and logged to logger as well as the timings and counters sent through the supplied StatsRecorder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultUUIDGenerator

func DefaultUUIDGenerator() string

The DefaultUUIDGenerator uses "github.com/satori/go.uuid"/V1 to generate RFC 4122 compatible uuids

Types

type AtomicCounter

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

func (*AtomicCounter) Decrement

func (counter *AtomicCounter) Decrement() int64

func (*AtomicCounter) Increment

func (counter *AtomicCounter) Increment() int64

func (*AtomicCounter) Load

func (counter *AtomicCounter) Load() int64

func (*AtomicCounter) LoadFast

func (counter *AtomicCounter) LoadFast() int64

func (*AtomicCounter) Store

func (counter *AtomicCounter) Store(val int64)

type DebugStatsRecorder

type DebugStatsRecorder struct{}

func (*DebugStatsRecorder) Counter

func (s *DebugStatsRecorder) Counter(stat string, amount int64)

func (*DebugStatsRecorder) DurationTimer

func (s *DebugStatsRecorder) DurationTimer(stat string, begin time.Time, end time.Time)

func (*DebugStatsRecorder) Gauge

func (s *DebugStatsRecorder) Gauge(stat string, amount int64)

func (*DebugStatsRecorder) Increment

func (s *DebugStatsRecorder) Increment(stat string)

func (*DebugStatsRecorder) Timer

func (s *DebugStatsRecorder) Timer(stat string, amount int64)

type Span

type Span struct {
	sync.Mutex
	Stats    StatsRecorder
	Id       string
	ParentId string
	SubSpans map[string]*SubSpan
	Counters map[string]int64
	Attrs    map[string]string
}

Span is the main object passed through a RequestHandler that stores all the metadata about a request. It should be initialized with a UUID through NewSpan().

func NewSpan

func NewSpan(id string) (s *Span)

Initialize a Span for a unique request with a UUID. This also initializes all of the substructures/maps for storing the metadata.

func (*Span) Add

func (s *Span) Add(name string, val int64) int64

Add increments the counter at name by val. names for counters also are unique per-Span.

func (*Span) Attr

func (s *Span) Attr(k, v string)

Attr stores arbitrary metadata for the Span as a key/value map.

func (*Span) CollectMemStats

func (s *Span) CollectMemStats()

CollectMemStats populates the span with a number of memory statistics from runtime.MemStats. Call this before logging your span for info about your applications memory usage

func (*Span) Duration

func (s *Span) Duration(name string) int64

Duration returns the nanosecond duration of the SubSpan at name.

func (*Span) Finish

func (s *Span) Finish(name string) (duration int64)

Finish marks the subspan at name as finished, returning the duration (useful for debug logging). This does not have to be called in the same goroutine or location as the .Start() for the SubSpan, in fact, you can call Finish on an unstarted SubSpan without error (the duration will be 0).

func (*Span) Increment

func (s *Span) Increment(name string) int64

Increment increments the counter at name by 1.

func (*Span) JSON

func (s *Span) JSON() string

JSON marshalls the Span into a JSON formatted string with all the subspans turned into their millisecond durations. This is the default for what is logged by the tcpez server.

func (*Span) MergeJSON

func (s *Span) MergeJSON(span string) (err error)

MergeJSON imports values from a wellformed json that looks like {"subspans": {}, "attrs": {}, "counters": {}}

func (*Span) MillisecondDuration

func (s *Span) MillisecondDuration(name string) float64

MillisecondDuration() returns a float64 of the duration with millisecond prescision.

func (*Span) Record

func (s *Span) Record()

Record flushes the counters and SubSpan durations to the provider StatsRecorder. By default this goes to /dev/null, but using the StatsdStatsdRecorder this can be flushed to Statsd (or any other service that conforms to the StatsRecorder interface)

func (*Span) Start

func (s *Span) Start(name string)

Start a subspan with name. names need to be unique per-Span as these are stored in a map of name->SubSpan. If you have multiple recouring calls to a subroutine in a request, consider naming them with `method-newuuid`

func (*Span) String

func (s *Span) String() string

String turns the Span into a k=v formatted string with the subspans turned into their millisecond durations.

func (*Span) SubSpan

func (s *Span) SubSpan(name string) (sub *SubSpan)

SubSpan returns the SubSpan at name. If the SubSpan does not exist, it initializes a new one with name.

func (*Span) SubSpanWithDuration

func (s *Span) SubSpanWithDuration(name string, msduration float64)

SubSpanWithDuration creates a new subspan with the duration expressed as a float64 of milliseconds.

type SpanJSON

type SpanJSON map[string]interface{}

type StatsRecorder

type StatsRecorder interface {
	Timer(stat string, amount int64)
	DurationTimer(stat string, begin time.Time, end time.Time)
	Gauge(stat string, amount int64)
	Counter(stat string, amount int64)
	Increment(stat string)
}

type SubSpan

type SubSpan struct {
	Name     string
	Started  time.Time
	Finished time.Time
}

A SubSpan represents an unique subroutine and its start/end times. These are coallated at the end of a Span and turned into Durations.

func (*SubSpan) Duration

func (s *SubSpan) Duration() int64

The Duration of the SubSpan as an int64 representing nanoseconds

func (*SubSpan) Finish

func (s *SubSpan) Finish(started time.Time) time.Time

func (*SubSpan) MillisecondDuration

func (s *SubSpan) MillisecondDuration() float64

The Duration of the subspan as a millisecond prescision float64

type UUIDGenerator

type UUIDGenerator func() string

A UUIDGenerator is a func that returns a unique id as a string

Jump to

Keyboard shortcuts

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