Commit df65b049 authored by Vaastav Anand's avatar Vaastav Anand

Add instrumentation for UserHandler and UserReviewHandler

parent c2dbd92c
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include "../ClientPool.h" #include "../ClientPool.h"
#include "../RedisClient.h" #include "../RedisClient.h"
#include "../ThriftClient.h" #include "../ThriftClient.h"
#include <xtrace/xtrace.h>
#include <xtrace/baggage.h>
namespace media_service { namespace media_service {
class UserReviewHandler : public UserReviewServiceIf { class UserReviewHandler : public UserReviewServiceIf {
...@@ -51,6 +53,15 @@ void UserReviewHandler::UploadUserReview( ...@@ -51,6 +53,15 @@ void UserReviewHandler::UploadUserReview(
int64_t timestamp, int64_t timestamp,
const std::map<std::string, std::string> &carrier) { const std::map<std::string, std::string> &carrier) {
std::map<std::string, std::string>::const_iterator baggage_it = carrier.find("baggage");
if (baggage_it != carrier.end()) {
SET_CURRENT_BAGGAGE(Baggage::deserialize(baggage_it->second));
}
if (!XTrace::IsTracing()) {
XTrace::StartTrace("UserReviewHandler");
}
XTRACE("UserReviewHandler::UploadUserReview", {{"RequestID", std::to_string(req_id)}});
// Initialize a span // Initialize a span
TextMapReader reader(carrier); TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map; std::map<std::string, std::string> writer_text_map;
...@@ -67,6 +78,7 @@ void UserReviewHandler::UploadUserReview( ...@@ -67,6 +78,7 @@ void UserReviewHandler::UploadUserReview(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR; se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to pop a client from MongoDB pool"; se.message = "Failed to pop a client from MongoDB pool";
XTRACE("Failed to pop a client from MongoDB pool");
throw se; throw se;
} }
...@@ -76,16 +88,19 @@ void UserReviewHandler::UploadUserReview( ...@@ -76,16 +88,19 @@ void UserReviewHandler::UploadUserReview(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR; se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection user-review from DB user-review"; se.message = "Failed to create collection user-review from DB user-review";
XTRACE("Failed to create collection user-review from DB user-review");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client); mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se; throw se;
} }
bson_t *query = bson_new(); bson_t *query = bson_new();
BSON_APPEND_INT64(query, "user_id", user_id); BSON_APPEND_INT64(query, "user_id", user_id);
XTRACE("MongoFindUser start");
auto find_span = opentracing::Tracer::Global()->StartSpan( auto find_span = opentracing::Tracer::Global()->StartSpan(
"MongoFindUser", {opentracing::ChildOf(&span->context())}); "MongoFindUser", {opentracing::ChildOf(&span->context())});
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts( mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
collection, query, nullptr, nullptr); collection, query, nullptr, nullptr);
XTRACE("MongoFindUser complete");
const bson_t *doc; const bson_t *doc;
bool found = mongoc_cursor_next(cursor, &doc); bool found = mongoc_cursor_next(cursor, &doc);
if (!found) { if (!found) {
...@@ -96,14 +111,17 @@ void UserReviewHandler::UploadUserReview( ...@@ -96,14 +111,17 @@ void UserReviewHandler::UploadUserReview(
"timestamp", BCON_INT64(timestamp), "}", "]" "timestamp", BCON_INT64(timestamp), "}", "]"
); );
bson_error_t error; bson_error_t error;
XTRACE("MongoInsert start");
auto insert_span = opentracing::Tracer::Global()->StartSpan( auto insert_span = opentracing::Tracer::Global()->StartSpan(
"MongoInsert", {opentracing::ChildOf(&span->context())}); "MongoInsert", {opentracing::ChildOf(&span->context())});
bool plotinsert = mongoc_collection_insert_one( bool plotinsert = mongoc_collection_insert_one(
collection, new_doc, nullptr, nullptr, &error); collection, new_doc, nullptr, nullptr, &error);
insert_span->Finish(); insert_span->Finish();
XTRACE("MongoInsert finish");
if (!plotinsert) { if (!plotinsert) {
LOG(error) << "Failed to insert user review of user " << user_id LOG(error) << "Failed to insert user review of user " << user_id
<< " to MongoDB: " << error.message; << " to MongoDB: " << error.message;
XTRACE("Failed to insert user review of user " + std::to_string(user_id) + "to MongoDB");
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR; se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = error.message; se.message = error.message;
...@@ -129,15 +147,18 @@ void UserReviewHandler::UploadUserReview( ...@@ -129,15 +147,18 @@ void UserReviewHandler::UploadUserReview(
); );
bson_error_t error; bson_error_t error;
bson_t reply; bson_t reply;
XTRACE("MongoUpdate start");
auto update_span = opentracing::Tracer::Global()->StartSpan( auto update_span = opentracing::Tracer::Global()->StartSpan(
"MongoUpdate", {opentracing::ChildOf(&span->context())}); "MongoUpdate", {opentracing::ChildOf(&span->context())});
bool plotupdate = mongoc_collection_find_and_modify( bool plotupdate = mongoc_collection_find_and_modify(
collection, query, nullptr, update, nullptr, false, false, collection, query, nullptr, update, nullptr, false, false,
true, &reply, &error); true, &reply, &error);
update_span->Finish(); update_span->Finish();
XTRACE("MongoUpdate finish");
if (!plotupdate) { if (!plotupdate) {
LOG(error) << "Failed to update user-review for user " << user_id LOG(error) << "Failed to update user-review for user " << user_id
<< " to MongoDB: " << error.message; << " to MongoDB: " << error.message;
XTRACE("Failed to update user-review for user " + std::to_string(user_id) + " to MongoDB");
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR; se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = error.message; se.message = error.message;
...@@ -162,9 +183,11 @@ void UserReviewHandler::UploadUserReview( ...@@ -162,9 +183,11 @@ void UserReviewHandler::UploadUserReview(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_REDIS_ERROR; se.errorCode = ErrorCode::SE_REDIS_ERROR;
se.message = "Cannot connected to Redis server"; se.message = "Cannot connected to Redis server";
XTRACE("Cannot connect to Redis server");
throw se; throw se;
} }
auto redis_client = redis_client_wrapper->GetClient(); auto redis_client = redis_client_wrapper->GetClient();
XTRACE("RedisUpdate start");
auto redis_span = opentracing::Tracer::Global()->StartSpan( auto redis_span = opentracing::Tracer::Global()->StartSpan(
"RedisUpdate", {opentracing::ChildOf(&span->context())}); "RedisUpdate", {opentracing::ChildOf(&span->context())});
auto num_reviews = redis_client->zcard(std::to_string(user_id)); auto num_reviews = redis_client->zcard(std::to_string(user_id));
...@@ -179,7 +202,9 @@ void UserReviewHandler::UploadUserReview( ...@@ -179,7 +202,9 @@ void UserReviewHandler::UploadUserReview(
} }
_redis_client_pool->Push(redis_client_wrapper); _redis_client_pool->Push(redis_client_wrapper);
redis_span->Finish(); redis_span->Finish();
XTRACE("RedisUpdate complete");
span->Finish(); span->Finish();
XTRACE("UserReviewHandler::WriteReview complete");
} }
void UserReviewHandler::ReadUserReviews( void UserReviewHandler::ReadUserReviews(
...@@ -187,6 +212,15 @@ void UserReviewHandler::ReadUserReviews( ...@@ -187,6 +212,15 @@ void UserReviewHandler::ReadUserReviews(
int64_t user_id, int32_t start, int32_t stop, int64_t user_id, int32_t start, int32_t stop,
const std::map<std::string, std::string> & carrier) { const std::map<std::string, std::string> & carrier) {
std::map<std::string, std::string>::const_iterator baggage_it = carrier.find("baggage");
if (baggage_it != carrier.end()) {
SET_CURRENT_BAGGAGE(Baggage::deserialize(baggage_it->second));
}
if (!XTrace::IsTracing()) {
XTrace::StartTrace("UserReviewHandler");
}
XTRACE("UserReviewHandler::ReadUserReviews", {{"RequestID", std::to_string(req_id)}});
// Initialize a span // Initialize a span
TextMapReader reader(carrier); TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map; std::map<std::string, std::string> writer_text_map;
...@@ -206,9 +240,11 @@ void UserReviewHandler::ReadUserReviews( ...@@ -206,9 +240,11 @@ void UserReviewHandler::ReadUserReviews(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_REDIS_ERROR; se.errorCode = ErrorCode::SE_REDIS_ERROR;
se.message = "Cannot connected to Redis server"; se.message = "Cannot connected to Redis server";
XTRACE("Cannot connect to Redis server");
throw se; throw se;
} }
auto redis_client = redis_client_wrapper->GetClient(); auto redis_client = redis_client_wrapper->GetClient();
XTRACE("RedisFind start");
auto redis_span = opentracing::Tracer::Global()->StartSpan( auto redis_span = opentracing::Tracer::Global()->StartSpan(
"RedisFind", {opentracing::ChildOf(&span->context())}); "RedisFind", {opentracing::ChildOf(&span->context())});
auto review_ids_future = redis_client->zrevrange( auto review_ids_future = redis_client->zrevrange(
...@@ -221,6 +257,7 @@ void UserReviewHandler::ReadUserReviews( ...@@ -221,6 +257,7 @@ void UserReviewHandler::ReadUserReviews(
review_ids_reply = review_ids_future.get(); review_ids_reply = review_ids_future.get();
} catch (...) { } catch (...) {
LOG(error) << "Failed to read review_ids from user-review-redis"; LOG(error) << "Failed to read review_ids from user-review-redis";
XTRACE("Failed to read review_ids from user-review-redis");
_redis_client_pool->Push(redis_client_wrapper); _redis_client_pool->Push(redis_client_wrapper);
throw; throw;
} }
...@@ -241,6 +278,7 @@ void UserReviewHandler::ReadUserReviews( ...@@ -241,6 +278,7 @@ void UserReviewHandler::ReadUserReviews(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR; se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to pop a client from MongoDB pool"; se.message = "Failed to pop a client from MongoDB pool";
XTRACE("Failed to pop a client from MongoDB pool");
throw se; throw se;
} }
auto collection = mongoc_client_get_collection( auto collection = mongoc_client_get_collection(
...@@ -249,6 +287,7 @@ void UserReviewHandler::ReadUserReviews( ...@@ -249,6 +287,7 @@ void UserReviewHandler::ReadUserReviews(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR; se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection user-review from MongoDB"; se.message = "Failed to create collection user-review from MongoDB";
XTRACE("Failed to create collection user-review from MongoDB");
throw se; throw se;
} }
...@@ -259,11 +298,13 @@ void UserReviewHandler::ReadUserReviews( ...@@ -259,11 +298,13 @@ void UserReviewHandler::ReadUserReviews(
"$slice", "[", "$slice", "[",
BCON_INT32(0), BCON_INT32(stop), BCON_INT32(0), BCON_INT32(stop),
"]", "}", "}"); "]", "}", "}");
XTRACE("MongoFindUserReviews start");
auto find_span = opentracing::Tracer::Global()->StartSpan( auto find_span = opentracing::Tracer::Global()->StartSpan(
"MongoFindUserReviews", {opentracing::ChildOf(&span->context())}); "MongoFindUserReviews", {opentracing::ChildOf(&span->context())});
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts( mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
collection, query, opts, nullptr); collection, query, opts, nullptr);
find_span->Finish(); find_span->Finish();
XTRACE("MongoFindUserReviews finish");
const bson_t *doc; const bson_t *doc;
bool found = mongoc_cursor_next(cursor, &doc); bool found = mongoc_cursor_next(cursor, &doc);
if (found) { if (found) {
...@@ -302,23 +343,28 @@ void UserReviewHandler::ReadUserReviews( ...@@ -302,23 +343,28 @@ void UserReviewHandler::ReadUserReviews(
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client); mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
} }
Baggage review_baggage = BRANCH_CURRENT_BAGGAGE();
std::future<std::vector<Review>> review_future = std::async( std::future<std::vector<Review>> review_future = std::async(
std::launch::async, [&]() { std::launch::async, [&]() {
BAGGAGE(review_baggage);
auto review_client_wrapper = _review_client_pool->Pop(); auto review_client_wrapper = _review_client_pool->Pop();
if (!review_client_wrapper) { if (!review_client_wrapper) {
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_THRIFT_CONN_ERROR; se.errorCode = ErrorCode::SE_THRIFT_CONN_ERROR;
se.message = "Failed to connected to review-storage-service"; se.message = "Failed to connected to review-storage-service";
XTRACE("Failed to connect to review-storage-service");
throw se; throw se;
} }
std::vector<Review> _return_reviews; std::vector<Review> _return_reviews;
auto review_client = review_client_wrapper->GetClient(); auto review_client = review_client_wrapper->GetClient();
try { try {
writer_text_map["baggage"] = GET_CURRENT_BAGGAGE().str();
review_client->ReadReviews( review_client->ReadReviews(
_return_reviews, req_id, review_ids, writer_text_map); _return_reviews, req_id, review_ids, writer_text_map);
} catch (...) { } catch (...) {
_review_client_pool->Push(review_client_wrapper); _review_client_pool->Push(review_client_wrapper);
LOG(error) << "Failed to read review from review-storage-service"; LOG(error) << "Failed to read review from review-storage-service";
XTRACE("Failed to read review from review-storage-service");
throw; throw;
} }
_review_client_pool->Push(review_client_wrapper); _review_client_pool->Push(review_client_wrapper);
...@@ -333,9 +379,11 @@ void UserReviewHandler::ReadUserReviews( ...@@ -333,9 +379,11 @@ void UserReviewHandler::ReadUserReviews(
ServiceException se; ServiceException se;
se.errorCode = ErrorCode::SE_REDIS_ERROR; se.errorCode = ErrorCode::SE_REDIS_ERROR;
se.message = "Cannot connected to Redis server"; se.message = "Cannot connected to Redis server";
XTRACE("Cannot connect to Redis server");
throw se; throw se;
} }
redis_client = redis_client_wrapper->GetClient(); redis_client = redis_client_wrapper->GetClient();
XTRACE("RedisUpdate start");
auto redis_update_span = opentracing::Tracer::Global()->StartSpan( auto redis_update_span = opentracing::Tracer::Global()->StartSpan(
"RedisUpdate", {opentracing::ChildOf(&span->context())}); "RedisUpdate", {opentracing::ChildOf(&span->context())});
redis_client->del(std::vector<std::string>{std::to_string(user_id)}); redis_client->del(std::vector<std::string>{std::to_string(user_id)});
...@@ -344,17 +392,21 @@ void UserReviewHandler::ReadUserReviews( ...@@ -344,17 +392,21 @@ void UserReviewHandler::ReadUserReviews(
std::to_string(user_id), options, redis_update_map); std::to_string(user_id), options, redis_update_map);
redis_client->commit(); redis_client->commit();
redis_update_span->Finish(); redis_update_span->Finish();
XTRACE("RedisUpdate finish");
} }
try { try {
_return = review_future.get(); _return = review_future.get();
JOIN_CURRENT_BAGGAGE(review_baggage);
} catch (...) { } catch (...) {
LOG(error) << "Failed to get review from review-storage-service"; LOG(error) << "Failed to get review from review-storage-service";
XTRACE("Failed to get review from review-storage-service");
if (!redis_update_map.empty()) { if (!redis_update_map.empty()) {
try { try {
zadd_reply_future.get(); zadd_reply_future.get();
} catch (...) { } catch (...) {
LOG(error) << "Failed to Update Redis Server"; LOG(error) << "Failed to Update Redis Server";
XTRACE("Failed to Update Redis Server");
} }
_redis_client_pool->Push(redis_client_wrapper); _redis_client_pool->Push(redis_client_wrapper);
} }
...@@ -366,6 +418,7 @@ void UserReviewHandler::ReadUserReviews( ...@@ -366,6 +418,7 @@ void UserReviewHandler::ReadUserReviews(
zadd_reply_future.get(); zadd_reply_future.get();
} catch (...) { } catch (...) {
LOG(error) << "Failed to Update Redis Server"; LOG(error) << "Failed to Update Redis Server";
XTRACE("Failed to Update Redis Server");
_redis_client_pool->Push(redis_client_wrapper); _redis_client_pool->Push(redis_client_wrapper);
throw; throw;
} }
...@@ -373,6 +426,8 @@ void UserReviewHandler::ReadUserReviews( ...@@ -373,6 +426,8 @@ void UserReviewHandler::ReadUserReviews(
} }
span->Finish(); span->Finish();
XTRACE("UserReviewHandler::ReadUserReviews complete");
DELETE_CURRENT_BAGGAGE();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment