Skip to content

Commit

Permalink
Otlp http log example (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Nov 17, 2021
1 parent 3db551f commit 97fbdaa
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
add_subdirectory(foo_library)
if(WITH_LOGS_PREVIEW)
add_subdirectory(logs_foo_library)
endif()
16 changes: 16 additions & 0 deletions examples/common/logs_foo_library/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "common_logs_foo_library",
srcs = [
"foo_library.cc",
],
hdrs = [
"foo_library.h",
],
defines = ["BAZEL_BUILD"],
deps = [
"//api",
"//sdk:headers",
],
)
3 changes: 3 additions & 0 deletions examples/common/logs_foo_library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(common_logs_foo_library foo_library.h foo_library.cc)
target_link_libraries(common_logs_foo_library PUBLIC ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_api)
38 changes: 38 additions & 0 deletions examples/common/logs_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#ifdef ENABLE_LOGS_PREVIEW
# include <map>
# include <string>
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/sdk/version/version.h"
# include "opentelemetry/trace/provider.h"

namespace logs = opentelemetry::logs;
namespace trace = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;

namespace
{
nostd::shared_ptr<trace::Tracer> get_tracer()
{
auto provider = trace::Provider::GetTracerProvider();
return provider->GetTracer("foo_library", OPENTELEMETRY_SDK_VERSION);
}

nostd::shared_ptr<logs::Logger> get_logger()
{
auto provider = logs::Provider::GetLoggerProvider();
return provider->GetLogger("foo_library_logger");
}
} // namespace

void foo_library()
{
auto span = get_tracer()->StartSpan("span 1");
auto scoped_span = trace::Scope(get_tracer()->StartSpan("foo_library"));
auto ctx = span->GetContext();
auto logger = get_logger();
logger->Log(opentelemetry::logs::Severity::kDebug, "name", "body", {}, {}, ctx.trace_id(),
ctx.span_id(), ctx.trace_flags(), opentelemetry::common::SystemTimestamp());
}
#endif
6 changes: 6 additions & 0 deletions examples/common/logs_foo_library/foo_library.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

void foo_library();
14 changes: 14 additions & 0 deletions examples/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ cc_binary(
"//sdk/src/trace",
],
)

cc_binary(
name = "example_otlp_http_log",
srcs = [
"http_log_main.cc",
],
deps = [
"//api",
"//examples/common/logs_foo_library:common_logs_foo_library",
"//exporters/otlp:otlp_http_exporter",
"//exporters/otlp:otlp_http_log_exporter",
"//sdk/src/trace",
],
)
11 changes: 11 additions & 0 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ if(WITH_OTLP_HTTP)
target_link_libraries(
example_otlp_http ${CMAKE_THREAD_LIBS_INIT} common_foo_library
opentelemetry_trace opentelemetry_exporter_otlp_http)
if(WITH_LOGS_PREVIEW)
add_executable(example_otlp_http_log http_log_main.cc)
target_link_libraries(
example_otlp_http_log
${CMAKE_THREAD_LIBS_INIT}
common_logs_foo_library
opentelemetry_trace
opentelemetry_logs
opentelemetry_exporter_otlp_http
opentelemetry_exporter_otlp_http_log)
endif()
endif()
5 changes: 3 additions & 2 deletions examples/otlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ This is an example of how to use the [OpenTelemetry
Protocol](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/README.md)
(OTLP) exporter.

The application in `grpc_main.cc` initializes an `OtlpGrpcExporter` instance and
the application in `http_main.cc` initializes an `OtlpHttpExporter` instance
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
SDK](https://github.com/open-telemetry/opentelemetry-cpp). The application then
calls a `foo_library` which has been instrumented using the [OpenTelemetry
Expand Down
93 changes: 93 additions & 0 deletions examples/otlp/http_log_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/exporters/otlp/otlp_http_exporter.h"
# include "opentelemetry/exporters/otlp/otlp_http_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::OtlpHttpExporterOptions opts;
void InitTracer()
{
// Create OTLP exporter instance
auto exporter = std::unique_ptr<trace_sdk::SpanExporter>(new otlp::OtlpHttpExporter(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);
}

opentelemetry::exporter::otlp::OtlpHttpLogExporterOptions logger_opts;
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 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.url = argv[1];
logger_opts.url = argv[1];
if (argc > 2)
{
std::string debug = argv[2];
opts.console_debug = debug != "" && debug != "0" && debug != "no";
}

if (argc > 3)
{
std::string binary_mode = argv[3];
if (binary_mode.size() >= 3 && binary_mode.substr(0, 3) == "bin")
{
opts.content_type = opentelemetry::exporter::otlp::HttpRequestContentType::kBinary;
logger_opts.content_type = opentelemetry::exporter::otlp::HttpRequestContentType::kBinary;
}
}
}
InitLogger();
InitTracer();
foo_library();
}
#else
int main()
{
return 0;
}
#endif

0 comments on commit 97fbdaa

Please sign in to comment.