Skip to content

Commit c10bc36

Browse files
authored
[Metric SDK] Synchronous Instruments - Aggregation Storage(s) creation for configured views (#1219)
1 parent 9157fd4 commit c10bc36

15 files changed

Lines changed: 322 additions & 155 deletions

sdk/include/opentelemetry/sdk/metrics/meter.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# include <chrono>
77
# include "opentelemetry/metrics/meter.h"
88
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
9+
# include "opentelemetry/sdk/metrics/instruments.h"
910
# include "opentelemetry/sdk/metrics/measurement_processor.h"
1011
# include "opentelemetry/sdk/metrics/meter_context.h"
1112
# include "opentelemetry/sdk/resource/resource.h"
@@ -20,7 +21,7 @@ class Meter final : public opentelemetry::metrics::Meter
2021
{
2122
public:
2223
/** Construct a new Meter with the given pipeline. */
23-
explicit Meter(std::shared_ptr<sdk::metrics::MeterContext> context,
24+
explicit Meter(std::shared_ptr<sdk::metrics::MeterContext> meter_context,
2425
std::unique_ptr<opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary>
2526
instrumentation_library =
2627
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create(
@@ -105,7 +106,12 @@ class Meter final : public opentelemetry::metrics::Meter
105106
// order of declaration is important here - instrumentation library should destroy after
106107
// meter-context.
107108
std::unique_ptr<sdk::instrumentationlibrary::InstrumentationLibrary> instrumentation_library_;
108-
std::shared_ptr<sdk::metrics::MeterContext> context_;
109+
std::shared_ptr<sdk::metrics::MeterContext> meter_context_;
110+
// Mapping between instrument-name and Aggregation Storage.
111+
std::unordered_map<std::string, std::shared_ptr<MetricStorage>> storage_registry_;
112+
113+
std::unique_ptr<WritableMetricStorage> RegisterMetricStorage(
114+
InstrumentDescriptor &instrument_descriptor);
109115
};
110116
} // namespace metrics
111117
} // namespace sdk

sdk/include/opentelemetry/sdk/metrics/meter_context.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class MeterContext
5252
*/
5353
MeasurementProcessor *GetMeasurementProcessor() const noexcept;
5454

55+
/**
56+
* Obtain the View Registry configured
57+
* @return The reference to view registry
58+
*/
59+
ViewRegistry *GetViewRegistry() const noexcept;
60+
5561
/**
5662
* Attaches a metric exporter to list of configured exporters for this Meter context.
5763
* @param exporter The metric exporter for this meter context. This
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
#ifndef ENABLE_METRICS_PREVIEW
6+
# include "opentelemetry/common/key_value_iterable_view.h"
7+
# include "opentelemetry/sdk/metrics/instruments.h"
8+
# include "opentelemetry/sdk/metrics/state/metric_storage.h"
9+
10+
# include <memory>
11+
12+
OPENTELEMETRY_BEGIN_NAMESPACE
13+
namespace sdk
14+
{
15+
namespace metrics
16+
{
17+
18+
class MultiMetricStorage : public WritableMetricStorage
19+
{
20+
public:
21+
void AddStorage(std::shared_ptr<WritableMetricStorage> storage) { storages_.push_back(storage); }
22+
23+
virtual void RecordLong(long value) noexcept override
24+
{
25+
for (auto &s : storages_)
26+
{
27+
s->RecordLong(value);
28+
}
29+
}
30+
31+
virtual void RecordLong(
32+
long value,
33+
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
34+
{
35+
for (auto &s : storages_)
36+
{
37+
s->RecordLong(value, attributes);
38+
}
39+
}
40+
41+
virtual void RecordDouble(double value) noexcept override
42+
{
43+
for (auto &s : storages_)
44+
{
45+
s->RecordDouble(value);
46+
}
47+
}
48+
49+
virtual void RecordDouble(
50+
double value,
51+
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
52+
{
53+
for (auto &s : storages_)
54+
{
55+
s->RecordDouble(value, attributes);
56+
}
57+
}
58+
59+
private:
60+
std::vector<std::shared_ptr<WritableMetricStorage>> storages_;
61+
};
62+
63+
} // namespace metrics
64+
} // namespace sdk
65+
OPENTELEMETRY_END_NAMESPACE
66+
#endif

sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage
2525
{
2626

2727
public:
28-
SyncMetricStorage(InstrumentDescriptor instrument_descriptor,
29-
const AggregationType aggregation_type,
30-
AttributesProcessor *attributes_processor = new DefaultAttributesProcessor())
28+
SyncMetricStorage(
29+
InstrumentDescriptor instrument_descriptor,
30+
const AggregationType aggregation_type,
31+
const AttributesProcessor *attributes_processor = new DefaultAttributesProcessor())
3132
: instrument_descriptor_(instrument_descriptor),
3233
aggregation_type_{aggregation_type},
3334
attributes_hashmap_(new AttributesHashMap()),
@@ -104,7 +105,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage
104105
InstrumentDescriptor instrument_descriptor_;
105106
AggregationType aggregation_type_;
106107
std::unique_ptr<AttributesHashMap> attributes_hashmap_;
107-
AttributesProcessor *attributes_processor_;
108+
const AttributesProcessor *attributes_processor_;
108109
std::function<std::unique_ptr<Aggregation>()> create_default_aggregation_;
109110

110111
std::unique_ptr<Aggregation> create_aggregation()

sdk/include/opentelemetry/sdk/metrics/sync_instruments.h

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,21 @@ namespace metrics
1919
class Synchronous
2020
{
2121
public:
22-
Synchronous(nostd::string_view name,
23-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
24-
*instrumentation_library,
25-
MeasurementProcessor *measurement_processor,
26-
nostd::string_view description = "",
27-
nostd::string_view unit = "")
28-
: name_(name),
29-
instrumentation_library_(instrumentation_library),
30-
measurement_processor_(measurement_processor),
31-
description_(description),
32-
unit_(unit)
22+
Synchronous(InstrumentDescriptor instrument_descriptor,
23+
std::unique_ptr<WritableMetricStorage> storage)
24+
: instrument_descriptor_(instrument_descriptor), storage_(std::move(storage))
3325
{}
3426

3527
protected:
36-
std::string name_;
37-
const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_;
38-
MeasurementProcessor *measurement_processor_;
39-
std::string description_;
40-
std::string unit_;
28+
InstrumentDescriptor instrument_descriptor_;
29+
std::unique_ptr<WritableMetricStorage> storage_;
4130
};
4231

4332
class LongCounter : public Synchronous, public opentelemetry::metrics::Counter<long>
4433
{
4534
public:
46-
LongCounter(nostd::string_view name,
47-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
48-
*instrumentation_library,
49-
MeasurementProcessor *measurement_processor,
50-
nostd::string_view description = "",
51-
nostd::string_view unit = "");
35+
LongCounter(InstrumentDescriptor instrument_descriptor,
36+
std::unique_ptr<WritableMetricStorage> storage);
5237

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

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

6146
public:
62-
DoubleCounter(nostd::string_view name,
63-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
64-
*instrumentation_library,
65-
MeasurementProcessor *measurement_processor,
66-
nostd::string_view description = "",
67-
nostd::string_view unit = "");
47+
DoubleCounter(InstrumentDescriptor instrument_descriptor,
48+
std::unique_ptr<WritableMetricStorage> storage);
6849

6950
void Add(double value,
7051
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
@@ -75,12 +56,8 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter
7556
class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter<long>
7657
{
7758
public:
78-
LongUpDownCounter(nostd::string_view name,
79-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
80-
*instrumentation_library,
81-
MeasurementProcessor *measurement_processor,
82-
nostd::string_view description = "",
83-
nostd::string_view unit = "");
59+
LongUpDownCounter(InstrumentDescriptor instrument_descriptor,
60+
std::unique_ptr<WritableMetricStorage> storage);
8461

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

@@ -90,12 +67,8 @@ class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpD
9067
class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter<double>
9168
{
9269
public:
93-
DoubleUpDownCounter(nostd::string_view name,
94-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
95-
*instrumentation_library,
96-
MeasurementProcessor *measurement_processor,
97-
nostd::string_view description = "",
98-
nostd::string_view unit = "");
70+
DoubleUpDownCounter(InstrumentDescriptor instrument_descriptor,
71+
std::unique_ptr<WritableMetricStorage> storage);
9972

10073
void Add(double value,
10174
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
@@ -106,12 +79,8 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U
10679
class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram<long>
10780
{
10881
public:
109-
LongHistogram(nostd::string_view name,
110-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
111-
*instrumentation_library,
112-
MeasurementProcessor *measurement_processor,
113-
nostd::string_view description = "",
114-
nostd::string_view unit = "");
82+
LongHistogram(InstrumentDescriptor instrument_descriptor,
83+
std::unique_ptr<WritableMetricStorage> storage);
11584

11685
void Record(long value,
11786
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
@@ -122,12 +91,8 @@ class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogr
12291
class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histogram<double>
12392
{
12493
public:
125-
DoubleHistogram(nostd::string_view name,
126-
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
127-
*instrumentation_library,
128-
MeasurementProcessor *measurement_processor,
129-
nostd::string_view description = "",
130-
nostd::string_view unit = "");
94+
DoubleHistogram(InstrumentDescriptor instrument_descriptor,
95+
std::unique_ptr<WritableMetricStorage> storage);
13196

13297
void Record(double value,
13398
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;

sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AttributesProcessor
2222
// Process the metric instrument attributes.
2323
// @returns The processed attributes
2424
virtual MetricAttributes process(
25-
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0;
25+
const opentelemetry::common::KeyValueIterable &attributes) const noexcept = 0;
2626
};
2727

2828
/**
@@ -33,7 +33,7 @@ class AttributesProcessor
3333
class DefaultAttributesProcessor : public AttributesProcessor
3434
{
3535
MetricAttributes process(
36-
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
36+
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
3737
{
3838
MetricAttributes result(attributes);
3939
return result;
@@ -54,7 +54,7 @@ class FilteringAttributesProcessor : public AttributesProcessor
5454
{}
5555

5656
MetricAttributes process(
57-
const opentelemetry::common::KeyValueIterable &attributes) noexcept override
57+
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
5858
{
5959
MetricAttributes result;
6060
attributes.ForEachKeyValue(

sdk/include/opentelemetry/sdk/metrics/view/view.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef ENABLE_METRICS_PREVIEW
66
# include "opentelemetry/nostd/string_view.h"
77
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
8+
# include "opentelemetry/sdk/metrics/instruments.h"
89
# include "opentelemetry/sdk/metrics/view/attributes_processor.h"
910

1011
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -21,26 +22,22 @@ class View
2122
{
2223
public:
2324
View(const std::string &name,
24-
const std::string &description = "",
25-
std::unique_ptr<opentelemetry::sdk::metrics::Aggregation> aggregation =
26-
std::unique_ptr<opentelemetry::sdk::metrics::Aggregation>(new DropAggregation()),
25+
const std::string &description = "",
26+
AggregationType aggregation_type = AggregationType::kDrop,
2727
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor =
2828
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor>(
2929
new opentelemetry::sdk::metrics::DefaultAttributesProcessor()))
3030
: name_(name),
3131
description_(description),
32-
aggregation_{std::move(aggregation)},
32+
aggregation_type_{aggregation_type},
3333
attributes_processor_{std::move(attributes_processor)}
3434
{}
3535

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

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

40-
virtual const opentelemetry::sdk::metrics::Aggregation &GetAggregation() const noexcept
41-
{
42-
return *aggregation_.get();
43-
}
40+
virtual AggregationType GetAggregationType() const noexcept { return aggregation_type_; }
4441

4542
virtual const opentelemetry::sdk::metrics::AttributesProcessor &GetAttributesProcessor()
4643
const noexcept
@@ -51,7 +48,7 @@ class View
5148
private:
5249
std::string name_;
5350
std::string description_;
54-
std::unique_ptr<opentelemetry::sdk::metrics::Aggregation> aggregation_;
51+
AggregationType aggregation_type_;
5552
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor_;
5653
};
5754
} // namespace metrics

0 commit comments

Comments
 (0)