Skip to content

Commit d9ea028

Browse files
authored
[Feature] Requests Bytes Counter (#1334)
1 parent 1b76f8c commit d9ea028

File tree

6 files changed

+196
-1
lines changed

6 files changed

+196
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (Feature) AgencyCache Interface
55
- (Feature) Agency Cache Poll EE Extension
66
- (Feature) Metrics Counter
7+
- (Feature) Requests Bytes Counter
78

89
## [1.2.29](https://github.com/arangodb/kube-arangodb/tree/1.2.29) (2023-06-08)
910
- (Maintenance) Add govulncheck to pipeline, update golangci-linter

pkg/util/arangod/conn/conn.executor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"io"
2828
"net/http"
2929
"reflect"
30+
31+
"github.com/arangodb/kube-arangodb/pkg/util/metrics/nctx"
3032
)
3133

3234
func NewExecutor[IN, OUT interface{}](conn Connection) Executor[IN, OUT] {
@@ -68,7 +70,7 @@ func (e executor[IN, OUT]) Execute(ctx context.Context, method string, endpoint
6870

6971
var out OUT
7072

71-
if err := json.NewDecoder(resp).Decode(&out); err != nil {
73+
if err := json.NewDecoder(nctx.WithRequestReadBytes(ctx, resp)).Decode(&out); err != nil {
7274
return nil, 0, err
7375
}
7476

pkg/util/metrics/nctx/counter.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package nctx
22+
23+
import (
24+
"sync"
25+
)
26+
27+
type Counter struct {
28+
lock sync.Mutex
29+
30+
value uint64
31+
}
32+
33+
func (c *Counter) Get() uint64 {
34+
c.lock.Lock()
35+
defer c.lock.Unlock()
36+
37+
return c.value
38+
}
39+
40+
func (c *Counter) add(v uint64) {
41+
c.lock.Lock()
42+
defer c.lock.Unlock()
43+
44+
c.value += v
45+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package nctx
22+
23+
import (
24+
"context"
25+
"io"
26+
)
27+
28+
const RequestReadBytesKey Key = "operator.requestReadBytes"
29+
30+
func (c *Counter) WithRequestReadBytes(ctx context.Context) context.Context {
31+
if ctx == nil {
32+
ctx = context.Background()
33+
}
34+
35+
return context.WithValue(ctx, RequestReadBytesKey, c)
36+
}
37+
38+
type requestReadBytes struct {
39+
c *Counter
40+
41+
in io.Reader
42+
}
43+
44+
func (r requestReadBytes) Read(p []byte) (n int, err error) {
45+
n, err = r.in.Read(p)
46+
r.c.add(uint64(n))
47+
return
48+
}
49+
50+
func WithRequestReadBytes(ctx context.Context, reader io.Reader) io.Reader {
51+
v := ctx.Value(RequestReadBytesKey)
52+
if v == nil {
53+
return reader
54+
}
55+
56+
z, ok := v.(*Counter)
57+
if !ok {
58+
return reader
59+
}
60+
61+
return requestReadBytes{
62+
c: z,
63+
in: reader,
64+
}
65+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package nctx
22+
23+
import (
24+
"bytes"
25+
"context"
26+
"io"
27+
"testing"
28+
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
func counterRequestReadMock(t *testing.T, ctx context.Context, data io.Reader) int {
33+
data = WithRequestReadBytes(ctx, data)
34+
dz, err := io.ReadAll(data)
35+
require.NoError(t, err)
36+
return len(dz)
37+
}
38+
39+
func Test_Counter_RequestRead(t *testing.T) {
40+
data := make([]byte, 64)
41+
42+
var c Counter
43+
44+
t.Run("Read without wrapper", func(t *testing.T) {
45+
require.EqualValues(t, 64, counterRequestReadMock(t, context.Background(), bytes.NewReader(data)))
46+
require.EqualValues(t, 0, c.Get())
47+
})
48+
49+
t.Run("Read with wrapper", func(t *testing.T) {
50+
require.EqualValues(t, 64, counterRequestReadMock(t, c.WithRequestReadBytes(context.Background()), bytes.NewReader(data)))
51+
require.EqualValues(t, 64, c.Get())
52+
})
53+
}

pkg/util/metrics/nctx/interface.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package nctx
22+
23+
import "context"
24+
25+
type Key string
26+
27+
type With interface {
28+
With(ctx context.Context) context.Context
29+
}

0 commit comments

Comments
 (0)