easyxporter

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: Apache-2.0 Imports: 14 Imported by: 4

README

easyxporter

a simple and easy framework to make a nodeexporter like exporter by very few codes

three step to write an Exporter

Step1:

fill your collect logic by impl easyxporter.Collector interface

// hardware_info_linux.go

const (
	hardwareInfoCollectorSubsystem = "hardware"
)

// 通过 sysinfo 获取server的硬件信息
type hardwareInfoCollector struct {
	osInfo      *prometheus.Desc
}

func NewHardwareInfoCollector(logger *logrus.Logger) (easyxporter.Collector, error) {
	return &hardwareInfoCollector{
		osInfo: prometheus.NewDesc(
			prometheus.BuildFQName(easyxporter.GetNameSpace(), hardwareInfoCollectorSubsystem, "os"),
			"OS information from sysinfo",
			[]string{"name", "vendor", "version", "release", "architecture"}, nil,
		),
	}, nil

}

func (h *hardwareInfoCollector) Update(ch chan<- prometheus.Metric) error {
	var si sysinfo.SysInfo
	si.GetSysInfo()

	ch <- prometheus.MustNewConstMetric(
		h.osInfo,
		prometheus.CounterValue,
		1,
		si.OS.Name, si.OS.Vendor, si.OS.Version, si.OS.Release, si.OS.Architecture,
	)

	return nil
}

Step2:

register your Collector to easyExporter

// hardware_info_linux.go

func init() {
	easyxporter.RegisterCollector(hardwareInfoCollectorSubsystem, true, NewHardwareInfoCollector)
}

Step3:

run EasyExporter in you main code

easyxporter.Run(easyxporter.ExporterOpts{
		Logger:        logger,
		ListenAddress: listenAddress,
		MetricsPath:   metricsPath,
		MaxRequests:   maxRequests,
		NameSpace:     "server",
	})

after alll, you exporter will be started as a metrics server.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoData = errors.New("collector returned no data")

ErrNoData indicates the collector found no data to collect, but had no other error.

Functions

func Flags

func Flags() *pflag.FlagSet

func InjectFlags added in v1.0.0

func InjectFlags(fs *pflag.FlagSet)

func IsNoDataError

func IsNoDataError(err error) bool

func RegisterAsyncCollector

func RegisterAsyncCollector(collector string, isDefaultEnabled bool, factory func(logger *logrus.Logger) (AsyncCollector, error))

注册 async collector

func RegisterCollector

func RegisterCollector(collector string, isDefaultEnabled bool, factory func(logger *logrus.Logger) (Collector, error))

注册collector

func StartAsyncCollector

func StartAsyncCollector(ctx context.Context, logger *logrus.Logger) error

开启所有的后台采集器并阻塞直到所有采集器结束

Types

type AsyncCollector

type AsyncCollector interface {
	Collector

	// 阻塞并持续在后台进行异步更新
	AsyncCollect(ctx context.Context) error
}

AsyncCollector 在后台采集metric并不断更新,在请求到来时返回最新的metric

type Collector

type Collector interface {
	// 同步获取metrics并通过prometheus registry暴露
	Update(ch chan<- prometheus.Metric) error
}

Collector 在每次请求到来时才进行采集并生成metric

type EasyCollector

type EasyCollector struct {
	Collectors map[string]Collector
	// contains filtered or unexported fields
}

func (EasyCollector) Collect

func (s EasyCollector) Collect(ch chan<- prometheus.Metric)

func (EasyCollector) Describe

func (s EasyCollector) Describe(ch chan<- *prometheus.Desc)

type Exporter added in v1.1.0

type Exporter interface {
	Run() error
}

func Build added in v1.1.0

func Build(
	addr string,
	nameSpace string,
	opts ...Option,
) Exporter

Build Exporter From options

type Option added in v1.1.0

type Option func(*exporterConfig)

func WitContext added in v1.1.0

func WitContext(ctx context.Context) Option

func WithLogger added in v1.1.0

func WithLogger(logger *logrus.Logger) Option

func WithMaxRequests added in v1.1.0

func WithMaxRequests(maxRequests int) Option

func WithMetricFilter added in v1.1.0

func WithMetricFilter(filter []string) Option

func WithMetricPath added in v1.1.0

func WithMetricPath(path string) Option

Jump to

Keyboard shortcuts

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