Skip to main content

grow_graylog/
exporter.rs

1//! Logging category: the [`GelfLogExporter`] and the log clients the [`GraylogClient`] creates.
2
3use 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/// The `LogExporter` of the logging category: converts records with
22/// [`GelfMessage::from_log_record`] and sends them through a [`GelfTransport`].
23#[derive(Debug, Clone)]
24pub struct GelfLogExporter {
25    transport: Arc<dyn GelfTransport>,
26    opts: FromLogRecordOptions,
27}
28
29impl GelfLogExporter {
30    /// An exporter over `transport`; `opts.host` is the GELF host of records without
31    /// `resource["host.name"]`.
32    pub fn new(transport: Arc<dyn GelfTransport>, opts: FromLogRecordOptions) -> Self {
33        GelfLogExporter { transport, opts }
34    }
35
36    /// The underlying transport.
37    pub fn transport(&self) -> &Arc<dyn GelfTransport> {
38        &self.transport
39    }
40}
41
42#[async_trait]
43impl LogExporter for GelfLogExporter {
44    /// Converts and sends records; `ExportOptions::gzip` is passed to the transport (only HTTP
45    /// uses it). Client-side rejections are counted.
46    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    /// Closes the transport.
64    async fn shutdown(&self) -> Result<()> {
65        self.transport.close().await
66    }
67}
68
69/// Options of [`GraylogClient::gelf_transport`].
70#[derive(Debug, Clone)]
71pub struct GelfTransportOptions {
72    /// UDP payload compression (default gzip).
73    pub compression: GelfCompression,
74    /// UDP datagram size (default 1420).
75    pub chunk_size: usize,
76    /// HTTP: newline-delimited bulk bodies (default true).
77    pub bulk: bool,
78    /// HTTP: extra headers (for example the input's authorization header).
79    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/// Options of [`GraylogClient::new_log_client`]: the batching options plus the transport choice.
94#[derive(Debug, Clone)]
95pub struct LogOptions {
96    /// Batch settings.
97    pub batch: BatchOptions,
98    /// Resource attributes merged into every record (record keys win): `service.name`,
99    /// `service.version`, `deployment.environment`, `host.name`.
100    pub resource: Attributes,
101    /// Redaction applied before records leave the process (default
102    /// [`RedactionProcessor::default`]); `None` disables it.
103    pub redaction: Option<RedactionProcessor>,
104    /// Minimum severity of loggers made by `LogClient::logger` (default `Info`).
105    pub min_severity: Severity,
106    /// `udp`, `tcp`, `http` or `otlp` (`None` = the configured GELF protocol).
107    pub protocol: Option<GelfProtocol>,
108    /// GELF host of records without `resource["host.name"]` (default: the local host name).
109    pub host: Option<String>,
110    /// Used instead of building a transport (closed with the log client).
111    pub transport: Option<Arc<dyn GelfTransport>>,
112    /// UDP, HTTP and header settings of the built transport.
113    pub transport_options: GelfTransportOptions,
114    /// OTLP: the input's bearer token.
115    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    /// Adds one resource attribute.
136    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    /// Sets the batch options.
145    pub fn batch(mut self, batch: BatchOptions) -> Self {
146        self.batch = batch;
147        self
148    }
149    /// Sets the protocol.
150    pub fn protocol(mut self, protocol: GelfProtocol) -> Self {
151        self.protocol = Some(protocol);
152        self
153    }
154}
155
156impl GraylogClient {
157    /// A GELF transport from the configuration for `protocol` (`None` = the configured
158    /// protocol): host `gelf_host`, port `gelf_port_for(protocol)`, HTTP URL
159    /// `gelf_http_endpoint`, TLS for TCP with `gelf_tls`. TCP and HTTP share the client's
160    /// timeouts and retry policy; HTTP also its TLS, proxy and hooks. The caller owns the
161    /// transport (close it when done).
162    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    /// A batching `LogClient` that sends through GELF (`udp`, `tcp`, `http`) or OTLP/gRPC
202    /// (`otlp`, endpoint `otlp_grpc_endpoint`). Closing the `GraylogClient` flushes and closes
203    /// it. Must be called inside a tokio runtime. A closed client returns `CANCELLED`.
204    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    /// A batching `LogClient` for any exporter (the transport fields of `opts` are ignored).
235    /// Closing the `GraylogClient` flushes and closes it. A closed client returns `CANCELLED`.
236    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}