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 Meter and MeterProvider in the SDK #1078

Merged
merged 15 commits into from
Nov 30, 2021
12 changes: 6 additions & 6 deletions sdk/include/opentelemetry/sdk/common/global_log_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ OPENTELEMETRY_END_NAMESPACE
#endif

#if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_WARN
# define OTEL_INTERNAL_LOG_WARN_1_ARGS(message) \
OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warn, message, \
{})
# define OTEL_INTERNAL_LOG_WARN_2_ARGS(message, attributes) \
OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warn, message, \
attributes)
# define OTEL_INTERNAL_LOG_WARN_1_ARGS(message) \
OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warning, \
message, {})
# define OTEL_INTERNAL_LOG_WARN_2_ARGS(message, attributes) \
OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warning, \
message, attributes)
# define OTEL_INTERNAL_LOG_WARN_MACRO(...) \
OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_WARN_2_ARGS, \
OTEL_INTERNAL_LOG_WARN_1_ARGS)
Expand Down
109 changes: 109 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <chrono>
# include "opentelemetry/metrics/meter.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/metrics/meter_context.h"
# include "opentelemetry/sdk/resource/resource.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
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,
std::unique_ptr<opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary>
instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create(
"")) noexcept;

nostd::shared_ptr<opentelemetry::metrics::Counter<long>> CreateLongCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;
reyang marked this conversation as resolved.
Show resolved Hide resolved

nostd::shared_ptr<opentelemetry::metrics::Counter<double>> CreateDoubleCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::ObservableCounter<long>> CreateLongObservableCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::ObservableCounter<double>>
CreateDoubleObservableCounter(nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Histogram<long>> CreateLongHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Histogram<double>> CreateDoubleHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::ObservableGauge<long>> CreateLongObservableGauge(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::ObservableGauge<double>> CreateDoubleObservableGauge(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::UpDownCounter<long>> CreateLongUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::ObservableUpDownCounter<long>>
CreateLongObservableUpDownCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::ObservableUpDownCounter<double>>
CreateDoubleObservableUpDownCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept override;

/** Returns the associated instruementation library */
const sdk::instrumentationlibrary::InstrumentationLibrary &GetInstrumentationLibrary()
const noexcept;

private:
// order of declaration is important here - instrumentation library should destroy after
// meter-context.
std::shared_ptr<sdk::instrumentationlibrary::InstrumentationLibrary> instrumentation_library_;
std::shared_ptr<sdk::metrics::MeterContext> context_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
98 changes: 98 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <chrono>
# include <memory>
# include <vector>
# include "opentelemetry/sdk/metrics/meter_exporter.h"
# include "opentelemetry/sdk/metrics/meter_reader.h"
# include "opentelemetry/sdk/metrics/view.h"
# include "opentelemetry/sdk/resource/resource.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
/**
* A class which stores the MeterProvider context.

*/
class MeterContext
{
public:
/**
* Initialize a new meter provider
* @param exporters The exporters to be configured with meter context
* @param readers The readers to be configured with meter context.
* @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::MeterExporter>> &&exporters,
std::vector<std::unique_ptr<MeterReader>> &&readers,
std::vector<std::unique_ptr<View>> &&views,
opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::Create({})) noexcept;

/**
* Obtain the resource associated with this meter context.
* @return The resource for this meter context
*/
const opentelemetry::sdk::resource::Resource &GetResource() const noexcept;

/**
* Attaches a meter exporter to list of configured exporters for this Meter context.
* @param exporter The meter exporter for this meter context. This
* must not be a nullptr.
*
* Note: This exporter 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 AddMeterExporter(std::unique_ptr<MeterExporter> exporter) noexcept;

/**
* Attaches a meter reader to list of configured readers for this Meter context.
* @param reader The meter reader for this meter context. This
* must not be a nullptr.
*
* Note: This reader 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 AddMeterReader(std::unique_ptr<MeterReader> reader) noexcept;

/**
* Attaches a View to list of configured Views for this Meter context.
* @param view The Views for this meter context. This
* must not be a nullptr.
*
* 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;

/**
* Force all active Exporters and Readers to flush any buffered meter data
* within the given timeout.
*/

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

/**
* Shutdown the Exporters and Readers associated with this meter provider.
*/
bool Shutdown() noexcept;

private:
opentelemetry::sdk::resource::Resource resource_;
std::vector<std::unique_ptr<MeterExporter>> exporters_;
std::vector<std::unique_ptr<MeterReader>> readers_;
std::vector<std::unique_ptr<View>> views_;
};

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

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <chrono>
# include "opentelemetry/sdk/common/exporter_utils.h"
# include "opentelemetry/sdk/metrics/recordable.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
/**
* MeterExporter defines the interface to be used by metrics libraries to
* push metrics data to the OpenTelemetry exporters.
*/
class MeterExporter
{
public:
virtual ~MeterExporter() = default;

/**
* Exports a batch of metrics recordables. This method must not be called
* concurrently for the same exporter instance.
* @param spans a span of unique pointers to metrics recordables
*/
virtual opentelemetry::sdk::common::ExportResult Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::metrics::Recordable>>
&spans) noexcept = 0;

/**
* Force flush the meter exporter.
*/
virtual bool ForceFlush(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;

/**
* Shut down the metric exporter.
* @param timeout an optional timeout.
* @return return the status of the operation.
*/
virtual bool Shutdown() noexcept = 0;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
102 changes: 102 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <memory>
# include <mutex>
# include <vector>
# include "opentelemetry/metrics/meter_provider.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/sdk/metrics/meter.h"
# include "opentelemetry/sdk/metrics/meter_context.h"
# include "opentelemetry/sdk/resource/resource.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class MeterProvider final : public opentelemetry::metrics::MeterProvider
{
public:
/**
* Initialize a new meter provider
* @param exporters The span exporters for this meter provider
* @param readers The readers for this meter provider.
* @param views The views for this meter provider
* @param resource The resources for this meter provider.
*/
MeterProvider(std::vector<std::unique_ptr<MeterExporter>> &&exporters,
std::vector<std::unique_ptr<MeterReader>> &&readers,
std::vector<std::unique_ptr<View>> &&views,
sdk::resource::Resource resource = sdk::resource::Resource::Create({})) noexcept;

/**
* Initialize a new meter provider with a specified context
* @param context The shared meter configuration/pipeline for this provider.
*/
explicit MeterProvider(std::shared_ptr<sdk::metrics::MeterContext> context) noexcept;

nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::string_view library_name,
reyang marked this conversation as resolved.
Show resolved Hide resolved
nostd::string_view library_version = "",
nostd::string_view schema_url = "") noexcept override;

/**
* Obtain the resource associated with this meter provider.
* @return The resource for this meter provider.
*/
const sdk::resource::Resource &GetResource() const noexcept;

/**
* Attaches a meter exporter to list of configured exporters for this Meter provider.
* @param exporter The meter exporter for this meter provider. This
* must not be a nullptr.
*
* Note: This exporter 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 AddMeterExporter(std::unique_ptr<MeterExporter> exporter) noexcept;

/**
* Attaches a meter reader to list of configured readers for this Meter providers.
* @param reader The meter reader for this meter provider. This
* must not be a nullptr.
*
* Note: This reader 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 AddMeterReader(std::unique_ptr<MeterReader> reader) noexcept;

/**
* Attaches a View to list of configured Views for this Meter provider.
* @param view The Views for this meter provider. This
* must not be a nullptr.
*
* 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;

/**
* Shutdown the meter provider.
*/
bool Shutdown() noexcept;

/**
* Force flush the meter provider.
*/
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;

private:
// // order of declaration is important here - meter should destroy only after resource.
std::vector<std::shared_ptr<sdk::metrics::Meter>> meters_;
std::shared_ptr<sdk::metrics::MeterContext> context_;
std::mutex lock_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Loading