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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ 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)

Important changes:

* [REMOVAL] Remove the jaeger exporter
[#2031](https://github.com/open-telemetry/opentelemetry-cpp/pull/2031)
* The CMake `WITH_JAEGER` option has been removed
* Please remove usage of `WITH_JAEGER` from user scripts and makefiles.

* [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.

## [1.9.1] 2023-05-26

* [DEPRECATION] Drop C++11 support
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
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
21 changes: 21 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,26 @@ 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))
{
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 @@ -57,6 +57,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