Logging Metrics
OpenFrame Documentation
Get the most out of OpenFrame · Book 30 minutes with Conrad, 25 years in IT. He'll show you what Mingo and Fae can take off your list.
OpenFrame Documentation
Logging Metrics is the in-process metric capture layer used by the Rust OpenFrame Client. It translates selected tracing event fields into an asynchronously shared MetricsStore, allowing the running client to retain the latest counters and gauges plus collected histogram samples. The implementation belongs to the openframe_agent_lib artifact from OpenFrame OSS Library and is executed by this repository's OpenFrame Client deployment.
The implementation is in the upstream metrics source. This module is a capture and storage mechanism: the supplied code does not expose metrics over HTTP, serialize them, or export them to an external monitoring backend.
Counter(u64), Gauge(f64), and Histogram(Vec<f64>).tracing_subscriber layer and a shared Arc<RwLock<MetricsStore>> together.counter, gauge, or histogram.get_metrics.Logging Metrics is part of the OpenFrame Client's logging namespace. It is designed to be attached during logging initialization, then made available to in-process consumers that hold the shared store returned by MetricsLayer::new.
flowchart LR
ClientRuntime["OpenFrame Client runtime"] --> LoggingSetup["Logging setup"]
LoggingSetup --> TracingSubscriber["Tracing subscriber"]
TracingSubscriber --> MetricsLayer["MetricsLayer"]
MetricsLayer --> SharedStore["Arc RwLock MetricsStore"]
SharedStore --> InProcessReader["In-process metric reader"]
ClientRuntime --> PermissionMonitoring["Permission monitoring"]
PermissionMonitoring --> InProcessReader
The client module tree also contains Logging Setup, Metrics Integration, and Permission Monitoring components. They are adjacent integration points, but the supplied Logging Metrics source only defines the layer and store; it does not itself install the layer or schedule metric collection.
| Component | Role | Behavior |
|---|---|---|
MetricValue |
Metric payload enum | Represents a counter as u64, a gauge as f64, or a histogram sample collection as Vec<f64>. |
Metric |
Stored metric record | Holds the internal name, current value, labels, and a UTC timestamp. Fields are private to the module. |
MetricsStore |
In-memory registry | Maps each metric name to one Metric. Provides explicit record and read operations. |
MetricsLayer |
Tracing integration | Implements tracing_subscriber::Layer and captures recognized event fields. |
MetricVisitor |
Event-field interpreter | Converts recognized numeric fields into a pending metric and attaches subsequent debug-formatted fields as labels. |
The store is keyed only by metric name. record_counter and record_gauge replace the whole record already stored under that name; they do not increment or retain a time series. record_histogram creates a histogram on first use and appends each subsequent sample to its vector.
A histogram keeps the labels and timestamp created with its first insertion. Counters and gauges refresh both labels and timestamp whenever they are recorded. Since metrics are keyed by name rather than name-plus-label set, labels are metadata for the one stored record, not dimensions that create separate series.
classDiagram
class MetricValue {
Counter u64
Gauge f64
Histogram Vec f64
}
class Metric {
String name
MetricValue value
HashMap labels
DateTime timestamp
}
class MetricsStore {
HashMap metrics
new()
record_counter()
record_gauge()
record_histogram()
get_metrics()
}
class MetricsLayer {
Arc store
new()
on_event()
}
class MetricVisitor {
Option metric
record_u64()
record_f64()
record_debug()
}
MetricsStore --> Metric
Metric --> MetricValue
MetricsLayer --> MetricsStore
MetricsLayer --> MetricVisitor
MetricsLayer::on_event creates a fresh visitor and calls event.record. The visitor recognizes numeric fields by their exact field names:
| Field name | Accepted Rust value | Stored value | Stored metric name |
|---|---|---|---|
counter |
u64 |
MetricValue::Counter |
counter |
gauge |
f64 |
MetricValue::Gauge |
gauge |
histogram |
f64 |
MetricValue::Histogram containing one sample |
histogram |
Other numeric field names are ignored. The implementation derives the metric name from the recognized field name; it does not read a separate custom-name field. Debug-recorded fields can become labels only after the visitor has established a metric tuple. Label values use Rust debug formatting, not a structured serialization format.
sequenceDiagram
participant Emitter as "Tracing event emitter"
participant Layer as "MetricsLayer"
participant Visitor as "MetricVisitor"
participant Store as "MetricsStore"
Emitter->>Layer: Emit event
Layer->>Visitor: Record event fields
Visitor->>Visitor: Recognize counter, gauge, or histogram
Visitor->>Visitor: Attach eligible debug fields as labels
Layer->>Store: Acquire blocking write lock
alt Counter value
Layer->>Store: Replace counter record
else Gauge value
Layer->>Store: Replace gauge record
else Histogram value
Layer->>Store: Append histogram sample
end
record_counter and record_gauge insert a newly constructed Metric under the supplied name. An existing entry with the same name is replaced. Although the counter variant is named Counter, its method stores the provided absolute value rather than performing an additive increment.
record_histogram looks up the entry by name. When absent, it creates a record containing an empty histogram, initial labels, and the current UTC timestamp. It then appends the supplied sample when the existing value is a histogram. If an entry exists under the same name with a different value variant, the method leaves that entry unchanged.
When a tracing event carries a histogram value, MetricsLayer iterates its captured values and calls record_histogram for every sample. The current visitor produces one sample per recognized histogram field, but the loop accommodates a histogram value containing multiple samples.
MetricsLayer is cloneable because it retains an Arc to a Tokio RwLock. Event handling uses blocking_write, so a tracing event obtains exclusive access synchronously before updating the store. Readers that retain the store handle should acquire an appropriate read lock before calling get_metrics.
get_metrics returns references to records rather than owned copies. Those references are only valid for the lifetime of the read guard that protects the store.
The intended lifecycle is to construct the layer and store as one pair, install the layer in the process tracing subscriber, emit conforming events, and read the shared store from an in-process consumer.
flowchart TD
CreateLayer["Create MetricsLayer and shared store"] --> InstallLayer["Install layer in tracing subscriber"]
InstallLayer --> EmitEvent["Emit tracing event"]
EmitEvent --> Recognized{"Recognized metric field?"}
Recognized -->|"No"| IgnoreEvent["Ignore for metrics"]
Recognized -->|"Yes"| VisitFields["Visit fields and collect labels"]
VisitFields --> WriteStore["Write record to MetricsStore"]
WriteStore --> ReadStore["Read metrics under read lock"]
A conceptual emitter must use a supported field name and numeric type. For example, a counter event must record counter as an unsigned 64-bit value; a gauge or histogram event must record the respective field as a 64-bit floating-point value. This is a behavioral description, not a public wire protocol or an assertion that all tracing macros accept identical field syntax.
counter, gauge, and histogram as storage keys. Repeated measurements of the same type overwrite counters and gauges, while histogram measurements accumulate in one vector.Metric fields are private, so consumers outside this module require an accessor or serialization mechanism that is not present in the supplied source to inspect metric details.blocking_write occurs inside the tracing layer callback. High event rates or long-held locks can make metric capture contend with writers and affect logging latency.When extending Logging Metrics, keep the capture contract, storage keying, and memory behavior aligned:
Logging Metrics captures client-process observations only. Log-file parsing, file rotation, file-source management, NATS log streaming, and log batch shipping are distinct logging responsibilities in the OpenFrame Client. The broader OpenFrame platform also contains client-side metric message and gateway metric facilities, but they are not invoked by the provided Logging Metrics implementation.