mirror of
https://github.com/arangodb/kube-arangodb.git
synced 2024-12-14 11:57:37 +00:00
[Feature] Requests Bytes Counter (#1334)
This commit is contained in:
parent
1b76f8c4e5
commit
d9ea028019
6 changed files with 196 additions and 1 deletions
|
@ -4,6 +4,7 @@
|
|||
- (Feature) AgencyCache Interface
|
||||
- (Feature) Agency Cache Poll EE Extension
|
||||
- (Feature) Metrics Counter
|
||||
- (Feature) Requests Bytes Counter
|
||||
|
||||
## [1.2.29](https://github.com/arangodb/kube-arangodb/tree/1.2.29) (2023-06-08)
|
||||
- (Maintenance) Add govulncheck to pipeline, update golangci-linter
|
||||
|
|
|
@ -27,6 +27,8 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/arangodb/kube-arangodb/pkg/util/metrics/nctx"
|
||||
)
|
||||
|
||||
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
|
|||
|
||||
var out OUT
|
||||
|
||||
if err := json.NewDecoder(resp).Decode(&out); err != nil {
|
||||
if err := json.NewDecoder(nctx.WithRequestReadBytes(ctx, resp)).Decode(&out); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
|
|
45
pkg/util/metrics/nctx/counter.go
Normal file
45
pkg/util/metrics/nctx/counter.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
//
|
||||
// DISCLAIMER
|
||||
//
|
||||
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
|
||||
package nctx
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Counter struct {
|
||||
lock sync.Mutex
|
||||
|
||||
value uint64
|
||||
}
|
||||
|
||||
func (c *Counter) Get() uint64 {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
return c.value
|
||||
}
|
||||
|
||||
func (c *Counter) add(v uint64) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.value += v
|
||||
}
|
65
pkg/util/metrics/nctx/counter_request_read.go
Normal file
65
pkg/util/metrics/nctx/counter_request_read.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
//
|
||||
// DISCLAIMER
|
||||
//
|
||||
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
|
||||
package nctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
const RequestReadBytesKey Key = "operator.requestReadBytes"
|
||||
|
||||
func (c *Counter) WithRequestReadBytes(ctx context.Context) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
return context.WithValue(ctx, RequestReadBytesKey, c)
|
||||
}
|
||||
|
||||
type requestReadBytes struct {
|
||||
c *Counter
|
||||
|
||||
in io.Reader
|
||||
}
|
||||
|
||||
func (r requestReadBytes) Read(p []byte) (n int, err error) {
|
||||
n, err = r.in.Read(p)
|
||||
r.c.add(uint64(n))
|
||||
return
|
||||
}
|
||||
|
||||
func WithRequestReadBytes(ctx context.Context, reader io.Reader) io.Reader {
|
||||
v := ctx.Value(RequestReadBytesKey)
|
||||
if v == nil {
|
||||
return reader
|
||||
}
|
||||
|
||||
z, ok := v.(*Counter)
|
||||
if !ok {
|
||||
return reader
|
||||
}
|
||||
|
||||
return requestReadBytes{
|
||||
c: z,
|
||||
in: reader,
|
||||
}
|
||||
}
|
53
pkg/util/metrics/nctx/counter_request_read_test.go
Normal file
53
pkg/util/metrics/nctx/counter_request_read_test.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
//
|
||||
// DISCLAIMER
|
||||
//
|
||||
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
|
||||
package nctx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func counterRequestReadMock(t *testing.T, ctx context.Context, data io.Reader) int {
|
||||
data = WithRequestReadBytes(ctx, data)
|
||||
dz, err := io.ReadAll(data)
|
||||
require.NoError(t, err)
|
||||
return len(dz)
|
||||
}
|
||||
|
||||
func Test_Counter_RequestRead(t *testing.T) {
|
||||
data := make([]byte, 64)
|
||||
|
||||
var c Counter
|
||||
|
||||
t.Run("Read without wrapper", func(t *testing.T) {
|
||||
require.EqualValues(t, 64, counterRequestReadMock(t, context.Background(), bytes.NewReader(data)))
|
||||
require.EqualValues(t, 0, c.Get())
|
||||
})
|
||||
|
||||
t.Run("Read with wrapper", func(t *testing.T) {
|
||||
require.EqualValues(t, 64, counterRequestReadMock(t, c.WithRequestReadBytes(context.Background()), bytes.NewReader(data)))
|
||||
require.EqualValues(t, 64, c.Get())
|
||||
})
|
||||
}
|
29
pkg/util/metrics/nctx/interface.go
Normal file
29
pkg/util/metrics/nctx/interface.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
//
|
||||
// DISCLAIMER
|
||||
//
|
||||
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||
//
|
||||
|
||||
package nctx
|
||||
|
||||
import "context"
|
||||
|
||||
type Key string
|
||||
|
||||
type With interface {
|
||||
With(ctx context.Context) context.Context
|
||||
}
|
Loading…
Reference in a new issue