@libo11y/spec 0.3.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,6 +51,11 @@ if (!result.ok) for (const v of result.violations) console.error(v.toString());
51
51
  const compiled = compileAppRegistry(registry); // throws AppRegistryError when invalid
52
52
  ```
53
53
 
54
+ `validateCompiledAppRegistry` checks a compiled registry that was built by
55
+ hand or read from JSON. It applies the rules that still apply to that form,
56
+ the namespace, each key's namespace and name, and each class, with the same
57
+ rule IDs and messages.
58
+
54
59
  The `@libo11y/spec/yaml` subpath reads a registry file. It decodes the
55
60
  YAML exactly as the Go implementation does, and it is the only module that
56
61
  depends on a YAML parser:
@@ -80,6 +85,6 @@ npx libo11y registry check o11y-registry.yaml
80
85
  ## Reference
81
86
 
82
87
  The specification itself is
83
- [docs/SPEC.md](https://github.com/jamesainslie/libo11y/blob/main/docs/SPEC.md).
84
- [docs/PARITY.md](https://github.com/jamesainslie/libo11y/blob/main/docs/PARITY.md)
88
+ [docs/SPEC.md](https://github.com/libo11y/libo11y/blob/main/docs/SPEC.md).
89
+ [docs/PARITY.md](https://github.com/libo11y/libo11y/blob/main/docs/PARITY.md)
85
90
  lists where the TypeScript packages match Go and where they differ.
@@ -53,6 +53,22 @@ export declare class AppRegistryError extends Error {
53
53
  * in name order, each attribute's rules in a fixed order.
54
54
  */
55
55
  export declare function validateAppRegistry(reg: AppRegistry): AppRegistryResult;
56
+ /** The result of validating a compiled registry: the registry itself, or every violation in rule order. */
57
+ export type CompiledAppRegistryResult = {
58
+ readonly ok: true;
59
+ readonly registry: CompiledAppRegistry;
60
+ } | {
61
+ readonly ok: false;
62
+ readonly violations: readonly AppRegistryViolation[];
63
+ };
64
+ /**
65
+ * Applies the rules that still apply to the compiled form, in validateAppRegistry's order and with its
66
+ * messages: the namespace, then each key in name order for its namespace, its naming and its class. A
67
+ * compiled registry is a plain structural type, so one built by hand or read from JSON may break them.
68
+ * A classes member that is not an object, or is empty, declares no attributes. Anything
69
+ * compileAppRegistry returned passes.
70
+ */
71
+ export declare function validateCompiledAppRegistry(reg: CompiledAppRegistry): CompiledAppRegistryResult;
56
72
  /**
57
73
  * Validates reg and returns each attribute's sensitivity class, the form a browser build embeds. The
58
74
  * classes object has no prototype, so a lookup never reaches Object.prototype, and both objects are
@@ -68,6 +68,36 @@ function compareCodePoints(a, b) {
68
68
  return d;
69
69
  }
70
70
  }
71
+ // The registry-level rules on the namespace.
72
+ function checkNamespace(namespace, fail) {
73
+ if (!NAMESPACE_PATTERN.test(namespace)) {
74
+ fail("libo11y_app_namespace_invalid", undefined, `namespace ${goQuote(namespace)} is not one snake_case segment`);
75
+ }
76
+ else if (APP_RESERVED_NAMESPACES.has(namespace)) {
77
+ fail("libo11y_app_namespace_reserved", undefined, `namespace ${goQuote(namespace)} belongs to libo11y, OpenTelemetry, or semconv`);
78
+ }
79
+ }
80
+ function failEmpty(fail) {
81
+ fail("libo11y_app_attributes_empty", undefined, "the registry declares no attributes");
82
+ }
83
+ // The rules on an attribute's name.
84
+ function checkName(namespace, name, fail) {
85
+ if (!name.startsWith(`${namespace}.`)) {
86
+ fail("libo11y_app_attr_outside_namespace", name, `not under the ${namespace} namespace`);
87
+ }
88
+ if (!NAME_PATTERN.test(name)) {
89
+ fail("libo11y_attr_naming", name, "not a dotted snake_case name with at least two segments");
90
+ }
91
+ }
92
+ // The rules on an attribute's sensitivity class.
93
+ function checkSensitivity(name, sensitivity, fail) {
94
+ if (sensitivity === "") {
95
+ fail("libo11y_sensitivity_missing", name, "no sensitivity class");
96
+ }
97
+ else if (!SENSITIVITIES.has(sensitivity)) {
98
+ fail("libo11y_sensitivity_unknown", name, `sensitivity ${goQuote(sensitivity)}`);
99
+ }
100
+ }
71
101
  /**
72
102
  * Applies the policy rules and returns every violation: registry-level rules first, then each attribute
73
103
  * in name order, each attribute's rules in a fixed order.
@@ -80,34 +110,18 @@ export function validateAppRegistry(reg) {
80
110
  if (reg.schema !== APP_REGISTRY_SCHEMA) {
81
111
  fail("libo11y_app_schema_unknown", undefined, `schema ${goQuote(reg.schema)}, want ${goQuote(APP_REGISTRY_SCHEMA)}`);
82
112
  }
83
- if (!NAMESPACE_PATTERN.test(reg.namespace)) {
84
- fail("libo11y_app_namespace_invalid", undefined, `namespace ${goQuote(reg.namespace)} is not one snake_case segment`);
85
- }
86
- else if (APP_RESERVED_NAMESPACES.has(reg.namespace)) {
87
- fail("libo11y_app_namespace_reserved", undefined, `namespace ${goQuote(reg.namespace)} belongs to libo11y, OpenTelemetry, or semconv`);
88
- }
113
+ checkNamespace(reg.namespace, fail);
89
114
  const names = Object.keys(reg.attributes).sort(compareCodePoints);
90
- if (names.length === 0) {
91
- fail("libo11y_app_attributes_empty", undefined, "the registry declares no attributes");
92
- }
115
+ if (names.length === 0)
116
+ failEmpty(fail);
93
117
  for (const name of names) {
94
118
  const a = reg.attributes[name];
95
119
  const values = a.values ?? [];
96
- if (!name.startsWith(`${reg.namespace}.`)) {
97
- fail("libo11y_app_attr_outside_namespace", name, `not under the ${reg.namespace} namespace`);
98
- }
99
- if (!NAME_PATTERN.test(name)) {
100
- fail("libo11y_attr_naming", name, "not a dotted snake_case name with at least two segments");
101
- }
120
+ checkName(reg.namespace, name, fail);
102
121
  if (!ATTRIBUTE_TYPES.has(a.type)) {
103
122
  fail("libo11y_app_type_unknown", name, `type ${goQuote(a.type)}`);
104
123
  }
105
- if (a.sensitivity === "") {
106
- fail("libo11y_sensitivity_missing", name, "no sensitivity class");
107
- }
108
- else if (!SENSITIVITIES.has(a.sensitivity)) {
109
- fail("libo11y_sensitivity_unknown", name, `sensitivity ${goQuote(a.sensitivity)}`);
110
- }
124
+ checkSensitivity(name, a.sensitivity, fail);
111
125
  if (a.cardinality === "") {
112
126
  fail("libo11y_cardinality_missing", name, "no cardinality budget");
113
127
  }
@@ -134,6 +148,34 @@ export function validateAppRegistry(reg) {
134
148
  }
135
149
  return violations.length === 0 ? { ok: true, registry: reg } : { ok: false, violations };
136
150
  }
151
+ /**
152
+ * Applies the rules that still apply to the compiled form, in validateAppRegistry's order and with its
153
+ * messages: the namespace, then each key in name order for its namespace, its naming and its class. A
154
+ * compiled registry is a plain structural type, so one built by hand or read from JSON may break them.
155
+ * A classes member that is not an object, or is empty, declares no attributes. Anything
156
+ * compileAppRegistry returned passes.
157
+ */
158
+ export function validateCompiledAppRegistry(reg) {
159
+ const violations = [];
160
+ const fail = (rule, attr, detail) => {
161
+ violations.push(new AppRegistryViolation(rule, attr, detail));
162
+ };
163
+ // The input may be deserialized, so nothing about its shape is assumed.
164
+ const namespace = reg.namespace;
165
+ const ns = typeof namespace === "string" ? namespace : "";
166
+ checkNamespace(ns, fail);
167
+ const classes = reg.classes;
168
+ const isObject = typeof classes === "object" && classes !== null && !Array.isArray(classes);
169
+ const names = isObject ? Object.keys(classes).sort(compareCodePoints) : [];
170
+ if (names.length === 0)
171
+ failEmpty(fail);
172
+ for (const name of names) {
173
+ checkName(ns, name, fail);
174
+ const sensitivity = classes[name];
175
+ checkSensitivity(name, sensitivity === undefined || sensitivity === null ? "" : String(sensitivity), fail);
176
+ }
177
+ return violations.length === 0 ? { ok: true, registry: reg } : { ok: false, violations };
178
+ }
137
179
  /**
138
180
  * Validates reg and returns each attribute's sensitivity class, the form a browser build embeds. The
139
181
  * classes object has no prototype, so a lookup never reaches Object.prototype, and both objects are
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libo11y/spec",
3
- "version": "0.3.0",
3
+ "version": "0.6.0",
4
4
  "description": "The libo11y telemetry specification for TypeScript: attribute keys, enums, event and metric names, and the sensitivity tables, generated from spec/registry.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",
23
- "url": "git+https://github.com/jamesainslie/libo11y.git",
23
+ "url": "git+https://github.com/libo11y/libo11y.git",
24
24
  "directory": "ts/packages/spec"
25
25
  },
26
26
  "dependencies": {