Skip to content

Commit

Permalink
[Metrics SDK] Only record non-negative / finite / Non-NAN histogram v…
Browse files Browse the repository at this point in the history
…alues (#1427)

* validate histogram value

* handle Nan

* add changelog

* divide by 0 error on windows

* fix markdown lint
  • Loading branch information
lalitb authored Jun 3, 2022
1 parent 81df64b commit 62b65fa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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:/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:/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

0 comments on commit 62b65fa

Please sign in to comment.