Skip to content

Commit

Permalink
Autocreate metric whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Nov 19, 2024
1 parent b80cc6d commit fd93cc1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"buildFlags": "-ldflags '-X github.com/vkcom/statshouse/internal/vkgo/build.trustedSubnetGroups=0.0.0.0/0'",
"args": [
"aggregator",
"--cluster=test_shard_localhost",
"--cluster=default",
"--log-level=trace",
"--agg-addr=localhost:13336",
"--kh=localhost:8123",
Expand Down
55 changes: 38 additions & 17 deletions internal/aggregator/autocreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ type autoCreate struct {
running bool // guard against double "run"
}

type KnownTags map[int32]knownTags
type KnownTags map[int32]namespaceKnownTags

type namespaceKnownTags struct {
knownTags // namespace level
groups map[int32]knownTags // group level tags, by group ID
}

type knownTags struct {
namespace map[string]KnownTag // namespace level tags
groups map[int32]map[string]KnownTag // group level tags, by group ID
p string // metric name prefix
m map[string]KnownTag // name to tag mapping
}

type KnownTagsJSON struct {
Expand All @@ -54,9 +59,10 @@ type KnownTagsJSON struct {
}

type KnownTag struct {
ID string `json:"id"`
RawKind string `json:"raw_kind,omitempty"`
SkipMapping bool `json:"skip_mapping,omitempty"`
ID string `json:"id"`
RawKind string `json:"raw_kind,omitempty"`
SkipMapping bool `json:"skip_mapping,omitempty"`
Whitelist []string `json:"whitelist,omitempty"`
}

func newAutoCreate(a *Aggregator, client *tlmetadata.Client, defaultNamespaceAllowed bool) *autoCreate {
Expand Down Expand Up @@ -351,24 +357,24 @@ func (m KnownTags) PublishDraftTags(meta *format.MetricMetaValue) int {
return 0
}
var n int
if len(c.namespace) != 0 {
n = publishDraftTags(meta, c.namespace)
if len(c.m) != 0 {
n = publishDraftTags(meta, c.knownTags)
}
if len(c.groups) == 0 ||
meta.GroupID == 0 ||
meta.GroupID == format.BuiltinGroupIDDefault {
return n
}
if v := c.groups[meta.GroupID]; len(v) != 0 {
if v := c.groups[meta.GroupID]; len(v.m) != 0 {
return n + publishDraftTags(meta, v)
}
return n
}

func publishDraftTags(meta *format.MetricMetaValue, knownTags map[string]KnownTag) int {
func publishDraftTags(meta *format.MetricMetaValue, t knownTags) int {
var n int
for k, v := range meta.TagsDraft {
tag, ok := knownTags[k]
tag, ok := t.m[k]
if !ok || tag.ID == "" {
continue
}
Expand All @@ -379,6 +385,15 @@ func publishDraftTags(meta *format.MetricMetaValue, knownTags map[string]KnownTa
n++
}
} else if x := format.TagIndex(tag.ID); 0 <= x && x < format.MaxTags && meta.Tags[x].Name == "" {
allow := len(tag.Whitelist) == 0 // empty whitelist allows all
for i := 0; i < len(tag.Whitelist) && !allow; i++ {
if strings.HasPrefix(meta.Name, t.p) {
allow = tag.Whitelist[i] == meta.Name[len(t.p):]
}
}
if !allow {
continue
}
if tag.RawKind != "" {
rawKind := tag.RawKind
if rawKind == "int" {
Expand Down Expand Up @@ -406,7 +421,7 @@ func ParseKnownTags(configS []byte, meta format.MetaStorageInterface) (KnownTags
if err != nil {
return nil, err
}
res := make(map[int32]knownTags)
res := make(map[int32]namespaceKnownTags)
for namespaceName, v := range s {
if namespaceName == "" {
return nil, fmt.Errorf("namespace not set")
Expand All @@ -418,7 +433,7 @@ func ParseKnownTags(configS []byte, meta format.MetaStorageInterface) (KnownTags
if namespace.ID == format.BuiltinNamespaceIDDefault {
return nil, fmt.Errorf("namespace can not be __default")
}
knownTagsG := make(map[int32]map[string]KnownTag, len(v.Groups))
knownTagsG := make(map[int32]knownTags, len(v.Groups))
for groupName, g := range v.Groups {
groupName := namespaceName + format.NamespaceSeparator + groupName
group := meta.GetGroupByName(groupName)
Expand All @@ -428,11 +443,17 @@ func ParseKnownTags(configS []byte, meta format.MetaStorageInterface) (KnownTags
if group.ID == format.BuiltinGroupIDDefault {
return nil, fmt.Errorf("scrape group can not be __default")
}
knownTagsG[group.ID] = g
knownTagsG[group.ID] = knownTags{
p: namespace.Name + format.NamespaceSeparator + groupName,
m: g,
}
}
res[namespace.ID] = knownTags{
namespace: v.Namespace,
groups: knownTagsG,
res[namespace.ID] = namespaceKnownTags{
knownTags: knownTags{
p: namespace.Name + format.NamespaceSeparator,
m: v.Namespace,
},
groups: knownTagsG,
}
}
return res, nil
Expand Down

0 comments on commit fd93cc1

Please sign in to comment.