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
24 changes: 12 additions & 12 deletions api/include/opentelemetry/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class Meter
virtual nostd::shared_ptr<Counter<long>> CreateLongCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;

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

/**
* Creates a Asynchronouse (Observable) counter with the passed characteristics and returns a
Expand All @@ -60,13 +60,13 @@ class Meter
nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;

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

/**
* Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram.
Expand All @@ -79,12 +79,12 @@ class Meter
virtual nostd::shared_ptr<Histogram<long>> CreateLongHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;

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

/**
* Creates a Asynchronouse (Observable) Gauge with the passed characteristics and returns a
Expand All @@ -100,13 +100,13 @@ class Meter
nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;

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

/**
* Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that
Expand All @@ -120,12 +120,12 @@ class Meter
virtual nostd::shared_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;

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

/**
* Creates a Asynchronouse (Observable) UpDownCounter with the passed characteristics and returns
Expand All @@ -141,13 +141,13 @@ class Meter
nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<ObservableUpDownCounter<double>> CreateDoubleObservableUpDownCounter(
nostd::string_view name,
void (*callback)(ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "1") noexcept = 0;
nostd::string_view unit = "") noexcept = 0;
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
Expand Down
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 = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Counter<double>> CreateDoubleCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") 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 = "") 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 = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Histogram<double>> CreateDoubleHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") 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 = "") 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 = "") noexcept override;

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

nostd::shared_ptr<opentelemetry::metrics::UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") 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 = "") 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 = "") 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/metric_exporter.h"
# include "opentelemetry/sdk/metrics/metric_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::MetricExporter>> &&exporters,
std::vector<std::unique_ptr<MetricReader>> &&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 metric exporter to list of configured exporters for this Meter context.
* @param exporter The metric 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 AddMetricExporter(std::unique_ptr<MetricExporter> exporter) noexcept;

/**
* Attaches a metric reader to list of configured readers for this Meter context.
* @param reader The metric 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 AddMetricReader(std::unique_ptr<MetricReader> 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<MetricExporter>> exporters_;
std::vector<std::unique_ptr<MetricReader>> readers_;
std::vector<std::unique_ptr<View>> views_;
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif
Loading