2024-02-03 11:05:10 +00:00
|
|
|
package metrics
|
2024-02-02 20:07:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-02-03 11:05:10 +00:00
|
|
|
"fmt"
|
2024-02-02 20:28:16 +00:00
|
|
|
"io/ioutil"
|
2024-02-02 20:07:29 +00:00
|
|
|
"net/http"
|
2024-02-03 11:05:10 +00:00
|
|
|
"regexp"
|
2024-02-02 20:07:29 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LokiMetricConfig struct {
|
2024-02-03 11:05:10 +00:00
|
|
|
Url string
|
|
|
|
BotUserAgents *[]regexp.Regexp
|
|
|
|
Enabled bool
|
2024-02-02 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if we should send a metric ping to Loki based on the served path.
|
2024-02-03 11:05:10 +00:00
|
|
|
func (c *LokiMetricConfig) ShouldSendMetrics(path, userAgent string) bool {
|
|
|
|
if !strings.HasSuffix(path, ".html") || !c.Enabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out bots
|
|
|
|
for _, pattern := range *c.BotUserAgents {
|
|
|
|
if pattern.MatchString(userAgent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2024-02-02 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 11:05:10 +00:00
|
|
|
func (c *LokiMetricConfig) SendMetricPing(domain, path, referrer string) {
|
|
|
|
msg := fmt.Sprintf("path=\"%s\" referrer=\"%s\"", path, referrer)
|
2024-02-02 20:07:29 +00:00
|
|
|
data := map[string]interface{}{
|
2024-02-02 20:36:58 +00:00
|
|
|
"streams": []map[string]interface{}{
|
2024-02-02 20:07:29 +00:00
|
|
|
{
|
|
|
|
"stream": map[string]string{
|
|
|
|
// Labels
|
|
|
|
"service": "rio",
|
|
|
|
"domain": domain,
|
|
|
|
"type": "metric",
|
|
|
|
},
|
|
|
|
"values": [][]interface{}{
|
|
|
|
{
|
|
|
|
strconv.Itoa(int(time.Now().UnixNano())),
|
2024-02-03 11:05:10 +00:00
|
|
|
msg,
|
2024-02-02 20:07:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to send metric ping to Loki: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-02 20:28:16 +00:00
|
|
|
log.Debugf("Sending payload %s", string(jsonData))
|
|
|
|
|
2024-02-02 20:07:29 +00:00
|
|
|
// Send the ping to the Loki server
|
|
|
|
go func() {
|
2024-02-02 20:28:16 +00:00
|
|
|
res, err := http.Post(
|
2024-02-02 20:07:29 +00:00
|
|
|
c.Url,
|
|
|
|
"application/json",
|
|
|
|
strings.NewReader(string(jsonData)),
|
|
|
|
)
|
2024-02-02 20:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to send payload to Loki: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 204 {
|
|
|
|
log.Errorf("Loki returned non-204 status code %d", res.StatusCode)
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to read body. No more specific error message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Errorf("-> %s", body)
|
|
|
|
}
|
2024-02-02 20:07:29 +00:00
|
|
|
}()
|
|
|
|
}
|
2024-02-03 11:05:10 +00:00
|
|
|
|
|
|
|
// Reads a JSON array of bot user agents from disk and parses them
|
|
|
|
// into regular expressions.
|
|
|
|
func ReadBotPatterns(file string) ([]regexp.Regexp, error) {
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to read bot metrics file: %v", err)
|
|
|
|
return []regexp.Regexp{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload []string
|
|
|
|
err = json.Unmarshal(content, &payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to unmarshal file: %v", err)
|
|
|
|
return []regexp.Regexp{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns := make([]regexp.Regexp, 0)
|
|
|
|
for _, v := range payload {
|
|
|
|
patterns = append(
|
|
|
|
patterns,
|
|
|
|
*regexp.MustCompile(v),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, nil
|
|
|
|
}
|