scraper

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 8 Imported by: 0

README

prometheus-exporter-scraper

This package is for parsing Prometheus-compliant metrics libraries to Go data types. It relies on regexp to parse line content into structs

Currently supports counter and gauge metrics. Histogram and summary support coming soon.

This is mostly useful if all of the following conditions are true:

  • You have a metrics exporter that you don't care to view as a timeseries, and is not scraped by Prometheus.
  • The application you're writing will be deployed to a location that can route to that metrics endpoint.

I am currently using this package to scrape single point-in-time metrics from an exporter with high cardinality in labels that could impact my Prometheus server's performance.

Usage

import scraper "github.com/starttoaster/prometheus-exporter-scraper"

// Create scraper -- replace with your metrics URL -- ignores errors
scrp, _ := scraper.NewWebScraper("http://localhost:8080/metrics")

// Scrape metrics -- ignores errors
data, _ := scrp.ScrapeWeb()

// Loop through gauge metrics -- you can do the same with counters by using data.Counters
for _, gauge := range data.Gauges {
    fmt.Println(gauge.Key) // Print the metric name (value of type string)
    fmt.Println(gauge.Value) // Print the metric value (value of type float64)
    fmt.Println(gauge.Labels) // Print out the metric labels (value of type map[string]string)
    fmt.Println(data.GetHelp(gauge.Key)) // Print out the help/info message for a particular metric name
    fmt.Println(data.Type(gauge.Key)) // Print out the type for a particular metric name
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileScraper

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

FileScraper custom type for scraping a file that contains the data from a prometheus metrics endpoint

func NewFileScraper

func NewFileScraper(path string) (*FileScraper, error)

NewFileScraper returns a new file scraper instance from a given filepath

func (*FileScraper) CloseFileScraper

func (s *FileScraper) CloseFileScraper() error

CloseFileScraper closes the file scraper gracefully

func (*FileScraper) ScrapeFile

func (s *FileScraper) ScrapeFile() (*ScrapeData, error)

ScrapeFile scrapes the file owned by a FileScraper instance and returns some ScrapeData

type MetricType

type MetricType string

MetricType represents the type of prometheus metric for a timeseries

const (
	// PrometheusGauge a gauge metric type name
	PrometheusGauge MetricType = "gauge"
	// PrometheusCounter a counter metric type name
	PrometheusCounter MetricType = "counter"
	// PrometheusHistogram a histogram metric type name
	PrometheusHistogram MetricType = "histogram"
	// PrometheusSummary a summary metric type name
	PrometheusSummary MetricType = "summary"
)

type PrometheusCounterMetric

type PrometheusCounterMetric struct {
	Key    string
	Labels map[string]string
	Value  int
}

PrometheusCounterMetric represents the data contained in an individual counter metric timeseries

type PrometheusGaugeMetric

type PrometheusGaugeMetric struct {
	Key    string
	Labels map[string]string
	Value  float64
}

PrometheusGaugeMetric represents the data contained in an individual gauge metric timeseries

type PrometheusHistogramMetric

type PrometheusHistogramMetric struct {
	Key    string
	Labels map[string]string
	Value  int
}

PrometheusHistogramMetric represents the data contained in an individual histogram metric timeseries TODO -- fix to represent an actual histogram metric's data

type PrometheusSummaryMetric

type PrometheusSummaryMetric struct {
	Key    string
	Labels map[string]string
	Value  int
}

PrometheusSummaryMetric represents the data contained in an individual summary metric timeseries TODO -- fix to represent an actual summary metric's data

type ScrapeData

type ScrapeData struct {
	Gauges   []PrometheusGaugeMetric
	Counters []PrometheusCounterMetric
	// contains filtered or unexported fields
}

ScrapeData contains all the serialized data from a prometheus compatible metrics endpoint

func (*ScrapeData) GetHelp

func (d *ScrapeData) GetHelp(metric string) string

GetHelp returns the help message for a given metric name. Returns a blank string if the metric had no help message

func (*ScrapeData) GetHelps

func (d *ScrapeData) GetHelps() map[string]string

GetHelps returns a map[string]string of key metric names and value metric help messages

func (*ScrapeData) GetType

func (d *ScrapeData) GetType(metric string) MetricType

GetType returns the type for a given metric name

func (*ScrapeData) GetTypes

func (d *ScrapeData) GetTypes() map[string]MetricType

GetTypes returns a map[string]MetricType of key metric names and value metric types

type WebScraper

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

WebScraper custom type for scraping an http endpoint that contains the data from a prometheus metrics endpoint

func NewWebScraper

func NewWebScraper(urlStr string) (*WebScraper, error)

NewWebScraper returns a new web scraper instance from a given url

func (*WebScraper) ScrapeWeb

func (s *WebScraper) ScrapeWeb() (*ScrapeData, error)

ScrapeWeb scrapes the url owned by the WebScraper instance and returns some ScrapeData

Jump to

Keyboard shortcuts

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