Skip to content

feat(logs): send logs in batches #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

## Unreleased

### Breaking changes
### Features

- refactor: remove `debug-logs` feature (#820) by @lcian
- The deprecated `debug-logs` feature of the `sentry` crate, used for the SDK's own internal logging, has been removed.
Support for [Sentry structured logs](https://docs.sentry.io/product/explore/logs/) has been added to the SDK.
- To set up logs, enable the `logs` feature of the `sentry` crate and set `enable_logs` to `true` in your client options.
- Then, use the `logger_trace!`, `logger_debug!`, `logger_info!`, `logger_warn!`, `logger_error!` and `logger_fatal!` macros to capture logs.
- To filter or update logs before they are sent, you can use the `before_send_log` client option.
- Please note that breaking changes could occur until the API is finalized.

- feat(logs): add log protocol types (#821) by @lcian
- feat(logs): add ability to capture and send logs (#823) by @lcian & @Swatinem
- feat(logs): add macro-based API (#827) by @lcian & @szokeasaurusrex
- feat(logs): send logs in batches (#831) by @lcian

### Behavioral changes

Expand All @@ -14,15 +22,10 @@
- This information is used as a fallback when capturing an event with tracing disabled or otherwise no ongoing span, to still allow related events to be linked by a trace.
- A new API `Scope::iter_trace_propagation_headers` has been provided that will use the fallback tracing information if there is no current `Span` on the `Scope`.

### Features
### Breaking changes

- feat(logs): add log protocol types (#821) by @lcian
- feat(logs): add ability to capture and send logs (#823) by @lcian & @Swatinem
- feat(logs): add macro-based API (#827) by @lcian & @szokeasaurusrex
- Support for [Sentry structured logs](https://docs.sentry.io/product/explore/logs/) has been added.
- To enable logs, enable the `UNSTABLE_logs` feature of the `sentry` crate and set `enable_logs` to `true` in your client options.
- Then, use the `logger_trace!`, `logger_debug!`, `logger_info!`, `logger_warn!`, `logger_error!` and `logger_fatal!` macros to capture logs.
- Please note that breaking changes could occur until the API is finalized.
- refactor: remove `debug-logs` feature (#820) by @lcian
- The deprecated `debug-logs` feature of the `sentry` crate, used for the SDK's own internal logging, has been removed.

## 0.38.1

Expand Down
2 changes: 1 addition & 1 deletion sentry-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ default = []
client = ["rand"]
test = ["client", "release-health"]
release-health = []
UNSTABLE_logs = []
logs = []

[dependencies]
log = { version = "0.4.8", optional = true, features = ["std"] }
Expand Down
47 changes: 35 additions & 12 deletions sentry-core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use std::panic::RefUnwindSafe;
use std::sync::{Arc, RwLock};
use std::time::Duration;

#[cfg(feature = "UNSTABLE_logs")]
use crate::protocol::EnvelopeItem;
#[cfg(feature = "release-health")]
use crate::protocol::SessionUpdate;
use rand::random;
use sentry_types::random_uuid;

use crate::constants::SDK_INFO;
#[cfg(feature = "logs")]
use crate::logs::LogsBatcher;
use crate::protocol::{ClientSdkInfo, Event};
#[cfg(feature = "release-health")]
use crate::session::SessionFlusher;
use crate::types::{Dsn, Uuid};
#[cfg(feature = "release-health")]
use crate::SessionMode;
use crate::{ClientOptions, Envelope, Hub, Integration, Scope, Transport};
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
use sentry_types::protocol::v7::{Log, LogAttribute};

impl<T: Into<ClientOptions>> From<T> for Client {
Expand Down Expand Up @@ -53,6 +53,8 @@ pub struct Client {
transport: TransportArc,
#[cfg(feature = "release-health")]
session_flusher: RwLock<Option<SessionFlusher>>,
#[cfg(feature = "logs")]
logs_batcher: RwLock<Option<LogsBatcher>>,
integrations: Vec<(TypeId, Arc<dyn Integration>)>,
pub(crate) sdk_info: ClientSdkInfo,
}
Expand All @@ -76,11 +78,20 @@ impl Clone for Client {
self.options.session_mode,
)));

#[cfg(feature = "logs")]
let logs_batcher = RwLock::new(if self.options.enable_logs {
Some(LogsBatcher::new(transport.clone()))
} else {
None
});

Client {
options: self.options.clone(),
transport,
#[cfg(feature = "release-health")]
session_flusher,
#[cfg(feature = "logs")]
logs_batcher,
integrations: self.integrations.clone(),
sdk_info: self.sdk_info.clone(),
}
Expand Down Expand Up @@ -150,11 +161,20 @@ impl Client {
options.session_mode,
)));

#[cfg(feature = "logs")]
let logs_batcher = RwLock::new(if options.enable_logs {
Some(LogsBatcher::new(transport.clone()))
} else {
None
});

Client {
options,
transport,
#[cfg(feature = "release-health")]
session_flusher,
#[cfg(feature = "logs")]
logs_batcher,
integrations,
sdk_info,
}
Expand Down Expand Up @@ -329,6 +349,10 @@ impl Client {
if let Some(ref flusher) = *self.session_flusher.read().unwrap() {
flusher.flush();
}
#[cfg(feature = "logs")]
if let Some(ref batcher) = *self.logs_batcher.read().unwrap() {
batcher.flush();
}
if let Some(ref transport) = *self.transport.read().unwrap() {
transport.flush(timeout.unwrap_or(self.options.shutdown_timeout))
} else {
Expand All @@ -346,6 +370,8 @@ impl Client {
pub fn close(&self, timeout: Option<Duration>) -> bool {
#[cfg(feature = "release-health")]
drop(self.session_flusher.write().unwrap().take());
#[cfg(feature = "logs")]
drop(self.logs_batcher.write().unwrap().take());
let transport_opt = self.transport.write().unwrap().take();
if let Some(transport) = transport_opt {
sentry_debug!("client close; request transport to shut down");
Expand All @@ -369,24 +395,21 @@ impl Client {
}

/// Captures a log and sends it to Sentry.
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
pub fn capture_log(&self, log: Log, scope: &Scope) {
if !self.options().enable_logs {
return;
}
if let Some(ref transport) = *self.transport.read().unwrap() {
if let Some(log) = self.prepare_log(log, scope) {
let mut envelope = Envelope::new();
let logs: EnvelopeItem = vec![log].into();
envelope.add_item(logs);
transport.send_envelope(envelope);
if let Some(log) = self.prepare_log(log, scope) {
if let Some(ref batcher) = *self.logs_batcher.read().unwrap() {
batcher.enqueue(log);
}
}
}

/// Prepares a log to be sent, setting the `trace_id` and other default attributes, and
/// processing it through `before_send_log`.
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
fn prepare_log(&self, mut log: Log, scope: &Scope) -> Option<Log> {
scope.apply_to_log(&mut log, self.options.send_default_pii);

Expand All @@ -399,7 +422,7 @@ impl Client {
Some(log)
}

#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
fn set_log_default_attributes(&self, log: &mut Log) {
if !log.attributes.contains_key("sentry.environment") {
if let Some(environment) = self.options.environment.as_ref() {
Expand Down
14 changes: 7 additions & 7 deletions sentry-core/src/clientoptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;

use crate::constants::USER_AGENT;
use crate::performance::TracesSampler;
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
use crate::protocol::Log;
use crate::protocol::{Breadcrumb, Event};
use crate::types::Dsn;
Expand Down Expand Up @@ -147,7 +147,7 @@ pub struct ClientOptions {
/// Callback that is executed for each Breadcrumb being added.
pub before_breadcrumb: Option<BeforeCallback<Breadcrumb>>,
/// Callback that is executed for each Log being added.
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
pub before_send_log: Option<BeforeCallback<Log>>,
// Transport options
/// The transport to use.
Expand All @@ -171,7 +171,7 @@ pub struct ClientOptions {
/// server integrations. Needs `send_default_pii` to be enabled to have any effect.
pub max_request_body_size: MaxRequestBodySize,
/// Determines whether captured structured logs should be sent to Sentry (defaults to false).
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
pub enable_logs: bool,
// Other options not documented in Unified API
/// Disable SSL verification.
Expand Down Expand Up @@ -232,7 +232,7 @@ impl fmt::Debug for ClientOptions {
#[derive(Debug)]
struct BeforeBreadcrumb;
let before_breadcrumb = self.before_breadcrumb.as_ref().map(|_| BeforeBreadcrumb);
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
let before_send_log = {
#[derive(Debug)]
struct BeforeSendLog;
Expand Down Expand Up @@ -279,7 +279,7 @@ impl fmt::Debug for ClientOptions {
.field("auto_session_tracking", &self.auto_session_tracking)
.field("session_mode", &self.session_mode);

#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
debug_struct
.field("enable_logs", &self.enable_logs)
.field("before_send_log", &before_send_log);
Expand Down Expand Up @@ -325,9 +325,9 @@ impl Default for ClientOptions {
trim_backtraces: true,
user_agent: Cow::Borrowed(USER_AGENT),
max_request_body_size: MaxRequestBodySize::Medium,
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
enable_logs: false,
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
before_send_log: None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Hub {
}

/// Captures a structured log.
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
pub fn capture_log(&self, log: Log) {
with_client_impl! {{
let top = self.inner.with(|stack| stack.top().clone());
Expand Down
4 changes: 3 additions & 1 deletion sentry-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,16 @@ pub use crate::intodsn::IntoDsn;
pub use crate::performance::*;
pub use crate::scope::{Scope, ScopeGuard};
pub use crate::transport::{Transport, TransportFactory};
#[cfg(feature = "UNSTABLE_logs")]
#[cfg(feature = "logs")]
mod logger; // structured logging macros exported with `#[macro_export]`

// client feature
#[cfg(feature = "client")]
mod client;
#[cfg(feature = "client")]
mod hub_impl;
#[cfg(all(feature = "client", feature = "logs"))]
mod logs;
#[cfg(feature = "client")]
mod session;

Expand Down
Loading
Loading