Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log sdk builder (#1486) #1524

Merged
merged 10 commits into from
Aug 2, 2022
Merged
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
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