use crate::graph::Graph;
use crate::projection::ids::{content_hash, element_id};
use std::collections::{BTreeMap, BTreeSet};
pub const FAMILY: &str = "otel";
pub const VENDOR_NS: &str = "domainforge";
pub const RESERVED_NAMESPACES: &[&str] = &[
"otel",
"service",
"telemetry",
"process",
"host",
"os",
"cloud",
"k8s",
"container",
"http",
"db",
"rpc",
"messaging",
"network",
"url",
"server",
"client",
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttrType {
String,
Int,
Double,
Boolean,
}
impl AttrType {
pub fn as_str(self) -> &'static str {
match self {
AttrType::String => "string",
AttrType::Int => "int",
AttrType::Double => "double",
AttrType::Boolean => "boolean",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attribute {
pub key: String,
pub r#type: AttrType,
pub brief: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpanKind {
Internal,
Producer,
Consumer,
Client,
Server,
}
impl SpanKind {
pub fn as_str(self) -> &'static str {
match self {
SpanKind::Internal => "internal",
SpanKind::Producer => "producer",
SpanKind::Consumer => "consumer",
SpanKind::Client => "client",
SpanKind::Server => "server",
}
}
}
#[derive(Debug, Clone)]
pub struct AttributeGroup {
pub id: String,
pub brief: String,
pub attributes: Vec<Attribute>,
}
#[derive(Debug, Clone)]
pub struct SpanSuggestion {
pub id: String,
pub brief: String,
pub span_kind: SpanKind,
pub attribute_refs: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct TelemetryIR {
pub model_ns: String,
pub model_ref: String,
pub model_hash: String,
pub groups: Vec<AttributeGroup>,
pub spans: Vec<SpanSuggestion>,
}
pub fn validate_attribute_key(model_ns: &str, key: &str) -> Result<(), String> {
let model_prefix = format!("{model_ns}.");
let vendor_prefix = format!("{VENDOR_NS}.");
if key.starts_with(&model_prefix) || key.starts_with(&vendor_prefix) {
return Ok(());
}
let ns = key.split('.').next().unwrap_or(key);
Err(format!(
"otel projection refuses attribute `{key}`: only the model namespace \
`{model_ns}.*` and the DomainForge vendor namespace `{VENDOR_NS}.*` may \
be minted; `{ns}.*` is reserved by OpenTelemetry semantic conventions"
))
}
fn segment(raw: &str) -> String {
let mut out = String::with_capacity(raw.len());
for ch in raw.chars() {
if ch.is_ascii_alphanumeric() {
out.push(ch.to_ascii_lowercase());
} else {
out.push('_');
}
}
if out.is_empty() {
return "_".to_string();
}
if out.starts_with(|c: char| c.is_ascii_digit()) {
out.insert(0, '_');
}
out
}
fn model_namespace(graph: &Graph) -> String {
let mut nss: BTreeSet<String> = BTreeSet::new();
for e in graph.all_entities() {
nss.insert(e.namespace().to_string());
}
for r in graph.all_roles() {
nss.insert(r.namespace().to_string());
}
for r in graph.all_resources() {
nss.insert(r.namespace().to_string());
}
for f in graph.all_flows() {
nss.insert(f.namespace().to_string());
}
for p in graph.all_policies() {
nss.insert(p.namespace.clone());
}
nss.into_iter()
.find(|s| !s.is_empty())
.map(|s| segment(&s))
.unwrap_or_else(|| VENDOR_NS.to_string())
}
impl TelemetryIR {
pub fn all_attributes(&self) -> Vec<&Attribute> {
let mut by_key: BTreeMap<&str, &Attribute> = BTreeMap::new();
for g in &self.groups {
for a in &g.attributes {
by_key.entry(&a.key).or_insert(a);
}
}
by_key.into_values().collect()
}
pub fn from_graph(graph: &Graph, model_ref: &str) -> Result<Self, String> {
let ns = model_namespace(graph);
let mut entities: Vec<_> = graph.all_entities();
entities.sort_by_key(|e| (e.name().to_string(), e.id().to_string()));
let mut resources: Vec<_> = graph.all_resources();
resources.sort_by_key(|r| (r.name().to_string(), r.id().to_string()));
let mut policies: Vec<_> = graph.all_policies();
policies.sort_by_key(|p| (p.name.clone(), p.id.to_string()));
let entity_name: BTreeMap<String, String> = entities
.iter()
.map(|e| (e.id().to_string(), e.name().to_string()))
.collect();
let resource_name: BTreeMap<String, String> = resources
.iter()
.map(|r| (r.id().to_string(), r.name().to_string()))
.collect();
let model_hash = compute_model_hash(&entities, &resources, &policies, graph);
let mut groups: Vec<AttributeGroup> = Vec::new();
let mut spans: Vec<SpanSuggestion> = Vec::new();
let mk = |key: String, ty: AttrType, brief: String| -> Result<Attribute, String> {
validate_attribute_key(&ns, &key)?;
Ok(Attribute {
key,
r#type: ty,
brief,
})
};
groups.push(AttributeGroup {
id: format!("registry.{VENDOR_NS}.resource"),
brief: "DomainForge correlation attributes carried on the OTel Resource. \
They bind every emitted span and metric back to the projected model \
and the specific domain element it describes."
.to_string(),
attributes: vec![
mk(
format!("{VENDOR_NS}.model.ref"),
AttrType::String,
"Provenance label of the source model this telemetry was projected from."
.to_string(),
)?,
mk(
format!("{VENDOR_NS}.model.hash"),
AttrType::String,
format!(
"Deterministic content hash of the projected model ({model_hash}). \
Runtime telemetry carrying this value is provably about this exact model."
),
)?,
mk(
format!("{VENDOR_NS}.element.id"),
AttrType::String,
"Deterministic element id (domainforge element_id) of the domain element a \
span or metric describes; the join key back to the model."
.to_string(),
)?,
],
});
for e in &entities {
let name = e.name();
let seg = segment(name);
let eid = element_id(FAMILY, &["entity", e.namespace(), name]);
groups.push(AttributeGroup {
id: format!("registry.{ns}.entity.{seg}"),
brief: format!("Telemetry attributes for entity `{name}` (element {eid})."),
attributes: vec![
mk(
format!("{ns}.entity.{seg}.id"),
AttrType::String,
format!("Deterministic element id of entity `{name}`: {eid}."),
)?,
mk(
format!("{ns}.entity.{seg}.name"),
AttrType::String,
format!("Human-authored name of entity `{name}`."),
)?,
],
});
}
let mut flow_keys: Vec<(String, String, String)> = Vec::new();
for f in graph.all_flows() {
let from = f.from_id().to_string();
let to = f.to_id().to_string();
let res = f.resource_id().to_string();
if !entity_name.contains_key(&from)
|| !entity_name.contains_key(&to)
|| !resource_name.contains_key(&res)
{
continue;
}
flow_keys.push((from, to, res));
}
flow_keys.sort();
flow_keys.dedup();
for (from, to, res) in &flow_keys {
let from_name = &entity_name[from];
let to_name = &entity_name[to];
let res_name = &resource_name[res];
let key = element_id(FAMILY, &["flow", res_name, from_name, to_name]);
let prefix = format!("{ns}.flow.{key}");
let attrs = vec![
mk(
format!("{prefix}.resource"),
AttrType::String,
format!("Resource moved by this flow: `{res_name}`."),
)?,
mk(
format!("{prefix}.source"),
AttrType::String,
format!("Source entity of this flow: `{from_name}`."),
)?,
mk(
format!("{prefix}.target"),
AttrType::String,
format!("Target entity of this flow: `{to_name}`."),
)?,
mk(
format!("{prefix}.quantity"),
AttrType::Double,
format!("Quantity of `{res_name}` moved from `{from_name}` to `{to_name}`."),
)?,
];
let refs: Vec<String> = attrs
.iter()
.map(|a| a.key.clone())
.chain(std::iter::once(format!("{VENDOR_NS}.element.id")))
.collect();
groups.push(AttributeGroup {
id: format!("registry.{ns}.flow.{key}"),
brief: format!(
"Telemetry attributes for the flow moving `{res_name}` from `{from_name}` \
to `{to_name}`."
),
attributes: attrs,
});
spans.push(SpanSuggestion {
id: format!("span.{ns}.flow.{key}"),
brief: format!(
"Suggested INTERNAL span for the `{from_name}` → `{to_name}` transfer of \
`{res_name}`."
),
span_kind: SpanKind::Internal,
attribute_refs: refs,
});
}
for p in &policies {
let name = &p.name;
let seg = segment(name);
groups.push(AttributeGroup {
id: format!("registry.{ns}.policy.{seg}"),
brief: format!(
"Telemetry attributes for the decision point of policy `{name}` \
({:?} / {:?}, priority {}).",
p.kind, p.modality, p.priority
),
attributes: vec![
mk(
format!("{ns}.policy.{seg}.decision"),
AttrType::String,
format!(
"Decision emitted when evaluating policy `{name}` \
(e.g. `allow` / `deny`)."
),
)?,
mk(
format!("{ns}.policy.{seg}.modality"),
AttrType::String,
format!("Deontic modality of policy `{name}`: {:?}.", p.modality),
)?,
],
});
}
groups.sort_by(|a, b| a.id.cmp(&b.id));
spans.sort_by(|a, b| a.id.cmp(&b.id));
Ok(TelemetryIR {
model_ns: ns,
model_ref: model_ref.to_string(),
model_hash,
groups,
spans,
})
}
}
fn compute_model_hash(
entities: &[&crate::primitives::Entity],
resources: &[&crate::primitives::Resource],
policies: &[&crate::policy::Policy],
graph: &Graph,
) -> String {
let mut parts: Vec<String> = Vec::new();
for e in entities {
parts.push(format!("entity:{}:{}", e.namespace(), e.name()));
}
for r in resources {
parts.push(format!("resource:{}:{}", r.namespace(), r.name()));
}
let mut flow_parts: Vec<String> = graph
.all_flows()
.iter()
.map(|f| format!("flow:{}:{}:{}", f.resource_id(), f.from_id(), f.to_id()))
.collect();
flow_parts.sort();
parts.extend(flow_parts);
for p in policies {
parts.push(format!("policy:{}:{}", p.namespace, p.name));
}
let refs: Vec<&str> = parts.iter().map(String::as_str).collect();
content_hash(&refs)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::parse_to_graph;
const SOURCE: &str = r#"
@namespace "demo"
Entity "Warehouse" in demo
Entity "Factory" in demo
Resource "CameraUnits" units in demo
Flow "CameraUnits" from "Warehouse" to "Factory" quantity 100
Policy positive_flow as: Flow.quantity > 0
"#;
fn ir() -> TelemetryIR {
let graph = parse_to_graph(SOURCE).expect("parses");
TelemetryIR::from_graph(&graph, "test.sea").expect("builds")
}
#[test]
fn every_attribute_is_namespaced() {
let ir = ir();
for a in ir.all_attributes() {
assert!(
a.key.starts_with("demo.") || a.key.starts_with("domainforge."),
"attribute `{}` escaped the model/vendor namespace",
a.key
);
}
}
#[test]
fn carries_correlation_attributes() {
let ir = ir();
let keys: BTreeSet<&str> = ir.all_attributes().iter().map(|a| a.key.as_str()).collect();
assert!(keys.contains("domainforge.model.hash"));
assert!(keys.contains("domainforge.element.id"));
assert!(keys.contains("domainforge.model.ref"));
}
#[test]
fn reserved_namespace_attribute_is_rejected() {
assert!(validate_attribute_key("demo", "service.name").is_err());
assert!(validate_attribute_key("demo", "otel.scope.name").is_err());
assert!(validate_attribute_key("demo", "telemetry.sdk.version").is_err());
assert!(validate_attribute_key("demo", "demo.entity.warehouse.id").is_ok());
assert!(validate_attribute_key("demo", "domainforge.model.hash").is_ok());
}
#[test]
fn flow_yields_internal_span_suggestion() {
let ir = ir();
assert_eq!(ir.spans.len(), 1);
assert_eq!(ir.spans[0].span_kind, SpanKind::Internal);
assert!(ir.spans[0]
.attribute_refs
.contains(&"domainforge.element.id".to_string()));
}
#[test]
fn deterministic_across_builds() {
let a = ir();
let b = ir();
assert_eq!(a.model_hash, b.model_hash);
assert_eq!(
a.all_attributes()
.iter()
.map(|x| x.key.clone())
.collect::<Vec<_>>(),
b.all_attributes()
.iter()
.map(|x| x.key.clone())
.collect::<Vec<_>>()
);
}
}