stats

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 4 Imported by: 0

README

Stats package

Package stats provides functions for reporting metrics to a service.

Code
import (
	"context"
	"net/http"
	"runtime/debug"
	"strconv"
	"time"

	opentracing "github.com/opentracing/opentracing-go"
	olog "github.com/opentracing/opentracing-go/log"
	scontext "github.com/deixis/spine/context"
	"github.com/deixis/spine/log"
	"github.com/deixis/spine/stats"
	"github.com/deixis/spine/tracing"
)

func mwStats(next ServeFunc) ServeFunc {
	return func(ctx context.Context, w ResponseWriter, r *Request) {
		stats := stats.FromContext(ctx)
		tags := map[string]string{
			"method": r.method,
			"path":   r.path,
		}
		stats.Inc("http.conc", tags)

		// Next middleware
		next(ctx, w, r)

		tags["status"] = strconv.Itoa(w.Code())
		stats.Histogram("http.call", 1, tags)
		stats.Timing("http.time", time.Since(r.startTime), tags)
		stats.Dec("http.conc", tags)
	}
}
Config
[stats.statsd]
  addr = "127.0.0.1"
  port = "8125"
  tags_format = "influxdb"

[stats.statsd.tags]
  foo = "bar"

Documentation

Overview

Package stats provides functions for reporting metrics to a service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(ctx context.Context, key string, n interface{}, meta ...map[string]string)

Count calls `Count` on the context `Stats`

func Dec

func Dec(ctx context.Context, key string, meta ...map[string]string)

Dec calls `Dec` on the context `Stats`

func Gauge

func Gauge(ctx context.Context, key string, n interface{}, meta ...map[string]string)

Gauge calls `Gauge` on the context `Stats`

func Histogram

func Histogram(ctx context.Context, key string, n interface{}, tags ...map[string]string)

Histogram calls `Histogram` on the context `Stats`

func Inc

func Inc(ctx context.Context, key string, meta ...map[string]string)

Inc calls `Inc` on the context `Stats`

func Timing

func Timing(ctx context.Context, key string, t time.Duration, meta ...map[string]string)

Timing calls `Timing` on the context `Stats`

func WithContext

func WithContext(ctx context.Context, s Stats) context.Context

WithContext returns a copy of parent in which `Stats` is stored

Types

type Stats

type Stats interface {
	Start()
	Stop()

	// Count is a simple counter
	Count(key string, n interface{}, meta ...map[string]string)
	// Inc increments the given counter by 1
	Inc(key string, meta ...map[string]string)
	// Dec decrements the given counter by 1
	Dec(key string, meta ...map[string]string)
	// Gauge measures the amount, level, or contents of something
	// The given value replaces the current one
	// e.g. in-flight requests, uptime, ...
	Gauge(key string, n interface{}, meta ...map[string]string)
	// Timing measures how long it takes to accomplish something
	// e.g. algorithm, request, ...
	Timing(key string, t time.Duration, meta ...map[string]string)
	// Histogram measures the distribution of values over the time
	Histogram(key string, n interface{}, tags ...map[string]string)

	// With returns a child Stats, and add meta to that Stats
	With(meta map[string]string) Stats
	// Log attaches a logger to a Stats instance
	Log(l log.Logger) Stats
}

Stats is an interface for app statistics

func FromContext

func FromContext(ctx contextutil.ValueContext) Stats

FromContext returns a `Stats` instance associated with `ctx`, or `NopStats` if no `Stats` instance could be found.

func NopStats

func NopStats() Stats

NopStats returns a stats adapter that does not do anything

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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