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

[Metric SDK] Synchronous Instruments - Aggregation Storage(s) creation for configured views #1219

Merged
merged 18 commits into from
Feb 24, 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
10 changes: 8 additions & 2 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# include <chrono>
# include "opentelemetry/metrics/meter.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/measurement_processor.h"
# include "opentelemetry/sdk/metrics/meter_context.h"
# include "opentelemetry/sdk/resource/resource.h"
Expand All @@ -20,7 +21,7 @@ class Meter final : public opentelemetry::metrics::Meter
{
public:
/** Construct a new Meter with the given pipeline. */
explicit Meter(std::shared_ptr<sdk::metrics::MeterContext> context,
explicit Meter(std::shared_ptr<sdk::metrics::MeterContext> meter_context,
std::unique_ptr<opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary>
instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create(
Expand Down Expand Up @@ -105,7 +106,12 @@ class Meter final : public opentelemetry::metrics::Meter
// order of declaration is important here - instrumentation library should destroy after
// meter-context.
std::unique_ptr<sdk::instrumentationlibrary::InstrumentationLibrary> instrumentation_library_;
std::shared_ptr<sdk::metrics::MeterContext> context_;
std::shared_ptr<sdk::metrics::MeterContext> meter_context_;
// Mapping between instrument-name and Aggregation Storage.
std::unordered_map<std::string, std::shared_ptr<MetricStorage>> storage_registry_;
esigo marked this conversation as resolved.
Show resolved Hide resolved

std::unique_ptr<WritableMetricStorage> RegisterMetricStorage(
esigo marked this conversation as resolved.
Show resolved Hide resolved
InstrumentDescriptor &instrument_descriptor);
};
} // namespace metrics
} // namespace sdk
Expand Down
6 changes: 6 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class MeterContext
*/
MeasurementProcessor *GetMeasurementProcessor() const noexcept;

/**
* Obtain the View Registry configured
* @return The reference to view registry
*/
ViewRegistry *GetViewRegistry() const noexcept;

/**
* Attaches a metric exporter to list of configured exporters for this Meter context.
* @param exporter The metric exporter for this meter context. This
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/state/metric_storage.h"

# include <memory>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

class MultiMetricStorage : public WritableMetricStorage
{
public:
void AddStorage(std::shared_ptr<WritableMetricStorage> storage) { storages_.push_back(storage); }

virtual void RecordLong(long value) noexcept override
{
for (auto &s : storages_)
{
s->RecordLong(value);
}
}

virtual void RecordLong(
long value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
{
for (auto &s : storages_)
{
s->RecordLong(value, attributes);
}
}

virtual void RecordDouble(double value) noexcept override
{
for (auto &s : storages_)
{
s->RecordDouble(value);
}
}

virtual void RecordDouble(
double value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
{
for (auto &s : storages_)
{
s->RecordDouble(value, attributes);
}
}

private:
std::vector<std::shared_ptr<WritableMetricStorage>> storages_;
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage
{

public:
SyncMetricStorage(InstrumentDescriptor instrument_descriptor,
const AggregationType aggregation_type,
AttributesProcessor *attributes_processor = new DefaultAttributesProcessor())
SyncMetricStorage(
InstrumentDescriptor instrument_descriptor,
const AggregationType aggregation_type,
const AttributesProcessor *attributes_processor = new DefaultAttributesProcessor())
: instrument_descriptor_(instrument_descriptor),
aggregation_type_{aggregation_type},
attributes_hashmap_(new AttributesHashMap()),
Expand Down Expand Up @@ -104,7 +105,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage
InstrumentDescriptor instrument_descriptor_;
AggregationType aggregation_type_;
std::unique_ptr<AttributesHashMap> attributes_hashmap_;
AttributesProcessor *attributes_processor_;
const AttributesProcessor *attributes_processor_;
std::function<std::unique_ptr<Aggregation>()> create_default_aggregation_;

std::unique_ptr<Aggregation> create_aggregation()
Expand Down
69 changes: 17 additions & 52 deletions sdk/include/opentelemetry/sdk/metrics/sync_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,21 @@ namespace metrics
class Synchronous
{
public:
Synchronous(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "")
: name_(name),
instrumentation_library_(instrumentation_library),
measurement_processor_(measurement_processor),
description_(description),
unit_(unit)
Synchronous(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage)
: instrument_descriptor_(instrument_descriptor), storage_(std::move(storage))
{}

protected:
std::string name_;
const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_;
MeasurementProcessor *measurement_processor_;
std::string description_;
std::string unit_;
InstrumentDescriptor instrument_descriptor_;
std::unique_ptr<WritableMetricStorage> storage_;
};

class LongCounter : public Synchronous, public opentelemetry::metrics::Counter<long>
{
public:
LongCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "");
LongCounter(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage);

void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override;

Expand All @@ -59,12 +44,8 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter
{

public:
DoubleCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "");
DoubleCounter(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage);

void Add(double value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
Expand All @@ -75,12 +56,8 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter
class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter<long>
{
public:
LongUpDownCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "");
LongUpDownCounter(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage);

void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override;

Expand All @@ -90,12 +67,8 @@ class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpD
class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter<double>
{
public:
DoubleUpDownCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "");
DoubleUpDownCounter(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage);

void Add(double value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
Expand All @@ -106,12 +79,8 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U
class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram<long>
{
public:
LongHistogram(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "");
LongHistogram(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage);

void Record(long value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
Expand All @@ -122,12 +91,8 @@ class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogr
class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histogram<double>
{
public:
DoubleHistogram(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
MeasurementProcessor *measurement_processor,
nostd::string_view description = "",
nostd::string_view unit = "");
DoubleHistogram(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<WritableMetricStorage> storage);

void Record(double value,
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AttributesProcessor
// Process the metric instrument attributes.
// @returns The processed attributes
virtual MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0;
const opentelemetry::common::KeyValueIterable &attributes) const noexcept = 0;
};

/**
Expand All @@ -33,7 +33,7 @@ class AttributesProcessor
class DefaultAttributesProcessor : public AttributesProcessor
{
MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
{
MetricAttributes result(attributes);
return result;
Expand All @@ -54,7 +54,7 @@ class FilteringAttributesProcessor : public AttributesProcessor
{}

MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
{
MetricAttributes result;
attributes.ForEachKeyValue(
Expand Down
15 changes: 6 additions & 9 deletions sdk/include/opentelemetry/sdk/metrics/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/view/attributes_processor.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -21,26 +22,22 @@ class View
{
public:
View(const std::string &name,
const std::string &description = "",
std::unique_ptr<opentelemetry::sdk::metrics::Aggregation> aggregation =
std::unique_ptr<opentelemetry::sdk::metrics::Aggregation>(new DropAggregation()),
const std::string &description = "",
AggregationType aggregation_type = AggregationType::kDrop,
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor =
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor>(
new opentelemetry::sdk::metrics::DefaultAttributesProcessor()))
: name_(name),
description_(description),
aggregation_{std::move(aggregation)},
aggregation_type_{aggregation_type},
attributes_processor_{std::move(attributes_processor)}
{}

virtual std::string GetName() const noexcept { return name_; }

virtual std::string GetDescription() const noexcept { return description_; }

virtual const opentelemetry::sdk::metrics::Aggregation &GetAggregation() const noexcept
{
return *aggregation_.get();
}
virtual AggregationType GetAggregationType() const noexcept { return aggregation_type_; }

virtual const opentelemetry::sdk::metrics::AttributesProcessor &GetAttributesProcessor()
const noexcept
Expand All @@ -51,7 +48,7 @@ class View
private:
std::string name_;
std::string description_;
std::unique_ptr<opentelemetry::sdk::metrics::Aggregation> aggregation_;
AggregationType aggregation_type_;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor_;
};
} // namespace metrics
Expand Down
Loading