Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore():cherry-pick k8s code from branch release/7.14 refs MONITOR-7364 #32

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions metricbeat/helper/easyops/cou_metric_builder.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package easyops

Check failure on line 1 in metricbeat/helper/easyops/cou_metric_builder.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import (
"fmt"
"github.com/elastic/elastic-agent-libs/mapstr"

Check failure on line 5 in metricbeat/helper/easyops/cou_metric_builder.go

View workflow job for this annotation

GitHub Actions / lint (linux)

File is not `goimports`-ed with -local github.com/elastic (goimports)

Check failure on line 5 in metricbeat/helper/easyops/cou_metric_builder.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

File is not `goimports`-ed with -local github.com/elastic (goimports)
"strings"

"github.com/elastic/beats/v7/libbeat/common"
)

type couMetricBuilder struct {
Expand All @@ -22,14 +21,14 @@
}
}

func (builder *couMetricBuilder) Build(events []common.MapStr) []common.MapStr {
var result []common.MapStr
func (builder *couMetricBuilder) Build(events []mapstr.M) []mapstr.M {
var result []mapstr.M
eventMap := GroupEventsByKeys(events, builder.groupKeys)
for _, es := range eventMap {
if len(es) == 0 {
continue
}
rs := common.MapStr{}
rs := mapstr.M{}
for _, groupKey := range builder.groupKeys {
// GetValue success in GroupEventsByKeys
val, _ := es[0].GetValue(groupKey)
Expand All @@ -45,7 +44,7 @@
return result
}

func (builder *couMetricBuilder) count(events []common.MapStr, originMetric []string, defaultValues map[string]interface{}) map[string]float64 {
func (builder *couMetricBuilder) count(events []mapstr.M, originMetric []string, defaultValues map[string]interface{}) map[string]float64 {
counters := map[string]float64{}
for field, defaultValue := range defaultValues {
if value, ok := defaultValue.(float64); ok {
Expand Down
11 changes: 5 additions & 6 deletions metricbeat/helper/easyops/div_metric_builder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package easyops

Check failure on line 1 in metricbeat/helper/easyops/div_metric_builder.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import (
"github.com/elastic/elastic-agent-libs/mapstr"

Check failure on line 4 in metricbeat/helper/easyops/div_metric_builder.go

View workflow job for this annotation

GitHub Actions / lint (linux)

File is not `goimports`-ed with -local github.com/elastic (goimports)

Check failure on line 4 in metricbeat/helper/easyops/div_metric_builder.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

File is not `goimports`-ed with -local github.com/elastic (goimports)
"math"

"github.com/elastic/beats/v7/libbeat/common"
)

type divMetricBuilder struct {
Expand All @@ -21,14 +20,14 @@
}
}

func (builder *divMetricBuilder) Build(events []common.MapStr) []common.MapStr {
var result []common.MapStr
func (builder *divMetricBuilder) Build(events []mapstr.M) []mapstr.M {
var result []mapstr.M
eventMap := GroupEventsByKeys(events, builder.groupKeys)
for _, es := range eventMap {
if len(es) == 0 {
continue
}
rs := common.MapStr{}
rs := mapstr.M{}
for _, groupKey := range builder.groupKeys {
// GetValue success in GroupEventsByKeys
val, _ := es[0].GetValue(groupKey)
Expand All @@ -40,7 +39,7 @@
return result
}

func (builder *divMetricBuilder) div(events []common.MapStr, originMetric []string) interface{} {
func (builder *divMetricBuilder) div(events []mapstr.M, originMetric []string) interface{} {
var floatResult float64 = 0
MetricLoop:
for index, metric := range originMetric {
Expand Down
10 changes: 5 additions & 5 deletions metricbeat/helper/easyops/sub_metric_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package easyops

Check failure on line 1 in metricbeat/helper/easyops/sub_metric_builder.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import "github.com/elastic/beats/v7/libbeat/common"
import "github.com/elastic/elastic-agent-libs/mapstr"

type subMetricBuilder struct {
baseBuilderFields
Expand All @@ -17,14 +17,14 @@
}
}

func (builder *subMetricBuilder) Build(events []common.MapStr) []common.MapStr {
var result []common.MapStr
func (builder *subMetricBuilder) Build(events []mapstr.M) []mapstr.M {
var result []mapstr.M
eventMap := GroupEventsByKeys(events, builder.groupKeys)
for _, es := range eventMap {
if len(es) == 0 {
continue
}
rs := common.MapStr{}
rs := mapstr.M{}
for _, groupKey := range builder.groupKeys {
// GetValue success in GroupEventsByKeys
val, _ := es[0].GetValue(groupKey)
Expand All @@ -36,7 +36,7 @@
return result
}

func (builder *subMetricBuilder) sub(events []common.MapStr, originMetric []string) interface{} {
func (builder *subMetricBuilder) sub(events []mapstr.M, originMetric []string) interface{} {
var floatResult float64 = 0
for index, metric := range originMetric {
for _, event := range events {
Expand Down
10 changes: 5 additions & 5 deletions metricbeat/helper/easyops/sum_metric_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package easyops

Check failure on line 1 in metricbeat/helper/easyops/sum_metric_builder.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import "github.com/elastic/beats/v7/libbeat/common"
import "github.com/elastic/elastic-agent-libs/mapstr"

type sumMetricBuilder struct {
baseBuilderFields
Expand All @@ -17,14 +17,14 @@
}
}

func (builder *sumMetricBuilder) Build(events []common.MapStr) []common.MapStr {
var result []common.MapStr
func (builder *sumMetricBuilder) Build(events []mapstr.M) []mapstr.M {
var result []mapstr.M
eventMap := GroupEventsByKeys(events, builder.groupKeys)
for _, es := range eventMap {
if len(es) == 0 {
continue
}
rs := common.MapStr{}
rs := mapstr.M{}
for _, groupKey := range builder.groupKeys {
// GetValue success in GroupEventsByKeys
val, _ := es[0].GetValue(groupKey)
Expand All @@ -36,7 +36,7 @@
return result
}

func (builder *sumMetricBuilder) sum(events []common.MapStr, originMetric []string) interface{} {
func (builder *sumMetricBuilder) sum(events []mapstr.M, originMetric []string) interface{} {
var floatResult float64 = 0
for _, metric := range originMetric {
for _, event := range events {
Expand Down
6 changes: 2 additions & 4 deletions metricbeat/helper/easyops/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package easyops

Check failure on line 1 in metricbeat/helper/easyops/types.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import (
"github.com/elastic/beats/v7/libbeat/common"
)
import "github.com/elastic/elastic-agent-libs/mapstr"

type AggregateType uint8

Expand All @@ -22,7 +20,7 @@
}

type AggregateMetricBuilder interface {
Build(events []common.MapStr) []common.MapStr
Build(events []mapstr.M) []mapstr.M
}

type baseBuilderFields struct {
Expand Down
9 changes: 4 additions & 5 deletions metricbeat/helper/easyops/util.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package easyops

Check failure on line 1 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import (
"fmt"
"github.com/elastic/elastic-agent-libs/mapstr"

Check failure on line 5 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (linux)

File is not `goimports`-ed with -local github.com/elastic (goimports)

Check failure on line 5 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

File is not `goimports`-ed with -local github.com/elastic (goimports)
"strings"

"github.com/elastic/beats/v7/libbeat/common"
)

func GenerateGroupValue(event common.MapStr, groupKeys []string) (string, error) {
func GenerateGroupValue(event mapstr.M, groupKeys []string) (string, error) {
groupValues := make([]string, 0, len(groupKeys))
for _, groupKey := range groupKeys {
value, err := event.GetValue(groupKey)
Expand All @@ -19,8 +18,8 @@
return strings.Join(groupValues, ";"), nil
}

func GroupEventsByKeys(events []common.MapStr, groupKeys []string) map[string][]common.MapStr {
result := map[string][]common.MapStr{}
func GroupEventsByKeys(events []mapstr.M, groupKeys []string) map[string][]mapstr.M {
result := map[string][]mapstr.M{}
for _, event := range events {
group, err := GenerateGroupValue(event, groupKeys)
if err == nil {
Expand All @@ -32,13 +31,13 @@

func ConvertNumericValue(value interface{}) float64 {
floatResult := 0.0
switch value.(type) {

Check failure on line 34 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (linux)

S1034: assigning the result of this type assertion to a variable (switch value := value.(type)) could eliminate type assertions in switch cases (gosimple)

Check failure on line 34 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

S1034: assigning the result of this type assertion to a variable (switch value := value.(type)) could eliminate type assertions in switch cases (gosimple)
case int:
floatResult = float64(value.(int))

Check failure on line 36 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (linux)

S1034(related information): could eliminate this type assertion (gosimple)

Check failure on line 36 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

S1034(related information): could eliminate this type assertion (gosimple)
case int8:
floatResult = float64(value.(int8))

Check failure on line 38 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (linux)

S1034(related information): could eliminate this type assertion (gosimple)

Check failure on line 38 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

S1034(related information): could eliminate this type assertion (gosimple)
case int16:
floatResult = float64(value.(int16))

Check failure on line 40 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (linux)

S1034(related information): could eliminate this type assertion (gosimple)

Check failure on line 40 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

S1034(related information): could eliminate this type assertion (gosimple)
case int32:
floatResult = float64(value.(int32))
case int64:
Expand All @@ -56,7 +55,7 @@
case float32:
floatResult = float64(value.(float32))
case float64:
floatResult = float64(value.(float64))

Check failure on line 58 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (linux)

unnecessary conversion (unconvert)

Check failure on line 58 in metricbeat/helper/easyops/util.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

unnecessary conversion (unconvert)
}
return floatResult
}
6 changes: 0 additions & 6 deletions metricbeat/module/kubernetes/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ func (s *NodeTestSuite) ReadTestFile(testFile string) []byte {
return body
}

//=======
// events, err := eventMapping(body, &prometheus.MetricsMapping{})
// assert.NoError(t, err, "error mapping "+testFile)
// assert.Equal(t, len(events), 1)
//>>>>>>> 99ddfa11c9 (chore(): new k8s node metrics refs MONITOR-4095)

func (s *NodeTestSuite) TestEventMapping() {
body := s.ReadTestFile(testFile)
events, err := eventMapping(body, s.Logger, &prometheus.MetricsMapping{})
Expand Down
Loading