Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to optimise attribute name collision checks. #1328

Merged
merged 9 commits into from
Aug 19, 2024
36 changes: 19 additions & 17 deletions policies/attribute_name_collisions.rego
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package after_resolution

deny[attr_registry_collision(description, name)] {
names := attr_names_except(excluded_const_collisions)
name := names[_]
const_name := to_const_name(name)
collisions:= { n | n := attr_names_except(excluded_const_collisions)[_]; n != name; to_const_name(n) == const_name }
count(collisions) > 0
# Data structures to make checking things faster.
attribute_names := [ data |
attr := input.groups[_].attributes[_].name
data := { "name": attr.name, "const_name": to_const_name(attr.name), "namespace_prefix": to_namespace_prefix(attr.name) }
]


deny[attr_registry_collision(description, name)] {
name := attribute_names
name2 := attribute_names
name.name != name2.name
name.const_name == name2.const_name
not excluded_const_collisions[name.name]
# TODO (https://github.com/open-telemetry/weaver/issues/279): provide other violation properties once weaver supports it.
description := sprintf("Attribute '%s' has the same constant name '%s' as '%s'.", [name, const_name, collisions])
description := sprintf("Attribute '%s' has the same constant name '%s' as '%s'.", [name.name, name.const_name, name2.name])
}

deny[attr_registry_collision(description, name)] {
names := attr_names_except(excluded_namespace_collisions)
name := names[_]

collisions:= { n | n := input.groups[_].attributes[_].name; startswith(n, to_namespace_prefix(name)) }
count(collisions) > 0
name := attribute_names
name2 := attribute_names
name.name != name2.name
name.namespace_prefix == name2.namespace_prefix
not excluded_namespace_collisions[name.namespace_prefix]

# TODO (https://github.com/open-telemetry/weaver/issues/279): provide other violation properties once weaver supports it.
description := sprintf("Attribute '%s' name is used as a namespace in the following attributes '%s'.", [name, collisions])
description := sprintf("Attribute '%s' name is used as a namespace in the following attributes '%s'.", [name.name, name2.name])
}

attr_registry_collision(description, attr_name) = violation {
Expand All @@ -40,10 +46,6 @@ to_const_name(name) = const_name {
const_name := replace(name, ".", "_")
}

attr_names_except(excluded) = names {
names := { n | n := input.groups[_].attributes[_].name } - excluded
}

# These lists contain exceptions for existing collisions that were introduced unintentionally.
# We'll have a way to specify how collision resolution happens in the schema -
# see phase 2 in https://github.com/open-telemetry/semantic-conventions/issues/1118#issuecomment-2173803006
Expand Down
Loading