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

Add InstrumentationInfo and Resource to the metrics data to be exported. #1299

Merged
merged 8 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
53 changes: 15 additions & 38 deletions sdk/include/opentelemetry/sdk/metrics/async_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# include "opentelemetry/metrics/async_instruments.h"
# include "opentelemetry/metrics/observer_result.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/metrics/instruments.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
Expand All @@ -19,22 +18,14 @@ class Asynchronous
{
public:
Asynchronous(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<T> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: name_(name),
instrumentation_library_{instrumentation_library},
callback_(callback),
description_(description),
unit_(unit)
: name_(name), callback_(callback), description_(description), unit_(unit)
{}

protected:
std::string name_;
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_;
void (*callback_)(opentelemetry::metrics::ObserverResult<T> &);
std::string description_;
std::string unit_;
Expand All @@ -45,12 +36,10 @@ class LongObservableCounter : public opentelemetry::metrics::ObservableCounter<l
{
public:
LongObservableCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, instrumentation_library, callback, description, unit)
: Asynchronous(name, callback, description, unit)

{}
};
Expand All @@ -60,12 +49,10 @@ class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter
{
public:
DoubleObservableCounter(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, instrumentation_library, callback, description, unit)
: Asynchronous(name, callback, description, unit)

{}
};
Expand All @@ -75,12 +62,10 @@ class LongObservableGauge : public opentelemetry::metrics::ObservableGauge<long>
{
public:
LongObservableGauge(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, instrumentation_library, callback, description, unit)
: Asynchronous(name, callback, description, unit)

{}
};
Expand All @@ -90,12 +75,10 @@ class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge<dou
{
public:
DoubleObservableGauge(nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, instrumentation_library, callback, description, unit)
: Asynchronous(name, callback, description, unit)

{}
};
Expand All @@ -104,14 +87,11 @@ class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpD
public Asynchronous<long>
{
public:
LongObservableUpDownCounter(
nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, instrumentation_library, callback, description, unit)
LongObservableUpDownCounter(nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, callback, description, unit)

{}
};
Expand All @@ -121,14 +101,11 @@ class DoubleObservableUpDownCounter
public Asynchronous<double>
{
public:
DoubleObservableUpDownCounter(
nostd::string_view name,
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, instrumentation_library, callback, description, unit)
DoubleObservableUpDownCounter(nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "")
: Asynchronous(name, callback, description, unit)
{}
};

Expand Down
17 changes: 16 additions & 1 deletion sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/metrics/data/metric_data.h"
# include "opentelemetry/sdk/resource/resource.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

/**
* Metric Data to be exported along with resources and
* Instrumentation library.
*/
struct ResourceMetrics
{
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library;
const opentelemetry::sdk::resource::Resource *resource;
MetricData metric_data;
};

/**
* MetricProducer is the interface that is used to make metric data available to the
* OpenTelemetry exporters. Implementations should be stateful, in that each call to
Expand All @@ -27,7 +41,8 @@ class MetricProducer
*
* @return a status of completion of method.
*/
virtual bool Collect(nostd::function_ref<bool(MetricData)> callback) noexcept = 0;
virtual bool Collect(
nostd::function_ref<bool(ResourceMetrics &&metric_data)> callback) noexcept = 0;
};

} // namespace metrics
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Meter final : public opentelemetry::metrics::Meter
/** collect metrics across all the meters **/
bool Collect(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp collect_ts,
nostd::function_ref<bool(MetricData &)> callback) noexcept;
nostd::function_ref<bool(MetricData &&)> callback) noexcept;

private:
// order of declaration is important here - instrumentation library should destroy after
Expand Down
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/metrics/metric_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/sdk/common/global_log_handler.h"
# include "opentelemetry/sdk/metrics/data/metric_data.h"
# include "opentelemetry/sdk/metrics/export/metric_producer.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/version.h"

Expand All @@ -18,7 +19,6 @@ namespace sdk
namespace metrics
{

class MetricProducer;
/**
* MetricReader defines the interface to collect metrics from SDK
*/
Expand All @@ -34,7 +34,7 @@ class MetricReader
* Collect the metrics from SDK.
* @return return the status of the operation.
*/
bool Collect(nostd::function_ref<bool(MetricData)> callback) noexcept;
bool Collect(nostd::function_ref<bool(ResourceMetrics &&metric_data)> callback) noexcept;

AggregationTemporality GetAggregationTemporality() const noexcept;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/common/attributemap_hash.h"
# include "opentelemetry/sdk/metrics/instruments.h"

# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/observer_result.h"
# include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
# include "opentelemetry/sdk/metrics/state/metric_collector.h"
# include "opentelemetry/sdk/metrics/state/metric_storage.h"
# include "opentelemetry/sdk/metrics/view/attributes_processor.h"

# include <memory>
# include "opentelemetry/sdk/metrics/observer_result.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
Expand All @@ -36,11 +35,12 @@ class AsyncMetricStorage : public MetricStorage
active_attributes_hashmap_(new AttributesHashMap())
{}

bool Collect(CollectorHandle *collector,
nostd::span<std::shared_ptr<CollectorHandle>> collectors,
opentelemetry::common::SystemTimestamp sdk_start_ts,
opentelemetry::common::SystemTimestamp collection_ts,
nostd::function_ref<bool(MetricData &)> metric_collection_callback) noexcept override
bool Collect(
CollectorHandle *collector,
nostd::span<std::shared_ptr<CollectorHandle>> collectors,
opentelemetry::common::SystemTimestamp sdk_start_ts,
opentelemetry::common::SystemTimestamp collection_ts,
nostd::function_ref<bool(MetricData &&)> metric_collection_callback) noexcept override
{
opentelemetry::sdk::metrics::ObserverResult<T> ob_res(attributes_processor_);

Expand All @@ -57,7 +57,7 @@ class AsyncMetricStorage : public MetricStorage

// TBD -> read aggregation from hashmap, and perform metric collection
MetricData metric_data;
if (metric_collection_callback(metric_data))
if (metric_collection_callback(std::move(metric_data)))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MetricCollector : public MetricProducer, public CollectorHandle
*
* @return a status of completion of method.
*/
bool Collect(nostd::function_ref<bool(MetricData)> callback) noexcept override;
bool Collect(nostd::function_ref<bool(ResourceMetrics &&metric_data)> callback) noexcept override;

bool ForceFlush(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept;

Expand Down
10 changes: 3 additions & 7 deletions sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MetricStorage
nostd::span<std::shared_ptr<CollectorHandle>> collectors,
opentelemetry::common::SystemTimestamp sdk_start_ts,
opentelemetry::common::SystemTimestamp collection_ts,
nostd::function_ref<bool(MetricData &)> callback) noexcept = 0;
nostd::function_ref<bool(MetricData &&)> callback) noexcept = 0;
};

class WritableMetricStorage
Expand Down Expand Up @@ -54,14 +54,10 @@ class NoopMetricStorage : public MetricStorage
nostd::span<std::shared_ptr<CollectorHandle>> collectors,
opentelemetry::common::SystemTimestamp sdk_start_ts,
opentelemetry::common::SystemTimestamp collection_ts,
nostd::function_ref<bool(MetricData &)> callback) noexcept override
nostd::function_ref<bool(MetricData &&)> callback) noexcept override
{
MetricData metric_data;
if (callback(metric_data))
{
return true;
}
return false;
return callback(std::move(metric_data));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage
nostd::span<std::shared_ptr<CollectorHandle>> collectors,
opentelemetry::common::SystemTimestamp sdk_start_ts,
opentelemetry::common::SystemTimestamp collection_ts,
nostd::function_ref<bool(MetricData &)> callback) noexcept override;
nostd::function_ref<bool(MetricData &&)> callback) noexcept override;

private:
InstrumentDescriptor instrument_descriptor_;
Expand Down
26 changes: 11 additions & 15 deletions sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ nostd::shared_ptr<metrics::ObservableCounter<long>> Meter::CreateLongObservableC
nostd::string_view unit) noexcept
{
return nostd::shared_ptr<metrics::ObservableCounter<long>>{
new LongObservableCounter(name, GetInstrumentationLibrary(), callback, description, unit)};
new LongObservableCounter(name, callback, description, unit)};
}

nostd::shared_ptr<metrics::ObservableCounter<double>> Meter::CreateDoubleObservableCounter(
Expand All @@ -74,7 +74,7 @@ nostd::shared_ptr<metrics::ObservableCounter<double>> Meter::CreateDoubleObserva
nostd::string_view unit) noexcept
{
return nostd::shared_ptr<metrics::ObservableCounter<double>>{
new DoubleObservableCounter(name, GetInstrumentationLibrary(), callback, description, unit)};
new DoubleObservableCounter(name, callback, description, unit)};
}

nostd::shared_ptr<metrics::Histogram<long>> Meter::CreateLongHistogram(
Expand Down Expand Up @@ -112,7 +112,7 @@ nostd::shared_ptr<metrics::ObservableGauge<long>> Meter::CreateLongObservableGau
nostd::string_view unit) noexcept
{
return nostd::shared_ptr<metrics::ObservableGauge<long>>{
new LongObservableGauge(name, GetInstrumentationLibrary(), callback, description, unit)};
new LongObservableGauge(name, callback, description, unit)};
}

nostd::shared_ptr<metrics::ObservableGauge<double>> Meter::CreateDoubleObservableGauge(
Expand All @@ -122,7 +122,7 @@ nostd::shared_ptr<metrics::ObservableGauge<double>> Meter::CreateDoubleObservabl
nostd::string_view unit) noexcept
{
return nostd::shared_ptr<metrics::ObservableGauge<double>>{
new DoubleObservableGauge(name, GetInstrumentationLibrary(), callback, description, unit)};
new DoubleObservableGauge(name, callback, description, unit)};
}

nostd::shared_ptr<metrics::UpDownCounter<long>> Meter::CreateLongUpDownCounter(
Expand Down Expand Up @@ -159,8 +159,8 @@ nostd::shared_ptr<metrics::ObservableUpDownCounter<long>> Meter::CreateLongObser
nostd::string_view description,
nostd::string_view unit) noexcept
{
return nostd::shared_ptr<metrics::ObservableUpDownCounter<long>>{new LongObservableUpDownCounter(
name, GetInstrumentationLibrary(), callback, description, unit)};
return nostd::shared_ptr<metrics::ObservableUpDownCounter<long>>{
new LongObservableUpDownCounter(name, callback, description, unit)};
}

nostd::shared_ptr<metrics::ObservableUpDownCounter<double>>
Expand All @@ -170,8 +170,7 @@ Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name,
nostd::string_view unit) noexcept
{
return nostd::shared_ptr<metrics::ObservableUpDownCounter<double>>{
new DoubleObservableUpDownCounter(name, GetInstrumentationLibrary(), callback, description,
unit)};
new DoubleObservableUpDownCounter(name, callback, description, unit)};
}

const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary()
Expand Down Expand Up @@ -213,18 +212,15 @@ std::unique_ptr<WritableMetricStorage> Meter::RegisterMetricStorage(
/** collect metrics across all the meters **/
bool Meter::Collect(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp collect_ts,
nostd::function_ref<bool(MetricData &)> callback) noexcept
nostd::function_ref<bool(MetricData &&)> callback) noexcept
{
std::vector<MetricData> data;
for (auto &metric_storage : storage_registry_)
{
// TBD - this needs to be asynchronous
metric_storage.second->Collect(collector, meter_context_->GetCollectors(),
meter_context_->GetSDKStartTime(), collect_ts,
[&callback](MetricData &metric_data) {
callback(metric_data);
return true;
});
metric_storage.second->Collect(
collector, meter_context_->GetCollectors(), meter_context_->GetSDKStartTime(), collect_ts,
[&callback](MetricData &&metric_data) { return callback(std::move(metric_data)); });
Copy link
Contributor

Choose a reason for hiding this comment

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

Would returning false from one callback stop the collection process?

Copy link
Member Author

Choose a reason for hiding this comment

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

There can be multiple instruments configured for a Meter. This will fail the collection for the instrument for which the callback failed, and the rest of the instrument collection would continue.

}
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/metrics/metric_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ AggregationTemporality MetricReader::GetAggregationTemporality() const noexcept
return aggregation_temporality_;
}

bool MetricReader::Collect(nostd::function_ref<bool(MetricData)> callback) noexcept
bool MetricReader::Collect(
nostd::function_ref<bool(ResourceMetrics &&metric_data)> callback) noexcept
{
if (!metric_producer_)
{
Expand Down
9 changes: 7 additions & 2 deletions sdk/src/metrics/state/metric_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ AggregationTemporality MetricCollector::GetAggregationTemporality() noexcept
return metric_reader_->GetAggregationTemporality();
}

bool MetricCollector::Collect(nostd::function_ref<bool(MetricData)> callback) noexcept
bool MetricCollector::Collect(
nostd::function_ref<bool(ResourceMetrics &&metric_data)> callback) noexcept
{
for (auto &meter : meter_context_->GetMeters())
{
auto collection_ts = std::chrono::system_clock::now();
meter->Collect(this, collection_ts, callback);
meter->Collect(this, collection_ts, [&](MetricData &&metric_data) {
ResourceMetrics resource_metrics{meter->GetInstrumentationLibrary(),
&meter_context_->GetResource(), std::move(metric_data)};
return callback(std::move(resource_metrics));
});
}
return true;
}
Expand Down
Loading