feat: Switch from Loki to something else
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
4b4bc9792b
commit
617b68f43e
@ -145,7 +145,7 @@ func runServer(ctx *cli.Context) error {
|
||||
acmeDnsProvider := ctx.String("acme-dns-provider")
|
||||
acmeDisable := ctx.Bool("acme-disable")
|
||||
defaultCsp := ctx.String("default-csp")
|
||||
lokiUrl := ctx.String("loki-url")
|
||||
metricsUrl := ctx.String("metrics-url")
|
||||
metricsBotList := ctx.String("metrics-bot-list")
|
||||
tokenFile := ctx.String("token-file")
|
||||
|
||||
@ -157,9 +157,9 @@ func runServer(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
// Set up the Loki metrics
|
||||
var lokiConfig metrics.LokiMetricConfig
|
||||
if lokiUrl == "" {
|
||||
lokiConfig = metrics.LokiMetricConfig{
|
||||
var lokiConfig metrics.MetricConfig
|
||||
if metricsUrl == "" {
|
||||
lokiConfig = metrics.MetricConfig{
|
||||
Enabled: false,
|
||||
}
|
||||
} else {
|
||||
@ -171,10 +171,10 @@ func runServer(ctx *cli.Context) error {
|
||||
}
|
||||
log.Infof("Read %d bot patterns from disk", len(patterns))
|
||||
|
||||
lokiConfig = metrics.LokiMetricConfig{
|
||||
lokiConfig = metrics.MetricConfig{
|
||||
Enabled: true,
|
||||
BotUserAgents: &patterns,
|
||||
Url: lokiUrl,
|
||||
Url: metricsUrl,
|
||||
}
|
||||
}
|
||||
|
||||
@ -414,10 +414,10 @@ func main() {
|
||||
EnvVars: []string{"DEFAULT_CSP"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "loki-url",
|
||||
Usage: "The URL for Loki metric pings",
|
||||
Name: "metrics-url",
|
||||
Usage: "The URL for metric pings",
|
||||
Value: "",
|
||||
EnvVars: []string{"LOKI_URL"},
|
||||
EnvVars: []string{"METRICS_URL"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "metrics-bot-list",
|
||||
|
@ -23,7 +23,7 @@ type GlobalContext struct {
|
||||
DefaultCSP string
|
||||
PagesDomain string
|
||||
Gitea *gitea.GiteaClient
|
||||
MetricConfig *metrics.LokiMetricConfig
|
||||
MetricConfig *metrics.MetricConfig
|
||||
Cache *CacheContext
|
||||
}
|
||||
|
||||
|
@ -2,25 +2,22 @@ package metrics
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type LokiMetricConfig struct {
|
||||
type MetricConfig struct {
|
||||
Url string
|
||||
BotUserAgents *[]regexp.Regexp
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
// Checks if we should send a metric ping to Loki based on the served path.
|
||||
func (c *LokiMetricConfig) ShouldSendMetrics(path, userAgent string) bool {
|
||||
func (c *MetricConfig) ShouldSendMetrics(path, userAgent string) bool {
|
||||
if !strings.HasSuffix(path, ".html") || !c.Enabled {
|
||||
return false
|
||||
}
|
||||
@ -35,35 +32,21 @@ func (c *LokiMetricConfig) ShouldSendMetrics(path, userAgent string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *LokiMetricConfig) SendMetricPing(domain, path, referrer string) {
|
||||
msg := fmt.Sprintf("path=\"%s\" referrer=\"%s\"", path, referrer)
|
||||
data := map[string]interface{}{
|
||||
"streams": []map[string]interface{}{
|
||||
{
|
||||
"stream": map[string]string{
|
||||
// Labels
|
||||
"service": "rio",
|
||||
"domain": domain,
|
||||
"type": "metric",
|
||||
},
|
||||
"values": [][]interface{}{
|
||||
{
|
||||
strconv.Itoa(int(time.Now().UnixNano())),
|
||||
msg,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
func (c *MetricConfig) SendMetricPing(domain, path, referrer string) {
|
||||
data := map[string]string{
|
||||
"domain": domain,
|
||||
"path": path,
|
||||
"referer": referrer,
|
||||
}
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to send metric ping to Loki: %v", err)
|
||||
log.Errorf("Failed to send metric ping: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("Sending payload %s", string(jsonData))
|
||||
|
||||
// Send the ping to the Loki server
|
||||
// Send the ping to the server
|
||||
go func() {
|
||||
res, err := http.Post(
|
||||
c.Url,
|
||||
@ -71,19 +54,13 @@ func (c *LokiMetricConfig) SendMetricPing(domain, path, referrer string) {
|
||||
strings.NewReader(string(jsonData)),
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to send payload to Loki: %v", err)
|
||||
log.Errorf("Failed to send payload to: %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)
|
||||
if res.StatusCode != 200 {
|
||||
log.Errorf("Server returned non-200 status code %d", res.StatusCode)
|
||||
}
|
||||
}()
|
||||
}
|
||||
@ -91,7 +68,7 @@ func (c *LokiMetricConfig) SendMetricPing(domain, path, referrer string) {
|
||||
// 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)
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Warnf("Failed to read bot metrics file: %v", err)
|
||||
return []regexp.Regexp{}, err
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func TestShouldPing(t *testing.T) {
|
||||
cfg := LokiMetricConfig{
|
||||
cfg := MetricConfig{
|
||||
Enabled: true,
|
||||
Url: "",
|
||||
BotUserAgents: &[]regexp.Regexp{
|
||||
|
Loading…
Reference in New Issue
Block a user