Skip to content

Commit

Permalink
Proof of concept.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Jul 7, 2023
1 parent d6cebe1 commit fdbef73
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 13 deletions.
14 changes: 14 additions & 0 deletions api/include/opentelemetry/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace common
{
class KeyValueIterable;
};

namespace metrics
{

Expand All @@ -26,9 +31,18 @@ class MeterProvider
* Optionally a version can be passed to create a named and versioned Meter
* instance.
*/

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
virtual nostd::shared_ptr<Meter> GetMeter(
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "",
const common::KeyValueIterable *attributes = nullptr) noexcept = 0;
#else
virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") noexcept = 0;
#endif
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
11 changes: 11 additions & 0 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,23 @@ class NoopMeterProvider final : public MeterProvider
public:
NoopMeterProvider() : meter_{nostd::shared_ptr<Meter>(new NoopMeter)} {}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<Meter> GetMeter(
nostd::string_view /* library_name */,
nostd::string_view /* library_version */,
nostd::string_view /* schema_url */,
const common::KeyValueIterable * /* attributes */) noexcept override
{
return meter_;
}
#else
nostd::shared_ptr<Meter> GetMeter(nostd::string_view /* library_name */,
nostd::string_view /* library_version */,
nostd::string_view /* schema_url */) noexcept override
{
return meter_;
}
#endif

private:
nostd::shared_ptr<Meter> meter_;
Expand Down
3 changes: 2 additions & 1 deletion api/include/opentelemetry/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "opentelemetry/common/macros.h"
#include "opentelemetry/detail/preprocessor.h"

#define OPENTELEMETRY_ABI_VERSION_NO 1
// For proof of concept only, testing version 2 build.
#define OPENTELEMETRY_ABI_VERSION_NO 2
#define OPENTELEMETRY_VERSION "1.9.1"
#define OPENTELEMETRY_VERSION_MAJOR 1
#define OPENTELEMETRY_VERSION_MINOR 9
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
3 changes: 2 additions & 1 deletion sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Meter final : public opentelemetry::metrics::Meter
explicit Meter(
std::weak_ptr<sdk::metrics::MeterContext> meter_context,
std::unique_ptr<opentelemetry::sdk::instrumentationscope::InstrumentationScope> scope =
opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("")) noexcept;
opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(""),
const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept;

nostd::unique_ptr<opentelemetry::metrics::Counter<uint64_t>> CreateUInt64Counter(
nostd::string_view name,
Expand Down
8 changes: 8 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider
*/
explicit MeterProvider(std::unique_ptr<MeterContext> context) noexcept;

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "",
const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept override;
#else
nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "") noexcept override;
#endif

/**
* Obtain the resource associated with this meter provider.
Expand Down
10 changes: 6 additions & 4 deletions sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ namespace metrics
namespace metrics = opentelemetry::metrics;
namespace nostd = opentelemetry::nostd;

Meter::Meter(
std::weak_ptr<MeterContext> meter_context,
std::unique_ptr<sdk::instrumentationscope::InstrumentationScope> instrumentation_scope) noexcept
Meter::Meter(std::weak_ptr<MeterContext> meter_context,
std::unique_ptr<sdk::instrumentationscope::InstrumentationScope> instrumentation_scope,
const opentelemetry::common::KeyValueIterable *attributes) noexcept
: scope_{std::move(instrumentation_scope)},
meter_context_{meter_context},
observable_registry_(new ObservableRegistry())
{}
{
// FIXME: use attributes
}

nostd::unique_ptr<metrics::Counter<uint64_t>> Meter::CreateUInt64Counter(
nostd::string_view name,
Expand Down
13 changes: 11 additions & 2 deletions sdk/src/metrics/meter_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ MeterProvider::MeterProvider(std::unique_ptr<ViewRegistry> views,
nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url) noexcept
nostd::string_view schema_url
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
,
const opentelemetry::common::KeyValueIterable *attributes
#endif
) noexcept
{
#if OPENTELEMETRY_ABI_VERSION_NO < 2
const opentelemetry::common::KeyValueIterable *attributes = nullptr;
#endif

if (name.data() == nullptr || name == "")
{
OTEL_INTERNAL_LOG_WARN("[MeterProvider::GetMeter] Library name is empty.");
Expand All @@ -54,7 +63,7 @@ nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
}
}
auto lib = instrumentationscope::InstrumentationScope::Create(name, version, schema_url);
auto meter = std::shared_ptr<Meter>(new Meter(context_, std::move(lib)));
auto meter = std::shared_ptr<Meter>(new Meter(context_, std::move(lib), attributes));
context_->AddMeter(meter);
return nostd::shared_ptr<metrics_api::Meter>{meter};
}
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

0 comments on commit fdbef73

Please sign in to comment.