From af75379971bab0fce4a2338edf5b7efcba9967aa Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sat, 30 Oct 2021 04:55:21 +0200 Subject: [PATCH] Extract foo library into a separate library (#1043) --- examples/CMakeLists.txt | 2 + examples/common/CMakeLists.txt | 1 + examples/common/foo_library/BUILD | 16 +++++++ examples/common/foo_library/CMakeLists.txt | 3 ++ .../foo_library/foo_library.cc | 0 .../foo_library/foo_library.h | 0 examples/jaeger/BUILD | 16 +------ examples/jaeger/CMakeLists.txt | 6 +-- examples/jaeger/main.cc | 6 ++- examples/multi_processor/BUILD | 15 +------ examples/multi_processor/CMakeLists.txt | 9 ++-- .../foo_library/foo_library.cc | 45 ------------------- .../multi_processor/foo_library/foo_library.h | 6 --- examples/multi_processor/main.cc | 6 ++- examples/otlp/BUILD | 17 +------ examples/otlp/CMakeLists.txt | 12 ++--- examples/otlp/foo_library/foo_library.cc | 36 --------------- examples/otlp/foo_library/foo_library.h | 6 --- examples/otlp/grpc_main.cc | 6 ++- examples/otlp/http_main.cc | 6 ++- examples/simple/BUILD | 16 +------ examples/simple/CMakeLists.txt | 9 ++-- examples/simple/foo_library/foo_library.cc | 45 ------------------- examples/simple/foo_library/foo_library.h | 6 --- examples/simple/main.cc | 6 ++- examples/zipkin/CMakeLists.txt | 6 +-- examples/zipkin/foo_library/foo_library.cc | 37 --------------- examples/zipkin/foo_library/foo_library.h | 6 --- examples/zipkin/main.cc | 6 ++- 29 files changed, 69 insertions(+), 282 deletions(-) create mode 100644 examples/common/CMakeLists.txt create mode 100644 examples/common/foo_library/BUILD create mode 100644 examples/common/foo_library/CMakeLists.txt rename examples/{jaeger => common}/foo_library/foo_library.cc (100%) rename examples/{jaeger => common}/foo_library/foo_library.h (100%) delete mode 100644 examples/multi_processor/foo_library/foo_library.cc delete mode 100644 examples/multi_processor/foo_library/foo_library.h delete mode 100644 examples/otlp/foo_library/foo_library.cc delete mode 100644 examples/otlp/foo_library/foo_library.h delete mode 100644 examples/simple/foo_library/foo_library.cc delete mode 100644 examples/simple/foo_library/foo_library.h delete mode 100644 examples/zipkin/foo_library/foo_library.cc delete mode 100644 examples/zipkin/foo_library/foo_library.h diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 327f067121..85db78a6e9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(common) +include_directories(common) if(WITH_OTLP_GRPC OR WITH_OTLP_HTTP) add_subdirectory(otlp) add_subdirectory(grpc) diff --git a/examples/common/CMakeLists.txt b/examples/common/CMakeLists.txt new file mode 100644 index 0000000000..38ff619308 --- /dev/null +++ b/examples/common/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(foo_library) diff --git a/examples/common/foo_library/BUILD b/examples/common/foo_library/BUILD new file mode 100644 index 0000000000..beffe5ca10 --- /dev/null +++ b/examples/common/foo_library/BUILD @@ -0,0 +1,16 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "common_foo_library", + srcs = [ + "foo_library.cc", + ], + hdrs = [ + "foo_library.h", + ], + defines = ["BAZEL_BUILD"], + deps = [ + "//api", + "//sdk:headers", + ], +) diff --git a/examples/common/foo_library/CMakeLists.txt b/examples/common/foo_library/CMakeLists.txt new file mode 100644 index 0000000000..a13512ffeb --- /dev/null +++ b/examples/common/foo_library/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(common_foo_library foo_library.h foo_library.cc) +target_link_libraries(common_foo_library PUBLIC ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_api) diff --git a/examples/jaeger/foo_library/foo_library.cc b/examples/common/foo_library/foo_library.cc similarity index 100% rename from examples/jaeger/foo_library/foo_library.cc rename to examples/common/foo_library/foo_library.cc diff --git a/examples/jaeger/foo_library/foo_library.h b/examples/common/foo_library/foo_library.h similarity index 100% rename from examples/jaeger/foo_library/foo_library.h rename to examples/common/foo_library/foo_library.h diff --git a/examples/jaeger/BUILD b/examples/jaeger/BUILD index c55033f1a2..0eca4845d7 100644 --- a/examples/jaeger/BUILD +++ b/examples/jaeger/BUILD @@ -1,17 +1,3 @@ -cc_library( - name = "foo_library", - srcs = [ - "foo_library/foo_library.cc", - ], - hdrs = [ - "foo_library/foo_library.h", - ], - deps = [ - "//api", - "//sdk:headers", - ], -) - # TODO: enable bazel build # cc_binary( # name = "example_jaeger", @@ -19,7 +5,7 @@ cc_library( # "main.cc", # ], # deps = [ -# ":foo_library", +# "//examples/common/foo_library:common_foo_library", # "//api", # "//exporters/jaeger:jaeger_exporter", # "//sdk/src/trace", diff --git a/examples/jaeger/CMakeLists.txt b/examples/jaeger/CMakeLists.txt index d705a8868c..713521c3a7 100644 --- a/examples/jaeger/CMakeLists.txt +++ b/examples/jaeger/CMakeLists.txt @@ -1,10 +1,6 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/jaeger/include) -add_library(jaeger_foo_library foo_library/foo_library.cc) -target_link_libraries(jaeger_foo_library ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_api) - add_executable(example_jaeger main.cc) target_link_libraries( - example_jaeger ${CMAKE_THREAD_LIBS_INIT} jaeger_foo_library + example_jaeger ${CMAKE_THREAD_LIBS_INIT} common_foo_library opentelemetry_trace opentelemetry_exporter_jaeger_trace) diff --git a/examples/jaeger/main.cc b/examples/jaeger/main.cc index 63ed476aad..4005e3960f 100644 --- a/examples/jaeger/main.cc +++ b/examples/jaeger/main.cc @@ -6,7 +6,11 @@ #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/provider.h" -#include "foo_library/foo_library.h" +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; diff --git a/examples/multi_processor/BUILD b/examples/multi_processor/BUILD index a5464bdfc2..71284e1473 100644 --- a/examples/multi_processor/BUILD +++ b/examples/multi_processor/BUILD @@ -1,24 +1,11 @@ -cc_library( - name = "foo_multi_library", - srcs = [ - "foo_library/foo_library.cc", - ], - hdrs = [ - "foo_library/foo_library.h", - ], - deps = [ - "//api", - ], -) - cc_binary( name = "example_multi_processor", srcs = [ "main.cc", ], deps = [ - ":foo_multi_library", "//api", + "//examples/common/foo_library:common_foo_library", "//exporters/memory:in_memory_span_exporter", "//exporters/ostream:ostream_span_exporter", "//sdk/src/trace", diff --git a/examples/multi_processor/CMakeLists.txt b/examples/multi_processor/CMakeLists.txt index 65c7c2e510..a1e4ac9ea5 100644 --- a/examples/multi_processor/CMakeLists.txt +++ b/examples/multi_processor/CMakeLists.txt @@ -1,10 +1,7 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include ${CMAKE_SOURCE_DIR}/exporters/memory/include) -add_library(foo_multi_library foo_library/foo_library.cc) -target_link_libraries(foo_multi_library opentelemetry_exporter_ostream_span - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) - add_executable(example_multi_processor main.cc) -target_link_libraries(example_multi_processor ${CMAKE_THREAD_LIBS_INIT} - foo_multi_library opentelemetry_trace) +target_link_libraries( + example_multi_processor ${CMAKE_THREAD_LIBS_INIT} common_foo_library + opentelemetry_trace opentelemetry_exporter_ostream_span) diff --git a/examples/multi_processor/foo_library/foo_library.cc b/examples/multi_processor/foo_library/foo_library.cc deleted file mode 100644 index b1df37a84c..0000000000 --- a/examples/multi_processor/foo_library/foo_library.cc +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "opentelemetry/trace/provider.h" - -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; - -namespace -{ -nostd::shared_ptr get_tracer() -{ - auto provider = trace::Provider::GetTracerProvider(); - return provider->GetTracer("foo_library"); -} - -void f1() -{ - auto span = get_tracer()->StartSpan("f1"); - auto scope = get_tracer()->WithActiveSpan(span); - - span->End(); -} - -void f2() -{ - auto span = get_tracer()->StartSpan("f2"); - auto scope = get_tracer()->WithActiveSpan(span); - - f1(); - f1(); - - span->End(); -} -} // namespace - -void foo_library() -{ - auto span = get_tracer()->StartSpan("library"); - auto scope = get_tracer()->WithActiveSpan(span); - - f2(); - - span->End(); -} diff --git a/examples/multi_processor/foo_library/foo_library.h b/examples/multi_processor/foo_library/foo_library.h deleted file mode 100644 index daebb2603f..0000000000 --- a/examples/multi_processor/foo_library/foo_library.h +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -void foo_library(); diff --git a/examples/multi_processor/main.cc b/examples/multi_processor/main.cc index 8246a2ec91..a75299f3ab 100644 --- a/examples/multi_processor/main.cc +++ b/examples/multi_processor/main.cc @@ -7,7 +7,11 @@ #include "opentelemetry/trace/provider.h" // Using an exporter that simply dumps span data to stdout. -#include "foo_library/foo_library.h" +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif #include "opentelemetry/exporters/memory/in_memory_span_exporter.h" #include "opentelemetry/exporters/ostream/span_exporter.h" diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 2892cfa86c..34842c01f2 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -1,24 +1,11 @@ -cc_library( - name = "foo_library", - srcs = [ - "foo_library/foo_library.cc", - ], - hdrs = [ - "foo_library/foo_library.h", - ], - deps = [ - "//api", - ], -) - cc_binary( name = "example_otlp_grpc", srcs = [ "grpc_main.cc", ], deps = [ - ":foo_library", "//api", + "//examples/common/foo_library:common_foo_library", "//exporters/otlp:otlp_grpc_exporter", "//sdk/src/trace", ], @@ -30,8 +17,8 @@ cc_binary( "http_main.cc", ], deps = [ - ":foo_library", "//api", + "//examples/common/foo_library:common_foo_library", "//exporters/otlp:otlp_http_exporter", "//sdk/src/trace", ], diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index 0e19afbcf8..4618695d3b 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -1,21 +1,17 @@ include_directories( - ${CMAKE_BINARY_DIR}/generated/third_party/opentelemetry-proto) -include_directories(${CMAKE_SOURCE_DIR}/exporters/otlp/include) - -add_library(otlp_foo_library foo_library/foo_library.cc) -target_link_libraries(otlp_foo_library ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_api) + ${CMAKE_BINARY_DIR}/generated/third_party/opentelemetry-proto + ${CMAKE_SOURCE_DIR}/exporters/otlp/include) if(WITH_OTLP_GRPC) add_executable(example_otlp_grpc grpc_main.cc) target_link_libraries( - example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library + example_otlp_grpc ${CMAKE_THREAD_LIBS_INIT} common_foo_library opentelemetry_trace opentelemetry_exporter_otlp_grpc gRPC::grpc++) endif() if(WITH_OTLP_HTTP) add_executable(example_otlp_http http_main.cc) target_link_libraries( - example_otlp_http ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library + example_otlp_http ${CMAKE_THREAD_LIBS_INIT} common_foo_library opentelemetry_trace opentelemetry_exporter_otlp_http) endif() diff --git a/examples/otlp/foo_library/foo_library.cc b/examples/otlp/foo_library/foo_library.cc deleted file mode 100644 index 05ee35d537..0000000000 --- a/examples/otlp/foo_library/foo_library.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "opentelemetry/trace/provider.h" - -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; - -namespace -{ -nostd::shared_ptr get_tracer() -{ - auto provider = trace::Provider::GetTracerProvider(); - return provider->GetTracer("foo_library"); -} - -void f1() -{ - auto scoped_span = trace::Scope(get_tracer()->StartSpan("f1")); -} - -void f2() -{ - auto scoped_span = trace::Scope(get_tracer()->StartSpan("f2")); - - f1(); - f1(); -} -} // namespace - -void foo_library() -{ - auto scoped_span = trace::Scope(get_tracer()->StartSpan("library")); - - f2(); -} diff --git a/examples/otlp/foo_library/foo_library.h b/examples/otlp/foo_library/foo_library.h deleted file mode 100644 index daebb2603f..0000000000 --- a/examples/otlp/foo_library/foo_library.h +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -void foo_library(); diff --git a/examples/otlp/grpc_main.cc b/examples/otlp/grpc_main.cc index 7789457909..303479f4a5 100644 --- a/examples/otlp/grpc_main.cc +++ b/examples/otlp/grpc_main.cc @@ -9,7 +9,11 @@ #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/provider.h" -#include "foo_library/foo_library.h" +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; diff --git a/examples/otlp/http_main.cc b/examples/otlp/http_main.cc index a35fb4b35b..2c5779bdd0 100644 --- a/examples/otlp/http_main.cc +++ b/examples/otlp/http_main.cc @@ -8,7 +8,11 @@ #include -#include "foo_library/foo_library.h" +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; diff --git a/examples/simple/BUILD b/examples/simple/BUILD index a8e843e29d..c87961789a 100644 --- a/examples/simple/BUILD +++ b/examples/simple/BUILD @@ -1,25 +1,11 @@ -cc_library( - name = "foo_library", - srcs = [ - "foo_library/foo_library.cc", - ], - hdrs = [ - "foo_library/foo_library.h", - ], - deps = [ - "//api", - ], -) - cc_binary( name = "example_simple", srcs = [ "main.cc", ], deps = [ - ":foo_library", "//api", + "//examples/common/foo_library:common_foo_library", "//exporters/ostream:ostream_span_exporter", - "//sdk/src/trace", ], ) diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index b86593ee37..4368039c1d 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -1,9 +1,6 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include) -add_library(foo_library foo_library/foo_library.cc) -target_link_libraries(foo_library opentelemetry_exporter_ostream_span - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) - add_executable(example_simple main.cc) -target_link_libraries(example_simple ${CMAKE_THREAD_LIBS_INIT} foo_library - opentelemetry_trace) +target_link_libraries( + example_simple ${CMAKE_THREAD_LIBS_INIT} common_foo_library + opentelemetry_trace opentelemetry_exporter_ostream_span) diff --git a/examples/simple/foo_library/foo_library.cc b/examples/simple/foo_library/foo_library.cc deleted file mode 100644 index b1df37a84c..0000000000 --- a/examples/simple/foo_library/foo_library.cc +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "opentelemetry/trace/provider.h" - -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; - -namespace -{ -nostd::shared_ptr get_tracer() -{ - auto provider = trace::Provider::GetTracerProvider(); - return provider->GetTracer("foo_library"); -} - -void f1() -{ - auto span = get_tracer()->StartSpan("f1"); - auto scope = get_tracer()->WithActiveSpan(span); - - span->End(); -} - -void f2() -{ - auto span = get_tracer()->StartSpan("f2"); - auto scope = get_tracer()->WithActiveSpan(span); - - f1(); - f1(); - - span->End(); -} -} // namespace - -void foo_library() -{ - auto span = get_tracer()->StartSpan("library"); - auto scope = get_tracer()->WithActiveSpan(span); - - f2(); - - span->End(); -} diff --git a/examples/simple/foo_library/foo_library.h b/examples/simple/foo_library/foo_library.h deleted file mode 100644 index daebb2603f..0000000000 --- a/examples/simple/foo_library/foo_library.h +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -void foo_library(); diff --git a/examples/simple/main.cc b/examples/simple/main.cc index 3d7102d009..bd1f1eeb08 100644 --- a/examples/simple/main.cc +++ b/examples/simple/main.cc @@ -6,7 +6,11 @@ #include "opentelemetry/trace/provider.h" // Using an exporter that simply dumps span data to stdout. -#include "foo_library/foo_library.h" +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif #include "opentelemetry/exporters/ostream/span_exporter.h" namespace diff --git a/examples/zipkin/CMakeLists.txt b/examples/zipkin/CMakeLists.txt index 40affab413..f55f363d72 100644 --- a/examples/zipkin/CMakeLists.txt +++ b/examples/zipkin/CMakeLists.txt @@ -1,10 +1,6 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/zipkin/include) -add_library(zipkin_foo_library foo_library/foo_library.cc) -target_link_libraries(zipkin_foo_library ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_api) - add_executable(example_zipkin main.cc) target_link_libraries( - example_zipkin ${CMAKE_THREAD_LIBS_INIT} zipkin_foo_library + example_zipkin ${CMAKE_THREAD_LIBS_INIT} common_foo_library opentelemetry_trace opentelemetry_exporter_zipkin_trace) diff --git a/examples/zipkin/foo_library/foo_library.cc b/examples/zipkin/foo_library/foo_library.cc deleted file mode 100644 index a0dec3c766..0000000000 --- a/examples/zipkin/foo_library/foo_library.cc +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#include "opentelemetry/sdk/version/version.h" -#include "opentelemetry/trace/provider.h" - -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); -} - -void f1() -{ - auto scoped_span = trace::Scope(get_tracer()->StartSpan("f1")); -} - -void f2() -{ - auto scoped_span = trace::Scope(get_tracer()->StartSpan("f2")); - - f1(); - f1(); -} -} // namespace - -void foo_library() -{ - auto scoped_span = trace::Scope(get_tracer()->StartSpan("library")); - - f2(); -} diff --git a/examples/zipkin/foo_library/foo_library.h b/examples/zipkin/foo_library/foo_library.h deleted file mode 100644 index daebb2603f..0000000000 --- a/examples/zipkin/foo_library/foo_library.h +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -void foo_library(); diff --git a/examples/zipkin/main.cc b/examples/zipkin/main.cc index 3ae7539cda..93ec9f3c3e 100644 --- a/examples/zipkin/main.cc +++ b/examples/zipkin/main.cc @@ -6,7 +6,11 @@ #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/provider.h" -#include "foo_library/foo_library.h" +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +#endif namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd;