1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::{events::Event, timestamps::Timestamp};
#[cfg(feature = "fp-bindgen")]
use fp_bindgen::prelude::Serializable;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use typed_builder::TypedBuilder;

/// A single event that is used within providers.
///
/// Events occur at a given time and optionally last until a given end time.
/// They may contain both event-specific metadata as well as OpenTelemetry
/// metadata.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct ProviderEvent {
    #[builder(setter(into))]
    pub time: Timestamp,

    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end_time: Option<Timestamp>,

    #[builder(default)]
    #[serde(flatten)]
    pub otel: OtelMetadata,

    #[builder(setter(into))]
    pub title: String,

    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub severity: Option<OtelSeverityNumber>,

    #[builder(default)]
    pub labels: BTreeMap<String, String>,
}

/// A single metric value.
///
/// Metric values are taken at a specific timestamp and contain a floating-point
/// value as well as OpenTelemetry metadata.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Metric {
    pub time: Timestamp,

    #[builder(default)]
    #[serde(flatten)]
    pub otel: OtelMetadata,

    pub value: f64,
}

/// Metadata following the OpenTelemetry metadata spec.
///
/// See also: https://github.com/open-telemetry/opentelemetry-specification/
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct OtelMetadata {
    /// OpenTelemetry attributes.
    ///
    /// See also: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/README.md
    pub attributes: BTreeMap<String, Value>,

    /// Resource metadata.
    ///
    /// See also: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md
    pub resource: BTreeMap<String, Value>,

    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trace_id: Option<OtelTraceId>,

    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub span_id: Option<OtelSpanId>,
}

/// SeverityNumber, as specified by OpenTelemetry:
///  https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct OtelSeverityNumber(pub u8);

/// Span ID, as specified by OpenTelemetry:
///  https://opentelemetry.io/docs/reference/specification/overview/#spancontext
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct OtelSpanId([u8; 8]);

impl OtelSpanId {
    /// Creates a new span ID from the raw data.
    pub fn new(data: [u8; 8]) -> Self {
        Self(data)
    }

    /// Returns the raw bytes of the span ID.
    pub fn raw(&self) -> &[u8; 8] {
        &self.0
    }
}

impl From<[u8; 8]> for OtelSpanId {
    fn from(value: [u8; 8]) -> Self {
        Self::new(value)
    }
}

/// Trace ID, as specified by OpenTelemetry:
///  https://opentelemetry.io/docs/reference/specification/overview/#spancontext
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct OtelTraceId([u8; 16]);

impl OtelTraceId {
    /// Creates a new trace ID from the raw data.
    pub fn new(data: [u8; 16]) -> Self {
        Self(data)
    }

    /// Returns the raw bytes of the trace ID.
    pub fn raw(&self) -> &[u8; 16] {
        &self.0
    }
}

impl From<[u8; 16]> for OtelTraceId {
    fn from(value: [u8; 16]) -> Self {
        Self::new(value)
    }
}

/// A timeline of events. It is shown split by days, so we store
/// events days as an array of day strings in the format "YYYY-MM-DD"
/// and a map of day strings to  all events that happen that day.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Timeline {
    pub days: Vec<String>,
    pub events_by_day: BTreeMap<String, Vec<Event>>,
    #[serde(flatten)]
    pub otel: OtelMetadata,
}

/// A series of metrics over time, with metadata.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Timeseries {
    #[builder(setter(into))]
    pub name: String,

    #[builder(default, setter(into))]
    pub labels: BTreeMap<String, String>,

    #[builder(default)]
    pub metrics: Vec<Metric>,

    #[builder(default)]
    #[serde(flatten)]
    pub otel: OtelMetadata,

    /// Whether the series should be rendered. Can be toggled by the user.
    #[serde(default)]
    pub visible: bool,
}