-
Notifications
You must be signed in to change notification settings - Fork 559
Add OTLP exporter example #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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", | ||
| "//exporters/otlp:otlp_exporter", | ||
| "//sdk/src/trace", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # OTLP Exporter Example | ||
|
|
||
| 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). | ||
|
|
||
| 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 [Getting Started](https://opentelemetry.io/docs/collector/about/). | ||
|
|
||
| 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: | ||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include "opentelemetry/context/threadlocal_context.h" | ||
| #include "opentelemetry/trace/provider.h" | ||
|
|
||
| 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"); | ||
| } | ||
|
|
||
| // 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"); | ||
| 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(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #pragma once | ||
|
|
||
| void foo_library(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<sdktrace::SpanExporter>(new otlp::OtlpExporter); | ||
|
|
||
| auto processor = std::shared_ptr<sdktrace::SpanProcessor>( | ||
| new sdktrace::SimpleSpanProcessor(std::move(exporter))); | ||
| auto provider = nostd::shared_ptr<trace::TracerProvider>(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(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.