Skip to content

Commit

Permalink
Sync loki.process: metric.coutners (#5415)
Browse files Browse the repository at this point in the history
* Sync loki.process: coutners

* Add comments for other synced files
  • Loading branch information
thampiotr authored Oct 9, 2023
1 parent 9a53c48 commit fa2fc38
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ internal API changes are not present.
Main (unreleased)
-----------------

### Bugfixes

- Fixed an issue where `loki.process` validation for stage `metric.counter` was
allowing invalid combination of configuration options. (@thampiotr)

v0.37.0-rc.1 (2023-10-06)
-----------------

Expand Down
4 changes: 3 additions & 1 deletion component/loki/process/metric/counters.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"fmt"
"time"
Expand Down Expand Up @@ -55,7 +57,7 @@ func (c *CounterConfig) Validate() error {
if c.MatchAll && c.Value != "" {
return fmt.Errorf("a 'counter' metric supports either 'match_all' or a 'value', but not both")
}
if c.CountEntryBytes && (!c.MatchAll && c.Action != "add") {
if c.CountEntryBytes && (!c.MatchAll || c.Action != "add") {
return fmt.Errorf("the 'count_entry_bytes' counter field must be specified along with match_all set to true or action set to 'add'")
}
return nil
Expand Down
96 changes: 96 additions & 0 deletions component/loki/process/metric/counters_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"testing"
"time"
Expand All @@ -9,6 +11,100 @@ import (
"github.com/stretchr/testify/assert"
)

var (
counterTestTrue = true
counterTestFalse = false
counterTestVal = "some val"
)

func Test_validateCounterConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config CounterConfig
err string
}{
{"invalid action",
CounterConfig{
Action: "del",
MaxIdle: 1 * time.Second,
},
"the 'action' counter field must be either 'inc' or 'add'",
},
{"invalid counter match all",
CounterConfig{
MatchAll: counterTestTrue,
Value: counterTestVal,
Action: "inc",
MaxIdle: 1 * time.Second,
},
"a 'counter' metric supports either 'match_all' or a 'value', but not both",
},
{"invalid counter match bytes",
CounterConfig{
MatchAll: counterTestFalse,
CountEntryBytes: counterTestTrue,
Action: "inc",
MaxIdle: 1 * time.Second,
},
"the 'count_entry_bytes' counter field must be specified along with match_all set to true or action set to 'add'",
},
{"invalid counter match bytes action",
CounterConfig{
MatchAll: counterTestTrue,
CountEntryBytes: counterTestTrue,
Action: "inc",
MaxIdle: 1 * time.Second,
},
"the 'count_entry_bytes' counter field must be specified along with match_all set to true or action set to 'add'",
},
{"valid counter match bytes",
CounterConfig{
MatchAll: counterTestTrue,
CountEntryBytes: counterTestTrue,
Action: "add",
MaxIdle: 1 * time.Second,
},
"",
},
{"valid",
CounterConfig{
Value: counterTestVal,
Action: "inc",
MaxIdle: 1 * time.Second,
},
"",
},
{"valid match all is false",
CounterConfig{
MatchAll: counterTestFalse,
Value: counterTestVal,
Action: "inc",
MaxIdle: 1 * time.Second,
},
"",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.config.Validate()

if err == nil {
if tt.err != "" {
t.Fatalf("Metrics stage validation error, expected error to cointain %q, but got no error", tt.err)
}
} else {
if tt.err == "" {
t.Fatalf("Metrics stage validation error, expected no error, but got %q", err)
}
assert.Contains(t, err.Error(), tt.err)
}
})
}
}

func TestCounterExpiration(t *testing.T) {
t.Parallel()
cfg := &CounterConfig{
Expand Down
2 changes: 2 additions & 0 deletions component/loki/process/metric/gauges.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"fmt"
"time"
Expand Down
2 changes: 2 additions & 0 deletions component/loki/process/metric/gauges_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"testing"
"time"
Expand Down
2 changes: 2 additions & 0 deletions component/loki/process/metric/histograms.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"fmt"
"time"
Expand Down
2 changes: 2 additions & 0 deletions component/loki/process/metric/histograms_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"testing"
"time"
Expand Down
2 changes: 2 additions & 0 deletions component/loki/process/metric/metricvec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package metric

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"strings"
"sync"
Expand Down
2 changes: 2 additions & 0 deletions component/loki/process/process_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package process

// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum.

import (
"context"
"os"
Expand Down

0 comments on commit fa2fc38

Please sign in to comment.