use serde::{Deserialize, Serialize};
use crate::{
env_config::{OtlpEndpoint, OtlpResourceAttrs},
mapping::{LogRecord, project_log},
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OtlpLogPayload {
pub resource: ResourceMessage,
pub endpoint: String,
pub records: Vec<LogRecord>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceMessage {
pub service_name: String,
pub service_version: String,
pub attributes: std::collections::BTreeMap<String, String>,
pub schema_url: String,
}
impl ResourceMessage {
#[must_use]
pub fn from_attrs(attrs: &OtlpResourceAttrs) -> Self {
Self {
service_name: attrs.service_name.clone(),
service_version: attrs.service_version.clone(),
attributes: attrs.to_semconv_map(),
schema_url: "https://opentelemetry.io/schemas/1.27.0".to_string(),
}
}
}
impl OtlpLogPayload {
#[must_use]
pub fn from_envelopes(
envs: &[obs_proto::obs::v1::ObsEnvelope],
resource: &OtlpResourceAttrs,
endpoint: &OtlpEndpoint,
) -> Self {
Self {
resource: ResourceMessage::from_attrs(resource),
endpoint: endpoint.url.clone(),
records: envs.iter().map(project_log).collect(),
}
}
}