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

Commit efb11f8

Browse files
committed
fix metrics
Signed-off-by: mathetake <[email protected]>
1 parent d88237a commit efb11f8

File tree

9 files changed

+88
-77
lines changed

9 files changed

+88
-77
lines changed

examples/metrics/main.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ func newRootContext(uint32) proxywasm.RootContext {
3939

4040
// override
4141
func (ctx *metricRootContext) OnVMStart(int) bool {
42-
ct, err := proxywasm.DefineCounterMetric(metricsName)
43-
if err != nil {
44-
proxywasm.LogCriticalf("error defining metrics: %v", err)
45-
}
46-
counter = ct
42+
counter = proxywasm.DefineCounterMetric(metricsName)
4743
return true
4844
}
4945

@@ -58,16 +54,10 @@ func newHttpContext(uint32) proxywasm.HttpContext {
5854

5955
// override
6056
func (ctx *metricHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
61-
prev, err := counter.Get()
62-
if err != nil {
63-
proxywasm.LogCriticalf("error retrieving previous metric: %v", err)
64-
}
65-
57+
prev := counter.Get()
6658
proxywasm.LogInfof("previous value of %s: %d", metricsName, prev)
6759

68-
if err := counter.Increment(1); err != nil {
69-
proxywasm.LogCriticalf("error incrementing metrics %v", err)
70-
}
60+
counter.Increment(1)
7161
proxywasm.LogInfo("incremented")
7262
return types.ActionContinue
7363
}

examples/metrics/main_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func TestMetric(t *testing.T) {
2929

3030
assert.Equal(t, "incremented", logs[len(logs)-1])
3131

32-
value, err := counter.Get()
33-
require.NoError(t, err)
32+
value := counter.Get()
3433
assert.Equal(t, uint64(3), value)
3534
}

examples/network/main.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ func newRootContext(uint32) proxywasm.RootContext {
3939
}
4040

4141
func (ctx *rootContext) OnVMStart(int) bool {
42-
var err error
43-
counter, err = proxywasm.DefineCounterMetric(connectionCounterName)
44-
if err != nil {
45-
proxywasm.LogCriticalf("failed to initialize connection counter: %v", err)
46-
}
42+
counter = proxywasm.DefineCounterMetric(connectionCounterName)
4743
return true
4844
}
4945

@@ -63,12 +59,14 @@ func (ctx *networkContext) OnNewConnection() types.Action {
6359

6460
func (ctx *networkContext) OnDownstreamData(dataSize int, _ bool) types.Action {
6561
if dataSize == 0 {
62+
panic("aa")
6663
return types.ActionContinue
6764
}
6865

6966
data, err := proxywasm.GetDownStreamData(0, dataSize)
7067
if err != nil && err != types.ErrorStatusNotFound {
71-
proxywasm.LogCritical(err.Error())
68+
proxywasm.LogCriticalf("failed to get downstream data: %v", err)
69+
return types.ActionContinue
7270
}
7371

7472
proxywasm.LogInfof(">>>>>> downstream data received >>>>>>\n%s", string(data))
@@ -87,7 +85,8 @@ func (ctx *networkContext) OnUpstreamData(dataSize int, _ bool) types.Action {
8785

8886
ret, err := proxywasm.GetProperty([]string{"upstream", "address"})
8987
if err != nil {
90-
proxywasm.LogCritical(err.Error())
88+
proxywasm.LogCriticalf("failed to get downstream data: %v", err)
89+
return types.ActionContinue
9190
}
9291

9392
proxywasm.LogInfof("remote address: %s", string(ret))
@@ -102,9 +101,6 @@ func (ctx *networkContext) OnUpstreamData(dataSize int, _ bool) types.Action {
102101
}
103102

104103
func (ctx *networkContext) OnStreamDone() {
105-
err := counter.Increment(1)
106-
if err != nil {
107-
proxywasm.LogCriticalf("failed to increment connection counter: %v", err)
108-
}
104+
counter.Increment(1)
109105
proxywasm.LogInfo("connection complete!")
110106
}

examples/network/main_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func TestNetwork_counter(t *testing.T) {
9494
require.Greater(t, len(logs), 0)
9595

9696
assert.Equal(t, "connection complete!", logs[len(logs)-1])
97-
actual, err := counter.Get()
98-
require.NoError(t, err)
97+
actual := counter.Get()
9998
assert.Equal(t, uint64(1), actual)
10099
}

examples/vm_plugin_configuration/main_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,37 @@ import (
1818
"strings"
1919
"testing"
2020

21-
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
21+
"github.com/stretchr/testify/require"
2222

2323
"github.com/stretchr/testify/assert"
2424

2525
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
2626
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
2727
)
2828

29-
func TestContext_OnConfigure(t *testing.T) {
29+
func TestContext_OnPluginStart(t *testing.T) {
3030
pluginConfigData := `{"name": "tinygo plugin configuration"}`
3131

32-
proxywasm.SetNewRootContext(newRootContext)
33-
host := proxytest.NewHostEmulator([]byte(pluginConfigData), nil)
32+
host := proxytest.NewHostEmulator([]byte(pluginConfigData), nil, newRootContext, nil, nil)
3433
defer host.Done() // release the emulation lock so that other test cases can insert their own host emulation
3534

3635
host.StartPlugin() // invoke OnPluginStart
3736

3837
logs := host.GetLogs(types.LogLevelInfo)
38+
require.Greater(t, len(logs), 0)
3939
msg := logs[len(logs)-1]
4040
assert.True(t, strings.Contains(msg, pluginConfigData))
4141
}
4242

4343
func TestContext_OnVMStart(t *testing.T) {
4444
vmConfigData := `{"name": "tinygo vm configuration"}`
45-
proxywasm.SetNewRootContext(newRootContext)
46-
host := proxytest.NewHostEmulator(nil, []byte(vmConfigData))
45+
host := proxytest.NewHostEmulator(nil, []byte(vmConfigData), newRootContext, nil, nil)
4746
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
4847

4948
host.StartVM() // invoke OnVMStart
5049

5150
logs := host.GetLogs(types.LogLevelInfo)
51+
require.Greater(t, len(logs), 0)
5252
msg := logs[len(logs)-1]
5353
assert.True(t, strings.Contains(msg, vmConfigData))
5454
}

proxytest/network.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (n *networkHostEmulator) NetworkFilterPutDownstreamData(contextID uint32, d
100100
stream.downstream = append(stream.downstream, data...)
101101
}
102102

103-
action := proxywasm.ProxyOnDownstreamData(contextID, len(stream.upstream), false)
103+
action := proxywasm.ProxyOnDownstreamData(contextID, len(stream.downstream), false)
104104
switch action {
105105
case types.ActionPause:
106106
return
@@ -117,6 +117,7 @@ func (n *networkHostEmulator) NetworkFilterInitConnection() (contextID uint32) {
117117
contextID = getNextContextID()
118118
proxywasm.ProxyOnContextCreate(contextID, rootContextID)
119119
proxywasm.ProxyOnNewConnection(contextID)
120+
n.streamStates[contextID] = &streamState{}
120121
return
121122
}
122123

proxytest/proxytest.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package proxytest
22

33
import (
4+
"log"
45
"sync"
56
"time"
67

@@ -168,20 +169,24 @@ func (h *hostEmulator) ProxySetProperty(*byte, int, *byte, int) types.Status {
168169

169170
// impl host rawhostcall.ProxyWASMHost
170171
func (h *hostEmulator) ProxyGetProperty(*byte, int, **byte, *int) types.Status {
171-
panic("unimplemented")
172+
log.Printf("ProxyGetProperty not implemented in the host emulator yet")
173+
return 0
172174
}
173175

174176
// impl host rawhostcall.ProxyWASMHost
175177
func (h *hostEmulator) ProxyResolveSharedQueue(vmIDData *byte, vmIDSize int, nameData *byte, nameSize int, returnID *uint32) types.Status {
176-
panic("unimplemented")
178+
log.Printf("ProxyResolveSharedQueue not implemented in the host emulator yet")
179+
return 0
177180
}
178181

179182
// impl host rawhostcall.ProxyWASMHost
180183
func (h *hostEmulator) ProxyCloseStream(streamType types.StreamType) types.Status {
181-
panic("unimplemented")
184+
log.Printf("ProxyCloseStream not implemented in the host emulator yet")
185+
return 0
182186
}
183187

184188
// impl host rawhostcall.ProxyWASMHost
185189
func (h *hostEmulator) ProxyDone() types.Status {
186-
panic("unimplemented")
190+
log.Printf("ProxyDone not implemented in the host emulator yet")
191+
return 0
187192
}

proxywasm/hostcall_metric.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,69 +27,100 @@ type (
2727

2828
// counter
2929

30-
func DefineCounterMetric(name string) (MetricCounter, error) {
30+
func DefineCounterMetric(name string) MetricCounter {
3131
var id uint32
3232
ptr := stringBytePtr(name)
3333
st := rawhostcall.ProxyDefineMetric(types.MetricTypeCounter, ptr, len(name), &id)
34-
return MetricCounter(id), types.StatusToError(st)
34+
if err := types.StatusToError(st); err != nil {
35+
LogCriticalf("define metric of name %s: %v", name, types.StatusToError(st))
36+
}
37+
return MetricCounter(id)
3538
}
3639

3740
func (m MetricCounter) ID() uint32 {
3841
return uint32(m)
3942
}
4043

41-
func (m MetricCounter) Get() (uint64, error) {
44+
func (m MetricCounter) Get() uint64 {
4245
var val uint64
4346
st := rawhostcall.ProxyGetMetric(m.ID(), &val)
44-
return val, types.StatusToError(st)
47+
if err := types.StatusToError(st); err != nil {
48+
LogCriticalf("get metric of %d: %v", m.ID(), types.StatusToError(st))
49+
panic("") // abort
50+
}
51+
return val
4552
}
4653

47-
func (m MetricCounter) Increment(offset uint64) error {
48-
return types.StatusToError(rawhostcall.ProxyIncrementMetric(m.ID(), int64(offset)))
54+
func (m MetricCounter) Increment(offset uint64) {
55+
if err := types.StatusToError(rawhostcall.ProxyIncrementMetric(m.ID(), int64(offset))); err != nil {
56+
LogCriticalf("increment %d by %d: %v", m.ID(), offset, err)
57+
panic("") // abort
58+
}
4959
}
5060

5161
// gauge
5262

53-
func DefineGaugeMetric(name string) (MetricGauge, error) {
63+
func DefineGaugeMetric(name string) MetricGauge {
5464
var id uint32
5565
ptr := stringBytePtr(name)
5666
st := rawhostcall.ProxyDefineMetric(types.MetricTypeGauge, ptr, len(name), &id)
57-
return MetricGauge(id), types.StatusToError(st)
67+
if err := types.StatusToError(st); err != nil {
68+
LogCriticalf("error define metric of name %s: %v", name, types.StatusToError(st))
69+
panic("") // abort
70+
}
71+
return MetricGauge(id)
5872
}
5973

6074
func (m MetricGauge) ID() uint32 {
6175
return uint32(m)
6276
}
6377

64-
func (m MetricGauge) Get() (int64, error) {
78+
func (m MetricGauge) Get() int64 {
6579
var val uint64
66-
st := rawhostcall.ProxyGetMetric(m.ID(), &val)
67-
return int64(val), types.StatusToError(st)
80+
if err := types.StatusToError(rawhostcall.ProxyGetMetric(m.ID(), &val)); err != nil {
81+
LogCriticalf("get metric of %d: %v", m.ID(), err)
82+
panic("") // abort
83+
}
84+
return int64(val)
6885
}
6986

70-
func (m MetricGauge) Add(offset int64) error {
71-
return types.StatusToError(rawhostcall.ProxyIncrementMetric(m.ID(), offset))
87+
func (m MetricGauge) Add(offset int64) {
88+
if err := types.StatusToError(rawhostcall.ProxyIncrementMetric(m.ID(), offset)); err != nil {
89+
LogCriticalf("error adding %d by %d: %v", m.ID(), offset, err)
90+
panic("") // abort
91+
}
7292
}
7393

7494
// histogram
7595

76-
func DefineHistogramMetric(name string) (MetricHistogram, error) {
96+
func DefineHistogramMetric(name string) MetricHistogram {
7797
var id uint32
7898
ptr := stringBytePtr(name)
7999
st := rawhostcall.ProxyDefineMetric(types.MetricTypeHistogram, ptr, len(name), &id)
80-
return MetricHistogram(id), types.StatusToError(st)
100+
if err := types.StatusToError(st); err != nil {
101+
LogCriticalf("error define metric of name %s: %v", name, types.StatusToError(st))
102+
panic("") // abort
103+
}
104+
return MetricHistogram(id)
81105
}
82106

83107
func (m MetricHistogram) ID() uint32 {
84108
return uint32(m)
85109
}
86110

87-
func (m MetricHistogram) Get() (uint64, error) {
111+
func (m MetricHistogram) Get() uint64 {
88112
var val uint64
89113
st := rawhostcall.ProxyGetMetric(m.ID(), &val)
90-
return val, types.StatusToError(st)
114+
if err := types.StatusToError(st); err != nil {
115+
LogCriticalf("get metric of %d: %v", m.ID(), types.StatusToError(st))
116+
panic("") // abort
117+
}
118+
return val
91119
}
92120

93-
func (m MetricHistogram) Record(value uint64) error {
94-
return types.StatusToError(rawhostcall.ProxyRecordMetric(m.ID(), value))
121+
func (m MetricHistogram) Record(value uint64) {
122+
if err := types.StatusToError(rawhostcall.ProxyRecordMetric(m.ID(), value)); err != nil {
123+
LogCriticalf("error adding %d: %v", m.ID(), err)
124+
panic("") // abort
125+
}
95126
}

proxywasm/hostcall_metric_test.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
"github.com/stretchr/testify/require"
87
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/rawhostcall"
98
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
109
)
@@ -78,16 +77,13 @@ func TestHostCall_Metric(t *testing.T) {
7877
} {
7978
t.Run(c.name, func(t *testing.T) {
8079
// define metric
81-
m, err := DefineCounterMetric(c.name)
82-
require.NoError(t, err)
80+
m := DefineCounterMetric(c.name)
8381

8482
// increment
85-
require.NoError(t, m.Increment(c.offset))
83+
m.Increment(c.offset)
8684

8785
// get
88-
value, err := m.Get()
89-
require.NoError(t, err)
90-
assert.Equal(t, c.offset, value)
86+
assert.Equal(t, c.offset, m.Get())
9187
})
9288
}
9389
})
@@ -101,16 +97,13 @@ func TestHostCall_Metric(t *testing.T) {
10197
} {
10298
t.Run(c.name, func(t *testing.T) {
10399
// define metric
104-
m, err := DefineGaugeMetric(c.name)
105-
require.NoError(t, err)
100+
m := DefineGaugeMetric(c.name)
106101

107102
// increment
108-
require.NoError(t, m.Add(c.offset))
103+
m.Add(c.offset)
109104

110105
// get
111-
value, err := m.Get()
112-
require.NoError(t, err)
113-
assert.Equal(t, c.offset, value)
106+
assert.Equal(t, c.offset, m.Get())
114107
})
115108
}
116109
})
@@ -124,16 +117,13 @@ func TestHostCall_Metric(t *testing.T) {
124117
} {
125118
t.Run(c.name, func(t *testing.T) {
126119
// define metric
127-
m, err := DefineHistogramMetric(c.name)
128-
require.NoError(t, err)
120+
m := DefineHistogramMetric(c.name)
129121

130122
// record
131-
require.NoError(t, m.Record(c.value))
123+
m.Record(c.value)
132124

133125
// get
134-
value, err := m.Get()
135-
require.NoError(t, err)
136-
assert.Equal(t, c.value, value)
126+
assert.Equal(t, c.value, m.Get())
137127
})
138128
}
139129
})

0 commit comments

Comments
 (0)