Scheduler + Observe
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
@@ -97,53 +98,49 @@ func (o *LokiController) GetLogs() {
|
||||
// The server connects to Loki's /loki/api/v1/tail WebSocket endpoint and
|
||||
// forwards every message it receives until the client disconnects.
|
||||
func LogsStreamHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("LogsStreamHandler")
|
||||
execID := strings.TrimSuffix(
|
||||
strings.TrimPrefix(r.URL.Path, "/oc/logs/"),
|
||||
"",
|
||||
)
|
||||
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
fmt.Println("LogsStreamHandler", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
/*
|
||||
var query map[string]interface{}
|
||||
if err := conn.ReadJSON(&query); err != nil {
|
||||
fmt.Println("LogsStreamHandler ReadJSON", err)
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
var query map[string]interface{}
|
||||
if err := conn.ReadJSON(&query); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
start := fmt.Sprintf("%v", query["start"])
|
||||
if len(start) > 10 {
|
||||
start = start[:10]
|
||||
}
|
||||
|
||||
start := time.Now().UTC().UnixNano()
|
||||
labels := []string{
|
||||
"workflow_execution_id=\"" + execID + "\"",
|
||||
}
|
||||
for k, v := range query {
|
||||
if k == "start" || k == "end" {
|
||||
continue
|
||||
}
|
||||
labels = append(labels, fmt.Sprintf("%v=\"%v\"", k, v))
|
||||
}
|
||||
|
||||
if len(labels) == 0 || len(start) < 10 {
|
||||
_ = conn.WriteJSON(map[string]string{"error": "missing start or query labels"})
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("LOKI START", start, labels)
|
||||
// Build Loki tail WS URL (http→ws, https→wss).
|
||||
lokiBase := config.GetConfig().LokiUrl
|
||||
lokiBase = strings.Replace(lokiBase, "https://", "wss://", 1)
|
||||
lokiBase = strings.Replace(lokiBase, "http://", "ws://", 1)
|
||||
|
||||
lokiURL := lokiBase + "/loki/api/v1/tail?" + url.Values{
|
||||
"query": {"{" + strings.Join(labels, ", ") + "}"},
|
||||
"start": {start + "000000000"}, // seconds → nanoseconds
|
||||
"start": {fmt.Sprintf("%v", start)},
|
||||
}.Encode()
|
||||
|
||||
lokiConn, _, err := gorillaws.DefaultDialer.Dial(lokiURL, nil)
|
||||
headers := http.Header{}
|
||||
headers.Set("X-Scope-OrgID", "1")
|
||||
|
||||
lokiConn, resp, err := gorillaws.DefaultDialer.Dial(lokiURL, headers)
|
||||
fmt.Println("LOKI LISTEN", lokiBase, err)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
fmt.Printf("Handshake failed: status=%d body=%s", resp.StatusCode, string(body))
|
||||
}
|
||||
_ = conn.WriteJSON(map[string]string{"error": "loki: " + err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -161,6 +158,7 @@ func LogsStreamHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
var result map[string]interface{}
|
||||
if json.Unmarshal(msg, &result) == nil {
|
||||
fmt.Println(result)
|
||||
if err := conn.WriteJSON(result); err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user