new: feat: add metrics endpoint and prometheus monitoring

This commit is contained in:
2024-03-10 14:00:07 +00:00
parent f7fee26ac1
commit 0aa941a69f
8 changed files with 144 additions and 27 deletions

View File

@@ -22,6 +22,12 @@ var bludv = IndexerMeta{
}
func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
i.metrics.IndexerDuration.WithLabelValues("bludv").Observe(time.Since(start).Seconds())
i.metrics.IndexerRequests.WithLabelValues("bludv").Inc()
}()
ctx := r.Context()
// supported query params: q, season, episode
q := r.URL.Query().Get("q")
@@ -38,6 +44,7 @@ func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
i.metrics.IndexerErrors.WithLabelValues("bludv").Inc()
return
}
defer resp.Body.Close()
@@ -46,6 +53,7 @@ func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
i.metrics.IndexerErrors.WithLabelValues("bludv").Inc()
return
}
@@ -174,7 +182,7 @@ func getTorrentsBluDV(ctx context.Context, i *Indexer, link string) ([]IndexedTo
magnetAudio = append(magnetAudio, audio...)
}
peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, infoHash, trackers)
peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, i.metrics, infoHash, trackers)
if err != nil {
fmt.Println(err)
}

View File

@@ -39,6 +39,12 @@ var replacer = strings.NewReplacer(
)
func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
i.metrics.IndexerDuration.WithLabelValues("comando").Observe(time.Since(start).Seconds())
i.metrics.IndexerRequests.WithLabelValues("comando").Inc()
}()
ctx := r.Context()
// supported query params: q, season, episode
q := r.URL.Query().Get("q")
@@ -55,6 +61,7 @@ func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
i.metrics.IndexerErrors.WithLabelValues("comando").Inc()
return
}
defer resp.Body.Close()
@@ -63,6 +70,7 @@ func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
i.metrics.IndexerErrors.WithLabelValues("comando").Inc()
return
}
@@ -206,7 +214,7 @@ func getTorrents(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent
magnetAudio = append(magnetAudio, audio...)
}
peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, infoHash, trackers)
peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, i.metrics, infoHash, trackers)
if err != nil {
fmt.Println(err)
}
@@ -365,8 +373,10 @@ func getDocument(ctx context.Context, i *Indexer, link string) (*goquery.Documen
// try to get from redis first
docCache, err := i.redis.Get(ctx, link)
if err == nil {
i.metrics.CacheHits.WithLabelValues("document_body").Inc()
return goquery.NewDocumentFromReader(ioutil.NopCloser(bytes.NewReader(docCache)))
}
defer i.metrics.CacheMisses.WithLabelValues("document_body").Inc()
resp, err := http.Get(link)
if err != nil {

View File

@@ -6,11 +6,13 @@ import (
"time"
"github.com/felipemarinho97/torrent-indexer/cache"
"github.com/felipemarinho97/torrent-indexer/monitoring"
"github.com/felipemarinho97/torrent-indexer/schema"
)
type Indexer struct {
redis *cache.Redis
redis *cache.Redis
metrics *monitoring.Metrics
}
type IndexerMeta struct {
@@ -39,9 +41,10 @@ type IndexedTorrent struct {
SeedCount int `json:"seed_count"`
}
func NewIndexers(redis *cache.Redis) *Indexer {
func NewIndexers(redis *cache.Redis, metrics *monitoring.Metrics) *Indexer {
return &Indexer{
redis: redis,
redis: redis,
metrics: metrics,
}
}