Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --scrape.file flag to scrape metrics from local files #37

Merged
merged 4 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ for creating an intuitive and interactive command-line interface.
- [x] Allow to select a specific metric to inspect, and show its series.
- [x] Metric search with fuzzy search.
- [x] [HTTP configuration file](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#http_config) to support custom HTTP client options like basic auth, custom headers, proxy configs, etc.
- [x] Support reading metrics from local files, to help analyze metrics that have already been collected.

## Planned Features

Expand Down
14 changes: 13 additions & 1 deletion cmd/cardinality.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,19 @@ func registerCardinalityCommand(app *extkingpin.App) {
_ bool,
) error {
scrapeURL := opts.ScrapeURL
scrapeFile := opts.ScrapeFile
timeoutDuration := opts.Timeout
httpConfigFile := opts.HttpConfigFile

if scrapeURL == "" && scrapeFile == "" {
return errors.New("No URL or file provided to scrape metrics. " +
"Please supply a target to scrape via `--scrape.url` or `--scrape.file` flags.")
}

if scrapeURL != "" && scrapeFile != "" {
return errors.New("The flags `--scrape.url` and `--scrape.file` are mutually exclusive.")
}

metricTable := newModel(nil, opts.OutputHeight, logger)
p := tea.NewProgram(metricTable)
metricTable.program = p
Expand All @@ -424,7 +434,8 @@ func registerCardinalityCommand(app *extkingpin.App) {

level.Info(logger).Log(
"msg", "scraping",
"url", scrapeURL,
"scrape_url", scrapeURL,
"scrape_file", scrapeFile,
"timeout", timeoutDuration,
"max_size", maxSize,
"http_config_file", httpConfigFile,
Expand All @@ -433,6 +444,7 @@ func registerCardinalityCommand(app *extkingpin.App) {
t0 := time.Now()
scraper := scrape.NewPromScraper(
scrapeURL,
scrapeFile,
logger,
scrape.WithTimeout(timeoutDuration),
scrape.WithMaxBodySize(maxSize),
Expand Down
9 changes: 7 additions & 2 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type Options struct {
ScrapeURL string
ScrapeFile string
OutputHeight int
MaxScrapeSize string
Timeout time.Duration
Expand All @@ -25,10 +26,14 @@ func (o *Options) MaxScrapeSizeBytes() (int64, error) {
}

func (o *Options) AddFlags(app extkingpin.AppClause) {
app.Flag("scrape-url", "URL to scrape metrics from").
Required().
app.Flag("scrape.url", "URL to scrape metrics from").
Default("").
StringVar(&o.ScrapeURL)

app.Flag("scrape.file", "File to scrape metrics from").
Default("").
StringVar(&o.ScrapeFile)

app.Flag("timeout", "Timeout for the scrape request").
Default("10s").
DurationVar(&o.Timeout)
Expand Down
57 changes: 56 additions & 1 deletion pkg/scrape/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -23,6 +24,7 @@ import (
type PromScraper struct {
httpConfigFile string
scrapeURL string
scrapeFilePath string
timeout time.Duration
logger log.Logger
series map[string]SeriesSet
Expand Down Expand Up @@ -56,7 +58,7 @@ func WithHttpConfigFile(file string) ScraperOption {
}
}

func NewPromScraper(scrapeURL string, logger log.Logger, opts ...ScraperOption) *PromScraper {
func NewPromScraper(scrapeURL string, scrapeFile string, logger log.Logger, opts ...ScraperOption) *PromScraper {
scOpts := &scrapeOpts{
timeout: 10 * time.Second,
maxBodySize: 10 * 1024 * 1024,
Expand All @@ -69,6 +71,7 @@ func NewPromScraper(scrapeURL string, logger log.Logger, opts ...ScraperOption)

return &PromScraper{
scrapeURL: scrapeURL,
scrapeFilePath: scrapeFile,
logger: logger,
timeout: scOpts.timeout,
maxBodySize: scOpts.maxBodySize,
Expand All @@ -79,6 +82,58 @@ func NewPromScraper(scrapeURL string, logger log.Logger, opts ...ScraperOption)
}

func (ps *PromScraper) Scrape() (*Result, error) {
if ps.scrapeFilePath != "" {
return ps.scrapeFile()
}

return ps.scrapeHTTP()
}

func (ps *PromScraper) scrapeFile() (*Result, error) {
var (
seriesSet map[string]SeriesSet
seriesScrapeText SeriesScrapeText
)

// Don't use os.ReadFile(); manually open the file so we can create an
// io.LimitReader from the file to enforce max body size.
f, err := os.Open(ps.scrapeFilePath)
if err != nil {
return &Result{}, fmt.Errorf("Failed to open file %s to scrape metrics: %w", ps.scrapeFilePath, err)
}
defer f.Close()

body, err := io.ReadAll(io.LimitReader(f, ps.maxBodySize))
if err != nil {
return &Result{}, fmt.Errorf("Failed reading file %s to scrape metrics: %w", ps.scrapeFilePath, err)
}

if int64(len(body)) >= ps.maxBodySize {
level.Warn(ps.logger).Log(
"msg", "metric file body size limit exceeded",
"limit_bytes", ps.maxBodySize,
"body_size", len(body),
)
return &Result{}, fmt.Errorf("metric file body size exceeded limit of %d bytes", ps.maxBodySize)
}

// assume that scraping metrics from a file implies they're in text format.
contentType := "text/plain"
ps.lastScrapeContentType = contentType
seriesSet, scrapeErr := ps.extractMetrics(body, contentType)
if scrapeErr != nil {
return &Result{}, fmt.Errorf("failed to extract metrics from file: %w", scrapeErr)
}
seriesScrapeText = ps.extractMetricSeriesText(body)

return &Result{
Series: seriesSet,
UsedContentType: contentType,
SeriesScrapeText: seriesScrapeText,
}, nil
}

func (ps *PromScraper) scrapeHTTP() (*Result, error) {
var (
seriesSet map[string]SeriesSet
scrapeErr error
Expand Down
Loading