Skip to content

Commit

Permalink
update: Resource parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Esonhugh committed Dec 1, 2024
1 parent d07dacb commit 4551e3c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
11 changes: 10 additions & 1 deletion pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestMetrics(t *testing.T) {
f, err := os.Open("./metrics")
f, err := os.Open("./metrics_output.txt")
if err != nil {
t.Fatalf("open file failed: %v", err)
t.Fail()
Expand All @@ -21,13 +21,22 @@ func TestMetrics(t *testing.T) {
t.Fail()
}

output, err := os.OpenFile("./output.txt", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
t.Fatalf("open output file failed: %v", err)
}
defer output.Close()
for scanner.Scan() {
line := scanner.Text()
res, err := rule.Match(line)
if err != nil {
continue
} else {
t.Logf("matched: %s", res.DumpString())
// _, _ = output.WriteString(res.DumpString() + "\n")
}
}
var res ResourceList = ConvertToResource(rule)
_, _ = output.WriteString(res.JSON() + "\n")
t.Logf(res.JSON())
}
9 changes: 6 additions & 3 deletions pkg/metrics/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ type MetricMatcher struct {
ptr any `json:"-"`
}

func NewMetricMatcher(label_name string) *MetricMatcher {
func NewMetricMatcher(t string) *MetricMatcher {
return &MetricMatcher{
Name: label_name,
Name: t,
grok: grok.New(),
Labels: make([]Label, 0),
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (mt *MetricMatcher) Compile() error {
}

func (mt *MetricMatcher) Match(target string) (res map[string]string, err error) {
if !strings.HasPrefix(target, mt.Header+"{") {
if !strings.HasPrefix(target, mt.Header) {
log.Debugf("not match: %s", target)
if COMMON_MATCH_GROK.MatchString(target) {
res, err = COMMON_MATCH_GROK.ParseString(target)
Expand All @@ -93,6 +93,9 @@ func (mt *MetricMatcher) Match(target string) (res map[string]string, err error)
return nil, errors.New("can't match")
} else {
res, err = mt.grok.ParseString(target)
if err != nil || res == nil || len(res) == 0 {
return nil, errors.New("match failed, no result found")
}
mt.setResult(res)
return res, err
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/metrics/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package metrics

import "encoding/json"

type Resource struct {
Namespace string `json:"namespace"`
Type string `json:"type"`
Name string `json:"name"`
Spec map[string]string `json:"spec"`
}

func NewResource(t string) *Resource {
return &Resource{
Type: t,
Spec: make(map[string]string),
}
}

func (r *Resource) AddLabelSpec(l Label) {
r.Spec[l.Key] = l.Value
}

func (r *Resource) AddSpec(key string, value string) {
r.Spec[key] = value
}

type ResourceList []*Resource

func (rl *ResourceList) JSON() string {
b, _ := json.Marshal(rl)
return string(b)
}

func ConvertToResource(r []*MetricMatcher) []*Resource {
var res []*Resource
for _, m := range r {
var resource *Resource
if m.Name == "endpoint_address" || m.Name == "endpoint_port" {
for i, c := range res {
if m.FindLabel("namespace") == c.Namespace && m.FindLabel("endpoint") == c.Name {
resource = res[i]
} else {
resource = NewResource("endpoint")
}
}
} else {
resource = NewResource(m.Name)
}

resource.Namespace = m.FindLabel("namespace")
if m.Name == "endpoint_address" || m.Name == "endpoint_port" {
resource.Name = m.FindLabel("endpoint")
} else {
resource.Name = m.FindLabel(m.Name)
}
// merge endpoint_address and endpoint_port
for _, l := range m.Labels {
if l.Key != "namespace" && l.Key != m.Name {
resource.AddLabelSpec(l)
}
}
res = append(res, resource)
}
return res
}

0 comments on commit 4551e3c

Please sign in to comment.