From cb349d2f213e3273f5d243ba3da668d61a8ff9ee Mon Sep 17 00:00:00 2001 From: Oblivion Date: Thu, 11 Nov 2021 22:38:13 +0000 Subject: [PATCH 1/8] otlp http log export example --- examples/common/logs_foo_library/BUILD | 16 ++++ .../common/logs_foo_library/foo_library.cc | 44 ++++++++++ .../common/logs_foo_library/foo_library.h | 6 ++ examples/otlp_log_exporter/BUILD | 13 +++ examples/otlp_log_exporter/http_log_main.cc | 86 +++++++++++++++++++ .../config.dev.yaml | 30 +++++++ 6 files changed, 195 insertions(+) create mode 100644 examples/common/logs_foo_library/BUILD create mode 100644 examples/common/logs_foo_library/foo_library.cc create mode 100644 examples/common/logs_foo_library/foo_library.h create mode 100644 examples/otlp_log_exporter/BUILD create mode 100644 examples/otlp_log_exporter/http_log_main.cc create mode 100644 examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml diff --git a/examples/common/logs_foo_library/BUILD b/examples/common/logs_foo_library/BUILD new file mode 100644 index 0000000000..956c7a5697 --- /dev/null +++ b/examples/common/logs_foo_library/BUILD @@ -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", + ], +) diff --git a/examples/common/logs_foo_library/foo_library.cc b/examples/common/logs_foo_library/foo_library.cc new file mode 100644 index 0000000000..07781b6d4d --- /dev/null +++ b/examples/common/logs_foo_library/foo_library.cc @@ -0,0 +1,44 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/version/version.h" +#include "opentelemetry/logs/provider.h" +#include "opentelemetry/trace/provider.h" +#include +#include + +namespace logs = opentelemetry::logs; +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +namespace +{ +nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("foo_library", OPENTELEMETRY_SDK_VERSION); +} + +nostd::shared_ptr get_logger() +{ + auto provider = logs::Provider::GetLoggerProvider(); + return provider->GetLogger("foo_library_logger", nostd::span()); +} +} + +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", + std::map(), + std::map(), + ctx.trace_id(), + ctx.span_id(), + ctx.trace_flags(), + opentelemetry::common::SystemTimestamp()); +} diff --git a/examples/common/logs_foo_library/foo_library.h b/examples/common/logs_foo_library/foo_library.h new file mode 100644 index 0000000000..daebb2603f --- /dev/null +++ b/examples/common/logs_foo_library/foo_library.h @@ -0,0 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +void foo_library(); diff --git a/examples/otlp_log_exporter/BUILD b/examples/otlp_log_exporter/BUILD new file mode 100644 index 0000000000..8a9ee8be5c --- /dev/null +++ b/examples/otlp_log_exporter/BUILD @@ -0,0 +1,13 @@ +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", + ], +) diff --git a/examples/otlp_log_exporter/http_log_main.cc b/examples/otlp_log_exporter/http_log_main.cc new file mode 100644 index 0000000000..1e3d45d466 --- /dev/null +++ b/examples/otlp_log_exporter/http_log_main.cc @@ -0,0 +1,86 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/exporters/otlp/otlp_http_log_exporter.h" +#include "opentelemetry/sdk/logs/simple_log_processor.h" +#include "opentelemetry/sdk/logs/logger_provider.h" +#include "opentelemetry/logs/provider.h" + +#include + +#ifdef BAZEL_BUILD +# include "examples/common/logs_foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; +namespace otlp = opentelemetry::exporter::otlp; +namespace sdklogs = opentelemetry::sdk::logs; +namespace logs_api = opentelemetry::logs; + +#include "opentelemetry/exporters/otlp/otlp_http_exporter.h" +#include "opentelemetry/sdk/trace/simple_processor.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/provider.h" + +namespace sdktrace = opentelemetry::sdk::trace; + +namespace +{ + +opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts; +void InitTracer() +{ + // Create OTLP exporter instance + auto exporter = std::unique_ptr(new otlp::OtlpHttpExporter(opts)); + auto processor = std::unique_ptr( + new sdktrace::SimpleSpanProcessor(std::move(exporter))); + auto provider = + nostd::shared_ptr(new sdktrace::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(new otlp::OtlpHttpLogExporter(logger_opts)); + auto processor = std::shared_ptr( + new sdklogs::SimpleLogProcessor(std::move(exporter))); + auto sdkProvider = std::shared_ptr(new sdklogs::LoggerProvider()); + sdkProvider->SetProcessor(processor); + auto apiProvider = nostd::shared_ptr(sdkProvider); + auto provider = nostd::shared_ptr(apiProvider); + opentelemetry::logs::Provider::SetLoggerProvider(provider); +} +} // namespace + +int main(int argc, char *argv[]) +{ + if (argc > 1) + { + 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; + } + } + } + // Removing this line will leave the default noop TracerProvider in place. + InitLogger(); + InitTracer(); + foo_library(); +} diff --git a/examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml b/examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml new file mode 100644 index 0000000000..0ac0c597a6 --- /dev/null +++ b/examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml @@ -0,0 +1,30 @@ +exporters: + logging: + loglevel: DEBUG + sampling_initial: 200 + sampling_thereafter: 200 +receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 + http: + endpoint: 0.0.0.0:4318 + #cors_allowed_origins: + #- '*' +processors: + batch: +service: + pipelines: + traces: + receivers: + - otlp + exporters: + - logging + processors: [batch] + logs: + receivers: + - otlp + exporters: + - logging + processors: [batch] From 70b3359e327564a992fc984cb4b40f0f5e12d80f Mon Sep 17 00:00:00 2001 From: Oblivion Date: Fri, 12 Nov 2021 17:29:10 +0000 Subject: [PATCH 2/8] format --- .../common/logs_foo_library/foo_library.cc | 33 ++++++++----------- examples/otlp/BUILD | 14 ++++++++ .../http_log_main.cc | 21 ++++++------ examples/otlp_log_exporter/BUILD | 13 -------- .../config.dev.yaml | 30 ----------------- 5 files changed, 38 insertions(+), 73 deletions(-) rename examples/{otlp_log_exporter => otlp}/http_log_main.cc (90%) delete mode 100644 examples/otlp_log_exporter/BUILD delete mode 100644 examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml diff --git a/examples/common/logs_foo_library/foo_library.cc b/examples/common/logs_foo_library/foo_library.cc index 07781b6d4d..32e2cd944d 100644 --- a/examples/common/logs_foo_library/foo_library.cc +++ b/examples/common/logs_foo_library/foo_library.cc @@ -1,13 +1,13 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/sdk/version/version.h" -#include "opentelemetry/logs/provider.h" -#include "opentelemetry/trace/provider.h" #include #include +#include "opentelemetry/logs/provider.h" +#include "opentelemetry/sdk/version/version.h" +#include "opentelemetry/trace/provider.h" -namespace logs = opentelemetry::logs; +namespace logs = opentelemetry::logs; namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; @@ -15,8 +15,8 @@ namespace { nostd::shared_ptr get_tracer() { - auto provider = trace::Provider::GetTracerProvider(); - return provider->GetTracer("foo_library", OPENTELEMETRY_SDK_VERSION); + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("foo_library", OPENTELEMETRY_SDK_VERSION); } nostd::shared_ptr get_logger() @@ -24,21 +24,16 @@ nostd::shared_ptr get_logger() auto provider = logs::Provider::GetLoggerProvider(); return provider->GetLogger("foo_library_logger", nostd::span()); } -} +} // namespace void foo_library() { - auto span = get_tracer()->StartSpan("span 1"); + 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", - std::map(), - std::map(), - ctx.trace_id(), - ctx.span_id(), - ctx.trace_flags(), - opentelemetry::common::SystemTimestamp()); + auto ctx = span->GetContext(); + auto logger = get_logger(); + logger->Log(opentelemetry::logs::Severity::kDebug, "name", "body", + std::map(), std::map(), + ctx.trace_id(), ctx.span_id(), ctx.trace_flags(), + opentelemetry::common::SystemTimestamp()); } diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 34842c01f2..1a8075ca93 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -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", + ], +) diff --git a/examples/otlp_log_exporter/http_log_main.cc b/examples/otlp/http_log_main.cc similarity index 90% rename from examples/otlp_log_exporter/http_log_main.cc rename to examples/otlp/http_log_main.cc index 1e3d45d466..5474f3cade 100644 --- a/examples/otlp_log_exporter/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -1,10 +1,14 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/exporters/otlp/otlp_http_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_log_exporter.h" -#include "opentelemetry/sdk/logs/simple_log_processor.h" -#include "opentelemetry/sdk/logs/logger_provider.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 @@ -17,14 +21,9 @@ namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; namespace otlp = opentelemetry::exporter::otlp; -namespace sdklogs = opentelemetry::sdk::logs; +namespace sdklogs = opentelemetry::sdk::logs; namespace logs_api = opentelemetry::logs; -#include "opentelemetry/exporters/otlp/otlp_http_exporter.h" -#include "opentelemetry/sdk/trace/simple_processor.h" -#include "opentelemetry/sdk/trace/tracer_provider.h" -#include "opentelemetry/trace/provider.h" - namespace sdktrace = opentelemetry::sdk::trace; namespace @@ -48,9 +47,9 @@ void InitLogger() { logger_opts.console_debug = true; // Create OTLP exporter instance - auto exporter = std::unique_ptr(new otlp::OtlpHttpLogExporter(logger_opts)); - auto processor = std::shared_ptr( - new sdklogs::SimpleLogProcessor(std::move(exporter))); + auto exporter = std::unique_ptr(new otlp::OtlpHttpLogExporter(logger_opts)); + auto processor = + std::shared_ptr(new sdklogs::SimpleLogProcessor(std::move(exporter))); auto sdkProvider = std::shared_ptr(new sdklogs::LoggerProvider()); sdkProvider->SetProcessor(processor); auto apiProvider = nostd::shared_ptr(sdkProvider); diff --git a/examples/otlp_log_exporter/BUILD b/examples/otlp_log_exporter/BUILD deleted file mode 100644 index 8a9ee8be5c..0000000000 --- a/examples/otlp_log_exporter/BUILD +++ /dev/null @@ -1,13 +0,0 @@ -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", - ], -) diff --git a/examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml b/examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml deleted file mode 100644 index 0ac0c597a6..0000000000 --- a/examples/otlp_log_exporter/opentelemetry-collector-config/config.dev.yaml +++ /dev/null @@ -1,30 +0,0 @@ -exporters: - logging: - loglevel: DEBUG - sampling_initial: 200 - sampling_thereafter: 200 -receivers: - otlp: - protocols: - grpc: - endpoint: 0.0.0.0:4317 - http: - endpoint: 0.0.0.0:4318 - #cors_allowed_origins: - #- '*' -processors: - batch: -service: - pipelines: - traces: - receivers: - - otlp - exporters: - - logging - processors: [batch] - logs: - receivers: - - otlp - exporters: - - logging - processors: [batch] From b8f856f6afb2e759e7c32354d7941226c56bf1c0 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Fri, 12 Nov 2021 18:21:46 +0000 Subject: [PATCH 3/8] cmake --- examples/common/CMakeLists.txt | 3 ++ .../common/logs_foo_library/CMakeLists.txt | 3 ++ examples/otlp/CMakeLists.txt | 11 +++++++ examples/otlp/http_log_main.cc | 31 ++++++++++--------- 4 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 examples/common/logs_foo_library/CMakeLists.txt diff --git a/examples/common/CMakeLists.txt b/examples/common/CMakeLists.txt index 38ff619308..f89faf4a2f 100644 --- a/examples/common/CMakeLists.txt +++ b/examples/common/CMakeLists.txt @@ -1 +1,4 @@ add_subdirectory(foo_library) +if(WITH_LOGS_PREVIEW) + add_subdirectory(logs_foo_library) +endif() diff --git a/examples/common/logs_foo_library/CMakeLists.txt b/examples/common/logs_foo_library/CMakeLists.txt new file mode 100644 index 0000000000..df14f5c98b --- /dev/null +++ b/examples/common/logs_foo_library/CMakeLists.txt @@ -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) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index 4618695d3b..f07e007b46 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -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() diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc index 5474f3cade..078195d933 100644 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -1,22 +1,23 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#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" +#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 +# include -#ifdef BAZEL_BUILD -# include "examples/common/logs_foo_library/foo_library.h" -#else -# include "foo_library/foo_library.h" -#endif +# 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; @@ -83,3 +84,5 @@ int main(int argc, char *argv[]) InitTracer(); foo_library(); } + +#endif \ No newline at end of file From c2c3bb28d79fbbe16aa5e202e676fa9b1ed1825d Mon Sep 17 00:00:00 2001 From: Oblivion Date: Fri, 12 Nov 2021 18:37:07 +0000 Subject: [PATCH 4/8] ci bazel --- examples/common/logs_foo_library/foo_library.cc | 13 +++++++------ examples/otlp/http_log_main.cc | 6 +++++- 2 files changed, 12 insertions(+), 7 deletions(-) mode change 100644 => 100755 examples/otlp/http_log_main.cc diff --git a/examples/common/logs_foo_library/foo_library.cc b/examples/common/logs_foo_library/foo_library.cc index 32e2cd944d..50ea4a48f2 100644 --- a/examples/common/logs_foo_library/foo_library.cc +++ b/examples/common/logs_foo_library/foo_library.cc @@ -1,11 +1,11 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 - -#include -#include -#include "opentelemetry/logs/provider.h" -#include "opentelemetry/sdk/version/version.h" -#include "opentelemetry/trace/provider.h" +#ifdef ENABLE_LOGS_PREVIEW +# include +# include +# include "opentelemetry/logs/provider.h" +# include "opentelemetry/sdk/version/version.h" +# include "opentelemetry/trace/provider.h" namespace logs = opentelemetry::logs; namespace trace = opentelemetry::trace; @@ -37,3 +37,4 @@ void foo_library() ctx.trace_id(), ctx.span_id(), ctx.trace_flags(), opentelemetry::common::SystemTimestamp()); } +#endif diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc old mode 100644 new mode 100755 index 078195d933..9880b72b95 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -84,5 +84,9 @@ int main(int argc, char *argv[]) InitTracer(); foo_library(); } - +#else +int main() +{ + return 0; +} #endif \ No newline at end of file From 4935ca4ef087079a3800f43f83bd9492139a6d7a Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 13 Nov 2021 16:11:06 +0000 Subject: [PATCH 5/8] readme --- examples/otlp/README.md | 5 +++-- examples/otlp/http_log_main.cc | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/otlp/README.md b/examples/otlp/README.md index bc911a59ac..2a70c2e4de 100644 --- a/examples/otlp/README.md +++ b/examples/otlp/README.md @@ -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 diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc index 9880b72b95..96e8d52f4d 100755 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -79,7 +79,6 @@ int main(int argc, char *argv[]) } } } - // Removing this line will leave the default noop TracerProvider in place. InitLogger(); InitTracer(); foo_library(); From c14c929e1d29a8844a567398e8ef2dbaf4eadf65 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 13 Nov 2021 16:44:58 +0000 Subject: [PATCH 6/8] logger options --- examples/otlp/http_log_main.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) mode change 100755 => 100644 examples/otlp/http_log_main.cc diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc old mode 100755 new mode 100644 index 96e8d52f4d..e61faf4c7a --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -63,7 +63,8 @@ int main(int argc, char *argv[]) { if (argc > 1) { - opts.url = argv[1]; + opts.url = argv[1]; + logger_opts.url = argv[1]; if (argc > 2) { std::string debug = argv[2]; @@ -75,7 +76,8 @@ int main(int argc, char *argv[]) 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; + opts.content_type = opentelemetry::exporter::otlp::HttpRequestContentType::kBinary; + logger_opts.content_type = opentelemetry::exporter::otlp::HttpRequestContentType::kBinary; } } } From c11771b8c5c782d985e174f943fa961de739fe01 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 12 Nov 2021 22:27:35 -0800 Subject: [PATCH 7/8] Build opentelemetry-proto collector interface with cmake only if WITH_OTLP_GRPC is enabled. (#1061) --- CMakeLists.txt | 6 +++-- cmake/opentelemetry-proto.cmake | 47 ++++++++++++++++++++++++++++++++- examples/CMakeLists.txt | 2 ++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b68a0d4fa7..6e5114bcd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -330,7 +330,9 @@ endif() if(WITH_OTLP) set(protobuf_MODULE_COMPATIBLE ON) find_package(Protobuf) - find_package(gRPC) + if(WITH_OTLP_GRPC) + find_package(gRPC) + endif() if((NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND) OR (NOT gRPC_FOUND)) if(WIN32 AND (NOT DEFINED CMAKE_TOOLCHAIN_FILE)) install_windows_deps() @@ -346,7 +348,7 @@ if(WITH_OTLP) if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND) find_package(Protobuf REQUIRED) endif() - if(NOT gRPC_FOUND) + if(WITH_OTLP_GRPC AND NOT gRPC_FOUND) find_package(gRPC) endif() if(WIN32) diff --git a/cmake/opentelemetry-proto.cmake b/cmake/opentelemetry-proto.cmake index e56749509d..1af3fbbf92 100644 --- a/cmake/opentelemetry-proto.cmake +++ b/cmake/opentelemetry-proto.cmake @@ -79,48 +79,56 @@ set(TRACE_SERVICE_PB_CPP_FILE set(TRACE_SERVICE_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.pb.h" ) +if(WITH_OTLP_GRPC) set(TRACE_SERVICE_GRPC_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.cc" ) set(TRACE_SERVICE_GRPC_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" ) +endif() set(LOGS_SERVICE_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.pb.cc" ) set(LOGS_SERVICE_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.pb.h" ) +if(WITH_OTLP_GRPC) set(LOGS_SERVICE_GRPC_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.cc" ) set(LOGS_SERVICE_GRPC_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.h" ) +endif() set(METRICS_SERVICE_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.pb.cc" ) set(METRICS_SERVICE_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h" ) +if(WITH_OTLP_GRPC) set(METRICS_SERVICE_GRPC_PB_CPP_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.cc" ) set(METRICS_SERVICE_GRPC_PB_H_FILE "${GENERATED_PROTOBUF_PATH}/opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.h" ) +endif() foreach(IMPORT_DIR ${PROTOBUF_IMPORT_DIRS}) list(APPEND PROTOBUF_INCLUDE_FLAGS "-I${IMPORT_DIR}") endforeach() - +if(WITH_OTLP_GRPC) if(CMAKE_CROSSCOMPILING) find_program(gRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) else() set(gRPC_CPP_PLUGIN_EXECUTABLE $) endif() +endif() +if(WITH_OTLP_GRPC) add_custom_command( OUTPUT ${COMMON_PB_H_FILE} ${COMMON_PB_CPP_FILE} @@ -151,9 +159,34 @@ add_custom_command( --plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" ${COMMON_PROTO} ${RESOURCE_PROTO} ${TRACE_PROTO} ${LOGS_PROTO} ${METRICS_PROTO} ${TRACE_SERVICE_PROTO} ${LOGS_SERVICE_PROTO} ${METRICS_SERVICE_PROTO}) +else() +add_custom_command( + OUTPUT ${COMMON_PB_H_FILE} + ${COMMON_PB_CPP_FILE} + ${RESOURCE_PB_H_FILE} + ${RESOURCE_PB_CPP_FILE} + ${TRACE_PB_H_FILE} + ${TRACE_PB_CPP_FILE} + ${LOGS_PB_H_FILE} + ${LOGS_PB_CPP_FILE} + ${METRICS_PB_H_FILE} + ${METRICS_PB_CPP_FILE} + ${TRACE_SERVICE_PB_H_FILE} + ${TRACE_SERVICE_PB_CPP_FILE} + ${LOGS_SERVICE_PB_H_FILE} + ${LOGS_SERVICE_PB_CPP_FILE} + ${METRICS_SERVICE_PB_H_FILE} + ${METRICS_SERVICE_PB_CPP_FILE} + COMMAND + ${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--proto_path=${PROTO_PATH}" + ${PROTOBUF_INCLUDE_FLAGS} "--cpp_out=${GENERATED_PROTOBUF_PATH}" + ${COMMON_PROTO} ${RESOURCE_PROTO} ${TRACE_PROTO} ${LOGS_PROTO} ${METRICS_PROTO} + ${TRACE_SERVICE_PROTO} ${LOGS_SERVICE_PROTO} ${METRICS_SERVICE_PROTO}) +endif() include_directories("${GENERATED_PROTOBUF_PATH}") +if(WITH_OTLP_GRPC) add_library( opentelemetry_proto STATIC ${COMMON_PB_CPP_FILE} @@ -167,6 +200,18 @@ add_library( ${LOGS_SERVICE_GRPC_PB_CPP_FILE} ${METRICS_SERVICE_PB_CPP_FILE} ${METRICS_SERVICE_GRPC_PB_CPP_FILE}) +else() +add_library( + opentelemetry_proto STATIC + ${COMMON_PB_CPP_FILE} + ${RESOURCE_PB_CPP_FILE} + ${TRACE_PB_CPP_FILE} + ${LOGS_PB_CPP_FILE} + ${METRICS_PB_CPP_FILE} + ${TRACE_SERVICE_PB_CPP_FILE} + ${LOGS_SERVICE_PB_CPP_FILE} + ${METRICS_SERVICE_PB_CPP_FILE}) +endif() if(needs_proto_download) add_dependencies(opentelemetry_proto opentelemetry-proto) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 85db78a6e9..70bca10221 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -2,6 +2,8 @@ add_subdirectory(common) include_directories(common) if(WITH_OTLP_GRPC OR WITH_OTLP_HTTP) add_subdirectory(otlp) +endif() +if(WITH_OTLP_GRPC) add_subdirectory(grpc) endif() if(WITH_ETW) From 2ead5688490a7433fb1536f29beba6029d1217d8 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Tue, 16 Nov 2021 15:10:19 +0000 Subject: [PATCH 8/8] comments --- .../common/logs_foo_library/foo_library.cc | 8 ++--- examples/otlp/http_log_main.cc | 34 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/examples/common/logs_foo_library/foo_library.cc b/examples/common/logs_foo_library/foo_library.cc index 50ea4a48f2..a5b8d9ae98 100644 --- a/examples/common/logs_foo_library/foo_library.cc +++ b/examples/common/logs_foo_library/foo_library.cc @@ -22,7 +22,7 @@ nostd::shared_ptr get_tracer() nostd::shared_ptr get_logger() { auto provider = logs::Provider::GetLoggerProvider(); - return provider->GetLogger("foo_library_logger", nostd::span()); + return provider->GetLogger("foo_library_logger"); } } // namespace @@ -32,9 +32,7 @@ void foo_library() 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", - std::map(), std::map(), - ctx.trace_id(), ctx.span_id(), ctx.trace_flags(), - opentelemetry::common::SystemTimestamp()); + logger->Log(opentelemetry::logs::Severity::kDebug, "name", "body", {}, {}, ctx.trace_id(), + ctx.span_id(), ctx.trace_flags(), opentelemetry::common::SystemTimestamp()); } #endif diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc index e61faf4c7a..0f1989d853 100644 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -19,13 +19,12 @@ # include "logs_foo_library/foo_library.h" # endif -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; -namespace otlp = opentelemetry::exporter::otlp; -namespace sdklogs = opentelemetry::sdk::logs; -namespace logs_api = opentelemetry::logs; - -namespace sdktrace = opentelemetry::sdk::trace; +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 { @@ -34,11 +33,11 @@ opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts; void InitTracer() { // Create OTLP exporter instance - auto exporter = std::unique_ptr(new otlp::OtlpHttpExporter(opts)); - auto processor = std::unique_ptr( - new sdktrace::SimpleSpanProcessor(std::move(exporter))); + auto exporter = std::unique_ptr(new otlp::OtlpHttpExporter(opts)); + auto processor = std::unique_ptr( + new trace_sdk::SimpleSpanProcessor(std::move(exporter))); auto provider = - nostd::shared_ptr(new sdktrace::TracerProvider(std::move(processor))); + nostd::shared_ptr(new trace_sdk::TracerProvider(std::move(processor))); // Set the global trace provider trace::Provider::SetTracerProvider(provider); } @@ -48,13 +47,14 @@ void InitLogger() { logger_opts.console_debug = true; // Create OTLP exporter instance - auto exporter = std::unique_ptr(new otlp::OtlpHttpLogExporter(logger_opts)); - auto processor = - std::shared_ptr(new sdklogs::SimpleLogProcessor(std::move(exporter))); - auto sdkProvider = std::shared_ptr(new sdklogs::LoggerProvider()); + auto exporter = + std::unique_ptr(new otlp::OtlpHttpLogExporter(logger_opts)); + auto processor = std::shared_ptr( + new logs_sdk::SimpleLogProcessor(std::move(exporter))); + auto sdkProvider = std::shared_ptr(new logs_sdk::LoggerProvider()); sdkProvider->SetProcessor(processor); - auto apiProvider = nostd::shared_ptr(sdkProvider); - auto provider = nostd::shared_ptr(apiProvider); + auto apiProvider = nostd::shared_ptr(sdkProvider); + auto provider = nostd::shared_ptr(apiProvider); opentelemetry::logs::Provider::SetLoggerProvider(provider); } } // namespace