Skip to content

Commit

Permalink
Add Experimental WASM/wasip1 support (#85)
Browse files Browse the repository at this point in the history
update dependencies
  • Loading branch information
jandelgado committed Oct 29, 2023
1 parent 5a321e1 commit e45e79d
Show file tree
Hide file tree
Showing 29 changed files with 324 additions and 180 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.20.x
go-version: 1.21.3
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
if: startsWith(github.ref, 'refs/tags/')
Expand All @@ -32,7 +32,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,37 @@ on:
- master

env:
GO_VERSION: 1.18.x
GO_VERSION: "1.21.3"
GO_LANG_CI_LINT_VERSION: "v1.53.3"

name: run tests
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Run linters
uses: golangci/golangci-lint-action@v3
with:
version: v1.46.2
version: ${{ env.GO_LANG_CI_LINT_VERSION }}

test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: create certificates
run: cd inttest/pki && ./mkcerts.sh
- name: Run RabbitMQ
run: cd inttest/rabbitmq && docker-compose up -d
- name: Install Go
if: success()
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: wait for RabbitMQ becoming ready
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for rabtap

## v1.39.0 (2023-10-29)

* experimental WASM support, use go 1.21, simplify code

## v1.38.2 (2023-02-24)

* update dependencies, use go 1.20
Expand Down
31 changes: 17 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ SOURCE=$(shell find . -name "*go" -a -not -path "./vendor/*" -not -path "./cmd/t
VERSION=$(shell git describe --tags)
TOXICMD:=docker-compose exec toxiproxy /go/bin/toxiproxy-cli

.PHONY: test-app test-lib build build tags short-test test run-broker clean dist-clean toxiproxy-setup toxiproxy-cmd
.PHONY: phony

build:
cd cmd/rabtap && GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \
"-s -w -X main.version=$(VERSION)" -o ../../bin/rabtap
build: phony
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \
"-s -w -X main.version=$(VERSION)" -o ./bin/rabtap ./cmd/rabtap
wasm-build: phony
CGO_ENABLED=1 GOOS=wasip1 GOARCH=wasm go build -o ./bin/rabtap-wasm ./cmd/rabtap

tags: $(SOURCE)
@gotags -f tags $(SOURCE)

lint:
lint: phony
golangci-lint run

short-test:
short-test: phony
go test -v -race github.com/jandelgado/rabtap/cmd/rabtap
go test -v -race github.com/jandelgado/rabtap/pkg

test-app:
test-app: phony
go test -race -v -tags "integration" -cover -coverprofile=coverage_app.out github.com/jandelgado/rabtap/cmd/rabtap

test-lib:
test-lib: phony
go test -race -v -tags "integration" -cover -coverprofile=coverage.out github.com/jandelgado/rabtap/pkg

test: test-app test-lib
Expand All @@ -32,24 +34,25 @@ test: test-app test-lib

# docker-compose up must be first called. Then create a proxy with
# this target and connect to to localhost:55672 (amqp).
toxiproxy-setup:
toxiproxy-setup: phony
$(TOXICMD) c amqp --listen :55672 --upstream rabbitmq:5672 || true

# call with e.g.
# make toxiproxy-cmd -- show help
# make TOXIARGS="toggle amqp" -- toggle amqp proxy
toxiproxy-cmd:
toxiproxy-cmd: phony
$(TOXICMD) $(TOXIARGS)

# run rabbitmq server for integration test using docker container.
run-broker:
run-broker: phony
cd inttest/pki && ./mkcerts.sh
cd inttest/rabbitmq && docker-compose up

dist-clean: clean
rm -rf *.out bin/ dist/

clean:
cd cmd/rabtap && go clean -r
cd cmd/testgen && go clean -r
clean: phony
go clean -r ./cmd/rabtap
go clean -r ./cmd/testgen

phony:
68 changes: 60 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ and exchanges, inspect broker.
* [Default RabbitMQ broker](#default-rabbitmq-broker)
* [Default RabbitMQ management API endpoint](#default-rabbitmq-management-api-endpoint)
* [Default RabbitMQ TLS config](#default-rabbitmq-tls-config)
* [Disable color output](#disable-color-output)
* [Colored output](#colored-output)
* [Examples](#examples)
* [Broker info](#broker-info)
* [Wire-tapping messages](#wire-tapping-messages)
Expand All @@ -56,6 +56,7 @@ and exchanges, inspect broker.
* [Build from source](#build-from-source)
* [Download and build using go install](#download-and-build-using-go-install)
* [Build using Makefile and tests](#build-using-makefile-and-tests)
* [Experimental WASM/wasip1 port](#experimental-wasmwasip1-port)
* [Test data generator](#test-data-generator)
* [Contributing](#contributing)
* [Author](#author)
Expand Down Expand Up @@ -135,21 +136,22 @@ compile from source.
## Usage

```
rabtap - RabbitMQ wire tap. github.com/jandelgado/rabtap
Usage:
rabtap -h|--help
rabtap info [--api=APIURI] [--consumers] [--stats] [--filter=EXPR] [--omit-empty]
[--show-default] [--mode=MODE] [--format=FORMAT] [-knv]
[--show-default] [--mode=MODE] [--format=FORMAT] [-kncv]
[(--tls-cert-file=CERTFILE --tls-key-file=KEYFILE)] [--tls-ca-file=CAFILE]
rabtap tap EXCHANGES [--uri=URI] [--saveto=DIR]
[--format=FORMAT] [--limit=NUM] [--idle-timeout=DURATION] [-jknsv]
[--format=FORMAT] [--limit=NUM] [--idle-timeout=DURATION] [-jkncsv]
[(--tls-cert-file=CERTFILE --tls-key-file=KEYFILE)] [--tls-ca-file=CAFILE]
rabtap (tap --uri=URI EXCHANGES)... [--saveto=DIR]
[--format=FORMAT] [--limit=NUM] [--idle-timeout=DURATION] [-jknsv]
[--format=FORMAT] [--limit=NUM] [--idle-timeout=DURATION] [-jkncsv]
[(--tls-cert-file=CERTFILE --tls-key-file=KEYFILE)] [--tls-ca-file=CAFILE]
rabtap sub QUEUE [--uri URI] [--saveto=DIR] [--format=FORMAT] [--limit=NUM]
[--offset=OFFSET] [--args=KV]... [(--reject [--requeue])] [-jksvn]
[--offset=OFFSET] [--args=KV]... [(--reject [--requeue])] [-jkcsvn]
[--idle-timeout=DURATION]
[(--tls-cert-file=CERTFILE --tls-key-file=KEYFILE)] [--tls-ca-file=CAFILE]
rabtap pub [--uri=URI] [SOURCE] [--exchange=EXCHANGE] [--format=FORMAT]
Expand Down Expand Up @@ -199,6 +201,7 @@ Arguments and options:
additional arguments. e.g. '--args=x-queue-type=quorum'
-b, --bindingkey=KEY binding key to use in bind queue command.
--by-connection output of info command starts with connections.
-c, --color force colored output
--confirms enable publisher confirms and wait for confirmations.
--consumers include consumers and connections in output of info command.
--delay=DELAY Time to wait between sending messages during publish.
Expand Down Expand Up @@ -379,9 +382,12 @@ $ echo "Hello" | rabtap pub --exchange amq.topic --routingkey "key"
...
```

#### Disable color output
#### Colored output

Set environment variable `NO_COLOR` to disable color output.
Output is colored, when writing to a terminal. This behaviour can be changed:
* set environment variable `NO_COLOR` to disable color output (or set
`--no-color` option)
* set `--color` option to force colored output

### Examples

Expand Down Expand Up @@ -957,6 +963,52 @@ run on localhost. Easiest way to start one is running `make run-broker`, which
will start a RabbitMQ docker container (i.e. `docker run -ti --rm -p 5672:5672
-p 15672:15672 rabbitmq:3-management`).

### Experimental WASM/wasip1 port

Rabtap can be compiled for Web Assembly (WASM) and the new `wasip1` `GOOS` and
run on the console using, e.g.
[wasirun](https://github.com/stealthrocket/wasi-go). Example:

```
$ go version
go version go1.21.3 linux/amd64
$ make wasm-build
CGO_ENABLED=1 GOOS=wasip1 GOARCH=wasm go build -o ./bin/rabtap-wasm ./cmd/rabtap
$ wasirun --version
wasirun v0.6.5
$ wasirun bin/rabtap-wasm -- --api "http://guest:password@localhost:15672/api" info --no-color
http://localhost:15672/api (broker ver='3.12.6', mgmt ver='3.12.6', cluster='rabbit@3ea5bf2bac2f')
└─ Vhost /
├─ amq.direct (exchange(direct), [D])
├─ amq.fanout (exchange(fanout), [D])
├─ amq.headers (exchange(headers), [D])
├─ amq.match (exchange(headers), [D])
├─ amq.rabbitmq.trace (exchange(topic), [D|I])
└─ amq.topic (exchange(topic), [D])
```

Another example using `wasirun` and `wasmedge` to publish and subscribe
to a queue:

```
$ URI="amqp://guest:password@localhost/"
$ rabtap queue create test
$ rabtap queue bind test to amq.topic --bindingkey=key
$ echo "hello" | wasmedge bin/rabtap-wasm --uri "$URI" pub --exchange amq.topic --routingkey=key
$ wasirun bin/rabtap-wasm -- --uri "$URI" sub test --limit=1
------ message received on 2023-10-29T11:48:56Z ------
exchange.......: amq.topic
routingkey.....: key
hello
```
See [my blog](https://jandelgado.github.io/blog/posts/rabtap-wasm) for details.

Limitations:
* environment variables like `RABTAP_AMQPURI` not supported, must specify
all options on the command line
* depending on the runtime, colors must explicitly set or disabled using `--color` and `--no-color`,
since terminal detection not working.

## Test data generator

A simple [test data generator tool](cmd/testgen/README.md) for manual tests is
Expand All @@ -978,6 +1030,6 @@ Jan Delgado (jdelgado at gmx dot net)

## Copyright and license

Copyright (c) 2017-2021 Jan Delgado.
Copyright (c) 2017-2023 Jan Delgado.
rabtap is licensed under the GPLv3 license.

1 change: 0 additions & 1 deletion cmd/rabtap/broker_info_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type BrokerInfoRendererConfig struct {
Format string // "text", "dot"
ShowStats bool
NoColor bool
}

// BrokerInfoRenderer renders a tree representation represented by a rootNode
Expand Down
2 changes: 1 addition & 1 deletion cmd/rabtap/broker_info_renderer_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type brokerInfoRendererText struct {
// NewBrokerInfoRendererText returns a BrokerInfoRenderer that renders for
// text console.
func NewBrokerInfoRendererText(config BrokerInfoRendererConfig) BrokerInfoRenderer {
colorizer := NewColorPrinter(config.NoColor)
colorizer := NewColorPrinter()
// TODO inject
templateFuncs := MergeTemplateFuncs(colorizer.GetFuncMap(), RabtapTemplateFuncs)
return &brokerInfoRendererText{
Expand Down
9 changes: 5 additions & 4 deletions cmd/rabtap/cmd_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"testing"

"github.com/fatih/color"
rabtap "github.com/jandelgado/rabtap/pkg"
"github.com/jandelgado/rabtap/pkg/testcommon"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,6 +40,7 @@ func TestCmdInfoByExchangeInTextFormatProducesExpectedTree(t *testing.T) {

var actual bytes.Buffer
rootURL, _ := url.Parse("http://rabbitmq/api")
color.NoColor = true
cmdInfo(context.TODO(),
CmdInfoArg{
rootNode: rootURL,
Expand All @@ -51,8 +53,7 @@ func TestCmdInfoByExchangeInTextFormatProducesExpectedTree(t *testing.T) {
OmitEmptyExchanges: false},
renderConfig: BrokerInfoRendererConfig{
Format: "text",
ShowStats: false,
NoColor: true},
ShowStats: false},
out: &actual})

expected := `http://rabbitmq/api (broker ver='3.6.9', mgmt ver='3.6.9', cluster='rabbit@08f57d1fe8ab')
Expand Down Expand Up @@ -97,6 +98,7 @@ func TestCmdInfoByConnectionInTextFormatProducesExpectedTree(t *testing.T) {
rootURL, _ := url.Parse("http://rabbitmq/api")

var actual bytes.Buffer
color.NoColor = true
cmdInfo(context.TODO(),
CmdInfoArg{
rootNode: rootURL,
Expand All @@ -109,8 +111,7 @@ func TestCmdInfoByConnectionInTextFormatProducesExpectedTree(t *testing.T) {
OmitEmptyExchanges: false},
renderConfig: BrokerInfoRendererConfig{
Format: "text",
ShowStats: false,
NoColor: true},
ShowStats: false},
out: &actual})

expected := `http://rabbitmq/api (broker ver='3.6.9', mgmt ver='3.6.9', cluster='rabbit@08f57d1fe8ab')
Expand Down
18 changes: 2 additions & 16 deletions cmd/rabtap/color_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package main
// print colors on the console, supporting golang templating

import (
"fmt"
"io"
"os"
"text/template"
Expand Down Expand Up @@ -66,21 +65,8 @@ func NewColorableWriter(file *os.File) io.Writer {
}

// NewColorPrinter returns a ColorPrinter used to color the console
// output. If noColor is set to true, a no-op color printer is returned.
func NewColorPrinter(noColor bool) ColorPrinter {
if noColor {
nullPrinter := fmt.Sprint
return ColorPrinter{nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter,
nullPrinter}
}
// output. Use color.NoColor = true to disable colors globally.
func NewColorPrinter() ColorPrinter {
return ColorPrinter{
URL: color.New(colorURL).SprintFunc(),
VHost: color.New(colorVHost).SprintFunc(),
Expand Down
Loading

0 comments on commit e45e79d

Please sign in to comment.