Skip to content

Commit 9efe9ca

Browse files
Merge pull request #7 from mdsol/fix/rate
Fix rate counting
2 parents 9d950dc + 3810f26 commit 9efe9ca

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ The released binary is expected to run in containers or linux-like hosts of `amd
88
For Windows 10+ environments you can run it in WSL2.
99
For other environments, please build your version from sources.
1010

11-
### Get the pre-build extended K6 binary
11+
### Build K6 binary
1212

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

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

2019
### Configuration
2120

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

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

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

pkg/otlp/rates.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import (
55
)
66

77
type rate struct {
8-
sum float64
9-
count float64
8+
sum float64
9+
counter float64
1010
}
1111

1212
func (r *rate) value() float64 {
13-
if r.count == 0 {
13+
if r.counter == 0 {
1414
return 0
1515
}
16-
return r.sum / r.count
16+
return r.sum / r.counter
1717
}
1818

1919
func (r *rate) combine(sample *k6m.Sample) float64 {
20-
r.count += 1.0
20+
r.counter += 1.0
2121
r.sum += sample.Value
2222
return r.value()
2323
}

pkg/otlp/wraprate.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

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

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

48-
if s.Value == 1 {
49-
w.metricSuccess.Add(params.ctx, int64(math.Floor(s.Value)), attrs)
50-
}
48+
w.metricTotal.Add(params.ctx, 1, attrs)
49+
w.metricSuccess.Add(params.ctx, int64(math.Floor(s.Value)), attrs)
5150
}

0 commit comments

Comments
 (0)