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

Commit d359ac1

Browse files
authored
Merge pull request #66 from tetratelabs/cleanup-examples
cleanup examples
2 parents fc8d355 + a1944e0 commit d359ac1

File tree

12 files changed

+160
-144
lines changed

12 files changed

+160
-144
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ run:
2525
docker run --entrypoint='/usr/local/bin/envoy' \
2626
-p 18000:18000 -p 8099:8099 \
2727
-w /tmp/envoy -v $(shell pwd):/tmp/envoy getenvoy/proxy-wasm-go-sdk-ci:istio-${ISTIO_VERSION} \
28-
-c /tmp/envoy/examples/${name}/envoy.yaml --concurrency 2
28+
-c /tmp/envoy/examples/${name}/envoy.yaml --concurrency 2 \
29+
--log-format-prefix-with-location '0' --log-format '%v' # --log-format-prefix-with-location will be removed at 1.17.0 release

examples/helloworld/README.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
## helloworld
22

3-
this example handles http request/response headers events and log all headers.
3+
this example periodically logs the current time in nanoseconds
44

5-
6-
```bash
7-
proxy_1 | [2020-03-25 09:04:56.149][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096149656000
8-
proxy_1 | [2020-03-25 09:04:56.154][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096154901000
9-
proxy_1 | [2020-03-25 09:04:56.172][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096172849000
10-
proxy_1 | [2020-03-25 09:04:56.174][15][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096174829000
11-
proxy_1 | [2020-03-25 09:04:56.177][14][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096177651000
12-
proxy_1 | [2020-03-25 09:04:57.149][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127097149361000
13-
proxy_1 | [2020-03-25 09:04:57.155][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127097155247000
14-
proxy_1 | [2020-03-25 09:04:57.174][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127097174066000
15-
proxy_1 | [2020-03-25 09:04:57.176][15][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127097176042000
16-
proxy_1 | [2020-03-25 09:04:57.178][14][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127097178863000
17-
proxy_1 | [2020-03-25 09:04:58.149][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127098149674000
18-
proxy_1 | [2020-03-25 09:04:58.155][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127098155890000
19-
proxy_1 | [2020-03-25 09:04:58.173][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127098173918000
20-
proxy_1 | [2020-03-25 09:04:58.176][15][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127098176045000
21-
proxy_1 | [2020-03-25 09:04:58.178][14][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127098178962000
22-
proxy_1 | [2020-03-25 09:04:59.150][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127099150824000
23-
proxy_1 | [2020-03-25 09:04:59.156][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127099156638000
24-
proxy_1 | [2020-03-25 09:04:59.174][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127099174223000
25-
proxy_1 | [2020-03-25 09:04:59.176][15][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127099176272000
26-
proxy_1 | [2020-03-25 09:04:59.179][14][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127099179191000
5+
```
6+
wasm log helloworld: proxy_on_vm_start from Go!
7+
wasm log helloworld: proxy_on_vm_start from Go!
8+
wasm log helloworld: OnTick on 1, it's 1601543078943978000
9+
wasm log helloworld: OnTick on 1, it's 1601543078947916000
10+
wasm log helloworld: OnTick on 1, it's 1601543078951979000
11+
wasm log helloworld: OnTick on 1, it's 1601543079947462000
12+
wasm log helloworld: OnTick on 1, it's 1601543079951503000
13+
wasm log helloworld: OnTick on 1, it's 1601543079955484000
2714
```

examples/http_auth_random/README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
this example authorize requests depending on the hash values of a response from http://httpbin.org/uuid.
44

5-
```bash
5+
### message on clients
6+
```
67
$ curl localhost:18000/uuid -v
78
* Trying ::1...
89
* TCP_NODELAY set
@@ -46,4 +47,47 @@ $ curl localhost:18000/uuid -v
4647
* Connection #0 to host localhost left intact
4748
access forbidden
4849
49-
```
50+
```
51+
52+
### message on Envoy
53+
54+
```
55+
wasm log: request header from: :authority: localhost:18000
56+
wasm log: request header from: :path: /uuid
57+
wasm log: request header from: :method: GET
58+
wasm log: request header from: user-agent: curl/7.68.0
59+
wasm log: request header from: accept: */*
60+
wasm log: request header from: x-forwarded-proto: http
61+
wasm log: request header from: x-request-id: fddeac7b-db59-453c-9956-7f1050dbf6d5
62+
wasm log: http call dispatched to httpbin
63+
wasm log: response header from httpbin: :status: 200
64+
wasm log: response header from httpbin: date: Thu, 01 Oct 2020 09:07:32 GMT
65+
wasm log: response header from httpbin: content-type: application/json
66+
wasm log: response header from httpbin: content-length: 53
67+
wasm log: response header from httpbin: connection: keep-alive
68+
wasm log: response header from httpbin: server: gunicorn/19.9.0
69+
wasm log: response header from httpbin: access-control-allow-origin: *
70+
wasm log: response header from httpbin: access-control-allow-credentials: true
71+
wasm log: response header from httpbin: x-envoy-upstream-service-time: 340
72+
wasm log: access granted
73+
wasm log: 2 finished
74+
wasm log: request header from: :authority: localhost:18000
75+
wasm log: request header from: :path: /uuid
76+
wasm log: request header from: :method: GET
77+
wasm log: request header from: user-agent: curl/7.68.0
78+
wasm log: request header from: accept: */*
79+
wasm log: request header from: x-forwarded-proto: http
80+
wasm log: request header from: x-request-id: 02628dd2-b985-4c4f-a2d2-164589c16f53
81+
wasm log: http call dispatched to httpbin
82+
wasm log: response header from httpbin: :status: 200
83+
wasm log: response header from httpbin: date: Thu, 01 Oct 2020 09:07:34 GMT
84+
wasm log: response header from httpbin: content-type: application/json
85+
wasm log: response header from httpbin: content-length: 53
86+
wasm log: response header from httpbin: connection: keep-alive
87+
wasm log: response header from httpbin: server: gunicorn/19.9.0
88+
wasm log: response header from httpbin: access-control-allow-origin: *
89+
wasm log: response header from httpbin: access-control-allow-credentials: true
90+
wasm log: response header from httpbin: x-envoy-upstream-service-time: 350
91+
wasm log: access forbidden
92+
wasm log: 3 finished
93+
```

examples/http_headers/README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
this example handles http request/response headers events and log all headers.
44

5-
6-
```bash
7-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :authority: localhost:18000
8-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :path: /
9-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :method: GET
10-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: user-agent: curl/7.54.0
11-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: accept: */*
12-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: hello: Go
13-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: x-forwarded-proto: http
14-
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: x-request-id: 6542a4ca-b6b3-4667-b41d-7ef4c3392946
15-
proxy_1 | [2020-03-25 09:09:24.942][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: response header: :status: 200
16-
proxy_1 | [2020-03-25 09:09:24.942][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: response header: content-length: 13
17-
proxy_1 | [2020-03-25 09:09:24.942][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: response header: content-type: text/plain
18-
proxy_1 | [2020-03-25 09:09:24.942][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: response header: date: Wed, 25 Mar 2020 09:09:24 GMT
19-
proxy_1 | [2020-03-25 09:09:24.942][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: response header: server: envoy
20-
proxy_1 | [2020-03-25 09:09:24.942][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: response header: x-envoy-upstream-service-time: 2
5+
```
6+
wasm log: request header --> :authority: localhost:18000
7+
wasm log: request header --> :path: /uuid
8+
wasm log: request header --> :method: GET
9+
wasm log: request header --> user-agent: curl/7.68.0
10+
wasm log: request header --> accept: */*
11+
wasm log: request header --> x-forwarded-proto: http
12+
wasm log: request header --> x-request-id: 5692b633-fd9c-4700-b4dd-7a58e2853eb4
13+
wasm log: response header <-- :status: 200
14+
wasm log: response header <-- content-length: 13
15+
wasm log: response header <-- content-type: text/plain
16+
wasm log: response header <-- date: Thu, 01 Oct 2020 09:10:09 GMT
17+
wasm log: response header <-- server: envoy
18+
wasm log: response header <-- x-envoy-upstream-service-time: 0
19+
wasm log: 2 finished
2120
```

examples/http_headers/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(int, bool) types.Action {
4141
}
4242

4343
for _, h := range hs {
44-
proxywasm.LogInfof("request header: %s: %s", h[0], h[1])
44+
proxywasm.LogInfof("request header --> %s: %s", h[0], h[1])
4545
}
4646
return types.ActionContinue
4747
}
@@ -54,7 +54,7 @@ func (ctx *httpHeaders) OnHttpResponseHeaders(int, bool) types.Action {
5454
}
5555

5656
for _, h := range hs {
57-
proxywasm.LogInfof("response header: %s: %s", h[0], h[1])
57+
proxywasm.LogInfof("response header <-- %s: %s", h[0], h[1])
5858
}
5959
return types.ActionContinue
6060
}

examples/http_headers/main_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
2121
logs := host.GetLogs(types.LogLevelInfo)
2222
require.Greater(t, len(logs), 1)
2323

24-
assert.Equal(t, "request header: key2: value2", logs[len(logs)-1])
25-
assert.Equal(t, "request header: key1: value1", logs[len(logs)-2])
24+
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-1])
25+
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-2])
2626
}
2727

2828
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
@@ -36,6 +36,6 @@ func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
3636
logs := host.GetLogs(types.LogLevelInfo)
3737
require.Greater(t, len(logs), 1)
3838

39-
assert.Equal(t, "response header: key2: value2", logs[len(logs)-1])
40-
assert.Equal(t, "response header: key1: value1", logs[len(logs)-2])
39+
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-1])
40+
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-2])
4141
}

examples/metrics/README.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@
44
this example creates simple request counter
55

66
```
7-
[2020-09-07 16:14:52.723][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 0
8-
[2020-09-07 16:14:52.723][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
9-
[2020-09-07 16:14:53.586][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 1
10-
[2020-09-07 16:14:53.586][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
11-
[2020-09-07 16:14:54.235][293742][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 2
12-
[2020-09-07 16:14:54.235][293742][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
13-
[2020-09-07 16:14:54.747][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 3
14-
[2020-09-07 16:14:54.747][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
15-
[2020-09-07 16:14:55.267][293722][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 4
16-
[2020-09-07 16:14:55.267][293722][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
17-
[2020-09-07 16:14:55.778][293742][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 5
18-
[2020-09-07 16:14:55.778][293742][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
19-
[2020-09-07 16:14:56.474][293722][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 6
20-
[2020-09-07 16:14:56.474][293722][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
21-
[2020-09-07 16:14:57.051][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: previous value of proxy_wasm_go.request_counter: 7
22-
[2020-09-07 16:14:57.051][293724][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: incremented
23-
```
7+
wasm log: previous value of proxy_wasm_go.request_counter: 0
8+
wasm log: incremented
9+
wasm log: previous value of proxy_wasm_go.request_counter: 1
10+
wasm log: incremented
11+
wasm log: previous value of proxy_wasm_go.request_counter: 2
12+
wasm log: incremented
13+
wasm log: previous value of proxy_wasm_go.request_counter: 3
14+
wasm log: incremented
15+
wasm log: previous value of proxy_wasm_go.request_counter: 4
16+
wasm log: incremented
17+
wasm log: previous value of proxy_wasm_go.request_counter: 5
18+
wasm log: incremented
19+
```

examples/network/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ this example handles tcp connections and output data into the log stream.
44

55

66
```bash
7-
[2020-09-23 15:19:57.054][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: new connection!
8-
[2020-09-23 15:19:57.055][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: downstream data received: GET /uuid HTTP/1.1
7+
wasm log: new connection!
8+
wasm log: >>>>>> downstream data received >>>>>>
9+
GET /uuid HTTP/1.1
910
Host: localhost:18000
1011
User-Agent: curl/7.68.0
1112
Accept: */*
1213

1314

14-
[2020-09-23 15:19:57.055][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: remote address: 127.0.0.1:8099
15-
[2020-09-23 15:19:57.055][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: upstream data received: HTTP/1.1 200 OK
15+
wasm log: remote address: 127.0.0.1:8099
16+
wasm log: <<<<<< upstream data received <<<<<<
17+
HTTP/1.1 200 OK
1618
content-length: 13
1719
content-type: text/plain
18-
date: Wed, 23 Sep 2020 06:19:56 GMT
20+
date: Thu, 01 Oct 2020 09:16:33 GMT
1921
server: envoy
2022

2123
example body
2224

23-
[2020-09-23 15:19:57.055][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: remote address: 127.0.0.1:8099
24-
[2020-09-23 15:19:57.055][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: downstream connection close!
25-
[2020-09-23 15:19:57.056][1008407][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:997] wasm log: connection complete!
25+
wasm log: downstream connection close!
26+
wasm log: connection complete!
2627
```

examples/network/main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ func (ctx context) OnNewConnection() types.Action {
4646
}
4747

4848
func (ctx context) OnDownstreamData(dataSize int, _ bool) types.Action {
49-
// TODO: dispatch http call
50-
5149
if dataSize == 0 {
5250
return types.ActionContinue
5351
}
@@ -57,7 +55,7 @@ func (ctx context) OnDownstreamData(dataSize int, _ bool) types.Action {
5755
proxywasm.LogCritical(err.Error())
5856
}
5957

60-
proxywasm.LogInfof("downstream data received: %s", string(data))
58+
proxywasm.LogInfof(">>>>>> downstream data received >>>>>>\n%s", string(data))
6159
return types.ActionContinue
6260
}
6361

@@ -67,22 +65,23 @@ func (ctx context) OnDownstreamClose(types.PeerType) {
6765
}
6866

6967
func (ctx context) OnUpstreamData(dataSize int, _ bool) types.Action {
68+
if dataSize == 0 {
69+
return types.ActionContinue
70+
}
71+
7072
ret, err := proxywasm.HostCallGetProperty([]string{"upstream", "address"})
7173
if err != nil {
7274
proxywasm.LogCritical(err.Error())
7375
}
7476

7577
proxywasm.LogInfof("remote address: %s", string(ret))
76-
if dataSize == 0 {
77-
return types.ActionContinue
78-
}
7978

8079
data, err := proxywasm.HostCallGetUpstreamData(0, dataSize)
8180
if err != nil && err != types.ErrorStatusNotFound {
8281
proxywasm.LogCritical(err.Error())
8382
}
8483

85-
proxywasm.LogInfof("upstream data received: %s", string(data))
84+
proxywasm.LogInfof("<<<<<< upstream data received <<<<<<\n%s", string(data))
8685
return types.ActionContinue
8786
}
8887

0 commit comments

Comments
 (0)