Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
216 changes: 216 additions & 0 deletions cmd/mirabelle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package cmd

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/coder/websocket"
"github.com/hokaccha/go-prettyjson"
"github.com/spf13/cobra"
)

func mirabelleHost() string {
mirabelleHost := os.Getenv("MIRABELLE_API_ENDPOINT")
if mirabelleHost == "" {
mirabelleHost = "localhost:5558"
}
return mirabelleHost
}

func mirabelleSubscribeCmd() *cobra.Command {
var query string
var channel string
var subscribeCmd = &cobra.Command{
Use: "subscribe",
Short: "Subscribe to a Mirabelle channel",
Run: func(cmd *cobra.Command, args []string) {
options := websocket.DialOptions{
HTTPHeader: http.Header{},
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
host := mirabelleHost()
values := url.Values{}
values.Add("query", base64.StdEncoding.EncodeToString([]byte(query)))
c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://%s/channel/%s?%s", host, channel, values.Encode()), &options)
cancel()
exitIfError(err)

signals := make(chan os.Signal, 1)
signal.Notify(
signals,
syscall.SIGINT,
syscall.SIGTERM)

var wg sync.WaitGroup
wg.Add(1)
wsCtx, wsCancel := context.WithCancel(context.Background())
go func() {
defer wg.Done()
ticker := time.NewTicker(8 * time.Second)
for {
select {
case <-wsCtx.Done():
return
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
err := c.Ping(ctx)
cancel()
if err != nil {
fmt.Printf("fail to send websocket heartbeat: %s", err.Error())
}
}

}
}()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-wsCtx.Done():
return
case s := <-signals:
if s == syscall.SIGINT || s == syscall.SIGTERM {
wsCancel()
return
}
}
}

}()
wg.Add(1)
go func() {
defer wg.Done()
formatter := prettyjson.NewFormatter()
for {
_, result, err := c.Read(wsCtx)
if err != nil {
fmt.Println("websocket error: " + err.Error())
wsCancel()
return
}
indented := &bytes.Buffer{}
if err := json.Indent(indented, result, "", " "); err != nil {
fmt.Println("fail to parse JSON response: " + err.Error())

}
fmt.Println(time.Now().Format("2006-01-02 15:04:05.00000"))
data, err := formatter.Format(result)
if err != nil {
fmt.Println("fail to format JSON response: " + err.Error())
}
fmt.Println(string(data))
}
}()
wg.Wait()
c.Close(websocket.StatusNormalClosure, "")

},
}
subscribeCmd.PersistentFlags().StringVar(&query, "query", "[:true]", "Query to execute")
subscribeCmd.PersistentFlags().StringVar(&channel, "channel", "", "Channel to subscribe to")
err := subscribeCmd.MarkPersistentFlagRequired("channel")
exitIfError(err)

return subscribeCmd
}

type Event struct {
Host string `json:"host,omitempty"`
Metric float64 `json:"metric,omitempty"`
Service string `json:"service,omitempty"`
Time int64 `json:"time"`
Name string `json:"name,omitempty"`
State string `json:"state,omitempty"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
Attributes map[string]string `json:"attributes,omitempty"`
}

type EventPayload struct {
Events []Event `json:"events"`
}

func mirabelleEventCmd() *cobra.Command {
var service string
var metric float64
var name string
var attributes []string
var state string
var description string
var stream string
var tags []string

var sendCmd = &cobra.Command{
Use: "send",
Short: "Send an event to Mirabelle",
Run: func(cmd *cobra.Command, args []string) {
now := time.Now().UnixNano()
hostname, err := os.Hostname()
exitIfError(err)
attributesMap, err := toMap(attributes)
exitIfError(err)
event := Event{
Metric: metric,
Host: hostname,
Service: service,
Time: now,
Name: name,
State: state,
Description: description,
Tags: tags,
Attributes: attributesMap,
}
client := http.Client{}
var reqBody io.Reader
payload := EventPayload{
Events: []Event{event},
}
json, err := json.Marshal(payload)
exitIfError(err)
reqBody = bytes.NewBuffer(json)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
host := mirabelleHost()
request, err := http.NewRequestWithContext(
ctx,
http.MethodPut,
fmt.Sprintf("http://%s/api/v1/stream/%s", host, stream),
reqBody)
exitIfError(err)
request.Header.Add("content-type", "application/json")
response, err := client.Do(request)
exitIfError(err)
b, err := io.ReadAll(response.Body)
exitIfError(err)
defer response.Body.Close()
if response.StatusCode >= 300 {
exitIfError(fmt.Errorf("Fail to send event (status %s): %s", response.Status, string(b)))
} else {
fmt.Println("Event successfully sent")
}

},
}
sendCmd.PersistentFlags().StringVar(&stream, "stream", "default", "The stream to which the event shold be sent")
sendCmd.PersistentFlags().StringVar(&service, "service", "", "Service name")
sendCmd.PersistentFlags().Float64Var(&metric, "metric", 0, "Metric value (float64)")
sendCmd.PersistentFlags().StringVar(&name, "name", "", "Event name")
sendCmd.PersistentFlags().StringVar(&description, "description", "", "Event description")
sendCmd.PersistentFlags().StringVar(&state, "state", "", "Event state")
sendCmd.PersistentFlags().StringSliceVar(&attributes, "attributes", []string{}, "key-value attributes (example: foo=bar)")
sendCmd.PersistentFlags().StringSliceVar(&tags, "tags", []string{}, "Event tags")

return sendCmd
}
15 changes: 15 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ func Execute() error {
rootCmd.AddCommand(healthcheck)
rootCmd.AddCommand(pushgateway)

// mirabelle

var mirabelle = &cobra.Command{
Use: "mirabelle",
Short: "Interact with Mirabelle",
}
mirabelle.AddCommand(mirabelleSubscribeCmd())
var mirabelleEvent = &cobra.Command{
Use: "event",
Short: "Manage Mirabelle events",
}
mirabelleEvent.AddCommand(mirabelleEventCmd())
mirabelle.AddCommand(mirabelleEvent)
rootCmd.AddCommand(mirabelle)

return rootCmd.Execute()
}

Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ go 1.22.0
require (
github.com/appclacks/go-client v0.0.0-20240715201443-0ce681171dc2
github.com/cheynewallace/tabby v1.1.1
github.com/coder/websocket v1.8.12
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f
github.com/spf13/cobra v1.8.0
)

require (
github.com/fatih/color v1.17.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.18.0 // indirect
)
31 changes: 15 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225 h1:Bi2DlXudM58MLRjAEWRNrJe8ChG54r5mE8aJTH6AkUg=
github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 h1:kTNVmMjdtr/X0ljUCyDCXUzDYnP3JL5fdDMwBSUveOg=
github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240618204225-51a556fa72ba h1://S8sLRrM/2eJLfYXcFOpsP7eDGwKDrHj0rqlXj6uSo=
github.com/appclacks/go-client v0.0.0-20240618204225-51a556fa72ba/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703201525-421246966bba h1:alp3NoMSmJ9bAw6Ubo02F3z4aBVM5HIkXplKOuaj4mc=
github.com/appclacks/go-client v0.0.0-20240703201525-421246966bba/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703203820-f7d948812083 h1:ESPN+6uiockjKr2XnTRQBkleMQsT5PH9S/CjVUIUdqg=
github.com/appclacks/go-client v0.0.0-20240703203820-f7d948812083/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703204325-c8a3257d9e08 h1:vjf4pQGIx8BCj1+wEsNBR04lyVzje74O7ozJQn6JO3w=
github.com/appclacks/go-client v0.0.0-20240703204325-c8a3257d9e08/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703205617-8b0425724375 h1:eVJ+l4qRByZ2L9O4xl2HC009T7TMw2z5EPnf+ArUH3A=
github.com/appclacks/go-client v0.0.0-20240703205617-8b0425724375/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040 h1:1Y8zxzWnIJJTN35I1m19Pf3Li7eRLQHAoAjjAta5ck4=
github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240715201443-0ce681171dc2 h1:Ep/5yN6WEakl+mVhHSrhmxtxQWKU5ZzA1db+b9N2fTE=
github.com/appclacks/go-client v0.0.0-20240715201443-0ce681171dc2/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/cheynewallace/tabby v1.1.1 h1:JvUR8waht4Y0S3JF17G6Vhyt+FRhnqVCkk8l4YrOU54=
github.com/cheynewallace/tabby v1.1.1/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions vendor/github.com/coder/websocket/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading