Skip to content

Commit

Permalink
[DOC] Add readme and examples for OTLP FILE exporters. (#2638)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Apr 17, 2024
1 parent 8f15440 commit 950c336
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ cc_binary(
],
)

cc_binary(
name = "example_otlp_file",
srcs = [
"file_main.cc",
],
tags = [
"examples",
"otlp",
"otlp_file",
],
deps = [
"//api",
"//examples/common/foo_library:common_foo_library",
"//exporters/otlp:otlp_file_exporter",
"//sdk/src/trace",
],
)

cc_binary(
name = "example_otlp_http_log",
srcs = [
Expand Down Expand Up @@ -75,6 +93,25 @@ cc_binary(
],
)

cc_binary(
name = "example_otlp_file_log",
srcs = [
"file_log_main.cc",
],
tags = [
"examples",
"otlp",
"otlp_file_log",
],
deps = [
"//api",
"//examples/common/logs_foo_library:common_logs_foo_library",
"//exporters/otlp:otlp_file_exporter",
"//exporters/otlp:otlp_file_log_record_exporter",
"//sdk/src/trace",
],
)

cc_binary(
name = "example_otlp_grpc_metric",
srcs = [
Expand Down Expand Up @@ -112,3 +149,22 @@ cc_binary(
"//sdk/src/metrics",
],
)

cc_binary(
name = "example_otlp_file_metric",
srcs = [
"file_metric_main.cc",
],
tags = [
"examples",
"metrics",
"otlp",
],
deps = [
"//api",
"//examples/common/metrics_foo_library:common_metrics_foo_library",
"//exporters/otlp:otlp_file_exporter",
"//exporters/otlp:otlp_file_metric_exporter",
"//sdk/src/metrics",
],
)
44 changes: 44 additions & 0 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,47 @@ if(WITH_OTLP_HTTP)
endif()

endif()

if(WITH_OTLP_FILE)
# TRACE

add_executable(example_otlp_file file_main.cc)

target_link_libraries(example_otlp_file ${CMAKE_THREAD_LIBS_INIT}
common_foo_library)

if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_file opentelemetry_cpp)
else()
target_link_libraries(example_otlp_file opentelemetry_trace
opentelemetry_exporter_otlp_file)
endif()

# METRIC

add_executable(example_otlp_file_metric file_metric_main.cc)

target_link_libraries(example_otlp_file_metric ${CMAKE_THREAD_LIBS_INIT}
common_metrics_foo_library)

if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_file_metric opentelemetry_cpp)
else()
target_link_libraries(example_otlp_file_metric opentelemetry_metrics
opentelemetry_exporter_otlp_file_metric)
endif()

# LOG

add_executable(example_otlp_file_log file_log_main.cc)

target_link_libraries(example_otlp_file_log ${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library)
if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_otlp_file_log opentelemetry_cpp)
else()
target_link_libraries(
example_otlp_file_log opentelemetry_trace opentelemetry_logs
opentelemetry_exporter_otlp_file opentelemetry_exporter_otlp_file_log)
endif()
endif()
124 changes: 124 additions & 0 deletions examples/otlp/file_log_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/otlp/otlp_file_client_options.h"
#include "opentelemetry/exporters/otlp/otlp_file_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h"
#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h"
#include "opentelemetry/logs/provider.h"
#include "opentelemetry/sdk/logs/exporter.h"
#include "opentelemetry/sdk/logs/logger_provider_factory.h"
#include "opentelemetry/sdk/logs/processor.h"
#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"

// sdk::TracerProvider and sdk::LoggerProvider is just used to call ForceFlush and prevent to cancel
// running exportings when destroy and shutdown exporters.It's optional to users.
#include "opentelemetry/sdk/logs/logger_provider.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"

#include <iostream>
#include <string>
#include <utility>

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

namespace trace = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;
namespace otlp = opentelemetry::exporter::otlp;
namespace logs_sdk = opentelemetry::sdk::logs;
namespace logs = opentelemetry::logs;
namespace trace_sdk = opentelemetry::sdk::trace;

namespace
{
opentelemetry::exporter::otlp::OtlpFileExporterOptions opts;
opentelemetry::exporter::otlp::OtlpFileLogRecordExporterOptions log_opts;
void InitTracer()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpFileExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
{
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
}

std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}

void InitLogger()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpFileLogRecordExporterFactory::Create(log_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
nostd::shared_ptr<logs::LoggerProvider> provider(
logs_sdk::LoggerProviderFactory::Create(std::move(processor)));

opentelemetry::logs::Provider::SetLoggerProvider(provider);
}

void CleanupLogger()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<logs::LoggerProvider> provider =
logs::Provider::GetLoggerProvider();
if (provider)
{
static_cast<logs_sdk::LoggerProvider *>(provider.get())->ForceFlush();
}

nostd::shared_ptr<logs::LoggerProvider> none;
opentelemetry::logs::Provider::SetLoggerProvider(none);
}
} // namespace

int main(int argc, char *argv[])
{
if (argc > 1)
{
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions fs_backend;
fs_backend.file_pattern = argv[1];
opts.backend_options = fs_backend;
if (argc > 2)
{
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions logs_fs_backend;
logs_fs_backend.file_pattern = argv[2];
log_opts.backend_options = logs_fs_backend;
}
else
{
log_opts.backend_options = std::ref(std::cout);
}
}
else
{
opts.backend_options = std::ref(std::cout);
}
InitLogger();
InitTracer();
foo_library();
CleanupTracer();
CleanupLogger();
}
71 changes: 71 additions & 0 deletions examples/otlp/file_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/otlp/otlp_file_exporter_factory.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"

// sdk::TracerProvider is just used to call ForceFlush and prevent to cancel running exportings when
// destroy and shutdown exporters.It's optional to users.
#include "opentelemetry/sdk/trace/tracer_provider.h"

#include <iostream>
#include <string>
#include <utility>

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

namespace trace = opentelemetry::trace;
namespace trace_sdk = opentelemetry::sdk::trace;
namespace otlp = opentelemetry::exporter::otlp;

namespace
{
opentelemetry::exporter::otlp::OtlpFileExporterOptions opts;
void InitTracer()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpFileExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
{
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
}

std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
} // namespace

int main(int argc, char *argv[])
{
if (argc > 1)
{
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions fs_backend;
fs_backend.file_pattern = argv[1];
opts.backend_options = fs_backend;
}
// Removing this line will leave the default noop TracerProvider in place.
InitTracer();

foo_library();

CleanupTracer();
}
Loading

1 comment on commit 950c336

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 950c336 Previous: 8f15440 Ratio
BM_SpinLockThrashing/4/process_time/real_time 1.4761897424856822 ms/iter 0.6471014859383566 ms/iter 2.28

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.