Skip to content

Commit

Permalink
Fix log sdk builder (open-telemetry#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Jul 28, 2022
1 parent 3a8f913 commit 925d18b
Show file tree
Hide file tree
Showing 20 changed files with 591 additions and 87 deletions.
19 changes: 8 additions & 11 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// 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/logger_provider_factory.h"
# include "opentelemetry/sdk/logs/simple_log_processor.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 +45,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
10 changes: 6 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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_http_client.h"

# include "opentelemetry/sdk/logs/exporter.h"

# include <chrono>
# include <cstddef>
# include <memory>
# include <string>

OPENTELEMETRY_BEGIN_NAMESPACE
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
};

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

#ifdef ENABLE_LOGS_PREVIEW

// MUST be first (absl)
# include "opentelemetry/exporters/otlp/otlp_grpc_log_exporter.h"

# include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
# include "opentelemetry/exporters/otlp/otlp_grpc_log_exporter_factory.h"
# include "opentelemetry/sdk/common/global_log_handler.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

std::unique_ptr<opentelemetry::sdk::logs::LogExporter> OtlpGrpcLogExporterFactory::Create()
{
OtlpGrpcExporterOptions options;
return Create(options);
}

std::unique_ptr<opentelemetry::sdk::logs::LogExporter> OtlpGrpcLogExporterFactory::Create(
const OtlpGrpcExporterOptions &options)
{
std::unique_ptr<opentelemetry::sdk::logs::LogExporter> exporter(new OtlpGrpcLogExporter(options));
return exporter;
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE

#endif
33 changes: 33 additions & 0 deletions exporters/otlp/src/otlp_http_log_exporter_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_http_log_exporter_factory.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_exporter.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_exporter_options.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

std::unique_ptr<opentelemetry::sdk::logs::LogExporter> OtlpHttpLogExporterFactory::Create()
{
OtlpHttpLogExporterOptions options;
return Create(options);
}

std::unique_ptr<opentelemetry::sdk::logs::LogExporter> OtlpHttpLogExporterFactory::Create(
const OtlpHttpLogExporterOptions &options)
{
std::unique_ptr<opentelemetry::sdk::logs::LogExporter> exporter(new OtlpHttpLogExporter(options));
return exporter;
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE

#endif
Loading

0 comments on commit 925d18b

Please sign in to comment.