initial commit - untested

This commit is contained in:
ycc 2024-07-04 09:14:25 +02:00
parent 4611e44c4c
commit f7eb7e4b81
4 changed files with 1947 additions and 0 deletions

12
go.mod Normal file
View File

@ -0,0 +1,12 @@
module oc-monitor
go 1.22.0
require github.com/grafana/loki-client-go v0.0.0-20230116142646-e7494d0ef70c
require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
)

1832
go.sum Normal file

File diff suppressed because it is too large Load Diff

78
loki_logger.go Normal file
View File

@ -0,0 +1,78 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type LokiLogger struct {
url string
client *http.Client
}
func NewLokiLogger(url string) *LokiLogger {
return &LokiLogger{
url: url,
client: &http.Client{Timeout: 10 * time.Second},
}
}
type logEntry struct {
Timestamp int64 `json:"ts"`
Line string `json:"line"`
}
type logStream struct {
Labels string `json:"labels"`
Entries []logEntry `json:"entries"`
}
type pushRequest struct {
Streams []logStream `json:"streams"`
}
func (l *LokiLogger) Log(label string, message string) error {
entry := logEntry{
Timestamp: time.Now().UnixNano(),
Line: message,
}
stream := logStream{
Labels: label,
Entries: []logEntry{entry},
}
reqBody := pushRequest{
Streams: []logStream{stream},
}
body, err := json.Marshal(reqBody)
if err != nil {
return err
}
req, err := http.NewRequest("POST", l.url, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := l.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func (l *LokiLogger) Close() {
// No cleanup needed for pure HTTP client
}

25
main.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"log"
"os/exec"
)
func main() {
// Initialize LokiLogger
lokiLogger := NewLokiLogger("http://localhost:3100/loki/api/v1/push") // Replace with your Loki URL
// Run the Argo command
cmd := exec.Command("argo", "submit", "your-workflow.yaml")
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("failed to run Argo command: %v", err)
}
// Send logs to Loki
if err := lokiLogger.Log(`{job="argo"}`, string(output)); err != nil {
log.Fatalf("failed to send logs to Loki: %v", err)
}
log.Println("Logs sent to Loki successfully.")
}