bugwatch 0.4.0

Official Rust SDK for Bugwatch - AI-Powered Error Tracking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Axum integration for Bugwatch.
//!
//! This module provides a Tower layer and utilities for automatically capturing
//! errors and request context in Axum applications.
//!
//! ## Usage with Environment Variables (Recommended)
//!
//! ```ignore
//! use axum::{Router, routing::get};
//! use bugwatch::integrations::axum::BugwatchLayer;
//!
//! // Set BUGWATCH_API_KEY=bw_live_xxxxx in your environment
//!
//! #[tokio::main]
//! async fn main() {
//!     let app = Router::new()
//!         .route("/", get(|| async { "Hello, World!" }))
//!         // Auto-initializes from BUGWATCH_API_KEY env var
//!         .layer(BugwatchLayer::from_env().expect("BUGWATCH_API_KEY not set"));
//!
//!     let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
//!     axum::serve(listener, app).await.unwrap();
//! }
//! ```
//!
//! ## Manual Initialization
//!
//! ```ignore
//! use axum::{Router, routing::get};
//! use bugwatch::{init, BugwatchOptions};
//! use bugwatch::integrations::axum::BugwatchLayer;
//!
//! #[tokio::main]
//! async fn main() {
//!     // Initialize Bugwatch manually
//!     init(BugwatchOptions::new("your-api-key"));
//!
//!     let app = Router::new()
//!         .route("/", get(|| async { "Hello, World!" }))
//!         .layer(BugwatchLayer::new());
//!
//!     let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
//!     axum::serve(listener, app).await.unwrap();
//! }
//! ```

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use axum::extract::{FromRequestParts, Request};
use axum::http::request::Parts;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use http::header::HeaderMap;
use pin_project_lite::pin_project;
use tower::{Layer, Service};

use crate::env::{get_env_options, EnvError};
use crate::types::{Breadcrumb, Level, RequestContext};
use crate::{add_breadcrumb, get_client, init};

use super::common::{build_url, extract_client_ip, filter_headers};

/// Tower layer for Bugwatch integration with Axum.
///
/// This layer automatically:
/// - Captures request context for errors
/// - Adds HTTP request breadcrumbs
/// - Reports server errors (5xx) to Bugwatch
#[derive(Clone, Default)]
pub struct BugwatchLayer {
    /// Whether to capture 5xx errors automatically
    capture_server_errors: bool,
    /// Whether to add request breadcrumbs
    add_breadcrumbs: bool,
}

impl BugwatchLayer {
    /// Create a new Bugwatch layer with default settings.
    ///
    /// Note: This assumes the SDK has already been initialized with `init()`.
    /// For automatic initialization from environment variables, use `from_env()`.
    pub fn new() -> Self {
        Self {
            capture_server_errors: true,
            add_breadcrumbs: true,
        }
    }

    /// Create a new Bugwatch layer, initializing from environment variables.
    ///
    /// This is the recommended way to create the layer. It will:
    /// 1. Initialize the Bugwatch SDK from `BUGWATCH_API_KEY` and other env vars
    /// 2. Create the layer with default settings
    ///
    /// # Errors
    ///
    /// Returns `EnvError::MissingApiKey` if `BUGWATCH_API_KEY` is not set.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use axum::Router;
    /// use bugwatch::integrations::axum::BugwatchLayer;
    ///
    /// // Requires BUGWATCH_API_KEY environment variable
    /// Router::new()
    ///     .layer(BugwatchLayer::from_env()?)
    /// ```
    pub fn from_env() -> Result<Self, EnvError> {
        // Initialize SDK if not already initialized
        if get_client().is_none() {
            let options = get_env_options()?;
            init(options);
        }
        Ok(Self::new())
    }

    /// Configure whether to automatically capture 5xx server errors.
    pub fn capture_server_errors(mut self, capture: bool) -> Self {
        self.capture_server_errors = capture;
        self
    }

    /// Configure whether to add HTTP request breadcrumbs.
    pub fn add_breadcrumbs(mut self, add: bool) -> Self {
        self.add_breadcrumbs = add;
        self
    }
}

impl<S> Layer<S> for BugwatchLayer {
    type Service = BugwatchService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        BugwatchService {
            inner,
            capture_server_errors: self.capture_server_errors,
            add_breadcrumbs: self.add_breadcrumbs,
        }
    }
}

/// The Bugwatch Tower service.
#[derive(Clone)]
pub struct BugwatchService<S> {
    inner: S,
    capture_server_errors: bool,
    add_breadcrumbs: bool,
}

impl<S> Service<Request> for BugwatchService<S>
where
    S: Service<Request, Response = Response> + Clone + Send + 'static,
    S::Future: Send,
{
    type Response = Response;
    type Error = S::Error;
    type Future = BugwatchFuture<S::Future>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request) -> Self::Future {
        // Extract request info before calling inner service
        let method = req.method().to_string();
        let path = req.uri().path().to_string();
        let query = req.uri().query().map(|q| q.to_string());
        let request_context = extract_request_context(&req);

        let future = self.inner.call(req);

        BugwatchFuture {
            inner: future,
            method,
            path,
            query,
            request_context,
            capture_server_errors: self.capture_server_errors,
            add_breadcrumbs: self.add_breadcrumbs,
        }
    }
}

pin_project! {
    /// Future for the Bugwatch service.
    pub struct BugwatchFuture<F> {
        #[pin]
        inner: F,
        method: String,
        path: String,
        query: Option<String>,
        request_context: RequestContext,
        capture_server_errors: bool,
        add_breadcrumbs: bool,
    }
}

impl<F, E> Future for BugwatchFuture<F>
where
    F: Future<Output = Result<Response, E>>,
{
    type Output = Result<Response, E>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        match this.inner.poll(cx) {
            Poll::Ready(Ok(response)) => {
                let status = response.status();

                // Add breadcrumb for the request
                if *this.add_breadcrumbs {
                    let mut data = HashMap::new();
                    data.insert("status_code".to_string(), serde_json::json!(status.as_u16()));
                    if let Some(ref url) = this.request_context.url {
                        data.insert("url".to_string(), serde_json::json!(url));
                    }

                    add_breadcrumb(
                        Breadcrumb::new("http", format!("{} {} -> {}", this.method, this.path, status.as_u16()))
                            .with_level(if status.is_server_error() {
                                Level::Error
                            } else if status.is_client_error() {
                                Level::Warning
                            } else {
                                Level::Info
                            })
                            .with_data(data),
                    );
                }

                // Capture server errors
                if *this.capture_server_errors && status.is_server_error() {
                    let message = format!(
                        "HTTP {} {} returned {}",
                        this.method,
                        this.path,
                        status.as_u16()
                    );

                    if let Some(client) = get_client() {
                        let mut tags = HashMap::new();
                        tags.insert("http.method".to_string(), this.method.clone());
                        tags.insert("http.status_code".to_string(), status.as_u16().to_string());
                        if let Some(ref url) = this.request_context.url {
                            tags.insert("http.url".to_string(), url.clone());
                        }

                        let mut extra = HashMap::new();
                        extra.insert(
                            "request".to_string(),
                            serde_json::to_value(&this.request_context).unwrap_or_default(),
                        );

                        client.capture_message_with_options(
                            &message,
                            Level::Error,
                            Some(tags),
                            Some(extra),
                        );
                    }
                }

                Poll::Ready(Ok(response))
            }
            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Extract request context from an Axum request.
fn extract_request_context(req: &Request) -> RequestContext {
    let headers = headers_to_map(req.headers());
    let filtered_headers = filter_headers(&headers);
    let client_ip = extract_client_ip(&headers);

    let scheme = req
        .uri()
        .scheme_str()
        .unwrap_or("http");

    let host = headers
        .get("host")
        .or_else(|| headers.get("Host"))
        .map(|h| h.as_str())
        .or_else(|| req.uri().host())
        .unwrap_or("unknown");

    RequestContext {
        url: Some(build_url(
            scheme,
            host,
            req.uri().path(),
            req.uri().query(),
        )),
        method: Some(req.method().to_string()),
        headers: Some(filtered_headers),
        query_string: req.uri().query().map(|q| q.to_string()),
        client_ip: client_ip.map(|ip| ip.to_string()),
        ..Default::default()
    }
}

/// Convert HeaderMap to HashMap.
fn headers_to_map(headers: &HeaderMap) -> HashMap<String, String> {
    headers
        .iter()
        .filter_map(|(name, value)| {
            value
                .to_str()
                .ok()
                .map(|v| (name.to_string(), v.to_string()))
        })
        .collect()
}

/// Axum extractor for Bugwatch request context.
///
/// Use this to access the Bugwatch request context in your handlers.
///
/// ## Example
///
/// ```ignore
/// use axum::extract::State;
/// use bugwatch::integrations::axum::BugwatchExt;
///
/// async fn handler(bugwatch: BugwatchExt) -> &'static str {
///     println!("Request URL: {:?}", bugwatch.request.url);
///     "Hello, World!"
/// }
/// ```
#[derive(Clone, Debug)]
pub struct BugwatchExt {
    /// The request context captured by the middleware.
    pub request: RequestContext,
}

impl<S> FromRequestParts<S> for BugwatchExt
where
    S: Send + Sync,
{
    type Rejection = std::convert::Infallible;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        let headers = headers_to_map(&parts.headers);
        let filtered_headers = filter_headers(&headers);
        let client_ip = extract_client_ip(&headers);

        let scheme = parts.uri.scheme_str().unwrap_or("http");
        let host = headers
            .get("host")
            .or_else(|| headers.get("Host"))
            .map(|h| h.as_str())
            .or_else(|| parts.uri.host())
            .unwrap_or("unknown");

        let request = RequestContext {
            url: Some(build_url(scheme, host, parts.uri.path(), parts.uri.query())),
            method: Some(parts.method.to_string()),
            headers: Some(filtered_headers),
            query_string: parts.uri.query().map(|q| q.to_string()),
            client_ip: client_ip.map(|ip| ip.to_string()),
            ..Default::default()
        };

        Ok(BugwatchExt { request })
    }
}

/// An error type that automatically captures to Bugwatch when converted to a response.
///
/// ## Example
///
/// ```ignore
/// use axum::response::IntoResponse;
/// use bugwatch::integrations::axum::BugwatchError;
///
/// async fn handler() -> Result<&'static str, BugwatchError> {
///     do_something().map_err(BugwatchError::from_error)?;
///     Ok("Success")
/// }
/// # fn do_something() -> Result<(), std::io::Error> { Ok(()) }
/// ```
pub struct BugwatchError {
    status: StatusCode,
    message: String,
    captured: bool,
}

impl BugwatchError {
    /// Create a new Bugwatch error from any error type.
    pub fn from_error<E: std::error::Error>(error: E) -> Self {
        Self::from_error_with_status(error, StatusCode::INTERNAL_SERVER_ERROR)
    }

    /// Create a new Bugwatch error with a custom status code.
    pub fn from_error_with_status<E: std::error::Error>(error: E, status: StatusCode) -> Self {
        // Capture the error to Bugwatch
        let event_id = if let Some(client) = get_client() {
            client.capture_error(&error)
        } else {
            String::new()
        };

        Self {
            status,
            message: error.to_string(),
            captured: !event_id.is_empty(),
        }
    }

    /// Create a new Bugwatch error from a message.
    pub fn from_message(message: impl Into<String>, status: StatusCode) -> Self {
        let message = message.into();

        // Capture as a message event
        if let Some(client) = get_client() {
            client.capture_message(&message, Level::Error);
        }

        Self {
            status,
            message,
            captured: true,
        }
    }

    /// Check if this error was captured to Bugwatch.
    pub fn was_captured(&self) -> bool {
        self.captured
    }
}

impl IntoResponse for BugwatchError {
    fn into_response(self) -> Response {
        (self.status, self.message).into_response()
    }
}

impl<E: std::error::Error> From<E> for BugwatchError {
    fn from(error: E) -> Self {
        Self::from_error(error)
    }
}

/// Manually capture an error with Axum request parts.
///
/// Use this function to capture errors with request context.
///
/// ## Example
///
/// ```ignore
/// use axum::extract::Request;
/// use bugwatch::integrations::axum::capture_axum_error;
///
/// async fn handler(req: Request) -> &'static str {
///     if let Err(e) = do_something() {
///         capture_axum_error(&req, &e);
///     }
///     "Hello"
/// }
/// # fn do_something() -> Result<(), std::io::Error> { Ok(()) }
/// ```
pub fn capture_axum_error<E: std::error::Error>(req: &Request, error: &E) -> String {
    if let Some(client) = get_client() {
        let request_context = extract_request_context(req);

        let mut tags = HashMap::new();
        tags.insert("http.method".to_string(), req.method().to_string());
        if let Some(ref url) = request_context.url {
            tags.insert("http.url".to_string(), url.clone());
        }

        let mut extra = HashMap::new();
        extra.insert(
            "request".to_string(),
            serde_json::to_value(&request_context).unwrap_or_default(),
        );

        client.capture_error_with_options(error, Level::Error, Some(tags), Some(extra))
    } else {
        String::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bugwatch_layer_builder() {
        let layer = BugwatchLayer::new()
            .capture_server_errors(false)
            .add_breadcrumbs(false);

        assert!(!layer.capture_server_errors);
        assert!(!layer.add_breadcrumbs);
    }

    #[test]
    fn test_headers_to_map() {
        let mut headers = HeaderMap::new();
        headers.insert("content-type", "application/json".parse().unwrap());
        headers.insert("x-request-id", "abc123".parse().unwrap());

        let map = headers_to_map(&headers);

        assert_eq!(map.get("content-type"), Some(&"application/json".to_string()));
        assert_eq!(map.get("x-request-id"), Some(&"abc123".to_string()));
    }

    #[test]
    fn test_from_env_missing_key() {
        // Temporarily remove the env var
        let original = std::env::var("BUGWATCH_API_KEY").ok();
        std::env::remove_var("BUGWATCH_API_KEY");

        let result = BugwatchLayer::from_env();
        assert!(result.is_err());

        // Restore
        if let Some(val) = original {
            std::env::set_var("BUGWATCH_API_KEY", val);
        }
    }
}