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

[Metrics SDK] Only record non-negative / finite / Non-NAN histogram values #1427

Merged
merged 7 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Increment the:

## [Unreleased]

* [METRICS] Only record non-negative / finite / Non-NAN histogram values([#1427](https://github.com/open-telemetry/opentelemetry-cpp/pull/1427))

## [1.4.0] 2022-05-17

* [API SDK] Upgrade proto to v0.17.0, update log data model ([#1383](https://github.com/open-telemetry/opentelemetry-cpp/pull/1383))
Expand Down
32 changes: 32 additions & 0 deletions sdk/src/metrics/sync_instruments.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/metrics/sync_instruments.h"
# include "opentelemetry/sdk/metrics/state/metric_storage.h"
# include "opentelemetry/sdk_config.h"

# include <cmath>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
Expand Down Expand Up @@ -139,11 +142,25 @@ void LongHistogram::Record(long value,
const opentelemetry::common::KeyValueIterable &attributes,
const opentelemetry::context::Context &context) noexcept
{
if (value < 0)
{
OTEL_INTERNAL_LOG_WARN(
"[LongHistogram::Record(value, attributes)] negative value provided to histogram Name:"
<< instrument_descriptor_.name_ << " Value:" << value);
return;
}
return storage_->RecordLong(value, attributes, context);
}

void LongHistogram::Record(long value, const opentelemetry::context::Context &context) noexcept
{
if (value < 0)
{
OTEL_INTERNAL_LOG_WARN(
"[LongHistogram::Record(value)] negative value provided to histogram Name:"
<< instrument_descriptor_.name_ << " Value:" << value);
return;
}
return storage_->RecordLong(value, context);
}

Expand All @@ -156,11 +173,26 @@ void DoubleHistogram::Record(double value,
const opentelemetry::common::KeyValueIterable &attributes,
const opentelemetry::context::Context &context) noexcept
{
if (value < 0 || std::isnan(value) || std::isinf(value))
{
OTEL_INTERNAL_LOG_WARN(
"[DoubleHistogram::Record(value, attributes)] negative/nan/infinite value provided to "
"histogram Name:"
<< instrument_descriptor_.name_);
return;
}
return storage_->RecordDouble(value, attributes, context);
}

void DoubleHistogram::Record(double value, const opentelemetry::context::Context &context) noexcept
{
if (value < 0 || std::isnan(value) || std::isinf(value))
{
OTEL_INTERNAL_LOG_WARN(
"[DoubleHistogram::Record(value)] negative/nan/infinite value provided to histogram Name:"
<< instrument_descriptor_.name_);
return;
}
return storage_->RecordDouble(value, context);
}

Expand Down
10 changes: 8 additions & 2 deletions sdk/test/metrics/sync_instruments_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# include "opentelemetry/sdk/metrics/state/multi_metric_storage.h"

# include <gtest/gtest.h>
# include <cmath>
# include <limits>

using namespace opentelemetry;
using namespace opentelemetry::sdk::instrumentationlibrary;
Expand Down Expand Up @@ -103,7 +105,7 @@ TEST(SyncInstruments, LongHistogram)
std::unique_ptr<WritableMetricStorage> metric_storage(new MultiMetricStorage());
LongHistogram counter(instrument_descriptor, std::move(metric_storage));
EXPECT_NO_THROW(counter.Record(10l, opentelemetry::context::Context{}));
EXPECT_NO_THROW(counter.Record(10l, opentelemetry::context::Context{}));
EXPECT_NO_THROW(counter.Record(-10l, opentelemetry::context::Context{})); // This is ignored

EXPECT_NO_THROW(counter.Record(
10l, opentelemetry::common::KeyValueIterableView<M>({{"abc", "123"}, {"xyz", "456"}}),
Expand All @@ -120,7 +122,11 @@ TEST(SyncInstruments, DoubleHistogram)
std::unique_ptr<WritableMetricStorage> metric_storage(new MultiMetricStorage());
DoubleHistogram counter(instrument_descriptor, std::move(metric_storage));
EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::context::Context{}));
EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::context::Context{}));
EXPECT_NO_THROW(counter.Record(-10.10, opentelemetry::context::Context{})); // This is ignored.
EXPECT_NO_THROW(counter.Record(std::numeric_limits<double>::quiet_NaN(),
opentelemetry::context::Context{})); // This is ignored too
EXPECT_NO_THROW(counter.Record(std::numeric_limits<double>::infinity(),
opentelemetry::context::Context{})); // This is ignored too

EXPECT_NO_THROW(counter.Record(
10.10, opentelemetry::common::KeyValueIterableView<M>({{"abc", "123"}, {"xyz", "456"}}),
Expand Down