Skip to content

Commit

Permalink
Upgrade instrumentation_library_metrics to scope_metrics (gabime#…
Browse files Browse the repository at this point in the history
…1507)

* Upgrade `instrumentation_library_metrics` to `scope_metrics`

Signed-off-by: owentou <[email protected]>

* Rename `InstrumentationLibrary` -> `InstrumentationScope`

Signed-off-by: owentou <[email protected]>

* Fix style

Signed-off-by: owentou <[email protected]>

* Fix compiling

Signed-off-by: owentou <[email protected]>

* Remove duplicated `ElasticSearchRecordable::SetInstrumentationLibrary`

Signed-off-by: owentou <[email protected]>

* Fix comments

Signed-off-by: owentou <[email protected]>

* Update the usage of `instrumentation_library` of prometheus exporter.

Signed-off-by: owentou <[email protected]>
  • Loading branch information
owent authored Jul 30, 2022
1 parent 0015475 commit 59e7496
Show file tree
Hide file tree
Showing 71 changed files with 613 additions and 571 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Increment the:
* [EXT] `curl::HttpClient` use `curl_multi_handle` instead of creating a thread
for every request and it's able to reuse connections now. ([#1317](https://github.com/open-telemetry/opentelemetry-cpp/pull/1317))
* [SEMANTIC CONVENTIONS] Upgrade to version 1.12.0 [#873](https://github.com/open-telemetry/opentelemetry-cpp/pull/873)
* [SDK] Rename `InstrumentationLibrary` to `InstrumentationScope` [#1507](https://github.com/open-telemetry/opentelemetry-cpp/pull/1507)

## [1.4.1] 2022-06-19

Expand Down
2 changes: 1 addition & 1 deletion docs/cpp-metrics-api-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ implementation of the `MeterProvider` into the API
**`GetMeter(name, version)` method must be supported**

* Expects 2 string arguments:
* name (required): identifies the instrumentation library.
* name (required): identifies the instrumentation scope.
* version (optional): specifies the version of the instrumenting library (the
library injecting OpenTelemetry calls into the code)

Expand Down
4 changes: 2 additions & 2 deletions docs/cpp-metrics-sdk-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ implementation of the `MeterProvider` into the API.
**`GetMeter(name, version)` method must be supported**

* Expects 2 string arguments:
* name (required): identifies the instrumentation library.
* name (required): identifies the instrumentation scope.
* version (optional): specifies the version of the instrumenting library (the
library injecting OpenTelemetry calls into the code).

Expand Down Expand Up @@ -142,7 +142,7 @@ public:
*
* Arguments:
* MeterProvider, the MeterProvider object that spawned this Meter.
* InstrumentationInfo, the name of the instrumentation library and, optionally,
* InstrumentationInfo, the name of the instrumentation scope and, optionally,
* the version.
*
*/
Expand Down
1 change: 1 addition & 0 deletions docs/public/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ INPUT = \
../../api/include/opentelemetry/nostd/unique_ptr.h \
../../api/include/opentelemetry/nostd/variant.h \
../../api/include/opentelemetry/trace \
../../sdk/include/opentelemetry/sdk/instrumentationscope \
../../sdk/include/opentelemetry/sdk/instrumentationlibrary \
../../sdk/include/opentelemetry/sdk/resource \
../../sdk/include/opentelemetry/sdk/trace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# include <unordered_map>

# include "nlohmann/json.hpp"
# include "opentelemetry/common/macros.h"
# include "opentelemetry/sdk/common/attribute_utils.h"
# include "opentelemetry/sdk/logs/recordable.h"
# include "opentelemetry/version.h"
Expand Down Expand Up @@ -201,27 +202,34 @@ class ElasticSearchRecordable final : public sdk::logs::Recordable
nlohmann::json GetJSON() noexcept { return json_; }

/**
* Set instrumentation_library for this log.
* @param instrumentation_library the instrumentation library to set
* Set instrumentation_scope for this log.
* @param instrumentation_scope the instrumentation scope to set
*/
void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept
void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope
&instrumentation_scope) noexcept override
{
json_["name"] = instrumentation_library.GetName();
instrumentation_library_ = &instrumentation_library;
json_["name"] = instrumentation_scope.GetName();
instrumentation_scope_ = &instrumentation_scope;
}

/** Returns the associated instruementation library */
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
GetInstrumentationLibrary() const noexcept
OPENTELEMETRY_DEPRECATED_MESSAGE("Please use GetInstrumentationScope instead")
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationLibrary()
const noexcept
{
return *instrumentation_library_;
return GetInstrumentationScope();
}

/** Returns the associated instruementation library */
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope()
const noexcept
{
return *instrumentation_scope_;
}

private:
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ =
nullptr;
};
} // namespace logs
} // namespace exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ class JaegerRecordable final : public sdk::trace::Recordable

void SetDuration(std::chrono::nanoseconds duration) noexcept override;

void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept override;
void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope
&instrumentation_scope) noexcept override;

private:
void AddTag(const std::string &key, const std::string &value, std::vector<thrift::Tag> &tags);
Expand Down
10 changes: 5 additions & 5 deletions exporters/jaeger/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ void JaegerRecordable::AddEvent(nostd::string_view name,
logs_.push_back(log);
}

void JaegerRecordable::SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept
void JaegerRecordable::SetInstrumentationScope(
const opentelemetry::sdk::instrumentationscope::InstrumentationScope
&instrumentation_scope) noexcept
{
AddTag("otel.library.name", instrumentation_library.GetName(), tags_);
AddTag("otel.library.version", instrumentation_library.GetVersion(), tags_);
AddTag("otel.library.name", instrumentation_scope.GetName(), tags_);
AddTag("otel.library.version", instrumentation_scope.GetVersion(), tags_);
}

void JaegerRecordable::AddLink(const trace::SpanContext &span_context,
Expand Down
14 changes: 7 additions & 7 deletions exporters/jaeger/test/jaeger_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <vector>
#include "opentelemetry/exporters/jaeger/recordable.h"
#include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
Expand All @@ -18,7 +18,7 @@ namespace common = opentelemetry::common;

using namespace jaegertracing;
using namespace opentelemetry::exporter::jaeger;
using namespace opentelemetry::sdk::instrumentationlibrary;
using namespace opentelemetry::sdk::instrumentationscope;
using std::vector;

using Attributes = std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>;
Expand Down Expand Up @@ -247,15 +247,15 @@ TEST(JaegerSpanRecordable, SetAttributes)
EXPECT_EQ(tags, expected_tags);
}

TEST(JaegerSpanRecordable, SetInstrumentationLibrary)
TEST(JaegerSpanRecordable, SetInstrumentationScope)
{
JaegerRecordable rec;

std::string library_name = "opentelemetry-cpp";
std::string library_version = "0.1.0";
auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version);
std::string library_name = "opentelemetry-cpp";
std::string library_version = "0.1.0";
auto instrumentation_scope = InstrumentationScope::Create(library_name, library_version);

rec.SetInstrumentationLibrary(*instrumentation_library);
rec.SetInstrumentationScope(*instrumentation_scope);

auto tags = rec.Tags();
EXPECT_EQ(tags.size(), 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
void printInstrumentationInfoMetricData(
const sdk::metrics::InstrumentationInfoMetrics &info_metrics);
void printInstrumentationInfoMetricData(const sdk::metrics::ScopeMetrics &info_metrics);
void printPointData(const opentelemetry::sdk::metrics::PointType &point_data);
void printPointAttributes(const opentelemetry::sdk::metrics::PointAttributes &point_attributes);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ class OStreamSpanExporter final : public opentelemetry::sdk::trace::SpanExporter

void printResources(const opentelemetry::sdk::resource::Resource &resources);

void printInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library);
void printInstrumentationScope(
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope);
};
} // namespace trace
} // namespace exporter
Expand Down
12 changes: 6 additions & 6 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,27 @@ sdk::common::ExportResult OStreamMetricExporter::Export(
if (isShutdown())
{
OTEL_INTERNAL_LOG_ERROR("[OStream Metric] Exporting "
<< data.instrumentation_info_metric_data_.size()
<< data.scope_metric_data_.size()
<< " records(s) failed, exporter is shutdown");
return sdk::common::ExportResult::kFailure;
}

for (auto &record : data.instrumentation_info_metric_data_)
for (auto &record : data.scope_metric_data_)
{
printInstrumentationInfoMetricData(record);
}
return sdk::common::ExportResult::kSuccess;
}

void OStreamMetricExporter::printInstrumentationInfoMetricData(
const sdk::metrics::InstrumentationInfoMetrics &info_metric)
const sdk::metrics::ScopeMetrics &info_metric)
{
// sout_ is shared
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
sout_ << "{";
sout_ << "\n name\t\t: " << info_metric.instrumentation_library_->GetName()
<< "\n schema url\t: " << info_metric.instrumentation_library_->GetSchemaURL()
<< "\n version\t: " << info_metric.instrumentation_library_->GetVersion();
sout_ << "\n name\t\t: " << info_metric.scope_->GetName()
<< "\n schema url\t: " << info_metric.scope_->GetSchemaURL()
<< "\n version\t: " << info_metric.scope_->GetVersion();
for (const auto &record : info_metric.metric_data_)
{
sout_ << "\n start time\t: " << timeToString(record.start_ts)
Expand Down
11 changes: 5 additions & 6 deletions exporters/ostream/src/span_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ sdk::common::ExportResult OStreamSpanExporter::Export(
sout_ << "\n resources : ";
printResources(span->GetResource());
sout_ << "\n instr-lib : ";
printInstrumentationLibrary(span->GetInstrumentationLibrary());
printInstrumentationScope(span->GetInstrumentationScope());
sout_ << "\n}\n";
}
}
Expand Down Expand Up @@ -161,12 +161,11 @@ void OStreamSpanExporter::printResources(const opentelemetry::sdk::resource::Res
}
}

void OStreamSpanExporter::printInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library)
void OStreamSpanExporter::printInstrumentationScope(
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope)
{
sout_ << instrumentation_library.GetName();
auto version = instrumentation_library.GetVersion();
sout_ << instrumentation_scope.GetName();
auto version = instrumentation_scope.GetVersion();
if (version.size())
{
sout_ << "-" << version;
Expand Down
36 changes: 16 additions & 20 deletions exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ TEST(OStreamMetricsExporter, ExportSumPointData)
auto resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{});
data.resource_ = &resource;
auto instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name",
"1.2.0");
auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
"library_name", "1.2.0");
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
Expand All @@ -53,8 +52,8 @@ TEST(OStreamMetricsExporter, ExportSumPointData)
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, sum_point_data},
{metric_sdk::PointAttributes{{"a1", "b1"}}, sum_point_data2}}};
data.instrumentation_info_metric_data_ = std::vector<metric_sdk::InstrumentationInfoMetrics>{
{instrumentation_library.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};

std::stringstream stdoutOutput;
std::streambuf *sbuf = std::cout.rdbuf();
Expand Down Expand Up @@ -105,9 +104,8 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
auto resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{});
data.resource_ = &resource;
auto instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name",
"1.2.0");
auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
"library_name", "1.2.0");
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
Expand All @@ -117,8 +115,8 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}, {"a2", "b2"}}, histogram_point_data},
{metric_sdk::PointAttributes{{"a1", "b1"}}, histogram_point_data2}}};
data.instrumentation_info_metric_data_ = std::vector<metric_sdk::InstrumentationInfoMetrics>{
{instrumentation_library.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};

std::stringstream stdoutOutput;
std::streambuf *sbuf = std::cout.rdbuf();
Expand Down Expand Up @@ -166,9 +164,8 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData)
auto resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{});
data.resource_ = &resource;
auto instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name",
"1.2.0");
auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
"library_name", "1.2.0");
metric_sdk::LastValuePointData last_value_point_data{};
last_value_point_data.value_ = 10.0;
last_value_point_data.is_lastvalue_valid_ = true;
Expand All @@ -186,8 +183,8 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData)
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{}, last_value_point_data},
{metric_sdk::PointAttributes{}, last_value_point_data2}}};
data.instrumentation_info_metric_data_ = std::vector<metric_sdk::InstrumentationInfoMetrics>{
{instrumentation_library.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};

std::stringstream stdoutOutput;
std::streambuf *sbuf = std::cout.rdbuf();
Expand Down Expand Up @@ -230,9 +227,8 @@ TEST(OStreamMetricsExporter, ExportDropPointData)
auto resource = opentelemetry::sdk::resource::Resource::Create(
opentelemetry::sdk::resource::ResourceAttributes{});
data.resource_ = &resource;
auto instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name",
"1.2.0");
auto scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
"library_name", "1.2.0");
metric_sdk::DropPointData drop_point_data{};
metric_sdk::DropPointData drop_point_data2{};
metric_sdk::MetricData metric_data{
Expand All @@ -244,8 +240,8 @@ TEST(OStreamMetricsExporter, ExportDropPointData)
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{}, drop_point_data},
{metric_sdk::PointAttributes{}, drop_point_data2}}};
data.instrumentation_info_metric_data_ = std::vector<metric_sdk::InstrumentationInfoMetrics>{
{instrumentation_library.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};

std::stringstream stdoutOutput;
std::streambuf *sbuf = std::cout.rdbuf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

# include "opentelemetry/proto/logs/v1/logs.pb.h"
# include "opentelemetry/proto/resource/v1/resource.pb.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"

# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
// clang-format on

# include "opentelemetry/common/macros.h"
# include "opentelemetry/sdk/common/attribute_utils.h"
# include "opentelemetry/sdk/logs/recordable.h"

Expand Down Expand Up @@ -91,24 +92,30 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable
void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override;

/**
* Set instrumentation_library for this log.
* @param instrumentation_library the instrumentation library to set
* Set instrumentation_scope for this log.
* @param instrumentation_scope the instrumentation scope to set
*/
void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept override;
void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope
&instrumentation_scope) noexcept override;

/** Returns the associated instruementation library */
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
GetInstrumentationLibrary() const noexcept;
OPENTELEMETRY_DEPRECATED_MESSAGE("Please use GetInstrumentationScope instead")
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationLibrary()
const noexcept
{
return GetInstrumentationScope();
}

/** Returns the associated instruementation scope */
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope()
const noexcept;

private:
proto::logs::v1::LogRecord log_record_;
const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
// TODO shared resource
// const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ =
nullptr;
};

} // namespace otlp
Expand Down
Loading

0 comments on commit 59e7496

Please sign in to comment.