Skip to content

Commit

Permalink
Test MetricArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
yinggeh committed Aug 13, 2024
1 parent 667858e commit 60f1e63
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 0 additions & 1 deletion include/triton/core/tritonserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2693,7 +2693,6 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricArgsDelete(
/// \param family The metric family to add this new metric to.
/// \param labels The array of labels to associate with this new metric.
/// \param label_count The number of labels.
/// bucket boundaries. For histogram only.
/// \return a TRITONSERVER_Error indicating success or failure.
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error* TRITONSERVER_MetricNew(
struct TRITONSERVER_Metric** metric,
Expand Down
3 changes: 1 addition & 2 deletions src/metric_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ MetricFamily::Add(
"Bucket boundaries not found in Metric args.");
}
if (args->kind() != TRITONSERVER_METRIC_KIND_HISTOGRAM) {
throw std::invalid_argument(
"Incorrect Metric args kind in histogram Metric constructor.");
throw std::invalid_argument("Metric args not set to histogram kind.");
}
auto histogram_family_ptr =
reinterpret_cast<prometheus::Family<prometheus::Histogram>*>(family_);
Expand Down
61 changes: 61 additions & 0 deletions src/test/metrics_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,67 @@ TEST_F(MetricsApiTest, TestHistogramEndToEnd)
ASSERT_EQ(NumMetricMatches(server_, description), 0);
}

// Test create a histogram metric without creating metric arguments
TEST_F(MetricsApiTest, TestHistogramNoMetricArgs)
{
// Create metric family
TRITONSERVER_MetricFamily* family;
TRITONSERVER_MetricKind kind = TRITONSERVER_METRIC_KIND_HISTOGRAM;
const char* name = "no_metric_args";
const char* description = "no metric args description";
FAIL_TEST_IF_ERR(
TRITONSERVER_MetricFamilyNew(&family, kind, name, description),
"Creating new metric family");

// MetricArgs not created
TRITONSERVER_MetricArgs* args = nullptr;
// Create metric
std::vector<const TRITONSERVER_Parameter*> labels;
TRITONSERVER_Metric* metric = nullptr;
auto err = TRITONSERVER_MetricNewWithArgs(
&metric, family, labels.data(), labels.size(), args);
EXPECT_THAT(
TRITONSERVER_ErrorMessage(err),
HasSubstr("Bucket boundaries not found in Metric args"));

// Cleanup
FAIL_TEST_IF_ERR(TRITONSERVER_MetricArgsDelete(args), "delete metric args");
FAIL_TEST_IF_ERR(
TRITONSERVER_MetricFamilyDelete(family), "delete metric family");
}

// Test create a histogram metric without setting metric arguments
TEST_F(MetricsApiTest, TestHistogramMetricArgsNotset)
{
// Create metric family
TRITONSERVER_MetricFamily* family;
TRITONSERVER_MetricKind kind = TRITONSERVER_METRIC_KIND_HISTOGRAM;
const char* name = "metric_args_not_set";
const char* description = "metric args not set description";
FAIL_TEST_IF_ERR(
TRITONSERVER_MetricFamilyNew(&family, kind, name, description),
"Creating new metric family");

// Create metric args object without setting it
TRITONSERVER_MetricArgs* args;
FAIL_TEST_IF_ERR(
TRITONSERVER_MetricArgsNew(&args), "Creating new metric args");

// Create metric
std::vector<const TRITONSERVER_Parameter*> labels;
TRITONSERVER_Metric* metric = nullptr;
auto err = TRITONSERVER_MetricNewWithArgs(
&metric, family, labels.data(), labels.size(), args);
EXPECT_THAT(
TRITONSERVER_ErrorMessage(err),
HasSubstr("Metric args not set to histogram kind"));

// Cleanup
FAIL_TEST_IF_ERR(TRITONSERVER_MetricArgsDelete(args), "delete metric args");
FAIL_TEST_IF_ERR(
TRITONSERVER_MetricFamilyDelete(family), "delete metric family");
}

// Test that a duplicate metric family can't be added
// with a conflicting type/kind
TEST_F(MetricsApiTest, TestDupeMetricFamilyDiffKind)
Expand Down

0 comments on commit 60f1e63

Please sign in to comment.