Program Listing for File metric_filter.h

Return to documentation for file (/tmp/B.puc0r6hi/BUILD/opentelemetry-cpp-1.27.0-build/opentelemetry-cpp-1.27.0/sdk/include/opentelemetry/sdk/metrics/export/metric_filter.h)

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstdint>
#include <functional>
#include <memory>

#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/metrics/data/metric_data.h"
#include "opentelemetry/sdk/metrics/instruments.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

class MetricFilter
{
public:
  enum class MetricFilterResult : std::uint8_t
  {
    kAccept,
    kDrop,
    kAcceptPartial,
  };

  enum class AttributesFilterResult : std::uint8_t
  {
    kAccept,
    kDrop,
  };

  using TestMetricFn = std::function<MetricFilterResult(
      const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope,
      opentelemetry::nostd::string_view name,
      const InstrumentType &type,
      opentelemetry::nostd::string_view unit)>;

  using TestAttributesFn = std::function<AttributesFilterResult(
      const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope,
      opentelemetry::nostd::string_view name,
      const InstrumentType &type,
      opentelemetry::nostd::string_view unit,
      const PointAttributes &attributes)>;

  // static
  static std::unique_ptr<MetricFilter> Create(const TestMetricFn &test_metric_fn,
                                              const TestAttributesFn &test_attributes_fn)
  {
    return std::make_unique<MetricFilter>(test_metric_fn, test_attributes_fn);
  }

  MetricFilter(const TestMetricFn &test_metric_fn, const TestAttributesFn &test_attributes_fn)
      : test_metric_fn_(test_metric_fn), test_attributes_fn_(test_attributes_fn)
  {}

  MetricFilterResult TestMetric(
      const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope,
      opentelemetry::nostd::string_view name,
      const InstrumentType &type,
      opentelemetry::nostd::string_view unit)
  {
    return test_metric_fn_(scope, name, type, unit);
  }

  AttributesFilterResult TestAttributes(
      const opentelemetry::sdk::instrumentationscope::InstrumentationScope &scope,
      opentelemetry::nostd::string_view name,
      const InstrumentType &type,
      opentelemetry::nostd::string_view unit,
      const PointAttributes &attributes)
  {
    return test_attributes_fn_(scope, name, type, unit, attributes);
  }

private:
  TestMetricFn test_metric_fn_;
  TestAttributesFn test_attributes_fn_;
};

}  // namespace metrics
}  // namespace sdk
OPENTELEMETRY_END_NAMESPACE