-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
49 lines (38 loc) · 1.05 KB
/
scraper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package scraper
import (
"fmt"
"net/url"
"os"
)
// FileScraper custom type for scraping a file that contains the data from a prometheus metrics endpoint
type FileScraper struct {
file *os.File
}
// NewFileScraper returns a new file scraper instance from a given filepath
func NewFileScraper(path string) (*FileScraper, error) {
s := new(FileScraper)
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("making new file scraper: %w", err)
}
s.file = file
return s, nil
}
// CloseFileScraper closes the file scraper gracefully
func (s *FileScraper) CloseFileScraper() error {
return s.file.Close()
}
// WebScraper custom type for scraping an http endpoint that contains the data from a prometheus metrics endpoint
type WebScraper struct {
url *url.URL
}
// NewWebScraper returns a new web scraper instance from a given url
func NewWebScraper(urlStr string) (*WebScraper, error) {
s := new(WebScraper)
u, err := url.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("making new web scraper: %w", err)
}
s.url = u
return s, nil
}