Skip to content

Commit

Permalink
Merge branch 'main' into fix_compile_with_clang16_libcxx
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Jul 21, 2023
2 parents b356360 + a15a9b8 commit 37f3dac
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 50 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Increment the:

## [Unreleased]

Breaking changes:

* [SDK] Add unit to Instrument selection criteria
[#2236](https://github.com/open-telemetry/opentelemetry-cpp/pull/2236)
* The `View` constructor and `ViewFactory::Create` method now takes a
`unit` criteria as optional third argument.
* Please adjust SDK configuration code accordingly.

## [1.10.0] 2023-07-11

* [REMOVAL] Remove the jaeger exporter
Expand Down
16 changes: 9 additions & 7 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,38 @@ void InitMetrics(const std::string &name)

// counter view
std::string counter_name = name + "_counter";
std::string unit = "counter-unit";

auto instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kCounter, counter_name);
metrics_sdk::InstrumentType::kCounter, counter_name, unit);

auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto sum_view =
metrics_sdk::ViewFactory::Create(name, "description", metrics_sdk::AggregationType::kSum);
auto sum_view = metrics_sdk::ViewFactory::Create(name, "description", unit,
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// observable counter view
std::string observable_counter_name = name + "_observable_counter";

auto observable_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kObservableCounter, observable_counter_name);
metrics_sdk::InstrumentType::kObservableCounter, observable_counter_name, unit);

auto observable_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description",
auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description", unit,
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector),
std::move(observable_sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
unit = "histogram-unit";

auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kHistogram, histogram_name);
metrics_sdk::InstrumentType::kHistogram, histogram_name, unit);

auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

Expand All @@ -96,7 +98,7 @@ void InitMetrics(const std::string &name)
std::move(histogram_aggregation_config));

auto histogram_view = metrics_sdk::ViewFactory::Create(
name, "description", metrics_sdk::AggregationType::kHistogram, aggregation_config);
name, "description", unit, metrics_sdk::AggregationType::kHistogram, aggregation_config);

p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
Expand Down
12 changes: 7 additions & 5 deletions examples/prometheus/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,29 @@ void InitMetrics(const std::string &name, const std::string &addr)

// counter view
std::string counter_name = name + "_counter";
std::string counter_unit = "unit";

auto instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kCounter, counter_name);
metrics_sdk::InstrumentType::kCounter, counter_name, counter_unit);

auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto sum_view = metrics_sdk::ViewFactory::Create(counter_name, "description",
auto sum_view = metrics_sdk::ViewFactory::Create(counter_name, "description", counter_unit,
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
std::string histogram_unit = "unit";

auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kHistogram, histogram_name);
metrics_sdk::InstrumentType::kHistogram, histogram_name, histogram_unit);

auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto histogram_view = metrics_sdk::ViewFactory::Create(histogram_name, "description",
metrics_sdk::AggregationType::kHistogram);
auto histogram_view = metrics_sdk::ViewFactory::Create(
histogram_name, "description", histogram_unit, metrics_sdk::AggregationType::kHistogram);

p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
Expand Down
2 changes: 2 additions & 0 deletions exporters/ostream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ cc_library(
name = "ostream_log_record_exporter",
srcs = [
"src/log_record_exporter.cc",
"src/log_record_exporter_factory.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/common_utils.h",
"include/opentelemetry/exporters/ostream/log_record_exporter.h",
"include/opentelemetry/exporters/ostream/log_record_exporter_factory.h",
],
strip_include_prefix = "include",
tags = ["ostream"],
Expand Down
12 changes: 7 additions & 5 deletions exporters/ostream/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if(OPENTELEMETRY_INSTALL)
DIRECTORY include/opentelemetry/exporters/ostream
DESTINATION include/opentelemetry/exporters
PATTERN "*.h"
PATTERN "log_Exporter.h" EXCLUDE)
PATTERN "log_record_exporter.h" EXCLUDE)
endif()

if(BUILD_TESTING)
Expand Down Expand Up @@ -77,7 +77,8 @@ if(BUILD_TESTING)
endif()

if(WITH_LOGS_PREVIEW)
add_library(opentelemetry_exporter_ostream_logs src/log_record_exporter.cc)
add_library(opentelemetry_exporter_ostream_logs
src/log_record_exporter.cc src/log_record_exporter_factory.cc)
set_target_properties(opentelemetry_exporter_ostream_logs
PROPERTIES EXPORT_NAME ostream_log_record_exporter)
set_target_version(opentelemetry_exporter_ostream_logs)
Expand All @@ -96,9 +97,10 @@ if(WITH_LOGS_PREVIEW)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(
DIRECTORY include/opentelemetry/exporters/ostream
DESTINATION include/opentelemetry/exporters
PATTERN "log_record_exporter.h")
FILES
"include/opentelemetry/exporters/ostream/log_record_exporter.h"
"include/opentelemetry/exporters/ostream/log_record_exporter_factory.h"
DESTINATION include/opentelemetry/exporters/ostream)
endif()

if(BUILD_TESTING)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#ifdef ENABLE_LOGS_PREVIEW
# include <iostream>
# include <memory>

# include "opentelemetry/sdk/version/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace logs
{
class LogRecordExporter;
} // namespace logs
} // namespace sdk

namespace exporter
{
namespace logs
{

/**
* Factory class for OStreamLogRecordExporter.
*/
class OPENTELEMETRY_EXPORT OStreamLogRecordExporterFactory
{
public:
/**
* Creates an OStreamLogRecordExporter writing to the default location.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogRecordExporter> Create();

/**
* Creates an OStreamLogRecordExporter writing to the given location.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogRecordExporter> Create(std::ostream &sout);
};

} // namespace logs
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
31 changes: 31 additions & 0 deletions exporters/ostream/src/log_record_exporter_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/exporters/ostream/log_record_exporter_factory.h"
# include "opentelemetry/exporters/ostream/log_record_exporter.h"

namespace logs_sdk = opentelemetry::sdk::logs;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace logs
{

std::unique_ptr<logs_sdk::LogRecordExporter> OStreamLogRecordExporterFactory::Create()
{
return Create(std::cout);
}

std::unique_ptr<logs_sdk::LogRecordExporter> OStreamLogRecordExporterFactory::Create(
std::ostream &sout)
{
std::unique_ptr<logs_sdk::LogRecordExporter> exporter(new OStreamLogRecordExporter(sout));
return exporter;
}

} // namespace logs
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
8 changes: 8 additions & 0 deletions exporters/ostream/test/ostream_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/exporters/ostream/log_record_exporter.h"
# include "opentelemetry/exporters/ostream/log_record_exporter_factory.h"
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
Expand Down Expand Up @@ -507,6 +508,13 @@ TEST(OStreamLogRecordExporter, IntegrationTestWithEventId)
}
}

// Test using the factory to create the ostream exporter
TEST(OStreamLogRecordExporter, Factory)
{
auto exporter = OStreamLogRecordExporterFactory::Create();
ASSERT_NE(exporter, nullptr);
}

} // namespace logs
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ class InstrumentSelector
{
public:
InstrumentSelector(opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name)
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view units)
: name_filter_{PredicateFactory::GetPredicate(name, PredicateType::kPattern)},
unit_filter_{PredicateFactory::GetPredicate(units, PredicateType::kExact)},
instrument_type_{instrument_type}
{}

// Returns name filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *GetNameFilter() const { return name_filter_.get(); }

// Returns unit filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *GetUnitFilter() const { return unit_filter_.get(); }
// Returns instrument filter.
InstrumentType GetInstrumentType() { return instrument_type_; }

private:
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> name_filter_;
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> unit_filter_;
opentelemetry::sdk::metrics::InstrumentType instrument_type_;
};
} // namespace metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class InstrumentSelectorFactory
public:
static std::unique_ptr<InstrumentSelector> Create(
opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name);
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view unit);
};

} // namespace metrics
Expand Down
3 changes: 3 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class View
public:
View(const std::string &name,
const std::string &description = "",
const std::string &unit = "",
AggregationType aggregation_type = AggregationType::kDefault,
std::shared_ptr<AggregationConfig> aggregation_config = nullptr,
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor =
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor>(
new opentelemetry::sdk::metrics::DefaultAttributesProcessor()))
: name_(name),
description_(description),
unit_(unit),
aggregation_type_{aggregation_type},
aggregation_config_{aggregation_config},
attributes_processor_{std::move(attributes_processor)}
Expand Down Expand Up @@ -60,6 +62,7 @@ class View
private:
std::string name_;
std::string description_;
std::string unit_;
AggregationType aggregation_type_;
std::shared_ptr<AggregationConfig> aggregation_config_;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor_;
Expand Down
7 changes: 7 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/view_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ class OPENTELEMETRY_EXPORT ViewFactory

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit);

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type);

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type,
std::shared_ptr<AggregationConfig> aggregation_config);

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type,
std::shared_ptr<AggregationConfig> aggregation_config,
std::unique_ptr<AttributesProcessor> attributes_processor);
Expand Down
1 change: 1 addition & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/view_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ViewRegistry
const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor)
{
return selector->GetNameFilter()->Match(instrument_descriptor.name_) &&
selector->GetUnitFilter()->Match(instrument_descriptor.unit_) &&
(selector->GetInstrumentType() == instrument_descriptor.type_);
}
};
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/metrics/view/instrument_selector_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ namespace metrics

std::unique_ptr<InstrumentSelector> InstrumentSelectorFactory::Create(
opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name)
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view unit)
{
std::unique_ptr<InstrumentSelector> instrument_selector(
new InstrumentSelector(instrument_type, name));
new InstrumentSelector(instrument_type, name, unit));
return instrument_selector;
}

Expand Down
Loading

0 comments on commit 37f3dac

Please sign in to comment.