Skip to content

Commit

Permalink
Merge pull request #32 from easyops-cn/henryxie/cp_k8s
Browse files Browse the repository at this point in the history
chore():cherry-pick k8s code from branch release/7.14 refs MONITOR-7364
  • Loading branch information
XieJCHenry authored Nov 16, 2023
2 parents 6eea694 + de653de commit 9fe8bb4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 37 deletions.
11 changes: 5 additions & 6 deletions metricbeat/helper/easyops/cou_metric_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package easyops

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

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

type couMetricBuilder struct {
Expand All @@ -22,14 +21,14 @@ func newCouMetricBuilder(field string, originMetric []string, groupKeys []string
}
}

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 @@ func (builder *couMetricBuilder) Build(events []common.MapStr) []common.MapStr {
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

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

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

type divMetricBuilder struct {
Expand All @@ -21,14 +20,14 @@ func newDivMetricBuilder(field string, originMetric []string, groupKeys []string
}
}

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 @@ func (builder *divMetricBuilder) Build(events []common.MapStr) []common.MapStr {
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

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 newSubMetricBuilder(field string, originMetric []string, groupKeys []string
}
}

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 @@ func (builder *subMetricBuilder) Build(events []common.MapStr) []common.MapStr {
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

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 newSumMetricBuilder(field string, originMetric []string, groupKeys []string
}
}

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 @@ func (builder *sumMetricBuilder) Build(events []common.MapStr) []common.MapStr {
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

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 AggregateMetricMap struct {
}

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
Expand Up @@ -2,12 +2,11 @@ package easyops

import (
"fmt"
"github.com/elastic/elastic-agent-libs/mapstr"
"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 @@ func GenerateGroupValue(event common.MapStr, groupKeys []string) (string, error)
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 Down
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

0 comments on commit 9fe8bb4

Please sign in to comment.