use opentelemetry::logs::AnyValue;
use opentelemetry_proto::tonic::{
collector::logs::v1::ExportLogsServiceRequest,
common::v1::{AnyValue as ProtoValue, KeyValue, any_value::Value as ProtoKind},
logs::v1::{LogRecord, ResourceLogs, ScopeLogs},
resource::v1::Resource,
};
use crate::request_traces::otlp;
pub(crate) mod deployments;
pub(crate) mod lifecycle;
pub(crate) mod sockets;
pub(crate) trait PlatformEvent: Send + 'static {
fn project_id(&self) -> &str;
fn to_record(&self) -> PlatformRecord;
}
pub(crate) trait PlatformEvents<E>: Send + Sync {
fn record(&self, event: E);
}
pub(crate) struct NoPlatformEvents;
impl<E> PlatformEvents<E> for NoPlatformEvents {
fn record(&self, _event: E) {}
}
#[derive(Clone)]
pub(crate) struct PlatformRecord {
pub name: &'static str,
pub at_ms: u64,
pub attributes: Vec<(&'static str, AnyValue)>,
pub body: Option<String>,
}
impl PlatformRecord {
pub(crate) fn export_request(self, gateway_id: &str) -> ExportLogsServiceRequest {
let record = LogRecord {
time_unix_nano: self.at_ms * 1_000_000,
event_name: self.name.to_owned(),
attributes: self
.attributes
.into_iter()
.map(|(key, value)| attribute(key, value))
.collect(),
body: self.body.map(|body| proto_value(AnyValue::from(body))),
..Default::default()
};
ExportLogsServiceRequest {
resource_logs: vec![ResourceLogs {
resource: Some(Resource {
attributes: vec![attribute(
otlp::GATEWAY_ID,
AnyValue::from(gateway_id.to_owned()),
)],
..Default::default()
}),
scope_logs: vec![ScopeLogs {
log_records: vec![record],
..Default::default()
}],
..Default::default()
}],
}
}
}
pub(crate) fn count(value: u64) -> AnyValue {
AnyValue::from(i64::try_from(value).unwrap_or(i64::MAX))
}
fn attribute(key: &str, value: AnyValue) -> KeyValue {
KeyValue {
key: key.to_owned(),
value: Some(proto_value(value)),
..Default::default()
}
}
fn proto_value(value: AnyValue) -> ProtoValue {
ProtoValue {
value: match value {
AnyValue::Int(value) => Some(ProtoKind::IntValue(value)),
AnyValue::Boolean(value) => Some(ProtoKind::BoolValue(value)),
AnyValue::String(value) => Some(ProtoKind::StringValue(value.to_string())),
_ => None,
},
}
}