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: View API #1110

Merged
merged 28 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c7b92a8
initial view commit
lalitb Dec 3, 2021
98f94e0
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 3, 2021
a6866c6
build fix
lalitb Dec 6, 2021
0d49214
Merge branch 'metrics-sdk-views-2' of github.com:lalitb/opentelemetry…
lalitb Dec 6, 2021
c41471a
build issue
lalitb Dec 6, 2021
7a7a57a
regex for gcc4.8
lalitb Dec 6, 2021
34ef0c5
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 6, 2021
514bd85
fix 4.8 build
lalitb Dec 6, 2021
8af6f2f
Merge branch 'metrics-sdk-views-2' of github.com:lalitb/opentelemetry…
lalitb Dec 6, 2021
4f84e35
fix test
lalitb Dec 7, 2021
9111528
more changes
lalitb Dec 7, 2021
189f293
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 7, 2021
f3e9172
add tests for view_registry
lalitb Dec 8, 2021
c013f68
Merge branch 'metrics-sdk-views-2' of github.com:lalitb/opentelemetry…
lalitb Dec 8, 2021
c537f8b
more tests for view_registry
lalitb Dec 8, 2021
f2f412b
add const variable:
lalitb Dec 8, 2021
918e7f2
disable predicate test for gcc4.8
lalitb Dec 8, 2021
0bd982d
Aggregation API should return reference to Aggregator
lalitb Dec 13, 2021
0ac15a2
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 13, 2021
0a5863e
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 14, 2021
7f91b4e
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 15, 2021
bcb43c6
review comments
lalitb Dec 16, 2021
2bfb652
log error for regex for gcc4.8
lalitb Dec 16, 2021
cb4d314
compile error
lalitb Dec 16, 2021
447cece
more compiler error
lalitb Dec 16, 2021
c28426b
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 16, 2021
31f681d
Merge branch 'main' into metrics-sdk-views-2
ThomsonTan Dec 17, 2021
5e14816
Merge branch 'main' into metrics-sdk-views-2
lalitb Dec 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
/**
* View defines the interface to allow SDK user to
* customize the metrics before exported.
*/

class View
class Aggregator
{
// TBD
};

class NoOpAggregator : public Aggregator
{
public:
virtual ~View() = default;
// TBD
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
#endif
52 changes: 52 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/instruments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/nostd/string_view.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
enum class InstrumentType
{
kCounter,
kHistogram,
kUpDownCounter,
kObservableCounter,
kObservableGauge,
kObservableUpDownCounter
};

enum class InstrumentValueType
{
kInt,
kLong,
kFloat,
kDouble
};

struct InstrumentDescriptor
{
std::string name_;
std::string description_;
std::string unit_;
InstrumentType type_;
InstrumentValueType valueType_;
};

/*class InstrumentSelector {
public:
InstrumentSelector(opentelemetry::nostd::string_view name,
opentelemetry::sdk::metrics::InstrumentType type): name_(name.data()), type_(type) {} InstrumentType
GetType(){return type_;} std::string GetNameFilter() { return name_;}

private:
std::string name_;
InstrumentType type_;
};*/
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
21 changes: 13 additions & 8 deletions sdk/include/opentelemetry/sdk/metrics/meter_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
# include <vector>
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/metric_reader.h"
# include "opentelemetry/sdk/metrics/view.h"
# include "opentelemetry/sdk/metrics/view/instrument_selector.h"
# include "opentelemetry/sdk/metrics/view/meter_selector.h"
# include "opentelemetry/sdk/metrics/view/view_registry.h"
# include "opentelemetry/sdk/resource/resource.h"
# include "opentelemetry/version.h"

Expand All @@ -31,11 +33,12 @@ class MeterContext
* @param views The views to be configured with meter context.
* @param resource The resource for this meter context.
*/
MeterContext(std::vector<std::unique_ptr<sdk::metrics::MetricExporter>> &&exporters,
std::vector<std::unique_ptr<MetricReader>> &&readers,
std::vector<std::unique_ptr<View>> &&views,
opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::Create({})) noexcept;
MeterContext(
std::vector<std::unique_ptr<sdk::metrics::MetricExporter>> &&exporters,
std::vector<std::unique_ptr<MetricReader>> &&readers,
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::Create({})) noexcept;

/**
* Obtain the resource associated with this meter context.
Expand Down Expand Up @@ -71,7 +74,9 @@ class MeterContext
* Note: This view may not receive any in-flight meter data, but will get newly created meter
* data. Note: This method is not thread safe, and should ideally be called from main thread.
*/
void AddView(std::unique_ptr<View> view) noexcept;
void AddView(std::unique_ptr<InstrumentSelector> instrument_selector,
std::unique_ptr<MeterSelector> meter_selector,
std::unique_ptr<View> view) noexcept;

/**
* Force all active Exporters and Readers to flush any buffered meter data
Expand All @@ -89,7 +94,7 @@ class MeterContext
opentelemetry::sdk::resource::Resource resource_;
std::vector<std::unique_ptr<MetricExporter>> exporters_;
std::vector<std::unique_ptr<MetricReader>> readers_;
std::vector<std::unique_ptr<View>> views_;
std::unique_ptr<ViewRegistry> views_;
};

} // namespace metrics
Expand Down
13 changes: 8 additions & 5 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider
* @param views The views for this meter provider
* @param resource The resources for this meter provider.
*/
MeterProvider(std::vector<std::unique_ptr<MetricExporter>> &&exporters,
std::vector<std::unique_ptr<MetricReader>> &&readers,
std::vector<std::unique_ptr<View>> &&views,
sdk::resource::Resource resource = sdk::resource::Resource::Create({})) noexcept;
MeterProvider(
std::vector<std::unique_ptr<MetricExporter>> &&exporters,
std::vector<std::unique_ptr<MetricReader>> &&readers,
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
sdk::resource::Resource resource = sdk::resource::Resource::Create({})) noexcept;

/**
* Initialize a new meter provider with a specified context
Expand Down Expand Up @@ -78,7 +79,9 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider
* Note: This view may not receive any in-flight meter data, but will get newly created meter
* data. Note: This method is not thread safe, and should ideally be called from main thread.
*/
void AddView(std::unique_ptr<View> view) noexcept;
void AddView(std::unique_ptr<InstrumentSelector> instrument_selector,
std::unique_ptr<MeterSelector> meter_selector,
std::unique_ptr<View> view) noexcept;

/**
* Shutdown the meter provider.
Expand Down
49 changes: 49 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/aggregation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include <memory>
# include "opentelemetry/sdk/metrics/aggregator/aggregator.h"
# include "opentelemetry/sdk/metrics/instruments.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class Aggregation
{
public:
virtual ~Aggregation() = default;
virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator(
opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept = 0;
};

class NoOpAggregation : public Aggregation
{

opentelemetry::sdk::metrics::Aggregator &CreateAggregator(
opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override
{
static opentelemetry::sdk::metrics::NoOpAggregator noop_aggregator;
return noop_aggregator;
}
};

class DefaultAggregation : public Aggregation
{

opentelemetry::sdk::metrics::Aggregator &CreateAggregator(
opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override
{
// TBD - This shouldn't return noop_aggregator
static opentelemetry::sdk::metrics::NoOpAggregator noop_aggregator;
return noop_aggregator;
}
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/common/attribute_utils.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
using MetricAttributes = opentelemetry::sdk::common::AttributeMap;

class AttributesProcessor
{
public:
virtual MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0;
};

class DefaultAttributesProcessor : public AttributesProcessor
{
MetricAttributes process(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC - by necessity, this API will need to take in an Iterator and return a concrete object. It's likely this would get used on the "hot path" of metrics. Does it make sense to have this API work only against AttributeMap, that way in the event you're just passing it through, there isn't a sort + unique step (I forget the details of attribute map).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, your understanding is correct here. The metrics API takes non-owning KeyValueIterable object as here, and the plan is to make a copy of this only when required in hot-path. Attribute-processor could be one of the places to create a copy in case of passthrough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this to (commonly) be a passthrough case. Can deal with it after initial implementation :)

const opentelemetry::common::KeyValueIterable &attributes) noexcept override
{
MetricAttributes result(attributes);
return result;
}
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/view/predicate.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class InstrumentSelector
{
public:
InstrumentSelector(opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name)
: name_filter_{new opentelemetry::sdk::metrics::PatternPredicate(name)},
instrument_type_{instrument_type}
{}

// Returns name filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *const GetNameFilter() { return name_filter_.get(); }

// Returns instrument filter.
InstrumentType GetInstrumentType() { return instrument_type_; }

private:
opentelemetry::sdk::metrics::InstrumentType instrument_type_;
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> name_filter_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
47 changes: 47 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/meter_selector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/metrics/view/predicate.h"
#ifndef ENABLE_METRICS_PREVIEW
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class MeterSelector
{
public:
MeterSelector(opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view version,
opentelemetry::nostd::string_view schema)
: name_filter_{new opentelemetry::sdk::metrics::ExactPredicate(name)},
version_filter_{new opentelemetry::sdk::metrics::ExactPredicate(version)},
schema_filter_{new opentelemetry::sdk::metrics::ExactPredicate(schema)}
{}

// Returns name filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *const GetNameFilter() { return name_filter_.get(); }

// Returns version filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *const GetVersionFilter()
{
return version_filter_.get();
}

// Returns schema filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *const GetSchemaFilter()
{
return schema_filter_.get();
}

private:
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> name_filter_;
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> version_filter_;
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> schema_filter_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Loading