Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add histogram metric type #374

Merged
merged 8 commits into from
Aug 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Disallow histogram.set, counter.observe, gauge.observe
yinggeh committed Aug 14, 2024
commit 5265d4713eb3d928036d2c9b0c4cac176a8411c2
23 changes: 23 additions & 0 deletions src/metric.cc
Original file line number Diff line number Diff line change
@@ -358,13 +358,36 @@ Metric::Increment(const double& value)
void
Metric::SetValue(const double& value)
{
// SetValue and Observe share the same C API TRITONSERVER_MetricSet.
// Throws if SetValue is called by a histogram metric.
TRITONSERVER_MetricKind kind;
THROW_IF_TRITON_ERROR(TRITONSERVER_GetMetricKind(
reinterpret_cast<TRITONSERVER_Metric*>(metric_address_), &kind));
if (kind == TRITONSERVER_METRIC_KIND_HISTOGRAM) {
throw PythonBackendException(
"TRITONSERVER_METRIC_KIND_HISTOGRAM does not support SetValue");
}

auto triton_metric = reinterpret_cast<TRITONSERVER_Metric*>(metric_address_);
THROW_IF_TRITON_ERROR(TRITONSERVER_MetricSet(triton_metric, value));
}

void
Metric::Observe(const double& value)
{
// SetValue and Observe share the same C API TRITONSERVER_MetricSet.
// Throws if Observe is called by a non-histogram metric.
TRITONSERVER_MetricKind kind;
THROW_IF_TRITON_ERROR(TRITONSERVER_GetMetricKind(
reinterpret_cast<TRITONSERVER_Metric*>(metric_address_), &kind));
if (kind == TRITONSERVER_METRIC_KIND_COUNTER) {
throw PythonBackendException(
"TRITONSERVER_METRIC_KIND_COUNTER does not support Observe");
} else if (kind == TRITONSERVER_METRIC_KIND_GAUGE) {
throw PythonBackendException(
"TRITONSERVER_METRIC_KIND_GAUGE does not support Observe");
}

auto triton_metric = reinterpret_cast<TRITONSERVER_Metric*>(metric_address_);
THROW_IF_TRITON_ERROR(TRITONSERVER_MetricSet(triton_metric, value));
}
4 changes: 2 additions & 2 deletions src/metric.h
Original file line number Diff line number Diff line change
@@ -166,8 +166,8 @@ class Metric {

// The labels of the metric, which is the identifier of the metric.
std::string labels_;
// Monotonically increasing values representing the
// bucket boundaries. For histogram only.
// Monotonically increasing values representing bucket boundaries for creating
// histogram metric.
std::optional<std::vector<double>> buckets_;
// The value used for incrementing or setting the metric.
double operation_value_;
4 changes: 2 additions & 2 deletions src/metric_family.h
Original file line number Diff line number Diff line change
@@ -97,8 +97,8 @@ class MetricFamily {

/// Create a metric from the metric family and store it in the metric map.
/// \param labels The labels of the metric.
/// \param buckets Monotonically increasing values representing the
/// bucket boundaries. For histogram only.
/// \param buckets Monotonically increasing values representing bucket
/// boundaries for creating histogram metric.
/// \return Returns the shared pointer to the created metric.
std::shared_ptr<Metric> CreateMetric(
const py::object& labels, const py::object& buckets);