Program Listing for File meter_context.h
↰ Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/opentelemetry-cpp/checkouts/stable/sdk/include/opentelemetry/sdk/metrics/meter_context.h
)
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <atomic>
#include <chrono>
#include <memory>
#include <vector>
#include "opentelemetry/common/spin_lock_mutex.h"
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/sdk/metrics/view/view_registry.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
// forward declaration
class CollectorHandle;
class InstrumentSelector;
class Meter;
class MetricReader;
class MeterSelector;
class MeterContext : public std::enable_shared_from_this<MeterContext>
{
public:
MeterContext(
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::Create({})) noexcept;
const opentelemetry::sdk::resource::Resource &GetResource() const noexcept;
ViewRegistry *GetViewRegistry() const noexcept;
bool ForEachMeter(nostd::function_ref<bool(std::shared_ptr<Meter> &meter)> callback) noexcept;
nostd::span<std::shared_ptr<Meter>> GetMeters() noexcept;
nostd::span<std::shared_ptr<CollectorHandle>> GetCollectors() noexcept;
opentelemetry::common::SystemTimestamp GetSDKStartTime() noexcept;
void AddMetricReader(std::shared_ptr<MetricReader> reader) noexcept;
void AddView(std::unique_ptr<InstrumentSelector> instrument_selector,
std::unique_ptr<MeterSelector> meter_selector,
std::unique_ptr<View> view) noexcept;
void AddMeter(std::shared_ptr<Meter> meter);
void RemoveMeter(nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url);
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
bool Shutdown() noexcept;
private:
opentelemetry::sdk::resource::Resource resource_;
std::vector<std::shared_ptr<CollectorHandle>> collectors_;
std::unique_ptr<ViewRegistry> views_;
opentelemetry::common::SystemTimestamp sdk_start_ts_;
std::vector<std::shared_ptr<Meter>> meters_;
#if defined(__cpp_lib_atomic_value_initialization) && \
__cpp_lib_atomic_value_initialization >= 201911L
std::atomic_flag shutdown_latch_{};
#else
std::atomic_flag shutdown_latch_ = ATOMIC_FLAG_INIT;
#endif
opentelemetry::common::SpinLockMutex forceflush_lock_;
opentelemetry::common::SpinLockMutex meter_lock_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE