1use std::sync::Arc;
4
5use async_trait::async_trait;
6
7use crate::client::GraylogClient;
8use crate::config::GelfProtocol;
9use crate::core::{Error, Result, Secret};
10use crate::gelf::{FromLogRecordOptions, GelfMessage};
11use crate::gelf_transport::{
12 GelfCompression, GelfHttpOptions, GelfHttpTransport, GelfSendOptions, GelfTcpOptions,
13 GelfTcpTransport, GelfTransport, GelfUdpOptions, GelfUdpTransport, DEFAULT_UDP_CHUNK_SIZE,
14};
15use crate::logging::{
16 AttrValue, Attributes, BatchOptions, BatchingLogClient, ExportOptions, ExportResult,
17 LogExporter, LogRecord, RedactionProcessor, Severity,
18};
19use crate::otlp::{OtlpGrpcLogExporter, OtlpGrpcLogExporterOptions};
20
21#[derive(Debug, Clone)]
24pub struct GelfLogExporter {
25 transport: Arc<dyn GelfTransport>,
26 opts: FromLogRecordOptions,
27}
28
29impl GelfLogExporter {
30 pub fn new(transport: Arc<dyn GelfTransport>, opts: FromLogRecordOptions) -> Self {
33 GelfLogExporter { transport, opts }
34 }
35
36 pub fn transport(&self) -> &Arc<dyn GelfTransport> {
38 &self.transport
39 }
40}
41
42#[async_trait]
43impl LogExporter for GelfLogExporter {
44 async fn export(&self, records: &[LogRecord], opts: ExportOptions) -> Result<ExportResult> {
47 let msgs: Vec<GelfMessage> = records
48 .iter()
49 .map(|r| GelfMessage::from_log_record(r, &self.opts))
50 .collect();
51 let res = self
52 .transport
53 .send(
54 &msgs,
55 GelfSendOptions {
56 gzip: Some(opts.gzip),
57 },
58 )
59 .await?;
60 Ok(ExportResult::partial(res.sent, res.rejected, res.error))
61 }
62
63 async fn shutdown(&self) -> Result<()> {
65 self.transport.close().await
66 }
67}
68
69#[derive(Debug, Clone)]
71pub struct GelfTransportOptions {
72 pub compression: GelfCompression,
74 pub chunk_size: usize,
76 pub bulk: bool,
78 pub headers: Vec<(String, String)>,
80}
81
82impl Default for GelfTransportOptions {
83 fn default() -> Self {
84 GelfTransportOptions {
85 compression: GelfCompression::Gzip,
86 chunk_size: DEFAULT_UDP_CHUNK_SIZE,
87 bulk: true,
88 headers: Vec::new(),
89 }
90 }
91}
92
93#[derive(Debug, Clone)]
95pub struct LogOptions {
96 pub batch: BatchOptions,
98 pub resource: Attributes,
101 pub redaction: Option<RedactionProcessor>,
104 pub min_severity: Severity,
106 pub protocol: Option<GelfProtocol>,
108 pub host: Option<String>,
110 pub transport: Option<Arc<dyn GelfTransport>>,
112 pub transport_options: GelfTransportOptions,
114 pub bearer_token: Option<Secret>,
116}
117
118impl Default for LogOptions {
119 fn default() -> Self {
120 LogOptions {
121 batch: BatchOptions::default(),
122 resource: Attributes::new(),
123 redaction: Some(RedactionProcessor::default()),
124 min_severity: Severity::Info,
125 protocol: None,
126 host: None,
127 transport: None,
128 transport_options: GelfTransportOptions::default(),
129 bearer_token: None,
130 }
131 }
132}
133
134impl LogOptions {
135 pub fn resource_attribute(
137 mut self,
138 key: impl Into<String>,
139 value: impl Into<AttrValue>,
140 ) -> Self {
141 self.resource.insert(key.into(), value.into());
142 self
143 }
144 pub fn batch(mut self, batch: BatchOptions) -> Self {
146 self.batch = batch;
147 self
148 }
149 pub fn protocol(mut self, protocol: GelfProtocol) -> Self {
151 self.protocol = Some(protocol);
152 self
153 }
154}
155
156impl GraylogClient {
157 pub fn gelf_transport(
163 &self,
164 protocol: Option<GelfProtocol>,
165 opts: GelfTransportOptions,
166 ) -> Result<Arc<dyn GelfTransport>> {
167 let cfg = self.config();
168 let protocol = protocol.unwrap_or(cfg.gelf_protocol());
169 Ok(match protocol {
170 GelfProtocol::Udp => Arc::new(GelfUdpTransport::new(GelfUdpOptions {
171 host: cfg.gelf_host().to_owned(),
172 port: cfg.gelf_port_for(GelfProtocol::Udp),
173 compression: opts.compression,
174 chunk_size: opts.chunk_size,
175 })?),
176 GelfProtocol::Tcp => Arc::new(GelfTcpTransport::new(GelfTcpOptions {
177 host: cfg.gelf_host().to_owned(),
178 port: cfg.gelf_port_for(GelfProtocol::Tcp),
179 tls: cfg.gelf_tls(),
180 tls_config: cfg.common().tls().clone(),
181 server_name: None,
182 connect_timeout: cfg.common().connect_timeout(),
183 write_timeout: cfg.common().request_timeout(),
184 retry: cfg.common().retry().clone(),
185 })?),
186 GelfProtocol::Http => Arc::new(GelfHttpTransport::new(GelfHttpOptions {
187 url: cfg.gelf_http_endpoint(),
188 gzip: true,
189 bulk: opts.bulk,
190 headers: opts.headers,
191 config: Some(cfg.common().clone()),
192 })?),
193 GelfProtocol::Otlp => {
194 return Err(Error::validation(
195 "gelfTransport: protocol must be udp, tcp or http",
196 ))
197 }
198 })
199 }
200
201 pub fn new_log_client(&self, opts: LogOptions) -> Result<BatchingLogClient> {
205 if self.is_closed() {
206 return Err(Error::cancelled("logClient: client is closed"));
207 }
208 let cfg = self.config();
209 let protocol = opts.protocol.unwrap_or(cfg.gelf_protocol());
210 let exporter: Arc<dyn LogExporter> = if protocol == GelfProtocol::Otlp {
211 Arc::new(OtlpGrpcLogExporter::new(OtlpGrpcLogExporterOptions {
212 endpoint: cfg.otlp_grpc_endpoint(),
213 bearer_token: opts.bearer_token.clone(),
214 headers: opts.transport_options.headers.clone(),
215 tls: cfg.common().tls().clone(),
216 timeout: cfg.common().request_timeout(),
217 connect_timeout: cfg.common().connect_timeout(),
218 })?)
219 } else {
220 let transport = match &opts.transport {
221 Some(t) => t.clone(),
222 None => self.gelf_transport(Some(protocol), opts.transport_options.clone())?,
223 };
224 Arc::new(GelfLogExporter::new(
225 transport,
226 FromLogRecordOptions {
227 host: opts.host.clone(),
228 },
229 ))
230 };
231 self.new_log_client_with_exporter(exporter, opts)
232 }
233
234 pub fn new_log_client_with_exporter(
237 &self,
238 exporter: Arc<dyn LogExporter>,
239 opts: LogOptions,
240 ) -> Result<BatchingLogClient> {
241 if self.is_closed() {
242 return Err(Error::cancelled("logClient: client is closed"));
243 }
244 let client = BatchingLogClient::builder(exporter)
245 .batch_options(opts.batch)
246 .resource(opts.resource)
247 .redaction(opts.redaction)
248 .min_severity(opts.min_severity)
249 .build()?;
250 let closing = client.clone();
251 self.on_close(move || {
252 Box::pin(async move {
253 let _ = crate::logging::LogClient::close(&closing).await;
254 })
255 });
256 Ok(client)
257 }
258}