Skip to content

Commit 79566e7

Browse files
committed
Update C API
1 parent fd5c44b commit 79566e7

File tree

8 files changed

+189
-98
lines changed

8 files changed

+189
-98
lines changed

include/triton/core/tritonserver.h

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct TRITONSERVER_Server;
6464
struct TRITONSERVER_ServerOptions;
6565
struct TRITONSERVER_Metric;
6666
struct TRITONSERVER_MetricFamily;
67+
struct TRITONSERVER_MetricArgs;
6768

6869
///
6970
/// TRITONSERVER API Version
@@ -2645,6 +2646,42 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricFamilyNew(
26452646
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
26462647
TRITONSERVER_MetricFamilyDelete(struct TRITONSERVER_MetricFamily* family);
26472648

2649+
/// Get the TRITONSERVER_MetricKind of the metric family.
2650+
///
2651+
/// \param metric The metric family object to query.
2652+
/// \param kind Returns the TRITONSERVER_MetricKind of metric.
2653+
/// \return a TRITONSERVER_Error indicating success or failure.
2654+
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
2655+
TRITONSERVER_GetMetricFamilyKind(
2656+
struct TRITONSERVER_MetricFamily* family, TRITONSERVER_MetricKind* kind);
2657+
2658+
/// Create a new metric args object. The caller takes ownership of the
2659+
/// TRITONSERVER_MetricArgs object and must call TRITONSERVER_MetricArgsDelete
2660+
/// to release the object.
2661+
///
2662+
/// \param args Returns the new metric args object.
2663+
/// \return a TRITONSERVER_Error indicating success or failure.
2664+
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricArgsNew(
2665+
struct TRITONSERVER_MetricArgs** args);
2666+
2667+
/// Set metric args with prometheus histogram metric parameter.
2668+
///
2669+
/// \param args The metric args object to set.
2670+
/// \param buckets The array of bucket boundaries.
2671+
/// \param buckets_count The number of bucket boundaries.
2672+
/// \return a TRITONSERVER_Error indicating success or failure.
2673+
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
2674+
TRITONSERVER_MetricArgsSetHistogram(
2675+
struct TRITONSERVER_MetricArgs* args, const double* buckets,
2676+
const uint64_t buckets_count);
2677+
2678+
/// Delete a metric args object.
2679+
///
2680+
/// \param args The metric args object.
2681+
/// \return a TRITONSERVER_Error indicating success or failure.
2682+
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricArgsDelete(
2683+
struct TRITONSERVER_MetricArgs* args);
2684+
26482685
/// Create a new metric object. The caller takes ownership of the
26492686
/// TRITONSERVER_Metric object and must call
26502687
/// TRITONSERVER_MetricDelete to release the object. The caller is also
@@ -2660,10 +2697,31 @@ TRITONSERVER_MetricFamilyDelete(struct TRITONSERVER_MetricFamily* family);
26602697
/// bucket boundaries. For histogram only.
26612698
/// \return a TRITONSERVER_Error indicating success or failure.
26622699
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricNew(
2700+
struct TRITONSERVER_Metric** metric,
2701+
struct TRITONSERVER_MetricFamily* family,
2702+
const struct TRITONSERVER_Parameter** labels, const uint64_t label_count);
2703+
2704+
/// Create a new metric object. The caller takes ownership of the
2705+
/// TRITONSERVER_Metric object and must call
2706+
/// TRITONSERVER_MetricDelete to release the object. The caller is also
2707+
/// responsible for ownership of the labels passed in.
2708+
/// Each label can be deleted immediately after creating the metric with
2709+
/// TRITONSERVER_ParameterDelete if not re-using the labels.
2710+
/// Metric args can be deleted immediately after creating the metric with
2711+
/// TRITONSERVER_MetricArgsDelete if not re-using the metric args.
2712+
///
2713+
/// \param metric Returns the new metric object.
2714+
/// \param family The metric family to add this new metric to.
2715+
/// \param labels The array of labels to associate with this new metric.
2716+
/// \param label_count The number of labels.
2717+
/// \param args Metric args that store additional arguments to construct
2718+
/// particular metric types, e.g. histogram.
2719+
/// \return a TRITONSERVER_Error indicating success or failure.
2720+
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricNewWithArgs(
26632721
struct TRITONSERVER_Metric** metric,
26642722
struct TRITONSERVER_MetricFamily* family,
26652723
const struct TRITONSERVER_Parameter** labels, const uint64_t label_count,
2666-
const void* buckets = nullptr);
2724+
const struct TRITONSERVER_MetricArgs* args);
26672725

26682726
/// Delete a metric object.
26692727
/// All TRITONSERVER_Metric* objects should be deleted BEFORE their
@@ -2700,8 +2758,9 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricValue(
27002758
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricIncrement(
27012759
struct TRITONSERVER_Metric* metric, double value);
27022760

2703-
/// Set the current value of metric to value.
2704-
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_GAUGE and returns
2761+
/// Set the current value of metric to value or observe the value to metric.
2762+
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_GAUGE and
2763+
/// TRITONSERVER_METRIC_KIND_HISTOGRAM. Returns
27052764
/// TRITONSERVER_ERROR_UNSUPPORTED for unsupported TRITONSERVER_MetricKind.
27062765
///
27072766
/// \param metric The metric object to update.
@@ -2710,16 +2769,6 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricIncrement(
27102769
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricSet(
27112770
struct TRITONSERVER_Metric* metric, double value);
27122771

2713-
/// Observe the current value of metric to value.
2714-
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_HISTOGRAM and returns
2715-
/// TRITONSERVER_ERROR_UNSUPPORTED for unsupported TRITONSERVER_MetricKind.
2716-
///
2717-
/// \param metric The metric object to update.
2718-
/// \param value The amount to observe metric's value to.
2719-
/// \return a TRITONSERVER_Error indicating success or failure.
2720-
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricObserve(
2721-
struct TRITONSERVER_Metric* metric, double value);
2722-
27232772
/// Collect metrics.
27242773
/// Supports metrics of kind TRITONSERVER_METRIC_KIND_COUNTER,
27252774
/// TRITONSERVER_METRIC_KIND_GAUGE, TRITONSERVER_METRIC_KIND_HISTOGRAM and
@@ -2732,7 +2781,7 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricObserve(
27322781
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricCollect(
27332782
struct TRITONSERVER_Metric* metric, void* value);
27342783

2735-
/// Get the TRITONSERVER_MetricKind of metric and its corresponding family.
2784+
/// Get the TRITONSERVER_MetricKind of metric of its corresponding family.
27362785
///
27372786
/// \param metric The metric object to query.
27382787
/// \param kind Returns the TRITONSERVER_MetricKind of metric.

python/tritonserver/_c/triton_bindings.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ class TRITONSERVER_Metric:
357357
) -> None: ...
358358
def increment(self, arg0: float) -> None: ...
359359
def set_value(self, arg0: float) -> None: ...
360-
def observe(self, arg0: float) -> None: ...
361360
@property
362361
def kind(self) -> TRITONSERVER_MetricKind: ...
363362
@property
@@ -385,7 +384,6 @@ class TRITONSERVER_MetricKind:
385384
__members__: ClassVar[dict] = ... # read-only
386385
COUNTER: ClassVar[TRITONSERVER_MetricKind] = ...
387386
GAUGE: ClassVar[TRITONSERVER_MetricKind] = ...
388-
HISTOGRAM: ClassVar[TRITONSERVER_MetricKind] = ...
389387
__entries: ClassVar[dict] = ...
390388
def __init__(self, value: int) -> None: ...
391389
def __eq__(self, other: object) -> bool: ...

python/tritonserver/_c/tritonserver_pybind.cc

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,16 +1672,14 @@ class PyMetric : public PyWrapper<struct TRITONSERVER_Metric> {
16721672
DESTRUCTOR_WITH_LOG(PyMetric, TRITONSERVER_MetricDelete);
16731673
PyMetric(
16741674
PyMetricFamily& family,
1675-
const std::vector<std::shared_ptr<PyParameter>>& labels,
1676-
const std::vector<double>* buckets)
1675+
const std::vector<std::shared_ptr<PyParameter>>& labels)
16771676
{
16781677
std::vector<const struct TRITONSERVER_Parameter*> params;
16791678
for (const auto& label : labels) {
16801679
params.emplace_back(label->Ptr());
16811680
}
16821681
ThrowIfError(TRITONSERVER_MetricNew(
1683-
&triton_object_, family.Ptr(), params.data(), params.size(),
1684-
reinterpret_cast<const void*>(buckets)));
1682+
&triton_object_, family.Ptr(), params.data(), params.size()));
16851683
owned_ = true;
16861684
}
16871685

@@ -1702,11 +1700,6 @@ class PyMetric : public PyWrapper<struct TRITONSERVER_Metric> {
17021700
ThrowIfError(TRITONSERVER_MetricSet(triton_object_, val));
17031701
}
17041702

1705-
void Observe(double val) const
1706-
{
1707-
ThrowIfError(TRITONSERVER_MetricObserve(triton_object_, val));
1708-
}
1709-
17101703
TRITONSERVER_MetricKind Kind() const
17111704
{
17121705
TRITONSERVER_MetricKind val = TRITONSERVER_METRIC_KIND_COUNTER;
@@ -2147,8 +2140,7 @@ PYBIND11_MODULE(triton_bindings, m)
21472140
// TRITONSERVER_MetricKind
21482141
py::enum_<TRITONSERVER_MetricKind>(m, "TRITONSERVER_MetricKind")
21492142
.value("COUNTER", TRITONSERVER_METRIC_KIND_COUNTER)
2150-
.value("GAUGE", TRITONSERVER_METRIC_KIND_GAUGE)
2151-
.value("HISTOGRAM", TRITONSERVER_METRIC_KIND_HISTOGRAM);
2143+
.value("GAUGE", TRITONSERVER_METRIC_KIND_GAUGE);
21522144
// TRITONSERVER_MetricFamily
21532145
py::class_<PyMetricFamily>(m, "TRITONSERVER_MetricFamily")
21542146
.def(py::init<
@@ -2157,13 +2149,12 @@ PYBIND11_MODULE(triton_bindings, m)
21572149
py::class_<PyMetric>(m, "TRITONSERVER_Metric")
21582150
.def(
21592151
py::init<
2160-
PyMetricFamily&, const std::vector<std::shared_ptr<PyParameter>>&,
2161-
const std::vector<double>*>(),
2152+
PyMetricFamily&,
2153+
const std::vector<std::shared_ptr<PyParameter>>&>(),
21622154
py::keep_alive<1, 2>())
21632155
.def_property_readonly("value", &PyMetric::Value)
21642156
.def("increment", &PyMetric::Increment)
21652157
.def("set_value", &PyMetric::SetValue)
2166-
.def("observe", &PyMetric::Observe)
21672158
.def_property_readonly("kind", &PyMetric::Kind);
21682159
}
21692160

src/metric_family.cc

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ MetricFamily::MetricFamily(
7272
void*
7373
MetricFamily::Add(
7474
std::map<std::string, std::string> label_map, Metric* metric,
75-
const std::vector<double>* buckets)
75+
const TritonServerMetricArgs* args)
7676
{
7777
void* prom_metric = nullptr;
7878
switch (kind_) {
7979
case TRITONSERVER_METRIC_KIND_COUNTER: {
80-
if (buckets != nullptr) {
80+
if (args != nullptr) {
8181
throw std::invalid_argument(
82-
"Unexpected buckets found in counter Metric constructor.");
82+
"Unexpected args found in counter Metric constructor.");
8383
}
8484
auto counter_family_ptr =
8585
reinterpret_cast<prometheus::Family<prometheus::Counter>*>(family_);
@@ -88,9 +88,9 @@ MetricFamily::Add(
8888
break;
8989
}
9090
case TRITONSERVER_METRIC_KIND_GAUGE: {
91-
if (buckets != nullptr) {
91+
if (args != nullptr) {
9292
throw std::invalid_argument(
93-
"Unexpected buckets found in gauge Metric constructor.");
93+
"Unexpected args found in gauge Metric constructor.");
9494
}
9595
auto gauge_family_ptr =
9696
reinterpret_cast<prometheus::Family<prometheus::Gauge>*>(family_);
@@ -99,13 +99,14 @@ MetricFamily::Add(
9999
break;
100100
}
101101
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
102-
if (buckets == nullptr) {
102+
if (args == nullptr) {
103103
throw std::invalid_argument(
104-
"Missing required buckets in histogram Metric constructor.");
104+
"Bucket boundaries not found in Metric constructor args.");
105105
}
106106
auto histogram_family_ptr =
107107
reinterpret_cast<prometheus::Family<prometheus::Histogram>*>(family_);
108-
auto histogram_ptr = &histogram_family_ptr->Add(label_map, *buckets);
108+
auto histogram_ptr =
109+
&histogram_family_ptr->Add(label_map, args->buckets());
109110
prom_metric = reinterpret_cast<void*>(histogram_ptr);
110111
break;
111112
}
@@ -206,7 +207,7 @@ MetricFamily::~MetricFamily()
206207
Metric::Metric(
207208
TRITONSERVER_MetricFamily* family,
208209
std::vector<const InferenceParameter*> labels,
209-
const std::vector<double>* buckets)
210+
const TritonServerMetricArgs* args)
210211
{
211212
family_ = reinterpret_cast<MetricFamily*>(family);
212213
kind_ = family_->Kind();
@@ -225,7 +226,7 @@ Metric::Metric(
225226
std::string(reinterpret_cast<const char*>(param->ValuePointer()));
226227
}
227228

228-
metric_ = family_->Add(label_map, this, buckets);
229+
metric_ = family_->Add(label_map, this, args);
229230
}
230231

231232
Metric::~Metric()
@@ -355,40 +356,6 @@ Metric::Set(double value)
355356
gauge_ptr->Set(value);
356357
break;
357358
}
358-
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
359-
return TRITONSERVER_ErrorNew(
360-
TRITONSERVER_ERROR_UNSUPPORTED,
361-
"TRITONSERVER_METRIC_KIND_HISTOGRAM does not support Set");
362-
}
363-
default:
364-
return TRITONSERVER_ErrorNew(
365-
TRITONSERVER_ERROR_UNSUPPORTED,
366-
"Unsupported TRITONSERVER_MetricKind");
367-
}
368-
369-
return nullptr; // Success
370-
}
371-
372-
TRITONSERVER_Error*
373-
Metric::Observe(double value)
374-
{
375-
if (metric_ == nullptr) {
376-
return TRITONSERVER_ErrorNew(
377-
TRITONSERVER_ERROR_INTERNAL,
378-
"Could not set metric value. Metric has been invalidated.");
379-
}
380-
381-
switch (kind_) {
382-
case TRITONSERVER_METRIC_KIND_COUNTER: {
383-
return TRITONSERVER_ErrorNew(
384-
TRITONSERVER_ERROR_UNSUPPORTED,
385-
"TRITONSERVER_METRIC_KIND_COUNTER does not support Observe");
386-
}
387-
case TRITONSERVER_METRIC_KIND_GAUGE: {
388-
return TRITONSERVER_ErrorNew(
389-
TRITONSERVER_ERROR_UNSUPPORTED,
390-
"TRITONSERVER_METRIC_KIND_GAUGE does not support Observe");
391-
}
392359
case TRITONSERVER_METRIC_KIND_HISTOGRAM: {
393360
auto histogram_ptr = reinterpret_cast<prometheus::Histogram*>(metric_);
394361
histogram_ptr->Observe(value);

src/metric_family.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#ifdef TRITON_ENABLE_METRICS
3030

31+
#include <cstring>
3132
#include <mutex>
3233
#include <set>
3334
#include <unordered_map>
@@ -38,6 +39,30 @@
3839

3940
namespace triton { namespace core {
4041

42+
//
43+
// TritonServerMetricArgs
44+
//
45+
// Implementation for TRITONSERVER_MetricArgs.
46+
//
47+
class TritonServerMetricArgs {
48+
public:
49+
TritonServerMetricArgs() = default;
50+
51+
void* SetHistogramArgs(const double* buckets, uint64_t bucket_count)
52+
{
53+
kind_ = TRITONSERVER_MetricKind::TRITONSERVER_METRIC_KIND_HISTOGRAM;
54+
buckets_.resize(bucket_count);
55+
std::memcpy(buckets_.data(), buckets, sizeof(double) * bucket_count);
56+
return nullptr;
57+
}
58+
59+
const std::vector<double>& buckets() const { return buckets_; }
60+
61+
private:
62+
TRITONSERVER_MetricKind kind_;
63+
std::vector<double> buckets_;
64+
};
65+
4166
//
4267
// Implementation for TRITONSERVER_MetricFamily.
4368
//
@@ -53,7 +78,7 @@ class MetricFamily {
5378

5479
void* Add(
5580
std::map<std::string, std::string> label_map, Metric* metric,
56-
const std::vector<double>* buckets = nullptr);
81+
const TritonServerMetricArgs* buckets);
5782
void Remove(void* prom_metric, Metric* metric);
5883

5984
int NumMetrics()
@@ -90,7 +115,7 @@ class Metric {
90115
Metric(
91116
TRITONSERVER_MetricFamily* family,
92117
std::vector<const InferenceParameter*> labels,
93-
const std::vector<double>* buckets = nullptr);
118+
const TritonServerMetricArgs* args);
94119
~Metric();
95120

96121
MetricFamily* Family() const { return family_; }

src/test/metrics_api_test.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ HistogramAPIHelper(TRITONSERVER_Metric* metric)
242242
double sum = 0.0;
243243
for (auto datum : data) {
244244
FAIL_TEST_IF_ERR(
245-
TRITONSERVER_MetricObserve(metric, datum), "observe metric value");
245+
TRITONSERVER_MetricSet(metric, datum), "observe metric value");
246246
sum += datum;
247247
}
248248

@@ -413,9 +413,9 @@ TEST_F(MetricsApiTest, TestHistogramEndToEnd)
413413
"example2", TRITONSERVER_PARAMETER_STRING, "histogram_label2"));
414414
std::vector<double> buckets = {0.1, 1.0, 2.5, 5.0, 10.0};
415415
FAIL_TEST_IF_ERR(
416-
TRITONSERVER_MetricNew(
416+
TRITONSERVER_MetricNewWithArgs(
417417
&metric, family, labels.data(), labels.size(),
418-
reinterpret_cast<void*>(&buckets)),
418+
reinterpret_cast<TRITONSERVER_MetricArgs*>(&buckets)),
419419
"Creating new metric");
420420
for (const auto label : labels) {
421421
TRITONSERVER_ParameterDelete(const_cast<TRITONSERVER_Parameter*>(label));
@@ -436,7 +436,6 @@ TEST_F(MetricsApiTest, TestHistogramEndToEnd)
436436
ASSERT_EQ(NumMetricMatches(server_, description), 0);
437437
}
438438

439-
440439
// Test that a duplicate metric family can't be added
441440
// with a conflicting type/kind
442441
TEST_F(MetricsApiTest, TestDupeMetricFamilyDiffKind)

0 commit comments

Comments
 (0)