Commit 0e12f212 authored by Vaastav Anand's avatar Vaastav Anand

Add instrumentation for MovieInfoHandler

parent bc4cd5b6
......@@ -13,6 +13,8 @@
#include "../../gen-cpp/MovieInfoService.h"
#include "../logger.h"
#include "../tracing.h"
#include <xtrace/xtrace.h>
#include <xtrace/baggage.h>
namespace media_service {
using json = nlohmann::json;
......@@ -62,6 +64,17 @@ void MovieInfoHandler::WriteMovieInfo(
const std::string & avg_rating,
int32_t num_rating,
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("MovieInfoHandler");
}
XTRACE("MovieInfoHandler::WriteMovieInfo", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -132,6 +145,7 @@ void MovieInfoHandler::WriteMovieInfo(
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(
......@@ -140,18 +154,22 @@ void MovieInfoHandler::WriteMovieInfo(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection movie-info from DB movie-info";
XTRACE("Failed to create collection movie-info from DB movie-info");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
}
bson_error_t error;
XTRACE("MongoInsertMovieInfo start");
auto insert_span = opentracing::Tracer::Global()->StartSpan(
"MongoInsertMovieInfo", { opentracing::ChildOf(&span->context()) });
bool plotinsert = mongoc_collection_insert_one (
collection, new_doc, nullptr, nullptr, &error);
insert_span->Finish();
XTRACE("MongoInsertMovieInfo finish");
if (!plotinsert) {
LOG(error) << "Error: Failed to insert movie-info to MongoDB: "
<< error.message;
XTRACE("Error: Failed to insert movie-info to MongoDB " + error.message);
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = error.message;
......@@ -166,6 +184,8 @@ void MovieInfoHandler::WriteMovieInfo(
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
span->Finish();
XTRACE("MovieInfoService::WriteMovieInfo complete");
DELETE_CURRENT_BAGGAGE();
}
void MovieInfoHandler::ReadMovieInfo(
......@@ -174,6 +194,16 @@ void MovieInfoHandler::ReadMovieInfo(
const std::string &movie_id,
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("MovieInfoHandler");
}
XTRACE("MovieInfoHandler::ReadMovieInfo", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -191,11 +221,13 @@ void MovieInfoHandler::ReadMovieInfo(
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = "Failed to pop a client from memcached pool";
XTRACE("Failed to pop a client from memcached pool");
throw se;
}
size_t movie_info_mmc_size;
uint32_t memcached_flags;
XTRACE("Memcached GetMovieInfo start");
auto get_span = opentracing::Tracer::Global()->StartSpan(
"MmcGetMovieInfo", { opentracing::ChildOf(&span->context()) });
char *movie_info_mmc = memcached_get(
......@@ -214,9 +246,11 @@ void MovieInfoHandler::ReadMovieInfo(
}
memcached_pool_push(_memcached_client_pool, memcached_client);
get_span->Finish();
XTRACE("Memcached GetMovieInfo finish");
if (movie_info_mmc) {
LOG(debug) << "Get movie-info " << movie_id << " cache hit from Memcached";
XTRACE("Cache hit in Memcached for movie " + std::to_string(movie_id));
json movie_info_json = json::parse(std::string(
movie_info_mmc, movie_info_mmc + movie_info_mmc_size));
_return.movie_id = movie_info_json["movie_id"];
......@@ -243,12 +277,14 @@ void MovieInfoHandler::ReadMovieInfo(
free(movie_info_mmc);
} else {
// If not cached in memcached
XTRACE("Cache miss in Memcached.Checking in MongoDB");
mongoc_client_t *mongodb_client = mongoc_client_pool_pop(
_mongodb_client_pool);
if (!mongodb_client) {
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;
}
......@@ -258,11 +294,13 @@ void MovieInfoHandler::ReadMovieInfo(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection user from DB user";
XTRACE("Failed to create collection user from DB user");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
}
bson_t *query = bson_new();
BSON_APPEND_UTF8(query, "movie_id", movie_id.c_str());
XTRACE("MongoFindMovieInfo start");
auto find_span = opentracing::Tracer::Global()->StartSpan(
"MongoFindMovieInfo", { opentracing::ChildOf(&span->context()) });
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
......@@ -270,10 +308,12 @@ void MovieInfoHandler::ReadMovieInfo(
const bson_t *doc;
bool found = mongoc_cursor_next(cursor, &doc);
find_span->Finish();
XTRACE("MongoFindMovieInfo finish");
if (!found) {
bson_error_t error;
if (mongoc_cursor_error (cursor, &error)) {
LOG(warning) << error.message;
XTRACE(error.message);
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
......@@ -284,6 +324,7 @@ void MovieInfoHandler::ReadMovieInfo(
throw se;
} else {
LOG(warning) << "Movie_id: " << movie_id << " doesn't exist in MongoDB";
XTRACE("Movie_id: " + std::to_string(movie_id) + " doesn't exist in MongoDB");
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
......@@ -295,6 +336,7 @@ void MovieInfoHandler::ReadMovieInfo(
}
} else {
LOG(debug) << "Movie_id: " << movie_id << " found in MongoDB";
XTRACE("Movie_id: " + std::to_string(movie_id) + " found in MongoDB");
auto movie_info_json_char = bson_as_json(doc, nullptr);
json movie_info_json = json::parse(movie_info_json_char);
_return.movie_id = movie_info_json["movie_id"];
......@@ -324,14 +366,17 @@ void MovieInfoHandler::ReadMovieInfo(
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
// upload movie-info to memcached
XTRACE("Uploading movie info to memcached");
memcached_client = memcached_pool_pop(
_memcached_client_pool, true, &memcached_rc);
if (!memcached_client) {
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = "Failed to pop a client from memcached pool";
XTRACE("Failed to pop a client from memcached pool");
throw se;
}
XTRACE("Memcached SetMovieInfo start");
auto set_span = opentracing::Tracer::Global()->StartSpan(
"MmcSetMovieInfo", { opentracing::ChildOf(&span->context()) });
......@@ -346,19 +391,34 @@ void MovieInfoHandler::ReadMovieInfo(
if (memcached_rc != MEMCACHED_SUCCESS) {
LOG(warning) << "Failed to set movie_info to Memcached: "
<< memcached_strerror(memcached_client, memcached_rc);
XTRACE("Failed to set movie_info to Memcached");
}
set_span->Finish();
XTRACE("Memcached SetMovieInfo complete");
bson_free(movie_info_json_char);
memcached_pool_push(_memcached_client_pool, memcached_client);
}
}
span->Finish();
XTRACE("MovieInfoHandler::ReadMovieInfo complete");
DELETE_CURRENT_BAGGAGE();
}
void MovieInfoHandler::UpdateRating(
int64_t req_id, const std::string& movie_id,
int32_t sum_uncommitted_rating, int32_t num_uncommitted_rating,
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("MovieInfoHandler");
}
XTRACE("MovieInfoHandler::UpdateRating", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -378,6 +438,7 @@ void MovieInfoHandler::UpdateRating(
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(
......@@ -386,16 +447,20 @@ void MovieInfoHandler::UpdateRating(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection social_graph from MongoDB";
XTRACE("Failed to create colletion social_graph from MongoDB");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
}
XTRACE("MongoFindMovieInfo start");
auto find_span = opentracing::Tracer::Global()->StartSpan(
"MongoFindMovieInfo", {opentracing::ChildOf(&span->context())});
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
collection, query, nullptr, nullptr);
XTRACE("MongoFindMovieInfo finish");
const bson_t *doc;
bool found = mongoc_cursor_next(cursor, &doc);
if (found) {
XTRACE("MovieInfo found in MongoDB");
bson_iter_t iter_0;
bson_iter_t iter_1;
bson_iter_init(&iter_0, doc);
......@@ -417,6 +482,7 @@ void MovieInfoHandler::UpdateRating(
"num_rating", BCON_INT32(num_rating), "}");
bson_error_t error;
bson_t reply;
XTRACE("MongoUpdateRating start");
auto update_span = opentracing::Tracer::Global()->StartSpan(
"MongoUpdateRating", {opentracing::ChildOf(&span->context())});
bool updated = mongoc_collection_find_and_modify(
......@@ -433,6 +499,7 @@ void MovieInfoHandler::UpdateRating(
if (!updated) {
LOG(error) << "Failed to update rating for movie " << movie_id
<< " to MongoDB: " << error.message;
XTRACE("Failed to update rating for movie " + std::to_string(movie_id) + " to MongoDB");
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to update rating for movie " + movie_id +
......@@ -444,9 +511,11 @@ void MovieInfoHandler::UpdateRating(
throw se;
}
update_span->Finish();
XTRACE("MongoUpdateRating finish");
}
}
XTRACE("Memcached Delete Start");
auto delete_span = opentracing::Tracer::Global()->StartSpan(
"MmcDelete", {opentracing::ChildOf(&span->context())});
memcached_return_t memcached_rc;
......@@ -456,13 +525,17 @@ void MovieInfoHandler::UpdateRating(
ServiceException se;
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = "Failed to pop a client from memcached pool";
XTRACE("Failed to pop a client from memcached pool");
throw se;
}
memcached_delete(memcached_client, movie_id.c_str(), movie_id.length(), 0);
memcached_pool_push(_memcached_client_pool, memcached_client);
delete_span->Finish();
XTRACE("Memcached Delete finish");
span->Finish();
XTRACE("MovieInfoHandler::UpdateRating complete");
DELETE_CURRENT_BAGGAGE();
}
} // namespace media_service
......
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