Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/typescript/src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export class OTelMetricEmitter {
readonly #actionCount: Counter; readonly #actionDuration: Histogram; readonly #dataFlowCount: Counter;
readonly #tokenCount: Counter; readonly #cost: Counter; readonly #classifications: ReadonlySet<string>;
constructor(meter: Meter, options: {classificationValues?: Iterable<string>} = {}) {
if (typeof options.classificationValues === "string") throw new Error("classificationValues must be an iterable of strings, not a string");
this.#classifications = new Set(options.classificationValues ?? []);
if (this.#classifications.size > 64) throw new Error("classificationValues cannot contain more than 64 entries");
for (const value of this.#classifications) if (!value || value.length > 128) throw new Error("classificationValues must be non-empty and at most 128 characters");
for (const value of this.#classifications) if (typeof value !== "string" || !value || value.length > 128 || value !== value.trim() || value === "_other") throw new Error("classificationValues must be non-empty and at most 128 characters");
this.#policyCount = meter.createCounter("agentrust.policy.decisions", {unit: "{decision}", description: "Policy decisions"});
this.#policyDuration = meter.createHistogram("agentrust.policy.evaluation.duration", {unit: "s", description: "Policy evaluation duration"});
this.#approvalCount = meter.createCounter("agentrust.approval.events", {unit: "{event}", description: "Approval lifecycle events"});
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ test("classification dimensions are bounded by an explicit allowlist", () => {
emitter.emit(fixture("data-flow.json"));
assert.equal(meter.instruments.get("agentrust.data_flow.events")!.calls[0]!.attributes!["agentrust.data_flow.classification"], "_other");
assert.throws(() => new OTelMetricEmitter(new RecordingMeter() as unknown as Meter, {classificationValues: Array.from({length: 65}, (_, i) => `class-${i}`)}), /more than 64/);
assert.throws(() => new OTelMetricEmitter(new RecordingMeter() as unknown as Meter, {classificationValues: "public" as unknown as Iterable<string>}), /not a string/);
for (const bad of [["_other"], [" public "], [123 as unknown as string]]) assert.throws(() => new OTelMetricEmitter(new RecordingMeter() as unknown as Meter, {classificationValues: bad}), /classificationValues/);
});

test("policy, approval, and action metrics expose only bounded semantic dimensions", () => {
Expand Down
16 changes: 13 additions & 3 deletions src/agentrust_telemetry/otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ def __init__(
*,
classification_values: frozenset[str] = frozenset(),
) -> None:
if len(classification_values) > 64:
if isinstance(classification_values, (str, bytes)):
raise ValueError("classification_values must be an iterable of strings, not a string")
normalized = frozenset(classification_values)
if len(normalized) > 64:
raise ValueError("classification_values cannot contain more than 64 entries")
if any(not value or len(value) > 128 for value in classification_values):
if any(
not isinstance(value, str)
or not value
or len(value) > 128
or value != value.strip()
or value == "_other"
for value in normalized
):
raise ValueError("classification_values must be non-empty and at most 128 characters")
self._classification_values = classification_values
self._classification_values = normalized
self._policy_count = meter.create_counter(
"agentrust.policy.decisions", unit="{decision}", description="Policy decisions"
)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_otel_projectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def create_histogram(self, *args, **kwargs):
OTelMetricEmitter(
Meter(), classification_values=frozenset(f"class-{i}" for i in range(65))
)
for bad_vals in ("public", b"public", frozenset({"_other"}), frozenset({" public "}), frozenset({123})):
with self.subTest(bad_vals=bad_vals):
with self.assertRaises(ValueError):
OTelMetricEmitter(Meter(), classification_values=bad_vals)

def test_action_metrics_exclude_action_and_agent_identifiers(self):
from opentelemetry.sdk.metrics import MeterProvider
Expand Down