Skip to content

Commit

Permalink
bug: filter out variable data from URI metric
Browse files Browse the repository at this point in the history
Closes #420
  • Loading branch information
jrconlin committed Feb 5, 2020
1 parent 2143c12 commit b0c220d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/web/middleware/sentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{cell::RefCell, rc::Rc};
use actix_service::{Service, Transform};
use actix_web::{
dev::{ServiceRequest, ServiceResponse},
http::uri::Uri,
Error, HttpMessage,
};
use futures::{
Expand All @@ -12,6 +13,8 @@ use futures::{

use crate::error::ApiError;
use crate::web::tags::Tags;
use lazy_static::lazy_static;
use regex;

pub struct SentryWrapper;

Expand Down Expand Up @@ -52,6 +55,28 @@ pub struct SentryWrapperMiddleware<S> {
service: Rc<RefCell<S>>,
}

impl<S, B> SentryWrapperMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
fn clean_uri(&self, uri: &Uri) -> String {
// Remove the variable userID and batch tokens from the URI tags
lazy_static! {
// most values are either "=true" or a lower case hex string.
static ref RE: regex::Regex = regex::Regex::new(r"=[a-z\d]{5,}").unwrap();
}
let trimmed = uri
.to_string()
.split("/")
.collect::<Vec<&str>>()
.split_off(3)
.join("/");
RE.replace_all(&trimmed, "=###").to_owned().to_string()
}
}

impl<S, B> Service for SentryWrapperMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
Expand All @@ -69,7 +94,7 @@ where

fn call(&mut self, sreq: ServiceRequest) -> Self::Future {
let mut tags = Tags::from_request_head(sreq.head());
let uri = sreq.head().uri.to_string();
let uri = self.clean_uri(&sreq.head().uri);
sreq.extensions_mut().insert(tags.clone());

Box::new(self.service.call(sreq).and_then(move |sresp| {
Expand Down

0 comments on commit b0c220d

Please sign in to comment.