Something went wrong. Try again.
Go port of clojure riemann
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123package main
import ( "context" "encoding/json" "log" "net/url" "sync/atomic" "time"
"github.com/gorilla/websocket" "go.iscode.ca/riemann")
func main() { log.Println("Starting Riemann Go Port Functional Validation Test...")
// 1. Initialize Go Riemann Core core := riemann.NewCore() core.AddTCPServer("127.0.0.1:6555") core.AddWebServer("127.0.0.1:6556")
// Set up simple stream pipeline: insert all events into index core.Streams( riemann.IndexStream(core.Index), )
// Start Go Riemann Core err := core.Start() if err != nil { log.Fatalf("Failed to start Go Riemann Core: %v", err) } defer core.Stop() log.Println("Go Riemann Core started successfully:") log.Println(" - TCP Server: 127.0.0.1:6555") log.Println(" - HTTP/WS Server: 127.0.0.1:6556")
// 2. Connect to the production Riemann WebSocket feed prodWSURL := url.URL{ Scheme: "ws", Host: "127.0.0.1:5556", Path: "/index", RawQuery: "query=true&subscribe=true", } log.Printf("Connecting to production Riemann stream at %s...", prodWSURL.String())
dialer := websocket.DefaultDialer conn, _, err := dialer.Dial(prodWSURL.String(), nil) if err != nil { log.Fatalf("Failed to connect to production WebSocket: %v", err) } defer func() { _ = conn.Close() }() log.Println("Connected to production feed! Listening for events...")
// 3. Channel to coordinate test duration (stream events for 8 seconds) ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) defer cancel()
var eventCount atomic.Int64
// Go routine to read production events and bridge them to the Go Core go func() { for { select { case <-ctx.Done(): return default: _, message, err := conn.ReadMessage() if err != nil { log.Printf("Read from production WebSocket failed: %v", err) return }
var ev riemann.Event err = json.Unmarshal(message, &ev) if err != nil { log.Printf("Failed to unmarshal event: %v. Raw: %s", err, string(message)) continue }
// Forward event into our Go Riemann Core pipeline core.Send(&ev)
count := eventCount.Add(1) if count%10 == 0 { log.Printf("Bridged %d events to Go Riemann...", count) } } } }()
// Wait for the duration to elapse <-ctx.Done() log.Printf("Bridging finished. Total events bridged: %d", eventCount.Load())
// 4. Query verification: query the Go Riemann index log.Println("\nRunning Verification Queries against Go Riemann Index...")
// Query 1: match all events (empty query or true) q1, err := riemann.ParseQuery("true") if err != nil { log.Fatalf("Failed to parse query: %v", err) } results := core.Index.Search(q1) log.Printf("Query 'true' returned %d active indexed states in Go Riemann.", len(results))
// Print a sample of 3 events from Go Riemann index sampleCount := min(len(results), 3) log.Printf("Sample of %d indexed events:", sampleCount) for i := range sampleCount { e := results[i] metric, _ := e.MetricAsFloat64() log.Printf(" - Host: %q | Service: %q | State: %q | Metric: %v", e.Host, e.Service, e.State, metric) }
// Query 2: Try a specific query (like service name starting with anything or matching some prefix if there are any) q2, err := riemann.ParseQuery(`service =~ "%"`) if err == nil { res2 := core.Index.Search(q2) log.Printf("Query 'service =~ \"%%\"' returned %d indexed states.", len(res2)) }}