-
Notifications
You must be signed in to change notification settings - Fork 2
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
Send per queue metrics #3
Open
Mongey
wants to merge
3
commits into
lipanski:master
Choose a base branch
from
Mongey:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
extern crate redis; | ||
extern crate curl; | ||
extern crate redis; | ||
extern crate serde; | ||
#[macro_use] extern crate serde_derive; | ||
#[macro_use] | ||
extern crate serde_derive; | ||
extern crate serde_json; | ||
extern crate time; | ||
#[macro_use] extern crate log; | ||
#[macro_use] | ||
extern crate log; | ||
extern crate env_logger; | ||
|
||
use curl::easy::{Easy, List as HeaderList}; | ||
use redis::{cmd, Client as RedisClient, Commands, Connection, RedisError}; | ||
use std::collections::HashMap; | ||
use std::collections::HashSet; | ||
use std::env; | ||
use std::io::Read; | ||
use std::error::Error; | ||
use std::fmt::Display; | ||
use std::io::Read; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
use std::collections::HashSet; | ||
use std::error::Error; | ||
use redis::{Client as RedisClient, Commands, cmd, Connection, RedisError}; | ||
use curl::easy::{Easy, List as HeaderList}; | ||
|
||
const DD_SERIES_URL: &str = "https://app.datadoghq.com/api/v1/series"; | ||
const DEFAULT_INTERVAL: u64 = 60; | ||
|
@@ -39,18 +42,28 @@ impl Series { | |
struct Metric { | ||
metric: String, | ||
points: Vec<(i64, u32)>, | ||
#[serde(rename="type")] | ||
#[serde(rename = "type")] | ||
metric_type: String, | ||
tags: Vec<String>, | ||
} | ||
|
||
impl Metric { | ||
fn new<M: Into<String>, MT: Into<String>>(metric: M, value: u32, metric_type: MT, tags: Vec<String>) -> Self { | ||
fn new<M: Into<String>, MT: Into<String>>( | ||
metric: M, | ||
value: u32, | ||
metric_type: MT, | ||
tags: Vec<String>, | ||
) -> Self { | ||
let metric = metric.into(); | ||
let points = vec![(time::get_time().sec, value)]; | ||
let metric_type = metric_type.into(); | ||
|
||
Metric { metric, points, metric_type, tags } | ||
Metric { | ||
metric, | ||
points, | ||
metric_type, | ||
tags, | ||
} | ||
} | ||
|
||
fn gauge<M: Into<String>>(metric: M, value: u32, tags: Vec<String>) -> Self { | ||
|
@@ -79,13 +92,19 @@ fn main() { | |
env_logger::init().unwrap(); | ||
|
||
let interval = env::var("INTERVAL") | ||
.map(|value| value.parse::<u64>().expect("INTERVAL is not a valid number.")) | ||
.map(|value| { | ||
value | ||
.parse::<u64>() | ||
.expect("INTERVAL is not a valid number.") | ||
}) | ||
.unwrap_or(DEFAULT_INTERVAL); | ||
|
||
let redis_url = env::var("REDIS_URL").expect("REDIS_URL is missing."); | ||
let redis_ns = Namespace::new(env::var("REDIS_NAMESPACE").ok()); | ||
let redis = RedisClient::open(&*redis_url).expect("Could not connect to the provided REDIS_URL."); | ||
let mut conn = establish_redis_connection(&redis).expect("Could not establish a connection to Redis."); | ||
let redis = | ||
RedisClient::open(&*redis_url).expect("Could not connect to the provided REDIS_URL."); | ||
let mut conn = | ||
establish_redis_connection(&redis).expect("Could not establish a connection to Redis."); | ||
let mut reconnect = false; | ||
|
||
let dd_api_key = env::var("DD_API_KEY").expect("DD_API_KEY is missing."); | ||
|
@@ -98,9 +117,8 @@ fn main() { | |
|
||
let mut previous_total_processed: Option<u32> = None; | ||
|
||
debug!("Welcome to datadog-sidekiq"); | ||
loop { | ||
sleep(Duration::new(interval, 0)); | ||
|
||
// Redis was down. Try to re-establish a connection. | ||
if reconnect { | ||
info!("Trying to connect to Redis again."); | ||
|
@@ -109,21 +127,39 @@ fn main() { | |
Ok(new_connection) => { | ||
conn = new_connection; | ||
reconnect = false; | ||
}, | ||
} | ||
Err(redis_err) => { | ||
error!("An error occured while trying to re-establish the Redis connection: {}", redis_err); | ||
error!( | ||
"An error occured while trying to re-establish the Redis connection: {}", | ||
redis_err | ||
); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
let mut series = Series::new(); | ||
|
||
match get_total_enqueued(&conn, &redis_ns) { | ||
Ok(Some(total_enqueued)) => { | ||
series.push(Metric::gauge("sidekiq.enqueued", total_enqueued, tags.clone())); | ||
}, | ||
Ok(None) => {}, | ||
match get_enqueued(&conn, &redis_ns) { | ||
Ok(ref enqueued_count_by_queue) => { | ||
for (queue_name, count) in enqueued_count_by_queue { | ||
let mut extra = vec!["queue_name:".to_string() + queue_name]; | ||
let mut new_tags = tags.clone(); | ||
new_tags.append(&mut extra); | ||
series.push(Metric::gauge("sidekiq.enqueued", *count, new_tags)); | ||
} | ||
|
||
let mut total_enqueued = 0; | ||
for (_queue_name, count) in enqueued_count_by_queue { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use something like https://doc.rust-lang.org/std/iter/trait.Sum.html |
||
total_enqueued += count; | ||
} | ||
series.push(Metric::gauge( | ||
"sidekiq.total_enqueued", | ||
total_enqueued, | ||
tags.clone(), | ||
)); | ||
} | ||
|
||
Err(redis_err) => { | ||
error!("A Redis error occured: {}", redis_err); | ||
reconnect = true; | ||
|
@@ -134,42 +170,64 @@ fn main() { | |
match get_total_processed(&conn, &redis_ns) { | ||
Ok(total_processed) => { | ||
if previous_total_processed.is_some() && total_processed.is_some() { | ||
let current_processed = total_processed.unwrap() - previous_total_processed.unwrap(); | ||
series.push(Metric::gauge("sidekiq.processed", current_processed, tags.clone())); | ||
let current_processed = | ||
total_processed.unwrap() - previous_total_processed.unwrap(); | ||
series.push(Metric::gauge( | ||
"sidekiq.processed", | ||
current_processed, | ||
tags.clone(), | ||
)); | ||
} | ||
|
||
previous_total_processed = total_processed; | ||
}, | ||
} | ||
Err(redis_err) => { | ||
error!("A Redis error occured: {}", redis_err); | ||
reconnect = true; | ||
continue; | ||
}, | ||
} | ||
} | ||
|
||
match deliver_series(&url, series) { | ||
Ok(_) => {}, | ||
Err(err) => { error!("An error occured while sending the series to DataDog: {}", err); } | ||
Ok(_) => {} | ||
Err(err) => { | ||
error!( | ||
"An error occured while sending the series to DataDog: {}", | ||
err | ||
); | ||
} | ||
} | ||
|
||
sleep(Duration::new(interval, 0)); | ||
} | ||
} | ||
|
||
fn establish_redis_connection(client: &RedisClient) -> Result<Connection, RedisError> { | ||
client.get_connection() | ||
} | ||
|
||
fn get_total_enqueued(conn: &Connection, redis_ns: &Namespace) -> Result<Option<u32>, RedisError> { | ||
fn get_enqueued( | ||
conn: &Connection, | ||
redis_ns: &Namespace, | ||
) -> Result<HashMap<String, u32>, RedisError> { | ||
let mut actual_result: HashMap<String, u32> = HashMap::new(); | ||
let queues: HashSet<String> = conn.smembers(redis_ns.wrap("queues"))?; | ||
|
||
let mut pipe = redis::pipe(); | ||
for queue in queues { | ||
|
||
for queue in &queues { | ||
let queue_name = ["queue", &queue].join(":"); | ||
pipe.add_command(cmd("LLEN").arg(redis_ns.wrap(queue_name))); | ||
} | ||
|
||
let total_per_queue: Vec<u32> = pipe.query(conn)?; | ||
let mut i: usize = 0; | ||
for queue in &queues { | ||
actual_result.insert(queue.to_string(), total_per_queue[i]); | ||
i += 1; | ||
} | ||
|
||
Ok(Some(total_per_queue.iter().sum())) | ||
Ok(actual_result) | ||
} | ||
|
||
fn get_total_processed(conn: &Connection, redis_ns: &Namespace) -> Result<Option<u32>, RedisError> { | ||
|
@@ -178,38 +236,57 @@ fn get_total_processed(conn: &Connection, redis_ns: &Namespace) -> Result<Option | |
|
||
fn deliver_series(url: &str, series: Series) -> Result<(), String> { | ||
let mut request = Easy::new(); | ||
request.post(true).map_err(|err| err.description().to_string())?; | ||
request.url(url).map_err(|err| err.description().to_string())?; | ||
request | ||
.post(true) | ||
.map_err(|err| err.description().to_string())?; | ||
request | ||
.url(url) | ||
.map_err(|err| err.description().to_string())?; | ||
|
||
let mut headers = HeaderList::new(); | ||
headers.append("content-type: application/json").map_err(|err| err.description().to_string())?; | ||
request.http_headers(headers).map_err(|err| err.description().to_string())?; | ||
headers | ||
.append("content-type: application/json") | ||
.map_err(|err| err.description().to_string())?; | ||
request | ||
.http_headers(headers) | ||
.map_err(|err| err.description().to_string())?; | ||
|
||
let body = serde_json::to_string(&series).map_err(|err| err.to_string())?; | ||
info!("\nPOST {}\n{}", url, body); | ||
|
||
request.post_field_size(body.len() as u64).map_err(|err| err.description().to_string())?; | ||
request | ||
.post_field_size(body.len() as u64) | ||
.map_err(|err| err.description().to_string())?; | ||
|
||
let mut response = vec![]; | ||
{ | ||
let mut transfer = request.transfer(); | ||
|
||
transfer.read_function(|into| { | ||
Ok(body.as_bytes().read(into).unwrap_or(0)) | ||
}).map_err(|err| err.description().to_string())?; | ||
transfer | ||
.read_function(|into| Ok(body.as_bytes().read(into).unwrap_or(0))) | ||
.map_err(|err| err.description().to_string())?; | ||
|
||
transfer.write_function(|data| { | ||
response.extend_from_slice(data); | ||
Ok(data.len()) | ||
}).map_err(|err| err.description().to_string())?; | ||
transfer | ||
.write_function(|data| { | ||
response.extend_from_slice(data); | ||
Ok(data.len()) | ||
}) | ||
.map_err(|err| err.description().to_string())?; | ||
|
||
transfer.perform().map_err(|err| err.description().to_string())?; | ||
transfer | ||
.perform() | ||
.map_err(|err| err.description().to_string())?; | ||
} | ||
|
||
let status_code = request.response_code().map_err(|err| err.description().to_string())?; | ||
let status_code = request | ||
.response_code() | ||
.map_err(|err| err.description().to_string())?; | ||
if status_code == 202 { | ||
Ok(()) | ||
} else { | ||
Err(format!("The DataDog server replied with an error: {}", String::from_utf8_lossy(&response))) | ||
Err(format!( | ||
"The DataDog server replied with an error: {}", | ||
String::from_utf8_lossy(&response) | ||
)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given the usage of
info!
around the existing code base I could see this asinfo!
as well (rather thandebug!
).