| 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | use std::sync::Arc; |
| 5 | |
| 6 | use crate::MetricRecord; |
| 7 | |
| 8 | /// Trait abstracting the export destination for metric records. |
| 9 | /// |
| 10 | /// Implementations receive a [`MetricRecord`] and are responsible for |
| 11 | /// serializing it in whatever format they prefer (JSON, CBOR, EMF, etc.) |
| 12 | /// and writing it to a destination such as stdout, a file, or a network |
| 13 | /// socket. |
| 14 | /// |
| 15 | /// `MetricRecord` implements `serde::Serialize` and `metrique_writer::Entry`, |
| 16 | /// so sinks can choose between serde-based formats (JSON via `serde_json`, |
| 17 | /// CBOR via `ciborium`, etc.) or the metrique writer pipeline. |
| 18 | /// |
| 19 | /// For field-level inspection, consumers should depend on |
| 20 | /// `s2n-tls-metrics-schema` directly and deserialize from the serialized |
| 21 | /// bytes. That crate carries no stability guarantees. |
| 22 | pub trait TelemetrySink: Send + Sync + 'static { |
| 23 | /// Export a single metric record. |
| 24 | fn export_record(&self, record: &MetricRecord); |
| 25 | } |
| 26 | |
| 27 | /// Blanket impl so that callers can pass an `Arc<T>` directly as a sink. |
| 28 | /// This is useful when the caller wants to share a single sink instance |
| 29 | /// across multiple subscribers — they wrap it in an `Arc` and pass clones |
| 30 | /// without the underlying sink type needing to implement `Clone`. |
| 31 | impl<T: TelemetrySink> TelemetrySink for Arc<T> { |
| 32 | fn export_record(&self, record: &MetricRecord) { |
| 33 | (**self).export_record(record) |
| 34 | } |
| 35 | } |