57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
|
package pages
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type LokiMetricConfig struct {
|
||
|
Url string
|
||
|
Enabled bool
|
||
|
}
|
||
|
|
||
|
// Checks if we should send a metric ping to Loki based on the served path.
|
||
|
func (c *LokiMetricConfig) shouldSendMetrics(path string) bool {
|
||
|
return strings.HasSuffix(path, ".html") && c.Enabled
|
||
|
}
|
||
|
|
||
|
func (c *LokiMetricConfig) sendMetricPing(domain, path string) {
|
||
|
data := map[string]interface{}{
|
||
|
"steams": []map[string]interface{}{
|
||
|
{
|
||
|
"stream": map[string]string{
|
||
|
// Labels
|
||
|
"service": "rio",
|
||
|
"domain": domain,
|
||
|
"type": "metric",
|
||
|
},
|
||
|
"values": [][]interface{}{
|
||
|
{
|
||
|
strconv.Itoa(int(time.Now().UnixNano())),
|
||
|
"path=" + path,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
jsonData, err := json.Marshal(data)
|
||
|
if err != nil {
|
||
|
log.Errorf("Failed to send metric ping to Loki: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Send the ping to the Loki server
|
||
|
go func() {
|
||
|
http.Post(
|
||
|
c.Url,
|
||
|
"application/json",
|
||
|
strings.NewReader(string(jsonData)),
|
||
|
)
|
||
|
}()
|
||
|
}
|