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

[API] Remove Meters #2205

Merged
merged 12 commits into from
Jul 10, 2023
Merged
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ Increment the:
* [API] Remove include_trace_context
[#2194](https://github.com/open-telemetry/opentelemetry-cpp/pull/2194)

* [API] Remove Meters
[#2205](https://github.com/open-telemetry/opentelemetry-cpp/pull/2205)

* [SDK] MeterProvider should own MeterContext, not share it
[#2218](https://github.com/open-telemetry/opentelemetry-cpp/pull/2218)

* [SDK] TracerProvider should own TracerContext, not share it
[#2221](https://github.com/open-telemetry/opentelemetry-cpp/pull/2221)

Important changes:

* [API] Remove Meters
[#2205](https://github.com/open-telemetry/opentelemetry-cpp/pull/2205)
* The CMake option `WITH_REMOVE_METER_PREVIEW` was added.
* This option is experimental, and may change in the future.
* Enabling it is an ABI breaking change.

Breaking changes:

* [REMOVAL] Remove the jaeger exporter
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ option(WITH_OPENTRACING "Whether to include the Opentracing shim" OFF)
option(OTELCPP_VERSIONED_LIBS "Whether to generate the versioned shared libs"
OFF)

#
# This option is experimental, subject to change in the spec:
#
# * https://github.com/open-telemetry/opentelemetry-specification/issues/2232
#
option(WITH_REMOVE_METER_PREVIEW
"EXPERIMENTAL, ABI BREAKING: Allow to remove a meter" OFF)

if(OTELCPP_VERSIONED_LIBS AND NOT BUILD_SHARED_LIBS)
message(FATAL_ERROR "OTELCPP_VERSIONED_LIBS=ON requires BUILD_SHARED_LIBS=ON")
endif()
Expand Down
5 changes: 5 additions & 0 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ if(WITH_ASYNC_EXPORT_PREVIEW)
target_compile_definitions(opentelemetry_api INTERFACE ENABLE_ASYNC_EXPORT)
endif()

if(WITH_REMOVE_METER_PREVIEW)
target_compile_definitions(opentelemetry_api
INTERFACE ENABLE_REMOVE_METER_PREVIEW)
endif()

# A better place should be in sdk, not api
if(WITH_OTLP_HTTP_SSL_PREVIEW)
target_compile_definitions(opentelemetry_api
Expand Down
5 changes: 5 additions & 0 deletions api/include/opentelemetry/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class MeterProvider
virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") noexcept = 0;
#ifdef ENABLE_REMOVE_METER_PREVIEW
virtual void RemoveMeter(nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") noexcept = 0;
#endif
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
7 changes: 7 additions & 0 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ class NoopMeterProvider final : public MeterProvider
return meter_;
}

#ifdef ENABLE_REMOVE_METER_PREVIEW
void RemoveMeter(nostd::string_view /* name */,
nostd::string_view /* version */,
nostd::string_view /* schema_url */) noexcept override
{}
#endif

private:
nostd::shared_ptr<Meter> meter_;
};
Expand Down
3 changes: 3 additions & 0 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ elif [[ "$1" == "cmake.maintainer.sync.test" ]]; then
-DWITH_OTLP_HTTP=ON \
-DWITH_OTLP_HTTP_SSL_PREVIEW=ON \
-DWITH_OTLP_HTTP_SSL_TLS_PREVIEW=ON \
-DWITH_REMOVE_METER_PREVIEW=ON \
-DWITH_PROMETHEUS=ON \
-DWITH_EXAMPLES=ON \
-DWITH_EXAMPLES_HTTP=ON \
Expand All @@ -129,6 +130,7 @@ elif [[ "$1" == "cmake.maintainer.async.test" ]]; then
-DWITH_OTLP_HTTP=ON \
-DWITH_OTLP_HTTP_SSL_PREVIEW=ON \
-DWITH_OTLP_HTTP_SSL_TLS_PREVIEW=ON \
-DWITH_REMOVE_METER_PREVIEW=ON \
-DWITH_PROMETHEUS=ON \
-DWITH_EXAMPLES=ON \
-DWITH_EXAMPLES_HTTP=ON \
Expand All @@ -153,6 +155,7 @@ elif [[ "$1" == "cmake.maintainer.cpp11.async.test" ]]; then
-DWITH_OTLP_HTTP=ON \
-DWITH_OTLP_HTTP_SSL_PREVIEW=ON \
-DWITH_OTLP_HTTP_SSL_TLS_PREVIEW=ON \
-DWITH_REMOVE_METER_PREVIEW=ON \
-DWITH_PROMETHEUS=ON \
-DWITH_EXAMPLES=ON \
-DWITH_EXAMPLES_HTTP=ON \
Expand Down
6 changes: 3 additions & 3 deletions exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest)
std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> trace_stub_interface(
trace_mock_stub);

auto trace_provider = opentelemetry::nostd::shared_ptr<opentelemetry::v1::trace::TracerProvider>(
auto trace_provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
opentelemetry::sdk::trace::TracerProviderFactory::Create(
opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create(
GetExporter(trace_stub_interface))));
Expand All @@ -174,7 +174,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest)

auto logger = provider->GetLogger("test", "opentelelemtry_library", "", schema_url,
{{"scope_key1", "scope_value"}, {"scope_key2", 2}});
std::unordered_map<std::string, opentelemetry::v1::common::AttributeValue> attributes;
std::unordered_map<std::string, opentelemetry::common::AttributeValue> attributes;
attributes["service.name"] = "unit_test_service";
attributes["tenant.id"] = "test_user";
attributes["bool_value"] = true;
Expand All @@ -197,7 +197,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest)
opentelemetry::trace::Provider::SetTracerProvider(
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::trace::NoopTracerProvider()));
trace_provider = opentelemetry::nostd::shared_ptr<opentelemetry::v1::trace::TracerProvider>();
trace_provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>();
}

} // namespace otlp
Expand Down
4 changes: 4 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
*/
void AddMeter(std::shared_ptr<Meter> meter);

void RemoveMeter(nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url);

/**
* Force all active Collectors to flush any buffered meter data
* within the given timeout.
Expand Down
6 changes: 6 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider
nostd::string_view version = "",
nostd::string_view schema_url = "") noexcept override;

#ifdef ENABLE_REMOVE_METER_PREVIEW
void RemoveMeter(nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url) noexcept override;
#endif

/**
* Obtain the resource associated with this meter provider.
* @return The resource for this meter provider.
Expand Down
27 changes: 27 additions & 0 deletions sdk/src/metrics/meter_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "opentelemetry/sdk/metrics/meter_context.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/metric_reader.h"
#include "opentelemetry/sdk/metrics/state/metric_collector.h"
#include "opentelemetry/sdk_config.h"
Expand Down Expand Up @@ -79,6 +80,32 @@ void MeterContext::AddMeter(std::shared_ptr<Meter> meter)
meters_.push_back(meter);
}

void MeterContext::RemoveMeter(nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url)
{
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(meter_lock_);

std::vector<std::shared_ptr<Meter>> filtered_meters;

for (auto &meter : meters_)
{
auto scope = meter->GetInstrumentationScope();
if (scope->equal(name, version, schema_url))
{
OTEL_INTERNAL_LOG_INFO("[MeterContext::RemoveMeter] removing meter name <"
<< name << ">, version <" << version << ">, URL <" << schema_url
<< ">");
}
else
{
filtered_meters.push_back(meter);
}
}

meters_.swap(filtered_meters);
marcalff marked this conversation as resolved.
Show resolved Hide resolved
}

bool MeterContext::Shutdown() noexcept
{
bool result = true;
Expand Down
17 changes: 17 additions & 0 deletions sdk/src/metrics/meter_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
return nostd::shared_ptr<metrics_api::Meter>{meter};
}

#ifdef ENABLE_REMOVE_METER_PREVIEW
void MeterProvider::RemoveMeter(nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url) noexcept
{
if (name.data() == nullptr || name == "")
{
OTEL_INTERNAL_LOG_WARN("[MeterProvider::RemoveMeter] Library name is empty.");
name = "";
}

const std::lock_guard<std::mutex> guard(lock_);

context_->RemoveMeter(name, version, schema_url);
}
#endif

const resource::Resource &MeterProvider::GetResource() const noexcept
{
return context_->GetResource();
Expand Down
4 changes: 2 additions & 2 deletions sdk/test/logs/log_record_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ class TestBodyLogger : public opentelemetry::logs::Logger
}
}

const opentelemetry::v1::common::AttributeValue &GetLastLogRecord() const noexcept
const opentelemetry::common::AttributeValue &GetLastLogRecord() const noexcept
{
return last_body_;
}

private:
opentelemetry::v1::common::AttributeValue last_body_;
opentelemetry::common::AttributeValue last_body_;
};

// Define a basic LoggerProvider class that returns an instance of the logger class defined above
Expand Down
37 changes: 36 additions & 1 deletion sdk/test/metrics/meter_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ using namespace opentelemetry::sdk::metrics;

TEST(MeterProvider, GetMeter)
{

MeterProvider mp1;
// std::unique_ptr<View> view{std::unique_ptr<View>()};
// MeterProvider mp1(std::move(exporters), std::move(readers), std::move(views);
Expand Down Expand Up @@ -59,3 +58,39 @@ TEST(MeterProvider, GetMeter)
mp1.ForceFlush();
mp1.Shutdown();
}

#ifdef ENABLE_REMOVE_METER_PREVIEW
TEST(MeterProvider, RemoveMeter)
{
MeterProvider mp;

auto m1 = mp.GetMeter("test", "1", "URL");
ASSERT_NE(nullptr, m1);

// Will return the same meter
auto m2 = mp.GetMeter("test", "1", "URL");
ASSERT_NE(nullptr, m2);
ASSERT_EQ(m1, m2);

mp.RemoveMeter("unknown", "0", "");

// Will decrease use_count() on m1 and m2
mp.RemoveMeter("test", "1", "URL");

// Will create a different meter
auto m3 = mp.GetMeter("test", "1", "URL");
ASSERT_NE(nullptr, m3);
ASSERT_NE(m1, m3);
ASSERT_NE(m2, m3);

// Will decrease use_count() on m3
mp.RemoveMeter("test", "1", "URL");

// Will do nothing
mp.RemoveMeter("test", "1", "URL");

// cleanup properly without crash
mp.ForceFlush();
mp.Shutdown();
}
#endif