Skip to content

Commit

Permalink
Otlp gRPC log example (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Nov 23, 2021
1 parent caade51 commit 1867688
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ cc_binary(
"//sdk/src/trace",
],
)

cc_binary(
name = "example_otlp_grpc_log",
srcs = [
"grpc_log_main.cc",
],
deps = [
"//api",
"//examples/common/logs_foo_library:common_logs_foo_library",
"//exporters/otlp:otlp_grpc_exporter",
"//exporters/otlp:otlp_grpc_log_exporter",
"//sdk/src/trace",
],
)
12 changes: 12 additions & 0 deletions examples/otlp/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ if(WITH_OTLP_GRPC)
target_link_libraries(
example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT} common_foo_library
opentelemetry_trace opentelemetry_exporter_otlp_grpc gRPC::grpc++)
if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_grpc_log grpc_log_main.cc)
target_link_libraries(
example_otlp_grpc_log
${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library
opentelemetry_trace
opentelemetry_logs
opentelemetry_exporter_otlp_grpc
opentelemetry_exporter_otlp_grpc_log
gRPC::grpc++)
endif()
endif()

if(WITH_OTLP_HTTP)
Expand Down
5 changes: 3 additions & 2 deletions examples/otlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Protocol](https://github.com/open-telemetry/opentelemetry-specification/blob/mai

The application in `grpc_main.cc` initializes an `OtlpGrpcExporter` instance,
the application in `http_main.cc` initializes an `OtlpHttpExporter` instance.
The application in `http_log_main.cc` initializes an `OtlpHttpLogExporter` instance
and they register a tracer provider from the [OpenTelemetry
The application in `http_log_main.cc` initializes an `OtlpHttpLogExporter` instance,
the application in `grpc_log_main.cc` initializes an `OtlpGrpcLogExporter` instance.
And they register a tracer provider from the [OpenTelemetry
SDK](https://github.com/open-telemetry/opentelemetry-cpp). The application then
calls a `foo_library` which has been instrumented using the [OpenTelemetry
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api).
Expand Down
78 changes: 78 additions & 0 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"
# include "opentelemetry/exporters/otlp/otlp_grpc_log_exporter.h"
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/sdk/logs/logger_provider.h"
# include "opentelemetry/sdk/logs/simple_log_processor.h"
# include "opentelemetry/sdk/trace/simple_processor.h"
# include "opentelemetry/sdk/trace/tracer_provider.h"
# include "opentelemetry/trace/provider.h"

# include <string>

# 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::OtlpGrpcExporterOptions opts;
void InitTracer()
{
// Create OTLP exporter instance
auto exporter = std::unique_ptr<trace_sdk::SpanExporter>(new otlp::OtlpGrpcExporter(opts));
auto processor = std::unique_ptr<trace_sdk::SpanProcessor>(
new trace_sdk::SimpleSpanProcessor(std::move(exporter)));
auto provider =
nostd::shared_ptr<trace::TracerProvider>(new trace_sdk::TracerProvider(std::move(processor)));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}

void InitLogger()
{
// Create OTLP exporter instance
auto exporter = std::unique_ptr<logs_sdk::LogExporter>(new otlp::OtlpGrpcLogExporter(opts));
auto processor = std::shared_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)));
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(new logs_sdk::LoggerProvider());
sdkProvider->SetProcessor(processor);
auto apiProvider = nostd::shared_ptr<logs::LoggerProvider>(sdkProvider);
auto provider = nostd::shared_ptr<logs::LoggerProvider>(apiProvider);
opentelemetry::logs::Provider::SetLoggerProvider(provider);
}
} // namespace

int main(int argc, char *argv[])
{
if (argc > 1)
{
opts.endpoint = argv[1];
if (argc > 2)
{
opts.use_ssl_credentials = true;
opts.ssl_credentials_cacert_path = argv[2];
}
}
InitLogger();
InitTracer();
foo_library();
}
#else
int main()
{
return 0;
}
#endif

0 comments on commit 1867688

Please sign in to comment.