From 694bb2c213531748fb4d57bf03c7fef7125240ed Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 25 Aug 2020 19:05:06 +0000 Subject: [PATCH 1/4] Add OTLP exporter example --- examples/otlp/BUILD | 25 ++++++++++++++++ examples/otlp/CMakeLists.txt | 6 ++++ examples/otlp/README.md | 31 ++++++++++++++++++++ examples/otlp/foo_library/foo_library.cc | 37 ++++++++++++++++++++++++ examples/otlp/foo_library/foo_library.h | 3 ++ examples/otlp/main.cc | 34 ++++++++++++++++++++++ 6 files changed, 136 insertions(+) create mode 100644 examples/otlp/BUILD create mode 100644 examples/otlp/CMakeLists.txt create mode 100644 examples/otlp/README.md create mode 100644 examples/otlp/foo_library/foo_library.cc create mode 100644 examples/otlp/foo_library/foo_library.h create mode 100644 examples/otlp/main.cc diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD new file mode 100644 index 0000000000..223658e2bc --- /dev/null +++ b/examples/otlp/BUILD @@ -0,0 +1,25 @@ +cc_library( + name = "foo_library", + srcs = [ + "foo_library/foo_library.cc", + ], + hdrs = [ + "foo_library/foo_library.h", + ], + deps = [ + "//api", + ], +) + +cc_binary( + name = "example_otlp", + srcs = [ + "main.cc", + ], + deps = [ + ":foo_library", + "//api", + "//sdk/src/trace", + "//exporters/otlp:otlp_exporter" + ], +) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt new file mode 100644 index 0000000000..4c5f2bbd57 --- /dev/null +++ b/examples/otlp/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(foo_library foo_library/foo_library.cc) +target_link_libraries(foo_library ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) + +add_executable(example_otlp main.cc) +target_link_libraries(example_otlp ${CMAKE_THREAD_LIBS_INIT} foo_library + opentelemetry_trace) diff --git a/examples/otlp/README.md b/examples/otlp/README.md new file mode 100644 index 0000000000..431d72344f --- /dev/null +++ b/examples/otlp/README.md @@ -0,0 +1,31 @@ +# OTLP Exporter Example + +This is an example of how to use the OTLP exporter. + +The application in `main.cc` initializes an `OtlpExporter` instance, and uses it to 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/master/api). + +Resulting spans are exported to the OpenTelemetry Collector using the OTLP exporter. The OpenTelemetry Collector can be configured to export to other backends (see list of [supported backends](https://github.com/open-telemetry/opentelemetry-collector/blob/master/exporter/README.md)). + +For instructions on downloading and running the OpenTelemetry Collector, see [OpenTelemetry Collector Getting Started](https://opentelemetry.io/docs/collector/about/). + +Here is an example of a Collector `config.yaml` file to export to [Zipkin](https://zipkin.io/) using the OTLP exporter: + +``` +receivers: + otlp: + protocols: + grpc: + endpoint: localhost:55680 +exporters: + zipkin: + endpoint: "http://localhost:9411/api/v2/spans" +service: + pipelines: + traces: + receivers: [otlp] + exporters: [zipkin] +``` + +Note that the OTLP exporter connects to the Collector at `localhost:55680` by default. + +Once you have the Collector running, see [CONTRIBUTING.md](../../CONTRIBUTING.md) for instructions on building and running the example. diff --git a/examples/otlp/foo_library/foo_library.cc b/examples/otlp/foo_library/foo_library.cc new file mode 100644 index 0000000000..3404027602 --- /dev/null +++ b/examples/otlp/foo_library/foo_library.cc @@ -0,0 +1,37 @@ +#include "opentelemetry/context/threadlocal_context.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"); +} + +void f1() +{ + auto span = get_tracer()->StartSpan("f1"); + span->End(); +} + +void f2() +{ + auto span = get_tracer()->StartSpan("f2"); + + f1(); + f1(); + span->End(); +} +} // namespace + +void foo_library() +{ + auto span = get_tracer()->StartSpan("library"); + + f2(); + span->End(); +} diff --git a/examples/otlp/foo_library/foo_library.h b/examples/otlp/foo_library/foo_library.h new file mode 100644 index 0000000000..7ac75c0e50 --- /dev/null +++ b/examples/otlp/foo_library/foo_library.h @@ -0,0 +1,3 @@ +#pragma once + +void foo_library(); diff --git a/examples/otlp/main.cc b/examples/otlp/main.cc new file mode 100644 index 0000000000..233b21438f --- /dev/null +++ b/examples/otlp/main.cc @@ -0,0 +1,34 @@ +#include "opentelemetry/exporters/otlp/otlp_exporter.h" +#include "opentelemetry/sdk/trace/simple_processor.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/provider.h" + +#include "foo_library/foo_library.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; +namespace sdktrace = opentelemetry::sdk::trace; +namespace otlp = opentelemetry::exporter::otlp; + +namespace +{ +void initTracer() +{ + // Create OTLP exporter instance + auto exporter = std::unique_ptr(new otlp::OtlpExporter); + + auto processor = std::shared_ptr( + new sdktrace::SimpleSpanProcessor(std::move(exporter))); + auto provider = nostd::shared_ptr(new sdktrace::TracerProvider(processor)); + // Set the global trace provider + trace::Provider::SetTracerProvider(provider); +} +} // namespace + +int main() +{ + // Removing this line will leave the default noop TracerProvider in place. + initTracer(); + + foo_library(); +} From dc58fd42d4f0666d47ac5329314f20377c1b386b Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 25 Aug 2020 19:30:59 +0000 Subject: [PATCH 2/4] Update README --- examples/otlp/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/otlp/README.md b/examples/otlp/README.md index 431d72344f..feb69661f1 100644 --- a/examples/otlp/README.md +++ b/examples/otlp/README.md @@ -1,14 +1,14 @@ # OTLP Exporter Example -This is an example of how to use the OTLP exporter. +This is an example of how to use the [OpenTelemetry Protocol](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/protocol/README.md) (OTLP) exporter. -The application in `main.cc` initializes an `OtlpExporter` instance, and uses it to 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/master/api). +The application in `main.cc` initializes an `OtlpExporter` instance and uses it to 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/master/api). -Resulting spans are exported to the OpenTelemetry Collector using the OTLP exporter. The OpenTelemetry Collector can be configured to export to other backends (see list of [supported backends](https://github.com/open-telemetry/opentelemetry-collector/blob/master/exporter/README.md)). +Resulting spans are exported to the **OpenTelemetry Collector** using the OTLP exporter. The OpenTelemetry Collector can be configured to export to other backends (see list of [supported backends](https://github.com/open-telemetry/opentelemetry-collector/blob/master/exporter/README.md)). -For instructions on downloading and running the OpenTelemetry Collector, see [OpenTelemetry Collector Getting Started](https://opentelemetry.io/docs/collector/about/). +For instructions on downloading and running the OpenTelemetry Collector, see [Getting Started](https://opentelemetry.io/docs/collector/about/). -Here is an example of a Collector `config.yaml` file to export to [Zipkin](https://zipkin.io/) using the OTLP exporter: +Here is an example of a Collector `config.yaml` file that can be used to export to [Zipkin](https://zipkin.io/) via the Collector using the OTLP exporter: ``` receivers: From ed5b7559120e203261872f378a8ba692bda5748d Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 25 Aug 2020 20:19:31 +0000 Subject: [PATCH 3/4] Fix formatting --- examples/otlp/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 223658e2bc..29a4035697 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -19,7 +19,7 @@ cc_binary( deps = [ ":foo_library", "//api", + "//exporters/otlp:otlp_exporter", "//sdk/src/trace", - "//exporters/otlp:otlp_exporter" ], ) From e72222f5000b39cfda9705fd13a68f02d2ae1dae Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 26 Aug 2020 00:26:33 +0000 Subject: [PATCH 4/4] Add TODO in foo_library --- examples/otlp/foo_library/foo_library.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/otlp/foo_library/foo_library.cc b/examples/otlp/foo_library/foo_library.cc index 3404027602..fb79781635 100644 --- a/examples/otlp/foo_library/foo_library.cc +++ b/examples/otlp/foo_library/foo_library.cc @@ -12,6 +12,9 @@ nostd::shared_ptr get_tracer() return provider->GetTracer("foo_library"); } +// TODO: Remove all calls to span->End() once context memory issue is fixed +// (https://github.com/open-telemetry/opentelemetry-cpp/issues/287) + void f1() { auto span = get_tracer()->StartSpan("f1");