lnmp-envelope
Operational metadata envelope for LNMP records, aligned with CloudEvents, Kafka Headers, and W3C Trace Context standards.
Overview
LNMP Envelope adds operational context (timestamp, source, trace ID, sequence) to LNMP records without affecting deterministic properties or semantic checksums.
Key Features
- ✅ Preserves Core Determinism: Envelope metadata does NOT affect
SemanticChecksum - ✅ Zero Overhead: Unused envelope features have no performance cost
- ✅ Standards Aligned: Compatible with CloudEvents, Kafka Headers, OpenTelemetry
- ✅ Transport Agnostic: Defined independently, bindings for HTTP/Kafka/gRPC
- ✅ Future Proof: Extensible via labels, unknown fields skipped gracefully
Industry Standards Alignment
| Standard | LNMP Envelope Mapping |
|---|---|
| CloudEvents | time → timestamp, source → source, id → trace_id + sequence |
| Kafka Headers | Record-level metadata separate from payload |
| W3C Trace Context | Compatible trace_id format for distributed tracing |
| OpenTelemetry | Seamless span context propagation |
Quick Start
use ;
// Create a record
let mut record = new;
record.add_field;
// Wrap with envelope
let envelope = new
.timestamp
.source
.trace_id
.sequence
.build;
assert!;
Encoding Formats
Binary (TLV)
Envelope metadata encoded as Type-Length-Value entries:
Type: 0x10 (Timestamp) | Length: 8 | Value: u64 BE
Type: 0x11 (Source) | Length: N | Value: UTF-8 string
Type: 0x12 (TraceID) | Length: M | Value: UTF-8 string
Type: 0x13 (Sequence) | Length: 8 | Value: u64 BE
Example:
use TlvEncoder;
let binary = encode.unwrap;
// 51 bytes: 10 00 08 00 00 01 93 59 7c 6d 78 11 00 0c ...
Text (Header Comment)
#ENVELOPE timestamp=1732373147000 source=auth-service trace_id="abc-123-xyz" sequence=42
F12=14532
F7=1
Transport Bindings
HTTP
POST /api/records HTTP/1.1
X-LNMP-Timestamp: 1732373147000
X-LNMP-Source: auth-service
X-LNMP-Trace-ID: abc-123-xyz
X-LNMP-Sequence: 42
Content-Type: application/lnmp-binary
<binary LNMP record>
Kafka
ProducerRecord
gRPC
metadata {
"lnmp-timestamp": "1732373147000"
"lnmp-source": "auth-service"
"lnmp-trace-id": "abc-123-xyz"
}
Use Cases
Distributed Tracing
use TraceContextExt;
let span = tracer.start;
let trace_id = span.span_context.trace_id.to_string;
let envelope = new
.trace_id
.build;
LLM Freshness Scoring
Multi-Tenant Routing
let envelope = new
.source
.label
.label
.build;
// Route based on source
match envelope.metadata.source.as_deref
API Reference
EnvelopeMetadata
LnmpEnvelope
EnvelopeBuilder
Fluent API for constructing envelopes:
new
.timestamp
.source
.trace_id
.sequence
.label
.build
Determinism Guarantee
Critical Invariant:
SemanticChecksum = f
Envelope metadata is NOT included in checksum computation.
Verification:
let record = /* ... */;
let cs1 = compute_record;
let envelope = new
.timestamp
.build;
let cs2 = compute_record;
assert_eq!; // ✅ MUST pass
Features
serde: Enable serde serialization support (optional)
Performance
- Zero overhead when unused: Envelope-unaware code runs at full speed
- TLV encoding: <100ns for typical metadata (4 fields)
- Binary size: ~50 bytes for full metadata (timestamp + source + trace_id + sequence)
Examples
See examples/ directory:
basic_usage.rs- Creating and encoding envelopes (binary TLV)text_format.rs- Text header format demonstrationhttp_binding.rs- HTTP header mapping (X-LNMP-* pattern)kafka_binding.rs- Kafka record headers integrationllm_freshness.rs- LLM freshness scoring with exponential decay
Run an example:
Documentation
Core Documentation
-
PERFORMANCE.md - Comprehensive performance guide
- Benchmark results and profiling
- Optimization strategies
- Comparison with alternatives (JSON, Protobuf, MessagePack)
- Zero-overhead guarantees
-
Formal Specification - Technical specification v1.0
- Binary TLV format details
- Text encoding rules
- Transport binding specifications
- Conformance requirements
-
API Documentation - Rust API docs (docs.rs)
Guides
- Quick Start - See Quick Start section above
- Use Cases - See Use Cases section
- Transport Bindings - See Transport Bindings section
- Performance - See PERFORMANCE.md
Benchmarks
Run performance benchmarks:
Latest Results (Apple M1):
- Binary TLV encode: ~125ns
- Binary TLV decode: ~72ns
- Text header encode: ~482ns
- Text header decode: ~415ns
See PERFORMANCE.md for detailed analysis.
Specification
Full technical specification: lnmp-envelope-v1.0.md
License
MIT
Contributing
Contributions welcome! Please ensure:
- All tests pass (
cargo test) - Code is formatted (
cargo fmt) - No clippy warnings (
cargo clippy)