Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 534524c

Browse files
authored
Merge pull request #73 from tetratelabs/fix-example
fix examples
2 parents 4953c64 + d294990 commit 534524c

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build.example:
88
tinygo build -o ./examples/${name}/main.go.wasm -scheduler=none -target=wasi -wasm-abi=generic ./examples/${name}/main.go
99

1010
build.examples:
11-
find ./examples -type f -name "main.go" | xargs -Ip tinygo build -o p.wasm -target=wasi -wasm-abi=generic p
11+
find ./examples -type f -name "main.go" | xargs -Ip tinygo build -o p.wasm -scheduler=none -target=wasi -wasm-abi=generic p
1212

1313
lint:
1414
golangci-lint run --build-tags proxytest

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@ proxy-wasm-go-sdk is powered by [TinyGo](https://tinygo.org/) and does not suppo
99

1010

1111
```golang
12-
1312
import (
1413
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
1514
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1615
)
1716

1817
var counter proxywasm.MetricCounter
1918

20-
const metricName = "proxy_wasm_go.request_counter"
21-
22-
type context struct{ proxywasm.DefaultContext }
19+
type metricRootContext struct { proxywasm.DefaultRootContext }
2320

24-
func (ctx *context) OnVMStart(int) bool {
25-
// initialize the new metric
26-
counter, _ = proxywasm.DefineCounterMetric(metricName)
21+
func (ctx *metricRootContext) OnVMStart(int) bool {
22+
// initialize the metric
23+
counter = proxywasm.DefineCounterMetric("proxy_wasm_go.request_counter")
2724
return true
2825
}
2926

30-
func (ctx *context) OnHttpRequestHeaders(int, bool) types.Action {
27+
type metricHttpContext struct { proxywasm.DefaultHttpContext }
28+
29+
func (ctx *metricHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
3130
// increment the request counter when we receive request headers
32-
counter.Increment(1)
31+
counter.Increment(1)
3332
return types.ActionContinue
3433
}
35-
3634
```
3735

3836
### requirements

e2e/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestE2E_helloworld(t *testing.T) {
6464
out := stdErr.String()
6565
fmt.Println(out)
6666
assert.True(t, strings.Contains(out, "wasm log helloworld: proxy_on_vm_start from Go!"))
67-
assert.True(t, strings.Contains(out, "wasm log helloworld: OnTick on "))
67+
assert.True(t, strings.Contains(out, "wasm log helloworld: It's"))
6868
}
6969

7070
func TestE2E_http_auth_random(t *testing.T) {

examples/helloworld/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package main
1616

1717
import (
18+
"math/rand"
19+
1820
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
1921
)
2022

@@ -36,15 +38,18 @@ func newHelloWorld(contextID uint32) proxywasm.RootContext {
3638

3739
// override
3840
func (ctx *helloWorld) OnVMStart(int) bool {
41+
rand.Seed(proxywasm.GetCurrentTime())
42+
3943
proxywasm.LogInfo("proxy_on_vm_start from Go!")
4044
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
4145
proxywasm.LogCriticalf("failed to set tick period: %v", err)
4246
}
47+
4348
return true
4449
}
4550

4651
// override
4752
func (ctx *helloWorld) OnTick() {
4853
t := proxywasm.GetCurrentTime()
49-
proxywasm.LogInfof("OnTick on %d, it's %d", ctx.contextID, t)
54+
proxywasm.LogInfof("It's %d: random value: %d", t, rand.Uint64())
5055
}

examples/helloworld/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestHelloWorld_OnTick(t *testing.T) {
2727
require.Greater(t, len(logs), 0)
2828
msg := logs[len(logs)-1]
2929

30-
assert.True(t, strings.Contains(msg, "OnTick on"))
30+
assert.True(t, strings.Contains(msg, "It's"))
3131
}
3232

3333
func TestHelloWorld_OnVMStart(t *testing.T) {

0 commit comments

Comments
 (0)