Skip to content

Commit 459fbdb

Browse files
jsuerethlmolkova
andauthored
Add policy enforcement for attribute registry. (open-telemetry#1208)
Co-authored-by: Liudmila Molkova <[email protected]>
1 parent 4d271fb commit 459fbdb

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ install-yamllint:
111111
yamllint:
112112
yamllint .
113113

114+
# Check semantic convention policies on YAML files
115+
.PHONY: check-policies
116+
check-policies:
117+
docker run --rm -v $(PWD)/model:/source -v $(PWD)/policies:/policies -v $(PWD)/templates:/templates \
118+
otel/weaver:${WEAVER_VERSION} registry check \
119+
--registry=/source \
120+
--diagnostic-format=ansi \
121+
--policy=/policies/registry.rego
122+
114123
# Generate markdown tables from YAML definitions
115124
.PHONY: table-generation
116125
table-generation:
@@ -180,7 +189,7 @@ fix-format:
180189
# Run all checks in order of speed / likely failure.
181190
# As a last thing, run attribute registry generation and git-diff for differences.
182191
.PHONY: check
183-
check: misspell markdownlint check-format markdown-toc compatibility-check markdown-link-check attribute-registry-generation
192+
check: misspell markdownlint check-format markdown-toc compatibility-check markdown-link-check check-policies attribute-registry-generation
184193
git diff --exit-code ':*.md' || (echo 'Generated markdown Table of Contents is out of date, please run "make markdown-toc" and commit the changes in this PR.' && exit 1)
185194
@echo "All checks complete"
186195

policies/registry.rego

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package before_resolution
2+
3+
# This file enforces policies requiring all attributes to be defined within
4+
# a semantic convention "registry". This is a naming/structure convention
5+
# used by semantic conventions.
6+
7+
# Helper to create attribute registry violations.
8+
attr_registry_violation(violation_id, group_id, attr_id) = violation {
9+
violation := {
10+
"id": violation_id,
11+
"type": "semantic_convention_policies",
12+
"category": "attribute_registry_checks",
13+
"group": group_id,
14+
"attr": attr_id,
15+
}
16+
}
17+
18+
# We only allow attribute groups in the attribute registry.
19+
deny[attr_registry_violation("attribute_registry_can_only_contain_attribute_groups", group.id, "")] {
20+
group := input.groups[_]
21+
startswith(group.id, "registry.")
22+
group.type != "attribute_group"
23+
}
24+
25+
# Any group that is NOT in the attribute registry that has an attribute id is
26+
# in violation of not using the attribute registry.
27+
deny[attr_registry_violation("attributes_must_be_defined_in_attribute_registry", group.id, attr.id)] {
28+
group := input.groups[_]
29+
not startswith(group.id, "registry.")
30+
attr := group.attributes[_]
31+
attr.id != null
32+
}
33+
34+
# A registry `attribute_group` containing at least one `ref` attribute is
35+
# considered invalid if it's not in the registry group.
36+
deny[attr_registry_violation("attributes_in_registry_cannot_reference_each_other", group.id, attr.ref)] {
37+
# TODO - this will need to be updated to support `embed` in the future.
38+
group := input.groups[_]
39+
startswith(group.id, "registry.")
40+
attr := group.attributes[_]
41+
attr.ref != null
42+
}

0 commit comments

Comments
 (0)