Skip to content

Commit

Permalink
Refactor metrics implementation to follow the API described in the
Browse files Browse the repository at this point in the history
metrics refactor RFC: pytorch#1492
  • Loading branch information
Naman Nandan committed Oct 21, 2022
1 parent 5cddf38 commit 2ac9efc
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 266 deletions.
3 changes: 1 addition & 2 deletions cpp/src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/file_system.cc)
list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/model_archive.cc)
list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/logging.cc)
list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/metrics/units.cc)
list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/metrics/dimension.cc)
list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/metrics/metric.cc)
list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/metrics/log_metric.cc)
add_library(ts_utils SHARED ${TS_UTILS_SOURCE_FILES})
target_include_directories(ts_utils PUBLIC ${TS_UTILS_SRC_DIR})
target_include_directories(ts_utils PRIVATE ${Boost_INCLUDE_DIRS})
Expand Down
14 changes: 0 additions & 14 deletions cpp/src/utils/metrics/dimension.cc

This file was deleted.

19 changes: 0 additions & 19 deletions cpp/src/utils/metrics/dimension.hh

This file was deleted.

11 changes: 11 additions & 0 deletions cpp/src/utils/metrics/emitter.hh
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_
53 changes: 53 additions & 0 deletions cpp/src/utils/metrics/log_metric.cc
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
33 changes: 33 additions & 0 deletions cpp/src/utils/metrics/log_metric.hh
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_
59 changes: 0 additions & 59 deletions cpp/src/utils/metrics/metric.cc

This file was deleted.

34 changes: 2 additions & 32 deletions cpp/src/utils/metrics/metric.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#define TS_CPP_UTILS_METRICS_METRIC_HH_

#include <string>
#include <vector>

#include "src/utils/metrics/dimension.hh"

namespace torchserve {
enum MetricType {
Expand All @@ -13,37 +10,10 @@ namespace torchserve {
HISTOGRAM
};

class MetricValue {
public:
MetricValue(const double& value, const uint64_t& timestamp, const std::string& request_id);
double GetValue() const;
uint64_t GetTimestamp() const;
const std::string GetRequestId() const;

private:
const double value;
const uint64_t timestamp;
const std::string request_id;
};

class Metric {
public:
Metric(const std::string& name, const std::string& unit,
const MetricType& type, const std::vector<Dimension>& dimensions);
const std::string GetName() const;
const std::string GetUnit() const;
MetricType GetType() const;
const std::vector<Dimension> GetDimensions() const;
const std::vector<MetricValue> GetValues() const;
void Update(const double& value, const std::string& request_id = "");
void Reset();

private:
const std::string name;
const std::string unit;
const MetricType type;
const std::vector<Dimension> dimensions;
std::vector<MetricValue> values;
virtual void Update(const double& value, const std::string& request_id = "") = 0;
virtual void Reset() = 0;
};
} // namespace torchserve

Expand Down
19 changes: 19 additions & 0 deletions cpp/src/utils/metrics/metric_cache.hh
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_
32 changes: 0 additions & 32 deletions cpp/test/utils/metrics/dimension_test.cc

This file was deleted.

Loading

0 comments on commit 2ac9efc

Please sign in to comment.