loki disable if non valid url
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2026-02-09 19:06:47 +01:00
parent cd24da25d2
commit c784f6f315
2 changed files with 209 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"sync"
"time"
)
@@ -13,6 +14,7 @@ type LokiWriter struct {
url string
labels map[string]string
httpClient *http.Client
disabled bool
// Circuit breaker fields
mu sync.RWMutex
@@ -38,15 +40,29 @@ const (
warningInterval = 1 * time.Minute // Minimum time between warning messages
)
func NewLokiWriter(url string, labels map[string]string) *LokiWriter {
func NewLokiWriter(rawURL string, labels map[string]string) *LokiWriter {
disabled := false
if rawURL == "" {
disabled = true
} else {
u, err := url.ParseRequestURI(rawURL)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
disabled = true
}
}
return &LokiWriter{
url: url,
url: rawURL,
labels: labels,
httpClient: &http.Client{},
disabled: disabled,
}
}
func (w *LokiWriter) Write(p []byte) (n int, err error) {
if w.disabled {
return len(p), nil
}
// Check circuit breaker status
w.mu.RLock()
if w.circuitOpen {