From 2886634ad4b7011e34bd04a46f6318d1f5b610b5 Mon Sep 17 00:00:00 2001 From: Esonhugh Date: Mon, 2 Dec 2024 23:46:38 +0800 Subject: [PATCH] update: metrics to resource merge hooks --- pkg/metrics/resource.go | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/pkg/metrics/resource.go b/pkg/metrics/resource.go index 528ff61..79ebeae 100644 --- a/pkg/metrics/resource.go +++ b/pkg/metrics/resource.go @@ -45,24 +45,43 @@ func (rl *ResourceList) JSON() string { return res } -func ConvertToResource(r []*MetricMatcher) []*Resource { +type ResourceMergeHook func(m *MetricMatcher, resource ResourceList) (res *Resource, addFlag bool) + +var EndpointMergeHook ResourceMergeHook = func(m *MetricMatcher, res ResourceList) (r *Resource, addFlag bool) { + 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 { + r = res[i] + return r, false + } + } + return NewResource("endpoint"), true + } + return nil, true +} + +func ConvertToResource(r []*MetricMatcher, hooks ...ResourceMergeHook) []*Resource { var res []*Resource + if len(hooks) == 0 { + hooks = append(hooks, EndpointMergeHook) + } + for _, m := range r { var resource *Resource var addFlag = true - resourceType := m.Name - 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] - addFlag = false - } + for _, hook := range hooks { + resource, addFlag = hook(m, res) + if resource != nil { + break } - resourceType = "endpoint" } - if addFlag { + resourceType := m.Name + if resource != nil { + resourceType = resource.Type + } + if resource == nil && addFlag { resource = NewResource(resourceType) }