package riemann_test import ( "fmt" "time" "go.iscode.ca/riemann" ) // ExampleWhere demonstrates how to implement Clojure's `(where ...)` macro using // the riemann.Where filtering helper to route matching events to target streams. func ExampleWhere() { // Create a filtered stream that only routes events matching host "prod-db" prodFilter := riemann.Where(func(e *riemann.Event) bool { return e.Host == "prod-db" }, riemann.StreamFunc(func(e *riemann.Event) { fmt.Printf("Received filtered event: host=%s service=%s\n", e.Host, e.Service) })) // Send a matching event (will be forwarded to the handler) prodFilter.Send(&riemann.Event{Host: "prod-db", Service: "cpu"}) // Send a non-matching event (will be silently discarded) prodFilter.Send(&riemann.Event{Host: "dev-web", Service: "cpu"}) // Output: // Received filtered event: host=prod-db service=cpu } // ExampleRollup demonstrates how to implement Clojure's `(rollup ...)` macro // to buffer rapid event spikes and forward only the latest state at most once per interval. func ExampleRollup() { // Forward only the latest event at most once every 100 milliseconds rollupStream := riemann.Rollup(100*time.Millisecond, riemann.StreamFunc(func(e *riemann.Event) { fmt.Printf("Rollup output metric: %v\n", e.Metric) })) // Dispatch multiple events rapidly within the window rollupStream.Send(&riemann.Event{Host: "web", Service: "requests", Metric: 10}) rollupStream.Send(&riemann.Event{Host: "web", Service: "requests", Metric: 20}) rollupStream.Send(&riemann.Event{Host: "web", Service: "requests", Metric: 30}) // Wait for the rollup interval to fire time.Sleep(150 * time.Millisecond) // Output: // Rollup output metric: 30 } // ExampleRate demonstrates how to implement Clojure's `(rate ...)` macro // to compute event frequency per second over a sliding interval. func ExampleRate() { // Calculate event rate every 100 milliseconds and print the result rateStream := riemann.Rate(100*time.Millisecond, riemann.StreamFunc(func(e *riemann.Event) { val, _ := e.MetricAsFloat64() fmt.Printf("Event rate calculated: %s = %.1f events/sec\n", e.Service, val) })) // Send 3 events in the window (corresponds to 30 events/sec) rateStream.Send(&riemann.Event{Host: "web", Service: "requests"}) rateStream.Send(&riemann.Event{Host: "web", Service: "requests"}) rateStream.Send(&riemann.Event{Host: "web", Service: "requests"}) // Wait for the reporting interval time.Sleep(150 * time.Millisecond) // Output: // Event rate calculated: requests rate = 30.0 events/sec } // ExampleCore_streams demonstrates how to build and register a multi-branch // stream processing pipeline, mirroring a complete Clojure `(streams ...)` configuration. func ExampleCore_streams() { // Initialize the Core engine core := riemann.NewCore() // Define downstream stream destinations opsAlerts := riemann.StreamFunc(func(e *riemann.Event) { fmt.Printf("ALERT: service %q on host %q has entered state %q!\n", e.Service, e.Host, e.State) }) index := riemann.IndexStream(core.Index) // Equivalent of Clojure: // (streams // (where (and (service "disk") (state "critical")) // ops-alerts) // index) pipeline := riemann.StreamFunc(func(e *riemann.Event) { // 1. Where disk is critical, alert ops if e.Service == "disk" && e.State == "critical" { opsAlerts.Send(e) } // 2. Index all events index.Send(e) }) // Register the pipeline with Core core.Streams(pipeline) // Send an event that triggers the alert pipeline branch core.Send(&riemann.Event{Host: "prod-db-1", Service: "disk", State: "critical"}) // Send a standard event (reaches the index, but bypasses the alert) core.Send(&riemann.Event{Host: "prod-db-1", Service: "cpu", State: "ok"}) // Verify the standard event was successfully stored in the index ev, found := core.Index.Lookup("prod-db-1", "cpu") if found { fmt.Printf("Indexed event lookup: host=%s service=%s state=%s\n", ev.Host, ev.Service, ev.State) } // Output: // ALERT: service "disk" on host "prod-db-1" has entered state "critical"! // Indexed event lookup: host=prod-db-1 service=cpu state=ok } // ExampleStreamFunc_geoip demonstrates how a caller can implement custom GeoIP // enrichment middleware using third-party MaxMind GeoIP libraries, populating // resolved locations into the Event.Extra map. func ExampleStreamFunc_geoip() { // Dummy type definition matching the GeoIP record structure type GeoIPRecord struct { City string `json:"city"` CountryCode string `json:"country-code"` CountryName string `json:"country-name"` } // This is a custom stream middleware that mimics GeoIP enrichment LocateIPAddr := func(ipAttribute string, child riemann.Stream) riemann.Stream { return riemann.StreamFunc(func(e *riemann.Event) { if e.Attributes == nil { child.Send(e) return } ipStr, ok := e.Attributes[ipAttribute] if !ok || ipStr == "" { child.Send(e) return } // In a real application, you would use oschwald/geoip2-golang to read an MMDB file: // db, err := geoip2.Open("GeoLite2-City.mmdb") // record, err := db.City(net.ParseIP(ipStr)) // Here, we simulate resolving "8.8.8.8" to Mountain View, US if ipStr == "8.8.8.8" { g := &GeoIPRecord{ City: "Mountain View", CountryCode: "US", CountryName: "United States", } if e.Extra == nil { e.Extra = make(map[string]any) } e.Extra["geoip"] = g } child.Send(e) }) } // Define our downstream destination handler destination := riemann.StreamFunc(func(e *riemann.Event) { if e.Extra != nil { if geoVal, ok := e.Extra["geoip"]; ok { if g, ok := geoVal.(*GeoIPRecord); ok { fmt.Printf("Enriched event: host=%s ip=%s city=%s country=%s\n", e.Host, e.Attributes["ip_address"], g.City, g.CountryCode) return } } } fmt.Printf("Standard event: host=%s\n", e.Host) }) // Wrap the destination stream with our GeoIP resolver middleware geoIPMiddleware := LocateIPAddr("ip_address", destination) // Send an event with an IP address to trigger the enrichment geoIPMiddleware.Send(&riemann.Event{ Host: "dns-resolver", Attributes: map[string]string{ "ip_address": "8.8.8.8", }, }) // Output: // Enriched event: host=dns-resolver ip=8.8.8.8 city=Mountain View country=US }