Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
151 changes: 141 additions & 10 deletions sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <cstring>
#include <string>
#include <unordered_map>
#include <utility>
Expand All @@ -18,8 +19,133 @@ namespace sdk
{
namespace metrics
{

using MetricAttributes = opentelemetry::sdk::metrics::FilteredOrderedAttributeMap;

/**
* Generic FNV hash implementation
*/

template <typename Ty, size_t s>
struct fnv_magic_prime_number
{
static constexpr const Ty value = 0x01000193U;
};

template <typename Ty>
struct fnv_magic_prime_number<Ty, 8>
{
static constexpr const Ty value = 0x100000001b3ULL;
};

template <typename Ty, size_t s>
struct fnv_magic_offset_basis
{
static constexpr const Ty value = 0x811C9DC5U;

static constexpr Ty fix(Ty hval) { return hval; }
};

template <typename Ty>
struct fnv_magic_offset_basis<Ty, 8>
{
static constexpr const Ty value = 0xCBF29CE484222325ULL;

static constexpr Ty fix(Ty hval) { return hval ^ (hval >> 32); }
};

template <typename Ty>
Ty fnv_n_buf(const void *buf, size_t len, Ty hval = fnv_magic_offset_basis<Ty, sizeof(Ty)>::value)
{
const unsigned char *bp = reinterpret_cast<const unsigned char *>(buf);
const unsigned char *be = bp + len;
Ty mn = fnv_magic_prime_number<Ty, sizeof(Ty)>::value;

while (bp < be)
{
hval *= mn;
hval ^= static_cast<Ty>(*bp++);
}

return fnv_magic_offset_basis<Ty, sizeof(Ty)>::fix(hval);
}

template <typename Ty>
Ty fnv_n_buf_a(const void *buf, size_t len, Ty hval = fnv_magic_offset_basis<Ty, sizeof(Ty)>::value)
{
const unsigned char *bp = reinterpret_cast<const unsigned char *>(buf);
const unsigned char *be = bp + len;
Ty mn = fnv_magic_prime_number<Ty, sizeof(Ty)>::value;

while (bp < be)
{
hval ^= static_cast<Ty>(*bp++);
hval *= mn;
}

return fnv_magic_offset_basis<Ty, sizeof(Ty)>::fix(hval);
}

/**
* Hash and equality for nostd::string_view, enabling safe use in unordered_map
* without requiring null termination.
*/

struct StringViewHash
{
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2020
using is_transparent = void;
# endif
#endif

std::size_t operator()(const std::string &s) const noexcept
{
return fnv_n_buf_a<std::size_t>(s.data(), s.size());
}

std::size_t operator()(opentelemetry::nostd::string_view sv) const noexcept
{
return fnv_n_buf_a<std::size_t>(sv.data(), sv.size());
}
};

struct StringViewEqual
{
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2020
using is_transparent = void;
# endif
#endif

template <typename Lhs, typename Rhs>
bool operator()(const Lhs &lhs, const Rhs &rhs) const noexcept
{
opentelemetry::nostd::string_view lsv(lhs.data(), lhs.size());
opentelemetry::nostd::string_view rsv(rhs.data(), rhs.size());

return lsv.size() == rsv.size() && std::memcmp(lsv.data(), rsv.data(), lsv.size()) == 0;
}
};

/**
* Cross-platform heterogeneous lookup wrapper.
* Falls back to std::string construction on libc++ (macOS) and pre-c++20,
* but uses direct lookup on libstdc++ (Linux).
*/

template <typename MapType>
inline auto find_hetero(MapType &&map, opentelemetry::nostd::string_view key)
{
#if defined(_LIBCPP_VERSION) || \
(!defined(OPENTELEMETRY_STL_VERSION) || OPENTELEMETRY_STL_VERSION < 2020)
return map.find(std::string(key));
#else
// libstdc++ + C++20: heterogeneous lookup works
return map.find(key);
#endif
}

/**
* The AttributesProcessor is responsible for customizing which
* attribute(s) are to be reported as metrics dimension(s).
Expand Down Expand Up @@ -65,12 +191,15 @@ class DefaultAttributesProcessor : public AttributesProcessor
class FilteringAttributesProcessor : public AttributesProcessor
{
public:
FilteringAttributesProcessor(std::unordered_map<std::string, bool> &&allowed_attribute_keys = {})
FilteringAttributesProcessor(
std::unordered_map<std::string, bool, StringViewHash, StringViewEqual>
&&allowed_attribute_keys = {})
: allowed_attribute_keys_(std::move(allowed_attribute_keys))
{}

FilteringAttributesProcessor(
const std::unordered_map<std::string, bool> &allowed_attribute_keys = {})
const std::unordered_map<std::string, bool, StringViewHash, StringViewEqual>
&allowed_attribute_keys = {})
: allowed_attribute_keys_(allowed_attribute_keys)
{}

Expand All @@ -80,7 +209,7 @@ class FilteringAttributesProcessor : public AttributesProcessor
MetricAttributes result;
attributes.ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
if (allowed_attribute_keys_.find(std::string(key)) != allowed_attribute_keys_.end())
if (find_hetero(allowed_attribute_keys_, key) != allowed_attribute_keys_.end())
{
result.SetAttribute(key, value);
return true;
Expand All @@ -94,11 +223,11 @@ class FilteringAttributesProcessor : public AttributesProcessor

bool isPresent(nostd::string_view key) const noexcept override
{
return (allowed_attribute_keys_.find(std::string(key)) != allowed_attribute_keys_.end());
return (find_hetero(allowed_attribute_keys_, key) != allowed_attribute_keys_.end());
}

private:
std::unordered_map<std::string, bool> allowed_attribute_keys_;
std::unordered_map<std::string, bool, StringViewHash, StringViewEqual> allowed_attribute_keys_;
};

/**
Expand All @@ -109,12 +238,14 @@ class FilteringAttributesProcessor : public AttributesProcessor
class FilteringExcludeAttributesProcessor : public AttributesProcessor
{
public:
FilteringExcludeAttributesProcessor(std::unordered_map<std::string, bool> &&exclude_list = {})
FilteringExcludeAttributesProcessor(
std::unordered_map<std::string, bool, StringViewHash, StringViewEqual> &&exclude_list = {})
: exclude_list_(std::move(exclude_list))
{}

FilteringExcludeAttributesProcessor(
const std::unordered_map<std::string, bool> &exclude_list = {})
const std::unordered_map<std::string, bool, StringViewHash, StringViewEqual> &exclude_list =
{})
: exclude_list_(exclude_list)
{}

Expand All @@ -124,7 +255,7 @@ class FilteringExcludeAttributesProcessor : public AttributesProcessor
MetricAttributes result;
attributes.ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
if (exclude_list_.find(std::string(key)) == exclude_list_.end())
if (find_hetero(exclude_list_, key) == exclude_list_.end())
{
result.SetAttribute(key, value);
return true;
Expand All @@ -138,11 +269,11 @@ class FilteringExcludeAttributesProcessor : public AttributesProcessor

bool isPresent(nostd::string_view key) const noexcept override
{
return (exclude_list_.find(std::string(key)) == exclude_list_.end());
return (find_hetero(exclude_list_, key) == exclude_list_.end());
}

private:
std::unordered_map<std::string, bool> exclude_list_;
std::unordered_map<std::string, bool, StringViewHash, StringViewEqual> exclude_list_;
};

} // namespace metrics
Expand Down
26 changes: 16 additions & 10 deletions sdk/test/metrics/attributes_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ using namespace opentelemetry::sdk::common;

TEST(AttributesProcessor, FilteringAttributesProcessor)
{
const int kNumFilterAttributes = 3;
std::unordered_map<std::string, bool> filter = {
{"attr2", true}, {"attr4", true}, {"attr6", true}};
const int kNumFilterAttributes = 3;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
filter = {{"attr2", true}, {"attr4", true}, {"attr6", true}};
const int kNumAttributes = 6;
std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"};
int values[kNumAttributes] = {10, 20, 30, 40, 50, 60};
Expand All @@ -38,9 +39,11 @@ TEST(AttributesProcessor, FilteringAttributesProcessor)

TEST(AttributesProcessor, FilteringAllAttributesProcessor)
{
const int kNumFilterAttributes = 0;
std::unordered_map<std::string, bool> filter = {};
const int kNumAttributes = 6;
const int kNumFilterAttributes = 0;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
filter = {};
const int kNumAttributes = 6;
std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"};
int values[kNumAttributes] = {10, 20, 30, 40, 50, 60};
std::map<std::string, int> attributes = {{keys[0], values[0]}, {keys[1], values[1]},
Expand All @@ -54,8 +57,9 @@ TEST(AttributesProcessor, FilteringAllAttributesProcessor)

TEST(AttributesProcessor, FilteringExcludeAttributesProcessor)
{
std::unordered_map<std::string, bool> filter = {
{"attr2", true}, {"attr4", true}, {"attr6", true}};
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
filter = {{"attr2", true}, {"attr4", true}, {"attr6", true}};
const int kNumAttributes = 7;
std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4",
"attr5", "attr6", "attr7"};
Expand All @@ -76,8 +80,10 @@ TEST(AttributesProcessor, FilteringExcludeAttributesProcessor)

TEST(AttributesProcessor, FilteringExcludeAllAttributesProcessor)
{
std::unordered_map<std::string, bool> filter = {};
const int kNumAttributes = 6;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
filter = {};
const int kNumAttributes = 6;
std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"};
int values[kNumAttributes] = {10, 20, 30, 40, 50, 60};
std::map<std::string, int> attributes = {{keys[0], values[0]}, {keys[1], values[1]},
Expand Down
16 changes: 12 additions & 4 deletions sdk/test/metrics/sum_aggregation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ TEST(HistogramToSumFilterAttributes, Double)
std::string instrument_name = "histogram1";
std::string instrument_desc = "histogram metrics";

std::unordered_map<std::string, bool> allowedattr;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
allowedattr;
allowedattr["attr1"] = true;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attrproc{
new opentelemetry::sdk::metrics::FilteringAttributesProcessor(allowedattr)};
Expand Down Expand Up @@ -154,7 +156,9 @@ TEST(HistogramToSumFilterAttributesWithCardinalityLimit, Double)
std::string instrument_desc = "histogram metrics";
size_t cardinality_limit = 10000;

std::unordered_map<std::string, bool> allowedattr;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
allowedattr;
allowedattr["attr1"] = true;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attrproc{
new opentelemetry::sdk::metrics::FilteringAttributesProcessor(allowedattr)};
Expand Down Expand Up @@ -281,7 +285,9 @@ TEST(CounterToSumFilterAttributes, Double)
std::string instrument_name = "counter1";
std::string instrument_desc = "counter metrics";

std::unordered_map<std::string, bool> allowedattr;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
allowedattr;
allowedattr["attr1"] = true;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attrproc{
new opentelemetry::sdk::metrics::FilteringAttributesProcessor(allowedattr)};
Expand Down Expand Up @@ -335,7 +341,9 @@ TEST(CounterToSumFilterAttributesWithCardinalityLimit, Double)
std::string instrument_desc = "counter metrics";
size_t cardinality_limit = 10000;

std::unordered_map<std::string, bool> allowedattr;
std::unordered_map<std::string, bool, opentelemetry::sdk::metrics::StringViewHash,
opentelemetry::sdk::metrics::StringViewEqual>
allowedattr;
allowedattr["attr1"] = true;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attrproc{
new opentelemetry::sdk::metrics::FilteringAttributesProcessor(allowedattr)};
Expand Down
Loading