forked from pytorch/serve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor metrics implementation to follow the API described in the
metrics refactor RFC: pytorch#1492
- Loading branch information
Naman Nandan
committed
Oct 21, 2022
1 parent
5cddf38
commit 2ac9efc
Showing
12 changed files
with
274 additions
and
266 deletions.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef TS_CPP_UTILS_METRICS_EMITTER_HH_ | ||
#define TS_CPP_UTILS_METRICS_EMITTER_HH_ | ||
|
||
namespace torchserve { | ||
class Emitter { | ||
public: | ||
virtual void Emit() = 0; | ||
}; | ||
} // namespace torchserve | ||
|
||
#endif // TS_CPP_UTILS_METRICS_EMITTER_HH_ |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <chrono> | ||
#include <boost/asio.hpp> | ||
|
||
#include "src/utils/metrics/units.hh" | ||
#include "src/utils/logging.hh" | ||
#include "src/utils/metrics/log_metric.hh" | ||
|
||
namespace torchserve { | ||
LogMetric::LogMetric(const std::string& name, const std::string& unit, | ||
const std::map<std::string, std::string>& dimensions, const MetricType& type) : | ||
name{name}, unit{Units::GetUnitMapping(unit)}, dimensions{dimensions}, type{type} {} | ||
|
||
void LogMetric::Update(const double& value, const std::string& request_id) { | ||
if(type == MetricType::COUNTER && value < 0.0) { | ||
return; | ||
} | ||
|
||
uint64_t time_since_epoch_in_seconds = std::chrono::duration_cast<std::chrono::seconds>( | ||
std::chrono::system_clock::now().time_since_epoch()).count(); | ||
values.push_back(std::make_tuple(value, time_since_epoch_in_seconds, request_id)); | ||
} | ||
|
||
void LogMetric::Reset() { | ||
values.clear(); | ||
} | ||
|
||
void LogMetric::Emit() { | ||
std::string dimensions_string = ""; | ||
for(auto iter = dimensions.begin(); iter != dimensions.end(); iter++) { | ||
dimensions_string += iter->first + ":" + iter->second; | ||
if (std::next(iter) != dimensions.end()) { | ||
dimensions_string += ","; | ||
} | ||
} | ||
std::string hostname = ::boost::asio::ip::host_name(); | ||
|
||
for(const auto& value_entry : values) { | ||
double value; | ||
std::uint64_t timestamp; | ||
std::string request_id; | ||
std::tie(value, timestamp, request_id) = value_entry; | ||
|
||
if (request_id.empty()) { | ||
TS_LOGF(INFO, "[METRICS]{}.{}:{}|#{}|#hostname:{},{}", | ||
name, unit, value, dimensions_string, hostname, timestamp); | ||
} | ||
else { | ||
TS_LOGF(INFO, "[METRICS]{}.{}:{}|#{}|#hostname:{},{},{}", | ||
name, unit, value, dimensions_string, hostname, timestamp, request_id); | ||
} | ||
} | ||
} | ||
} // namespace torchserve |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef TS_CPP_UTILS_METRICS_LOG_METRIC_HH_ | ||
#define TS_CPP_UTILS_METRICS_LOG_METRIC_HH_ | ||
|
||
#include <string> | ||
#include <tuple> | ||
#include <vector> | ||
#include <map> | ||
|
||
#include "src/utils/metrics/metric.hh" | ||
#include "src/utils/metrics/emitter.hh" | ||
|
||
namespace torchserve { | ||
// Value entry for log metric consisting of <metric_value, timestamp, request_id> | ||
typedef std::tuple<double, std::uint64_t, std::string> LogMetricValue; | ||
|
||
class LogMetric : public Metric, public Emitter { | ||
public: | ||
LogMetric(const std::string& name, const std::string& unit, | ||
const std::map<std::string, std::string>& dimensions, const MetricType& type); | ||
void Update(const double& value, const std::string& request_id = ""); | ||
void Reset(); | ||
void Emit(); | ||
|
||
private: | ||
const std::string name; | ||
const std::string unit; | ||
const std::map<std::string, std::string> dimensions; | ||
const MetricType type; | ||
std::vector<LogMetricValue> values; | ||
}; | ||
} // namespace torchserve | ||
|
||
#endif // TS_CPP_UTILS_METRICS_LOG_METRIC_HH_ |
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef TS_CPP_UTILS_METRICS_METRIC_CACHE_HH_ | ||
#define TS_CPP_UTILS_METRICS_METRIC_CACHE_HH_ | ||
|
||
#include <string> | ||
#include <map> | ||
|
||
#include "src/utils/metrics/metric.hh" | ||
|
||
namespace torchserve { | ||
class MetricCache { | ||
public: | ||
virtual Metric AddMetric(const std::string& name, const std::string& unit, | ||
const std::map<std::string, std::string>& dimensions, const MetricType& type) = 0; | ||
virtual Metric GetMetric(const std::string& name, const std::map<std::string, std::string>& dimensions) = 0; | ||
virtual void Flush() = 0; | ||
}; | ||
} // namespace torchserve | ||
|
||
#endif // TS_CPP_UTILS_METRICS_METRIC_CACHE_HH_ |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.