gcloud_observability/logs/api/
mod.rs1mod batcher;
10mod entry;
11
12pub use batcher::GcpCloudLoggingHandle;
13pub(crate) use batcher::{spawn, ApiSink, LogEntrySink};
14
15pub use gcloud_sdk::google::api::MonitoredResource;
18
19use crate::errors::GcloudObservabilityResult;
20use crate::ids::LogId;
21use gcloud_sdk::google::logging::v2::logging_service_v2_client::LoggingServiceV2Client;
22use gcloud_sdk::google::logging::v2::WriteLogEntriesRequest;
23use gcloud_sdk::{GoogleApi, GoogleAuthMiddleware};
24use std::time::Duration;
25
26const DEFAULT_BATCH_SIZE: usize = 200;
27const DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(2);
28const DEFAULT_QUEUE_CAPACITY: usize = 10_000;
29const DEFAULT_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
30
31pub struct GcpCloudLoggingApiConfig {
34 pub(crate) log_id: LogId,
35 pub(crate) resource: Option<MonitoredResource>,
36 pub(crate) batch_size: usize,
37 pub(crate) flush_interval: Duration,
38 pub(crate) queue_capacity: usize,
39 pub(crate) shutdown_timeout: Duration,
40}
41
42impl GcpCloudLoggingApiConfig {
43 pub fn new(log_id: LogId) -> Self {
46 Self {
47 log_id,
48 resource: None,
49 batch_size: DEFAULT_BATCH_SIZE,
50 flush_interval: DEFAULT_FLUSH_INTERVAL,
51 queue_capacity: DEFAULT_QUEUE_CAPACITY,
52 shutdown_timeout: DEFAULT_SHUTDOWN_TIMEOUT,
53 }
54 }
55
56 pub fn with_resource(mut self, resource: MonitoredResource) -> Self {
61 self.resource = Some(resource);
62 self
63 }
64
65 pub fn with_batch_size(mut self, batch_size: usize) -> Self {
67 self.batch_size = batch_size;
68 self
69 }
70
71 pub fn with_flush_interval(mut self, flush_interval: Duration) -> Self {
74 self.flush_interval = flush_interval;
75 self
76 }
77
78 pub fn with_queue_capacity(mut self, queue_capacity: usize) -> Self {
82 self.queue_capacity = queue_capacity;
83 self
84 }
85
86 pub fn with_shutdown_timeout(mut self, shutdown_timeout: Duration) -> Self {
89 self.shutdown_timeout = shutdown_timeout;
90 self
91 }
92
93 pub(crate) fn log_name(&self, google_project_id: &str) -> String {
96 format!(
97 "projects/{google_project_id}/logs/{}",
98 self.log_id.to_string().replace('/', "%2F")
99 )
100 }
101
102 pub(crate) fn resource_or_default(&self, google_project_id: &str) -> MonitoredResource {
103 self.resource.clone().unwrap_or_else(|| MonitoredResource {
104 r#type: "global".to_string(),
105 labels: [("project_id".to_string(), google_project_id.to_string())]
106 .into_iter()
107 .collect(),
108 })
109 }
110}
111
112pub(crate) struct GcloudLogEntrySink {
113 client: GoogleApi<LoggingServiceV2Client<GoogleAuthMiddleware>>,
114}
115
116impl GcloudLogEntrySink {
117 pub(crate) async fn new() -> GcloudObservabilityResult<Self> {
118 let client = GoogleApi::from_function(
119 LoggingServiceV2Client::new,
120 "https://logging.googleapis.com",
121 None,
122 )
123 .await?;
124 Ok(Self { client })
125 }
126}
127
128#[async_trait::async_trait]
129impl LogEntrySink for GcloudLogEntrySink {
130 async fn write(&self, request: WriteLogEntriesRequest) -> Result<(), String> {
131 self.client
132 .get()
133 .write_log_entries(gcloud_sdk::tonic::Request::new(request))
134 .await
135 .map(|_| ())
136 .map_err(|status| status.to_string())
137 }
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143
144 #[test]
145 fn slash_of_a_log_id_is_url_encoded_in_the_log_name() {
146 let config = GcpCloudLoggingApiConfig::new(LogId::from_static("orders/checkout-v1.2_a"));
147
148 assert_eq!(
149 config.log_name("orders-prod"),
150 "projects/orders-prod/logs/orders%2Fcheckout-v1.2_a"
151 );
152 }
153}