metrics

package module
v0.0.0-...-ae36d4e Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 25 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectAllProcesses

func CollectAllProcesses() []message.Composer

CollectAllProcesses returns a slice of populated ProcessInfo message.Composer interfaces for all processes currently running on a system.

func CollectBasicGoStats

func CollectBasicGoStats() message.Composer

CollectBasicGoStats returns some very basic runtime statistics about the current go process, using runtime.MemStats and runtime.NumGoroutine.

The data reported for the runtime event metrics (e.g. mallocs, frees, gcs, and cgo calls,) are the counts since the last time metrics were collected, and are reported as rates calculated since the last time the statics were collected.

Values are cached between calls, to produce the deltas. For the best results, collect these messages on a regular interval.

Internally, this uses message.Fields message type, which means the order of the fields when serialized is not defined and applications cannot manipulate the Raw value of this composer.

The basic idea is taken from https://github.com/YoSmudge/go-stats.

func CollectProcessInfo

func CollectProcessInfo(pid int32) message.Composer

CollectProcessInfo returns a populated ProcessInfo message.Composer instance for the specified pid.

func CollectProcessInfoSelf

func CollectProcessInfoSelf() message.Composer

CollectProcessInfoSelf returns a populated ProcessInfo message.Composer for the pid of the current process.

func CollectProcessInfoSelfWithChildren

func CollectProcessInfoSelfWithChildren() []message.Composer

CollectProcessInfoSelfWithChildren returns a slice of populated ProcessInfo message.Composer instances for the current process and all children processes.

func CollectProcessInfoWithChildren

func CollectProcessInfoWithChildren(pid int32) []message.Composer

CollectProcessInfoWithChildren returns a slice of populated ProcessInfo message.Composer instances for the process with the specified pid and all children processes for that process.

func CollectSystemInfo

func CollectSystemInfo() message.Composer

CollectSystemInfo returns a populated SystemInfo object, without a message.

func MakeProcessInfo

func MakeProcessInfo(pid int32, message string) message.Composer

NewProcessInfo constructs a fully configured and populated Processinfo message.Composer instance for the specified process.

func MakeSystemInfo

func MakeSystemInfo(message string) message.Composer

MakeSystemInfo builds a populated SystemInfo object with the specified message.

func NewFilter

func NewFilter(ctx context.Context, sender send.Sender, opts CollectOptions) send.Sender

NewFilter produces a sender that persists metrics collection to ftdc files by filtering out appropratly typed messages to the logger. In this model, applications can send metrics to the logger without needing to configure any additional logging infrastructure or setup. Metrics get persisted to the timeseries files of the filter, and also (potentially) written to the logging event system. All messages are passed to the underlying sender

The context passed to the constructor controls the life-cycle of the collectors, and the close method on the sender blocks until all resources are released. The options control the behavior of the collector: how often data is flushed, how many samples are in each compressed block, and if the metrics collector should include all structured messages or only those that implement SchemaComposer.

Errors encountered are propagated to the underlying sender's ErrorHandling facility.

While this implementation is robust, and the birch/FTDC data format is compact and useable, it is also minimal and is perhaps a better model for other kinds of integration: production systems might want to stream metrics to some kind of central service or use a different persistence format, but the general model is robust.

func RenderHistogramBSON

func RenderHistogramBSON(
	wr *bytes.Buffer,
	key string,
	labels fun.Future[*dt.Pairs[string, string]],
	sample *dt.Pairs[float64, int64],
	ts time.Time,
)

func RenderMetricBSON

func RenderMetricBSON(buf *bytes.Buffer, key string, labels fun.Future[*dt.Pairs[string, string]], value int64, ts time.Time)

func SeriesRendererBSON

func SeriesRendererBSON() series.Renderer

Types

type CollectOptions

type CollectOptions struct {
	FlushInterval     time.Duration
	SampleCount       int
	BlockCount        int
	OutputFilePrefix  string
	CaptureStructured bool

	// The WriterConstructor returns a writable object (typically
	// a file) to stream metrics too. This can be a (simple)
	// wrapper around os.Create in the common case.
	WriterConstructor func(string) (io.WriteCloser, error)
}

CollectOptions are the settings to provide the behavior of the collection process.

func DefaultCollectionOptions

func DefaultCollectionOptions() CollectOptions

DefaultCollectionOptions produces a reasonable collection constructor for most basic use. The WriterConstructorField

func (CollectOptions) Validate

func (opts CollectOptions) Validate() error

type CounterType

type CounterType int8
const (
	CounterTypeCurrent CounterType = iota
	CounterTypeDeltas
	CounterTypeRates
)

type GoRuntimeInfo

type GoRuntimeInfo struct {
	Message string `bson:"msg" json:"msg" yaml:"msg"`
	Payload struct {
		HeapObjects uint64        `bson:"memory.objects.heap" json:"memory.objects.heap" yaml:"memory.objects.heap"`
		Alloc       uint64        `bson:"memory.summary.alloc" json:"memory.summary.alloc" yaml:"memory.summary.alloc"`
		HeapSystem  uint64        `bson:"memory.summary.system" json:"memory.summary.system" yaml:"memory.summary.system"`
		HeapIdle    uint64        `bson:"memory.heap.idle" json:"memory.heap.idle" yaml:"memory.heap.idle"`
		HeapInUse   uint64        `bson:"memory.heap.used" json:"memory.heap.used" yaml:"memory.heap.used"`
		Mallocs     int64         `bson:"memory.counters.mallocs" json:"memory.counters.mallocs" yaml:"memory.counters.mallocs"`
		Frees       int64         `bson:"memory.counters.frees" json:"memory.counters.frees" yaml:"memory.counters.frees"`
		GC          int64         `bson:"gc.rate" json:"gc.rate" yaml:"gc.rate"`
		GCPause     time.Duration `bson:"gc.pause.duration.last" json:"gc.pause.last" yaml:"gc.pause.last"`
		GCLatency   time.Duration `bson:"gc.pause.duration.latency" json:"gc.pause.duration.latency" yaml:"gc.pause.duration.latency"`
		Goroutines  int64         `bson:"goroutines.total" json:"goroutines.total" yaml:"goroutines.total"`
		CgoCalls    int64         `bson:"cgo.calls" json:"cgo.calls" yaml:"cgo.calls"`
	}
	message.Base `json:"meta,omitempty" bson:"meta,omitempty" yaml:"meta,omitempty"`
	// contains filtered or unexported fields
}

GoRuntimeInfo provides a structured format for data about the current go runtime. Also implements the message composer interface.

func CollectGoStatsDeltas

func CollectGoStatsDeltas() *GoRuntimeInfo

CollectGoStatsDeltas constructs a Composer, which is a GoRuntimeInfo internally, that contains data collected from the Go runtime about the state of memory use and garbage collection.

The data reported for the runtime event metrics (e.g. mallocs, frees, gcs, and cgo calls,) are the counts since the last time metrics were collected.

Values are cached between calls, to produce the deltas. For the best results, collect these messages on a regular interval.

GoRuntimeInfo also implements the message.Composer interface.

func CollectGoStatsRates

func CollectGoStatsRates() *GoRuntimeInfo

CollectGoStatsRates constructs a Composer, which is a GoRuntimeInfo internally, that contains data collected from the Go runtime about the state of memory use and garbage collection.

The data reported for the runtime event metrics (e.g. mallocs, frees, gcs, and cgo calls,) are the counts since the last time metrics were collected, divided by the time since the last time the metric was collected, to produce a rate, which is calculated using integer division.

For the best results, collect these messages on a regular interval.

func CollectGoStatsTotals

func CollectGoStatsTotals() *GoRuntimeInfo

CollectGoStatsTotals constructs a Composer, which is a GoRuntimeInfo internally, that contains data collected from the Go runtime about the state of memory use and garbage collection.

The data reported for the runtime event metrics (e.g. mallocs, frees, gcs, and cgo calls,) are totals collected since the beginning on the runtime.

GoRuntimeInfo also implements the message.Composer interface.

func MakeGoStatsDeltas

func MakeGoStatsDeltas(msg string) *GoRuntimeInfo

MakeGoStatsDeltas has the same semantics as CollectGoStatsDeltas, but additionally allows you to set a message string to annotate the data.

GoRuntimeInfo also implements the message.Composer interface.

func MakeGoStatsRates

func MakeGoStatsRates(msg string) *GoRuntimeInfo

MakeGoStatsRates has the same semantics as CollectGoStatsRates, but additionally allows you to set a message string to annotate the data.

func MakeGoStatsTotals

func MakeGoStatsTotals(msg string) *GoRuntimeInfo

MakeGoStatsTotals has the same semantics as CollectGoStatsTotals, but additionally allows you to set a message string to annotate the data.

GoRuntimeInfo also implements the message.Composer interface.

func (*GoRuntimeInfo) Loggable

func (s *GoRuntimeInfo) Loggable() bool

Loggable returns true when the GoRuntimeInfo structure is populated. Loggable is part of the Composer interface.

func (*GoRuntimeInfo) MarshalDocument

func (s *GoRuntimeInfo) MarshalDocument() (*birch.Document, error)

func (*GoRuntimeInfo) Raw

func (s *GoRuntimeInfo) Raw() any

Raw is part of the Composer interface and returns the GoRuntimeInfo object itself.

func (*GoRuntimeInfo) Schema

func (*GoRuntimeInfo) Schema() string

func (*GoRuntimeInfo) String

func (s *GoRuntimeInfo) String() string

func (*GoRuntimeInfo) Structured

func (*GoRuntimeInfo) Structured() bool

type ProcessInfo

type ProcessInfo struct {
	Message string `json:"msg" bson:"msg"`
	Payload struct {
		Pid            int32                    `json:"pid" bson:"pid"`
		Parent         int32                    `json:"parentPid" bson:"parentPid"`
		Threads        int                      `json:"numThreads" bson:"numThreads"`
		Command        string                   `json:"command" bson:"command"`
		CPU            StatCPUTimes             `json:"cpu" bson:"cpu"`
		IoStat         process.IOCountersStat   `json:"io" bson:"io"`
		NetStat        []net.IOCountersStat     `json:"net" bson:"net"`
		Memory         process.MemoryInfoStat   `json:"mem" bson:"mem"`
		MemoryPlatform process.MemoryInfoExStat `json:"memExtra" bson:"memExtra"`
		Errors         []string                 `json:"errors" bson:"errors"`
	}

	message.Base `json:"metadata,omitempty" bson:"metadata,omitempty"`
	// contains filtered or unexported fields
}

ProcessInfo holds the data for per-process statistics (e.g. cpu, memory, io). The Process info composers produce messages in this form.

func (*ProcessInfo) Loggable

func (p *ProcessInfo) Loggable() bool

Loggable returns true when the Processinfo structure has been populated.

func (*ProcessInfo) MarshalDocument

func (p *ProcessInfo) MarshalDocument() (*birch.Document, error)

func (*ProcessInfo) Raw

func (p *ProcessInfo) Raw() any

Raw always returns the ProcessInfo object, however it will call the Collect method of the base operation first.

func (*ProcessInfo) Schema

func (*ProcessInfo) Schema() string

func (*ProcessInfo) String

func (p *ProcessInfo) String() string

String returns a string representation of the message, lazily rendering the message, and caching it privately.

func (*ProcessInfo) Structured

func (*ProcessInfo) Structured() bool

type SchemaComposer

type SchemaComposer interface {
	message.Composer
	// Schema describes the format of the Raw() method for a
	// message. This is provided as an alternative to simply using
	// fmt.Sprintf("%T", msg), and makes it possible to maintain
	// schema versions.
	Schema() string
}

SchemaComposer wraps a message.Composer and adds a method for describing a message object's raw format.

type StatCPUTimes

type StatCPUTimes struct {
	User      int64 `json:"user" bson:"user"`
	System    int64 `json:"system" bson:"system"`
	Idle      int64 `json:"idle" bson:"idle"`
	Nice      int64 `json:"nice" bson:"nice"`
	Iowait    int64 `json:"iowait" bson:"iowait"`
	Irq       int64 `json:"irq" bson:"irq"`
	Softirq   int64 `json:"softirq" bson:"softirq"`
	Steal     int64 `json:"steal" bson:"steal"`
	Guest     int64 `json:"guest" bson:"guest"`
	GuestNice int64 `json:"guestNice" bson:"guestNice"`
}

StatCPUTimes provides a mirror of gopsutil/cpu.TimesStat with integers rather than floats.

type SystemInfo

type SystemInfo struct {
	Message string `json:"msg" bson:"msg"`
	Payload struct {
		CPU        StatCPUTimes          `json:"cpu" bson:"cpu"`
		CPUPercent float64               `json:"cpu_percent" bson:"cpu_percent"`
		NumCPU     int                   `json:"num_cpus" bson:"num_cpus"`
		VMStat     mem.VirtualMemoryStat `json:"vmstat" bson:"vmstat"`
		NetStat    net.IOCountersStat    `json:"netstat" bson:"netstat"`
		Partitions []disk.PartitionStat  `json:"partitions" bson:"partitions"`
		Usage      []disk.UsageStat      `json:"usage" bson:"usage"`
		IOStat     []disk.IOCountersStat `json:"iostat" bson:"iostat"`
		Errors     []string              `json:"errors" bson:"errors"`
	}
	message.Base `json:"metadata,omitempty" bson:"metadata,omitempty�"`
	// contains filtered or unexported fields
}

SystemInfo is a type that implements message.Composer but also collects system-wide resource utilization statistics about memory, CPU, and network use, along with an optional message.

func (*SystemInfo) Loggable

func (s *SystemInfo) Loggable() bool

Loggable returns true when the Processinfo structure has been populated.

func (*SystemInfo) MarshalDocument

func (s *SystemInfo) MarshalDocument() (*birch.Document, error)

func (*SystemInfo) Raw

func (s *SystemInfo) Raw() any

Raw always returns the SystemInfo object.

func (*SystemInfo) Schema

func (*SystemInfo) Schema() string

func (*SystemInfo) String

func (s *SystemInfo) String() string

String returns a string representation of the message, lazily rendering the message, and caching it privately.

func (*SystemInfo) Structured

func (*SystemInfo) Structured() bool

Jump to

Keyboard shortcuts

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