Skip to content

Commit

Permalink
ostream logs example
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSparc committed Feb 19, 2023
1 parent 95f6c66 commit 9e2703f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ endif()
if(WITH_PROMETHEUS)
add_subdirectory(prometheus)
endif()
if(WITH_LOGS_PREVIEW)
add_subdirectory(logs_simple)
endif()
add_subdirectory(plugin)
add_subdirectory(simple)
add_subdirectory(batch)
Expand Down
19 changes: 19 additions & 0 deletions examples/logs_simple/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

cc_binary(
name = "example_logs_simple",
srcs = [
"main.cc",
],
tags = [
"examples",
"ostream",
],
deps = [
"//api",
"//examples/common/logs_foo_library:common_logs_foo_library",
"//exporters/ostream:ostream_span_exporter",
"//exporters/ostream:ostream_log_record_exporter",
],
)
23 changes: 23 additions & 0 deletions examples/logs_simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

if(DEFINED OPENTELEMETRY_BUILD_DLL)
add_definitions(-DOPENTELEMETRY_BUILD_IMPORT_DLL)
endif()

include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include)

if(WITH_LOGS_PREVIEW)
add_executable(example_logs_simple main.cc)
target_link_libraries(example_logs_simple ${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library)

if(DEFINED OPENTELEMETRY_BUILD_DLL)
target_link_libraries(example_logs_simple opentelemetry_cpp)
else()
target_link_libraries(example_logs_simple opentelemetry_trace
opentelemetry_logs
opentelemetry_exporter_ostream_span
opentelemetry_exporter_ostream_logs)
endif()
endif()
13 changes: 13 additions & 0 deletions examples/logs_simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Simple Logs Example

In this example, the application in `main.cc` initializes an `OStreamLogRecordExporter` instance and registers a logger
provider, as well as initializes a `StdoutSpanExporter` instance and registers a tracer provider from the [OpenTelemetry
SDK](https://github.com/open-telemetry/opentelemetry-cpp).

The application then calls a `logs_foo_library` which has been instrumented using the [OpenTelemetry
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api).
Resulting logs and traces are directed to stdout.

See [CONTRIBUTING.md](../../CONTRIBUTING.md) for instructions on building and
running the example.
82 changes: 82 additions & 0 deletions examples/logs_simple/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/exporters/ostream/span_exporter_factory.h"
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
# include "opentelemetry/sdk/trace/tracer_provider_factory.h"
# include "opentelemetry/trace/provider.h"

# include "opentelemetry/exporters/ostream/log_record_exporter.h"
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/sdk/logs/logger_provider_factory.h"
# include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"

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

namespace logs_api = opentelemetry::logs;
namespace logs_sdk = opentelemetry::sdk::logs;
namespace logs_exporter = opentelemetry::exporter::logs;

namespace trace_api = opentelemetry::trace;
namespace trace_sdk = opentelemetry::sdk::trace;
namespace trace_exporter = opentelemetry::exporter::trace;

namespace
{
void InitTracer()
{
// Create ostream span exporter instance
auto exporter = trace_exporter::OStreamSpanExporterFactory::Create();
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::shared_ptr<trace_api::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));

// Set the global trace provider
trace_api::Provider::SetTracerProvider(provider);
}

void CleanupTracer()
{
std::shared_ptr<trace_api::TracerProvider> none;
trace_api::Provider::SetTracerProvider(none);
}

void InitLogger()
{
// Create ostream log exporter instance
auto exporter = std::unique_ptr<logs_sdk::LogRecordExporter>(new logs_exporter::OStreamLogRecordExporter);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
std::shared_ptr<logs_api::LoggerProvider> provider(
logs_sdk::LoggerProviderFactory::Create(std::move(processor)));

// Set the global logger provider
logs_api::Provider::SetLoggerProvider(provider);
}

void CleanupLogger()
{
std::shared_ptr<logs_api::LoggerProvider> none;
logs_api::Provider::SetLoggerProvider(none);
}

} // namespace

int main()
{
InitTracer();
InitLogger();
foo_library();
CleanupTracer();
CleanupLogger();
}
#else
int main()
{
return 0;
}
#endif // ENABLE_LOGS_PREVIEW

0 comments on commit 9e2703f

Please sign in to comment.