Commit df65b049 authored by Vaastav Anand's avatar Vaastav Anand

Add instrumentation for UserHandler and UserReviewHandler

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