Skip to content

Commit

Permalink
Merge pull request #7 from mdsol/fix/rate
Browse files Browse the repository at this point in the history
Fix rate counting
  • Loading branch information
ykitamura-mdsol authored Jul 4, 2024
2 parents 9d950dc + 3810f26 commit 9efe9ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ The released binary is expected to run in containers or linux-like hosts of `amd
For Windows 10+ environments you can run it in WSL2.
For other environments, please build your version from sources.

### Get the pre-build extended K6 binary
### Build K6 binary

- Find the [latest release}(https://github.com/mdsol/xk6-output-otlp/releases).
- Download `k6.tar.gz` archive, and extract the `k6` binary with
You should have `Go 1.22+` and `make` installed.

```sh
tar -xvzf k6.tar.gz
```
- clone the [repository](https://github.com/mdsol/xk6-output-otlp) using git;
- run `make build`;
- the `k6` binary is in the project's `./bin` subfolder.

### Configuration

Expand Down Expand Up @@ -71,7 +70,7 @@ If `<name>` is a name of the original rate metric, it produces:

- `k6_<name>_total` OTLP counter which contains the total number of occurrences;
- `k6_<name>_success_total` OTLP counter which contains the total number of successful occurrences;
- `k6_<name>_success_rate` OTLP Gauge, which contains the pre-computed rate.
- `k6_<name>_success_rate` OTLP Gauge, which contains the pre-computed rate over the whole metric family.

The latest pre-computed rate value should match appropriate K6 output.

Expand Down
10 changes: 5 additions & 5 deletions pkg/otlp/rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
)

type rate struct {
sum float64
count float64
sum float64
counter float64
}

func (r *rate) value() float64 {
if r.count == 0 {
if r.counter == 0 {
return 0
}
return r.sum / r.count
return r.sum / r.counter
}

func (r *rate) combine(sample *k6m.Sample) float64 {
r.count += 1.0
r.counter += 1.0
r.sum += sample.Value
return r.value()
}
11 changes: 5 additions & 6 deletions pkg/otlp/wraprate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func newRateWrapper(id int, name string) (Wrapper, error) {
metricTotal, err := meter.Int64Counter(name)
metricTotal, err := meter.Int64Counter(name + "_total")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -42,10 +42,9 @@ type rateWrapper struct {
func (w *rateWrapper) Record(s *k6m.Sample) {
attrs := om.WithAttributes(attributes(s.TimeSeries.Tags)...)

w.metricRate.Record(params.ctx, w.rate.combine(s), attrs)
w.metricTotal.Add(params.ctx, int64(math.Floor(s.Value)))
// Here we count rate over whole metric family as we see the result in the K6 output.
w.metricRate.Record(params.ctx, w.rate.combine(s), om.WithAttributes(sessionAttrs...))

if s.Value == 1 {
w.metricSuccess.Add(params.ctx, int64(math.Floor(s.Value)), attrs)
}
w.metricTotal.Add(params.ctx, 1, attrs)
w.metricSuccess.Add(params.ctx, int64(math.Floor(s.Value)), attrs)
}

0 comments on commit 9efe9ca

Please sign in to comment.