-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |