Skip to content

Commit f7d30b1

Browse files
jsuerethlmolkova
andauthored
Attempt to optimise attribute name collision checks. (open-telemetry#1328)
Co-authored-by: Liudmila Molkova <limolkova@microsoft.com>
1 parent 9e7ce95 commit f7d30b1

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

policies/attribute_name_collisions.rego

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
package after_resolution
22

3+
# Data structures to make checking things faster.
4+
attribute_names := { data |
5+
group := input.groups[_]
6+
attr := group.attributes[_]
7+
data := { "name": attr.name, "const_name": to_const_name(attr.name), "namespace_prefix": to_namespace_prefix(attr.name) }
8+
}
9+
10+
311
deny[attr_registry_collision(description, name)] {
4-
names := attr_names_except(excluded_const_collisions)
5-
name := names[_]
6-
const_name := to_const_name(name)
7-
collisions:= { n | n := attr_names_except(excluded_const_collisions)[_]; n != name; to_const_name(n) == const_name }
12+
some i
13+
name := attribute_names[i].name
14+
const_name := attribute_names[i].const_name
15+
not excluded_const_collisions[name]
16+
collisions := [other.name |
17+
other := attribute_names[_]
18+
other.name != name
19+
other.const_name == const_name
20+
not excluded_const_collisions[other.name]
21+
]
822
count(collisions) > 0
9-
1023
# TODO (https://github.com/open-telemetry/weaver/issues/279): provide other violation properties once weaver supports it.
1124
description := sprintf("Attribute '%s' has the same constant name '%s' as '%s'.", [name, const_name, collisions])
1225
}
1326

1427
deny[attr_registry_collision(description, name)] {
15-
names := attr_names_except(excluded_namespace_collisions)
16-
name := names[_]
17-
18-
collisions:= { n | n := input.groups[_].attributes[_].name; startswith(n, to_namespace_prefix(name)) }
28+
some i
29+
name := attribute_names[i].name
30+
prefix := attribute_names[i].namespace_prefix
31+
not excluded_namespace_collisions[name]
32+
collisions := [other.name |
33+
other := attribute_names[_]
34+
other.name != name
35+
startswith(other.name, prefix)
36+
]
1937
count(collisions) > 0
20-
2138
# TODO (https://github.com/open-telemetry/weaver/issues/279): provide other violation properties once weaver supports it.
22-
description := sprintf("Attribute '%s' name is used as a namespace in the following attributes '%s'.", [name, collisions])
39+
description := sprintf("Attribute '%s' is used as a namespace in '%s'.", [name, collisions])
2340
}
2441

2542
attr_registry_collision(description, attr_name) = violation {
@@ -40,10 +57,6 @@ to_const_name(name) = const_name {
4057
const_name := replace(name, ".", "_")
4158
}
4259

43-
attr_names_except(excluded) = names {
44-
names := { n | n := input.groups[_].attributes[_].name } - excluded
45-
}
46-
4760
# These lists contain exceptions for existing collisions that were introduced unintentionally.
4861
# We'll have a way to specify how collision resolution happens in the schema -
4962
# see phase 2 in https://github.com/open-telemetry/semantic-conventions/issues/1118#issuecomment-2173803006

0 commit comments

Comments
 (0)