Skip to content

Commit

Permalink
Add InstrumentationInfo and Resource to the metrics data to be export…
Browse files Browse the repository at this point in the history
…ed. (#1299)
  • Loading branch information
lalitb authored Apr 4, 2022
1 parent 48a4060 commit be75bbc
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 106 deletions.
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
23 changes: 22 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,33 @@

#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 InstrumentationInfoMetrics
{
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_;
std::vector<MetricData> metric_data_;
};

struct ResourceMetrics
{
const opentelemetry::sdk::resource::Resource *resource_;
std::vector<InstrumentationInfoMetrics> instrumentation_info_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 +47,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
7 changes: 3 additions & 4 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ class Meter final : public opentelemetry::metrics::Meter
const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary()
const noexcept;

/** collect metrics across all the meters **/
bool Collect(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp collect_ts,
nostd::function_ref<bool(MetricData &)> callback) noexcept;
/** collect metrics across all the instruments configured for the meter **/
std::vector<MetricData> Collect(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp collect_ts) noexcept;

private:
// order of declaration is important here - instrumentation library should destroy after
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/metrics/metric_exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MetricExporter
* concurrently for the same exporter instance.
* @param data metrics data
*/
virtual opentelemetry::sdk::common::ExportResult Export(const MetricData &data) noexcept = 0;
virtual opentelemetry::sdk::common::ExportResult Export(const ResourceMetrics &data) noexcept = 0;

/**
* Force flush the exporter.
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 @@ -40,7 +39,7 @@ class AsyncMetricStorage : 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 &)> metric_collection_callback) noexcept override
nostd::function_ref<bool(MetricData)> metric_collection_callback) noexcept override
{
opentelemetry::sdk::metrics::ObserverResult<T> ob_res(attributes_processor_);

Expand All @@ -57,7 +56,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
4 changes: 2 additions & 2 deletions sdk/src/metrics/export/periodic_exporting_metric_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ void PeriodicExportingMetricReader::DoBackgroundWork()
std::atomic<bool> cancel_export_for_timeout{false};
auto start = std::chrono::steady_clock::now();
auto future_receive = std::async(std::launch::async, [this, &cancel_export_for_timeout] {
Collect([this, &cancel_export_for_timeout](MetricData data) {
Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) {
if (cancel_export_for_timeout)
{
OTEL_INTERNAL_LOG_ERROR(
"[Periodic Exporting Metric Reader] Collect took longer configured time: "
<< export_timeout_millis_.count() << " ms, and timed out");
return false;
}
this->exporter_->Export(data);
this->exporter_->Export(metric_data);
return true;
});
});
Expand Down
Loading

1 comment on commit be75bbc

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: be75bbc Previous: 48a4060 Ratio
BM_ProcYieldSpinLockThrashing/1/process_time/real_time 0.23892608848777977 ms/iter 0.10786544409491938 ms/iter 2.22

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.