Commit 7061c7e0 authored by Vaastav Anand's avatar Vaastav Anand

Add instrumentation for PlotService

parent a064c70d
......@@ -12,6 +12,8 @@
#include "../../gen-cpp/PlotService.h"
#include "../logger.h"
#include "../tracing.h"
#include <xtrace/xtrace.h>
#include <xtrace/baggage.h>
namespace media_service {
......@@ -45,6 +47,15 @@ void PlotHandler::ReadPlot(
int64_t plot_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("PlotHandler");
}
XTRACE("PlotHandler::ReadPlot", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -62,6 +73,7 @@ void PlotHandler::ReadPlot(
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;
}
......@@ -69,6 +81,7 @@ void PlotHandler::ReadPlot(
uint32_t memcached_flags;
// Look for the movie id from memcached
XTRACE("MemcachedGetPlot start");
auto get_span = opentracing::Tracer::Global()->StartSpan(
"MmcGetPlot", { opentracing::ChildOf(&span->context()) });
auto plot_id_str = std::to_string(plot_id);
......@@ -85,25 +98,30 @@ void PlotHandler::ReadPlot(
se.errorCode = ErrorCode::SE_MEMCACHED_ERROR;
se.message = memcached_strerror(memcached_client, memcached_rc);
memcached_pool_push(_memcached_client_pool, memcached_client);
XTRACE("Plot not found in memcached");
throw se;
}
get_span->Finish();
XTRACE("MemcachedGetPlot finish");
memcached_pool_push(_memcached_client_pool, memcached_client);
// If cached in memcached
if (plot_mmc) {
LOG(debug) << "Get plot " << plot_mmc
<< " cache hit from Memcached";
XTRACE("GetPlot cache hit in Memcached");
_return = std::string(plot_mmc);
free(plot_mmc);
} else {
// If not cached in memcached
XTRACE("GetPlot cache miss in Memcached");
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");
free(plot_mmc);
throw se;
}
......@@ -113,6 +131,7 @@ void PlotHandler::ReadPlot(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection plot from DB plot";
XTRACE("Failed to create collection plot from DB plot");
free(plot_mmc);
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
......@@ -121,6 +140,7 @@ void PlotHandler::ReadPlot(
bson_t *query = bson_new();
BSON_APPEND_INT64(query, "plot_id", plot_id);
XTRACE("MongoFindPlot start");
auto find_span = opentracing::Tracer::Global()->StartSpan(
"MongoFindPlot", { opentracing::ChildOf(&span->context()) });
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(
......@@ -128,13 +148,16 @@ void PlotHandler::ReadPlot(
const bson_t *doc;
bool found = mongoc_cursor_next(cursor, &doc);
find_span->Finish();
XTRACE("MongoFindPlot finish");
if (found) {
XTRACE("Plot found in MongoDB");
bson_iter_t iter;
if (bson_iter_init_find(&iter, doc, "plot")) {
char *plot_mongo_char = bson_iter_value(&iter)->value.v_utf8.str;
size_t plot_mongo_len = bson_iter_value(&iter)->value.v_utf8.len;
LOG(debug) << "Find plot " << plot_id << " cache miss";
XTRACE("Find plot cache miss");
_return = std::string(plot_mongo_char, plot_mongo_char + plot_mongo_len);
bson_destroy(query);
mongoc_cursor_destroy(cursor);
......@@ -144,6 +167,7 @@ void PlotHandler::ReadPlot(
_memcached_client_pool, true, &memcached_rc);
// Upload the plot to memcached
XTRACE("Memcached SetPlot start");
auto set_span = opentracing::Tracer::Global()->StartSpan(
"MmcSetPlot", { opentracing::ChildOf(&span->context()) });
memcached_rc = memcached_set(
......@@ -156,14 +180,17 @@ void PlotHandler::ReadPlot(
static_cast<uint32_t>(0)
);
set_span->Finish();
XTRACE("Memcached SetPlot finish");
if (memcached_rc != MEMCACHED_SUCCESS) {
LOG(warning) << "Failed to set plot to Memcached: "
<< memcached_strerror(memcached_client, memcached_rc);
XTRACE("Failed to set plot to Memcached");
}
memcached_pool_push(_memcached_client_pool, memcached_client);
} else {
LOG(error) << "Attribute plot is not find in MongoDB";
XTRACE("Attribute plot was not found in MongoDB");
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
......@@ -176,6 +203,7 @@ void PlotHandler::ReadPlot(
}
} else {
LOG(error) << "Plot_id " << plot_id << " is not found in MongoDB";
XTRACE("Plot_id " + std::to_string(plot_id) + " was not found in MongoDB");
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
......@@ -188,6 +216,9 @@ void PlotHandler::ReadPlot(
}
}
span->Finish();
XTRACE("PlotHandler::WritePlot complete");
DELETE_CURRENT_BAGGAGE();
}
void PlotHandler::WritePlot(
......@@ -195,6 +226,16 @@ void PlotHandler::WritePlot(
int64_t plot_id,
const std::string &plot,
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("PlotHandler");
}
XTRACE("PlotHandler::WritePlot", {{"RequestID", std::to_string(req_id)}});
// Initialize a span
TextMapReader reader(carrier);
std::map<std::string, std::string> writer_text_map;
......@@ -215,6 +256,7 @@ void PlotHandler::WritePlot(
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(
......@@ -223,18 +265,22 @@ void PlotHandler::WritePlot(
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = "Failed to create collection plot from DB plot";
XTRACE("Failed to create collection plot from DB plot");
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
throw se;
}
bson_error_t error;
XTRACE("MongoInsertPlot start");
auto insert_span = opentracing::Tracer::Global()->StartSpan(
"MongoInsertPlot", { opentracing::ChildOf(&span->context()) });
bool plotinsert = mongoc_collection_insert_one (
collection, new_doc, nullptr, nullptr, &error);
insert_span->Finish();
XTRACE("MongoInserPlot finish");
if (!plotinsert) {
LOG(error) << "Error: Failed to insert plot to MongoDB: "
<< error.message;
XTRACE("Error:Failed to insert plot to MongoDB: " + std::string(error.message));
ServiceException se;
se.errorCode = ErrorCode::SE_MONGODB_ERROR;
se.message = error.message;
......@@ -249,6 +295,8 @@ void PlotHandler::WritePlot(
mongoc_client_pool_push(_mongodb_client_pool, mongodb_client);
span->Finish();
XTRACE("PlotHandler::WritePlot 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