Skip to content

Commit

Permalink
Merge branch 'main' into recordable-spandata
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Aug 2, 2022
2 parents dbfd7eb + 70fd2dc commit fdcc018
Show file tree
Hide file tree
Showing 44 changed files with 1,013 additions and 155 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Increment the:

## [Unreleased]

* [LOG SDK] Add log sdk builders (#1486) [#1524](https://github.com/open-telemetry/opentelemetry-cpp/pull/1524)

## [1.5.0] 2022-07-29

* [EXPORTER BUILD] Add resources to dep list of prometheus exporter test [#1527](https://github.com/open-telemetry/opentelemetry-cpp/pull/1527)
Expand Down
6 changes: 4 additions & 2 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
# include "foo_library.h"
# include <chrono>
# include <map>
# include <memory>
# include <thread>
# include <vector>
# include "opentelemetry/context/context.h"
# include "opentelemetry/metrics/provider.h"
# include "opentelemetry/nostd/shared_ptr.h"

namespace nostd = opentelemetry::nostd;
namespace metrics_api = opentelemetry::metrics;
Expand Down Expand Up @@ -72,8 +74,8 @@ void foo_library::histogram_example(const std::string &name)
std::string histogram_name = name + "_histogram";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name);
auto context = opentelemetry::context::Context{};
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name, "des", "unit");
auto context = opentelemetry::context::Context{};
while (true)
{
double val = (rand() % 700) + 1.1;
Expand Down
10 changes: 8 additions & 2 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ void initMetrics(const std::string &name)
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::unique_ptr<metric_sdk::View> histogram_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kHistogram}};
std::shared_ptr<opentelemetry::sdk::metrics::AggregationConfig> aggregation_config{
new opentelemetry::sdk::metrics::HistogramAggregationConfig<double>};
static_cast<opentelemetry::sdk::metrics::HistogramAggregationConfig<double> *>(
aggregation_config.get())
->boundaries_ =
std::list<double>{0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0};
std::unique_ptr<metric_sdk::View> histogram_view{new metric_sdk::View{
name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}};
p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
metrics_api::Provider::SetMeterProvider(provider);
Expand Down
21 changes: 8 additions & 13 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_LOGS_PREVIEW
// Make sure to include GRPC exporter first because otherwise Abseil may create
// ambiguity with `nostd::variant`. See issue:
// https://github.com/open-telemetry/opentelemetry-cpp/issues/880
# include "opentelemetry/exporters/otlp/otlp_grpc_log_exporter.h"

# include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
# include "opentelemetry/exporters/otlp/otlp_grpc_log_exporter_factory.h"
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/sdk/logs/logger_provider.h"
# include "opentelemetry/sdk/logs/simple_log_processor.h"
# include "opentelemetry/sdk/logs/logger_provider_factory.h"
# include "opentelemetry/sdk/logs/simple_log_processor_factory.h"
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
# include "opentelemetry/sdk/trace/tracer_provider_factory.h"
# include "opentelemetry/trace/provider.h"
Expand Down Expand Up @@ -47,12 +43,11 @@ void InitTracer()
void InitLogger()
{
// Create OTLP exporter instance
auto exporter = std::unique_ptr<logs_sdk::LogExporter>(new otlp::OtlpGrpcLogExporter(opts));
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(
new logs_sdk::LoggerProvider(std::unique_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)))));
auto apiProvider = nostd::shared_ptr<logs::LoggerProvider>(sdkProvider);
auto provider = nostd::shared_ptr<logs::LoggerProvider>(apiProvider);
auto exporter = otlp::OtlpGrpcLogExporterFactory::Create(opts);
auto processor = logs_sdk::SimpleLogProcessorFactory::Create(std::move(exporter));
nostd::shared_ptr<logs::LoggerProvider> provider(
logs_sdk::LoggerProviderFactory::Create(std::move(processor)));

opentelemetry::logs::Provider::SetLoggerProvider(provider);
}
} // namespace
Expand Down
19 changes: 9 additions & 10 deletions examples/otlp/http_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_exporter.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_exporter_factory.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_exporter_options.h"
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/sdk/logs/logger_provider.h"
# include "opentelemetry/sdk/logs/simple_log_processor.h"
# include "opentelemetry/sdk/logs/logger_provider_factory.h"
# include "opentelemetry/sdk/logs/simple_log_processor_factory.h"
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
# include "opentelemetry/sdk/trace/tracer_provider_factory.h"
# include "opentelemetry/trace/provider.h"
Expand Down Expand Up @@ -46,13 +47,11 @@ void InitLogger()
{
logger_opts.console_debug = true;
// Create OTLP exporter instance
auto exporter =
std::unique_ptr<logs_sdk::LogExporter>(new otlp::OtlpHttpLogExporter(logger_opts));
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(
new logs_sdk::LoggerProvider(std::unique_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)))));
auto apiProvider = nostd::shared_ptr<logs::LoggerProvider>(sdkProvider);
auto provider = nostd::shared_ptr<logs::LoggerProvider>(apiProvider);
auto exporter = otlp::OtlpHttpLogExporterFactory::Create(logger_opts);
auto processor = logs_sdk::SimpleLogProcessorFactory::Create(std::move(exporter));
std::shared_ptr<logs::LoggerProvider> provider =
logs_sdk::LoggerProviderFactory::Create(std::move(processor));

opentelemetry::logs::Provider::SetLoggerProvider(provider);
}
} // namespace
Expand Down
37 changes: 37 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ cc_library(
name = "otlp_http_log_exporter",
srcs = [
"src/otlp_http_log_exporter.cc",
"src/otlp_http_log_exporter_factory.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h",
"include/opentelemetry/exporters/otlp/otlp_http_log_exporter_factory.h",
"include/opentelemetry/exporters/otlp/otlp_http_log_exporter_options.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
Expand Down Expand Up @@ -192,11 +195,13 @@ cc_library(
name = "otlp_grpc_log_exporter",
srcs = [
"src/otlp_grpc_log_exporter.cc",
"src/otlp_grpc_log_exporter_factory.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_log_exporter.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_log_exporter_factory.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
Expand Down Expand Up @@ -324,6 +329,22 @@ cc_test(
],
)

cc_test(
name = "otlp_http_log_exporter_factory_test",
srcs = ["test/otlp_http_log_exporter_factory_test.cc"],
tags = [
"otlp",
"otlp_http_log",
"test",
],
deps = [
":otlp_http_log_exporter",
"//api",
"//ext/src/http/client/nosend:http_client_nosend",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "otlp_http_metric_exporter_test",
srcs = ["test/otlp_http_metric_exporter_test.cc"],
Expand Down Expand Up @@ -356,6 +377,22 @@ cc_test(
],
)

cc_test(
name = "otlp_grpc_log_exporter_factory_test",
srcs = ["test/otlp_grpc_log_exporter_factory_test.cc"],
tags = [
"otlp",
"otlp_grpc_log",
"test",
],
deps = [
":otlp_grpc_log_exporter",
"//api",
"//sdk/src/logs",
"@com_google_googletest//:gtest_main",
],
)

otel_cc_benchmark(
name = "otlp_grpc_exporter_benchmark",
srcs = ["test/otlp_grpc_exporter_benchmark.cc"],
Expand Down
32 changes: 28 additions & 4 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ if(WITH_OTLP_GRPC)

list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_grpc)

add_library(opentelemetry_exporter_otlp_grpc_log
src/otlp_grpc_log_exporter.cc)
add_library(
opentelemetry_exporter_otlp_grpc_log src/otlp_grpc_log_exporter.cc
src/otlp_grpc_log_exporter_factory.cc)

set_target_properties(opentelemetry_exporter_otlp_grpc_log
PROPERTIES EXPORT_NAME otlp_grpc_log_exporter)
Expand Down Expand Up @@ -90,8 +91,9 @@ if(WITH_OTLP_HTTP)
list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_http)

if(WITH_LOGS_PREVIEW)
add_library(opentelemetry_exporter_otlp_http_log
src/otlp_http_log_exporter.cc)
add_library(
opentelemetry_exporter_otlp_http_log
src/otlp_http_log_exporter.cc src/otlp_http_log_exporter_factory.cc)

set_target_properties(opentelemetry_exporter_otlp_http_log
PROPERTIES EXPORT_NAME otlp_http_log_exporter)
Expand Down Expand Up @@ -216,6 +218,17 @@ if(BUILD_TESTING)
TARGET otlp_grpc_log_exporter_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_grpc_log_exporter_test)

add_executable(otlp_grpc_log_exporter_factory_test
test/otlp_grpc_log_exporter_factory_test.cc)
target_link_libraries(
otlp_grpc_log_exporter_factory_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB}
opentelemetry_exporter_otlp_grpc_log opentelemetry_logs)
gtest_add_tests(
TARGET otlp_grpc_log_exporter_factory_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_grpc_log_exporter_factory_test)
endif()
endif()

Expand Down Expand Up @@ -254,6 +267,17 @@ if(BUILD_TESTING)
TARGET otlp_http_log_exporter_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_log_exporter_test)

add_executable(otlp_http_log_exporter_factory_test
test/otlp_http_log_exporter_factory_test.cc)
target_link_libraries(
otlp_http_log_exporter_factory_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB}
opentelemetry_exporter_otlp_http_log opentelemetry_logs)
gtest_add_tests(
TARGET otlp_http_log_exporter_factory_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_log_exporter_factory_test)
endif()

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

#pragma once

#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
# include "opentelemetry/sdk/logs/exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

/**
* Factory class for OtlpGrpcLogExporter.
*/
class OtlpGrpcLogExporterFactory
{
public:
/**
* Create a OtlpGrpcLogExporter.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogExporter> Create();

/**
* Create a OtlpGrpcLogExporter.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogExporter> Create(
const OtlpGrpcExporterOptions &options);
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE

#endif /* ENABLE_LOGS_PREVIEW */
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# include "opentelemetry/exporters/otlp/otlp_http_client.h"

# include "opentelemetry/exporters/otlp/otlp_environment.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_exporter_options.h"

# include <chrono>
# include <cstddef>
Expand All @@ -21,47 +22,6 @@ namespace exporter
namespace otlp
{

/**
* Struct to hold OTLP exporter options.
*/
struct OtlpHttpLogExporterOptions
{
// The endpoint to export to. By default
// @see
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md
// @see https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver
std::string url = GetOtlpDefaultHttpLogEndpoint();

// By default, post json data
HttpRequestContentType content_type = HttpRequestContentType::kJson;

// If convert bytes into hex. By default, we will convert all bytes but id into base64
// This option is ignored if content_type is not kJson
JsonBytesMappingKind json_bytes_mapping = JsonBytesMappingKind::kHexId;

// If using the json name of protobuf field to set the key of json. By default, we will use the
// field name just like proto files.
bool use_json_name = false;

// Whether to print the status of the exporter in the console
bool console_debug = false;

// TODO: Enable/disable to verify SSL certificate
std::chrono::system_clock::duration timeout = GetOtlpDefaultLogTimeout();

// Additional HTTP headers
OtlpHeaders http_headers = GetOtlpDefaultLogHeaders();

# ifdef ENABLE_ASYNC_EXPORT
// Concurrent requests
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlpgrpc-concurrent-requests
std::size_t max_concurrent_requests = 64;

// Requests per connections
std::size_t max_requests_per_connection = 8;
# endif
};

/**
* The OTLP exporter exports log data in OpenTelemetry Protocol (OTLP) format.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_http_log_exporter_options.h"
# include "opentelemetry/sdk/logs/exporter.h"

# include <memory>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

/**
* Factory class for OtlpHttpLogExporter.
*/
class OtlpHttpLogExporterFactory
{
public:
/**
* Create a OtlpHttpLogExporter.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogExporter> Create();

/**
* Create a OtlpHttpLogExporter.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogExporter> Create(
const OtlpHttpLogExporterOptions &options);
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
Loading

0 comments on commit fdcc018

Please sign in to comment.