Skip to content

Commit

Permalink
Merge branch 'main' into remove_jaeger_exporter_1938
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored Jun 28, 2023
2 parents bc8e9d9 + efde010 commit b01e93e
Show file tree
Hide file tree
Showing 37 changed files with 1,122 additions and 115 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ if(NOT WITH_API_ONLY)
endif()
endif()

include(cmake/opentelemetry-build-external-component.cmake)

if(OPENTELEMETRY_INSTALL)
# Export cmake config and support find_packages(opentelemetry-cpp CONFIG)
# Write config file for find_packages(opentelemetry-cpp CONFIG)
Expand Down
30 changes: 30 additions & 0 deletions cmake/opentelemetry-build-external-component.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# Enable building external components through otel-cpp build
# The config options are
# - OPENTELEMETRY_EXTERNAL_COMPONENT_PATH - Setting local path of the external
# component as env variable
# - OPENTELEMETRY_EXTERNAL_COMPONENT_URL Setting github-repo of external component
# as env variable


# Add custom vendor component from local path, or GitHub repo
if(DEFINED $ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_PATH})
# Add custom component path to build tree and consolidate binary artifacts in
# current project binary output directory.
add_subdirectory($ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_PATH}
${PROJECT_BINARY_DIR}/external)
elseif(DEFINED $ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_URL})
# This option requires CMake 3.11+: add standard remote repo to build tree.
include(FetchContent)
FetchContent_Declare(
external
GIT_REPOSITORY $ENV{OPENTELEMETRY_EXTERNAL_COMPONENT_URL}
GIT_TAG "main")
FetchContent_GetProperties(external)
if(NOT external_POPULATED)
FetchContent_Populate(external)
add_subdirectory(${external_SOURCE_DIR})
endif()
endif()
90 changes: 56 additions & 34 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@

#include <memory>
#include <thread>
#include "opentelemetry/exporters/ostream/metric_exporter.h"

#include "opentelemetry/exporters/ostream/metric_exporter_factory.h"
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
#include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "opentelemetry/sdk/metrics/meter_provider_factory.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
#include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h"
#include "opentelemetry/sdk/metrics/view/meter_selector_factory.h"
#include "opentelemetry/sdk/metrics/view/view_factory.h"

#ifdef BAZEL_BUILD
# include "examples/common/metrics_foo_library/foo_library.h"
#else
# include "metrics_foo_library/foo_library.h"
#endif

namespace metric_sdk = opentelemetry::sdk::metrics;
namespace metrics_sdk = opentelemetry::sdk::metrics;
namespace common = opentelemetry::common;
namespace exportermetrics = opentelemetry::exporter::metrics;
namespace metrics_api = opentelemetry::metrics;
Expand All @@ -27,59 +33,75 @@ namespace

void InitMetrics(const std::string &name)
{
std::unique_ptr<metric_sdk::PushMetricExporter> exporter{
new exportermetrics::OStreamMetricExporter};
auto exporter = exportermetrics::OStreamMetricExporterFactory::Create();

std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

// Initialize and set the global MeterProvider
metric_sdk::PeriodicExportingMetricReaderOptions options;
metrics_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)};
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);

auto reader =
metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options);

auto u_provider = metrics_sdk::MeterProviderFactory::Create();
auto *p = static_cast<metrics_sdk::MeterProvider *>(u_provider.get());

p->AddMetricReader(std::move(reader));

// counter view
std::string counter_name = name + "_counter";
std::unique_ptr<metric_sdk::InstrumentSelector> instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, counter_name)};
std::unique_ptr<metric_sdk::MeterSelector> meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> sum_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};

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

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

auto sum_view =
metrics_sdk::ViewFactory::Create(name, "description", 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";
std::unique_ptr<metric_sdk::InstrumentSelector> observable_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kObservableCounter,
observable_counter_name)};
std::unique_ptr<metric_sdk::MeterSelector> observable_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> observable_sum_view{
new metric_sdk::View{name, "test_description", metric_sdk::AggregationType::kSum}};

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

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

auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description",
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";
std::unique_ptr<metric_sdk::InstrumentSelector> histogram_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)};
std::unique_ptr<metric_sdk::MeterSelector> histogram_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::shared_ptr<opentelemetry::sdk::metrics::AggregationConfig> aggregation_config{
new opentelemetry::sdk::metrics::HistogramAggregationConfig};
static_cast<opentelemetry::sdk::metrics::HistogramAggregationConfig *>(aggregation_config.get())
->boundaries_ = std::vector<double>{0.0, 50.0, 100.0, 250.0, 500.0, 750.0,
1000.0, 2500.0, 5000.0, 10000.0, 20000.0};
std::unique_ptr<metric_sdk::View> histogram_view{new metric_sdk::View{
name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}};

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

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

auto histogram_aggregation_config = std::unique_ptr<metrics_sdk::HistogramAggregationConfig>(
new metrics_sdk::HistogramAggregationConfig);

histogram_aggregation_config->boundaries_ = std::vector<double>{
0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0, 20000.0};

std::shared_ptr<metrics_sdk::AggregationConfig> aggregation_config(
std::move(histogram_aggregation_config));

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

p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));

std::shared_ptr<opentelemetry::metrics::MeterProvider> provider(std::move(u_provider));
metrics_api::Provider::SetMeterProvider(provider);
}

Expand Down
21 changes: 20 additions & 1 deletion examples/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ cc_binary(
)

cc_binary(
name = "example_otlp_grpc_metrics",
name = "example_otlp_grpc_metric",
srcs = [
"grpc_metric_main.cc",
],
Expand All @@ -93,3 +93,22 @@ cc_binary(
"//sdk/src/metrics",
],
)

cc_binary(
name = "example_otlp_http_metric",
srcs = [
"http_metric_main.cc",
],
tags = [
"examples",
"metrics",
"otlp",
],
deps = [
"//api",
"//examples/common/metrics_foo_library:common_metrics_foo_library",
"//exporters/otlp:otlp_http_exporter",
"//exporters/otlp:otlp_http_metric_exporter",
"//sdk/src/metrics",
],
)
34 changes: 26 additions & 8 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ include_directories(
${CMAKE_SOURCE_DIR}/exporters/otlp/include)

if(WITH_OTLP_GRPC)
# TRACE

add_executable(example_otlp_grpc grpc_main.cc)

target_link_libraries(example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -21,6 +23,18 @@ if(WITH_OTLP_GRPC)
target_link_libraries(example_otlp_grpc opentelemetry_trace
opentelemetry_exporter_otlp_grpc)
endif()

# METRIC

add_executable(example_otlp_grpc_metric grpc_metric_main.cc)

target_link_libraries(
example_otlp_grpc_metric ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_grpc_metrics)

# LOG

if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_grpc_log grpc_log_main.cc)

Expand All @@ -37,6 +51,8 @@ if(WITH_OTLP_GRPC)
endif()

if(WITH_OTLP_HTTP)
# TRACE

add_executable(example_otlp_http http_main.cc)

target_link_libraries(example_otlp_http ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -49,6 +65,16 @@ if(WITH_OTLP_HTTP)
opentelemetry_exporter_otlp_http)
endif()

# METRIC

add_executable(example_otlp_http_metric http_metric_main.cc)
target_link_libraries(
example_otlp_http_metric ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_http_metric)

# LOG

if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_http_log http_log_main.cc)
target_link_libraries(example_otlp_http_log ${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -66,11 +92,3 @@ if(WITH_OTLP_HTTP)

endif()
endif()

if(WITH_OTLP_GRPC)
add_executable(example_otlp_metric_grpc grpc_metric_main.cc)
target_link_libraries(
example_otlp_metric_grpc ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library opentelemetry_metrics
opentelemetry_exporter_otlp_grpc_metrics)
endif()
35 changes: 22 additions & 13 deletions examples/otlp/grpc_metric_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/meter_context_factory.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "opentelemetry/sdk/metrics/meter_provider_factory.h"

#include <memory>
#include <thread>
Expand All @@ -25,24 +28,30 @@ namespace otlp_exporter = opentelemetry::exporter::otlp;
namespace
{

otlp_exporter::OtlpGrpcMetricExporterOptions options;
otlp_exporter::OtlpGrpcMetricExporterOptions exporter_options;

void InitMetrics()
{
auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(options);
auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(exporter_options);

std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

// Initialize and set the global MeterProvider
metric_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
std::unique_ptr<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)};
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);
p->AddMetricReader(std::move(reader));
metric_sdk::PeriodicExportingMetricReaderOptions reader_options;
reader_options.export_interval_millis = std::chrono::milliseconds(1000);
reader_options.export_timeout_millis = std::chrono::milliseconds(500);

auto reader =
metric_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), reader_options);

auto context = metric_sdk::MeterContextFactory::Create();
context->AddMetricReader(std::move(reader));

std::shared_ptr<metric_sdk::MeterContext> s_context(std::move(context));

auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context);
std::shared_ptr<opentelemetry::metrics::MeterProvider> provider(std::move(u_provider));

metrics_api::Provider::SetMeterProvider(provider);
}
Expand All @@ -59,14 +68,14 @@ int main(int argc, char *argv[])
std::string example_type;
if (argc > 1)
{
options.endpoint = argv[1];
exporter_options.endpoint = argv[1];
if (argc > 2)
{
example_type = argv[2];
if (argc > 3)
{
options.use_ssl_credentials = true;
options.ssl_credentials_cacert_path = argv[3];
exporter_options.use_ssl_credentials = true;
exporter_options.ssl_credentials_cacert_path = argv[3];
}
}
}
Expand Down
Loading

0 comments on commit b01e93e

Please sign in to comment.