metric

package
v0.0.0-...-cc56150 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// KeyRT RT 日志类型.
	KeyRT LogType = "RT"
	// KeyQPS QPS 日志类型.
	KeyQPS = "QPS"
	// KeySuccessRate SuccessRate 日志类型.
	KeySuccessRate = "SUCCESS_RATE"
	// KeyFailRate FailRate 日志类型.
	KeyFailRate = "FAIL_RATE"
	// KeyHitRate HitRate 日志类型.
	KeyHitRate = "HIT_RATE"
	// KeyCUR CUR 日志类型.
	KeyCUR = "CUR"

	// HB 特殊处理,每?s记录一次.
	HB = "HB"
)
View Source
const TimeLayout = "20060102150405000"

Variables

View Source
var DefaultRunner = NewRunner(EnvOption())

DefaultRunner is the default runner for metric recording.

View Source
var EPSILON = math.Nextafter(1, 2) - 1

EPSILON。 math.Nextafter(1, 2) 返回的结果是比 1 稍微大一点的浮点数, 它是浮点数 1 在计算机中表示的下一个可表示的值。将这个值减去 1,得到的是 浮点数 1 和它的下一个可表示值之间的差距,这通常被称为“机器精度”或 EPSILON。

Functions

func FloatEquals

func FloatEquals(a, b float64) bool

func Max

func Max(n int, max, v float64) float64

Max returns the max of two number.

func Min

func Min(n int, min, v float64) float64

Min returns the min of two number.

func QPS1

func QPS1(keys ...string)

QPS1 makes a QPS Recorder and then record a request with times 1.

func Stop

func Stop()

Stop stops the default runner.

Types

type CurRecorder

type CurRecorder struct{ Recorder }

CurRecorder record 瞬时值(Gauge).

func Cur

func Cur(keys ...string) CurRecorder

Cur makes a Cur Recorder.

func (CurRecorder) Ks

func (c CurRecorder) Ks(k *ks.Ks) CurRecorder

Ks add extra keys.

func (CurRecorder) Record

func (c CurRecorder) Record(v1 float64)

Record records v1.

type FailRateRecorder

type FailRateRecorder struct{ Recorder }

FailRateRecorder record success rate.

func FailRate

func FailRate(keys ...string) FailRateRecorder

FailRate creates a FailRateRecorder.

func (FailRateRecorder) IncrFail

func (c FailRateRecorder) IncrFail()

IncrFail increment success count.

func (FailRateRecorder) IncrTotal

func (c FailRateRecorder) IncrTotal()

IncrTotal increment total.

func (FailRateRecorder) Ks

Ks add extra keys.

type HitRateRecorder

type HitRateRecorder struct{ Recorder }

HitRateRecorder record hit rate.

func HitRate

func HitRate(keys ...string) HitRateRecorder

HitRate makes a HitRateRecorder.

func (HitRateRecorder) IncrHit

func (c HitRateRecorder) IncrHit()

IncrHit increment success count.

func (HitRateRecorder) IncrTotal

func (c HitRateRecorder) IncrTotal()

IncrTotal increment total.

func (HitRateRecorder) Ks

Ks add extra keys.

type Key

type Key struct {
	Keys    []string
	Checked bool
	// contains filtered or unexported fields
}

Key defines a slice of keys.

func NewKey

func NewKey(keys []string) Key

NewKey create Keys.

func (*Key) Check

func (k *Key) Check()

Check checks the validation of keys.

type Line

type Line struct {
	Ks *ks.Ks `json:"-"`

	K1  string `json:"k1,omitempty"`
	K2  string `json:"k2,omitempty"`
	K3  string `json:"k3,omitempty"`
	K4  string `json:"k4,omitempty"`
	K5  string `json:"k5,omitempty"`
	K6  string `json:"k6,omitempty"`
	K7  string `json:"k7,omitempty"`
	K8  string `json:"k8,omitempty"`
	K9  string `json:"k9,omitempty"`
	K10 string `json:"k10,omitempty"`
	K11 string `json:"k11,omitempty"`
	K12 string `json:"k12,omitempty"`
	K13 string `json:"k13,omitempty"`
	K14 string `json:"k14,omitempty"`
	K15 string `json:"k15,omitempty"`
	K16 string `json:"k16,omitempty"`
	K17 string `json:"k17,omitempty"`
	K18 string `json:"k18,omitempty"`
	K19 string `json:"k19,omitempty"`
	K20 string `json:"k20,omitempty"`

	Key      string  `json:"key"` // {{k1}}#{{k2}}#{{k3}}
	LogType  LogType `json:"logtype"`
	Time     string  `json:"time"` // yyyyMMddHHmmssSSS
	Hostname string  `json:"hostname"`

	Keys []string `json:"-"`
	Min  float64  `json:"min"` // 每次采集区间(METRICS_INTERVAL)中 v1  最小/大值
	Max  float64  `json:"max"` // 只对 RT 生效

	V1 float64 `json:"v1"` // 小数
	V2 float64 `json:"v2"` // 只有比率类型的时候,才用到v2
	V3 float64 `json:"v3"` // RT 当 [300-400) ms 时 v3 = 1
	V4 float64 `json:"v4"` // RT 当 [400-500) ms 时 v4 = 1
	V5 float64 `json:"v5"` // RT 当 [500-600) ms 时 v5 = 1
	V6 float64 `json:"v6"` // RT 当 [600-700) ms 时 v6 = 1
	V7 float64 `json:"v7"` // RT 当 [700-800) ms 时 v7 = 1
	V8 float64 `json:"v8"` // RT 当 [800-900) ms 时 v8 = 1
	V9 float64 `json:"v9"` // RT 当 [900-∞) ms 时 v9 = 1

	N int `json:"n"` // 上次记录完日志后,累积的写次数
}

Line represents a metric rotate line structure in rotate file.

func (*Line) ToLineProtocol

func (l *Line) ToLineProtocol() (string, error)

ToLineProtocol print l to a influxdb v1 line protocol format.

type LogType

type LogType string

LogType means the logMetrics type.

type Option

type Option struct {
	AppName         string        `env:"APP_NAME"`                            // 应用名称,默认使用当前进程的PID
	LogPath         string        `default:"/tmp/log/metrics" env:"LOG_PATH"` // 日志路径
	MetricsInterval time.Duration `default:"1s" env:"METRICS_INTERVAL"`       // 每隔多少时间记录一次日志
	HBInterval      time.Duration `default:"20s" env:"HB_INTERVAL"`           // 每隔多少时间记录一次心跳日志
	ChanCap         int           `default:"1000" env:"CHAN_CAP"`             // 指标通道容量,当指标大量发送容量堆满时,自动扔弃
	MaxBackups      int           `default:"7" env:"MAX_BACKUPS"`             // 最大保留天数
	AutoDrop        bool          `env:"AUTO_DROP"`                           // 在指标来不及处理时,是否自动扔弃
	Debug           bool          `env:"DEBUG"`                               // 开启测试模式
}

Option defines the option for runner.

type OptionFn

type OptionFn func(o *Option)

OptionFn defines the function for options setting.

func AppName

func AppName(v string) OptionFn

AppName sets the app name.

func ChanCap

func ChanCap(v int) OptionFn

ChanCap sets the capacity of metrics line channel.

func EnvOption

func EnvOption(filenames ...string) OptionFn

EnvOption sets the options from env.

func LogPath

func LogPath(v string) OptionFn

LogPath sets the logMetrics path of metrics logMetrics files.

func MaxBackups

func MaxBackups(v int) OptionFn

MaxBackups sets max backups of metrics logMetrics files.

func MetricsInterval

func MetricsInterval(v time.Duration) OptionFn

MetricsInterval sets the interval to write logMetrics line.

type QPSRecorder

type QPSRecorder struct{ Recorder }

QPSRecorder is a QPS recorder.

func QPS

func QPS(keys ...string) QPSRecorder

QPS makes a QPS Recorder.

func (QPSRecorder) Ks

func (q QPSRecorder) Ks(k *ks.Ks) QPSRecorder

Ks add extra keys.

func (QPSRecorder) Record

func (q QPSRecorder) Record(times float64)

Record records a request.

type RTRecorder

type RTRecorder struct {
	Start time.Time
	Recorder
}

RTRecorder is a Round-Time recorder 平均响应时间.

func RT

func RT(keys ...string) RTRecorder

RT makes an RT Recorder.

func (RTRecorder) Ks

func (r RTRecorder) Ks(k *ks.Ks) RTRecorder

Ks add extra keys.

func (RTRecorder) Record

func (r RTRecorder) Record()

Record records a round-time.

func (RTRecorder) RecordSince

func (r RTRecorder) RecordSince(start time.Time)

RecordSince records a round-time since start.

type Recorder

type Recorder struct {
	Runner  *Runner
	LogType LogType
	Key
}

Recorder record rate.

func MakeRecorder

func MakeRecorder(logType LogType, keys []string) Recorder

MakeRecorder creates a Recorder.

func (Recorder) PutRecord

func (c Recorder) PutRecord(v1, v2 float64, vx ...float64)

PutRecord put a metric record to channel.

type Runner

type Runner struct {
	MetricsLogfile io.Writer
	HBLogfile      io.Writer

	C chan *Line

	AppName string

	MetricsInterval time.Duration
	HBInterval      time.Duration
	// contains filtered or unexported fields
}

Runner is a runner for metric rotate writing.

func NewRunner

func NewRunner(ofs ...OptionFn) *Runner

NewRunner creates a Runner.

func (*Runner) AsyncPut

func (r *Runner) AsyncPut(keys Key, logType LogType, v1, v2 float64, vx ...float64)

AsyncPut new a metric line.

func (*Runner) Cur

func (r *Runner) Cur(keys ...string) CurRecorder

Cur makes a Cur Recorder.

func (*Runner) FailRate

func (r *Runner) FailRate(keys ...string) FailRateRecorder

FailRate creates a FailRateRecorder.

func (*Runner) HitRate

func (r *Runner) HitRate(keys ...string) HitRateRecorder

HitRate makes a HitRateRecorder.

func (*Runner) MakeRecorder

func (r *Runner) MakeRecorder(logType LogType, keys []string) Recorder

MakeRecorder creates a Recorder.

func (*Runner) QPS

func (r *Runner) QPS(keys ...string) QPSRecorder

QPS makes a QPS Recorder.

func (*Runner) QPS1

func (r *Runner) QPS1(keys ...string)

QPS1 makes a QPS Recorder and then record a request with times 1.

func (*Runner) RT

func (r *Runner) RT(keys ...string) RTRecorder

RT makes an RT Recorder.

func (*Runner) Start

func (r *Runner) Start()

Start starts the runner.

func (*Runner) Stop

func (r *Runner) Stop()

Stop stops the runner.

func (*Runner) SuccessRate

func (r *Runner) SuccessRate(keys ...string) SuccessRateRecorder

SuccessRate makes a SuccessRateRecorder.

type SuccessRateRecorder

type SuccessRateRecorder struct{ Recorder }

SuccessRateRecorder record success rate.

func SuccessRate

func SuccessRate(keys ...string) SuccessRateRecorder

SuccessRate makes a SuccessRateRecorder.

func (SuccessRateRecorder) IncrSuccess

func (c SuccessRateRecorder) IncrSuccess()

IncrSuccess increment success count.

func (SuccessRateRecorder) IncrTotal

func (c SuccessRateRecorder) IncrTotal()

IncrTotal increment total.

func (SuccessRateRecorder) Ks

Ks add extra keys.

Jump to

Keyboard shortcuts

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