-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,10 @@ | ||
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 | ||
opentelemetry_trace opentelemetry_exporter_zipkin_trace) |
This file contains 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 @@ | ||
# Zipkin Exporter Example | ||
|
||
This is an example of how to use the Zipkin exporter. | ||
|
||
The application in `main.cc` initializes an `ZipkinExporter` 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/main/api). | ||
|
||
Resulting spans are exported to the Zipkin server using the Zipkin exporter. | ||
|
||
Note that the Zipkin exporter connects to the server at `localhost:9411` by | ||
default. | ||
|
||
## Running Zipkin server locally | ||
|
||
The quick way to run the Zipkin server is using Docker container : | ||
|
||
``console | ||
|
||
$ docker run -d -p 9411:9411 openzipkin/zipkin | ||
|
||
`` | ||
|
||
## Running Zipkin example | ||
|
||
Build this example using instructions in [INSTALL.md](../../INSTALL.md). | ||
|
||
## Viewing the traces | ||
|
||
Please visit the Zipkin UI endpoint `http://localhost:9411` |
This file contains 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,37 @@ | ||
// 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<trace::Tracer> 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(); | ||
} |
This file contains 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 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
void foo_library(); |
This file contains 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,45 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/exporters/zipkin/zipkin_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 zipkin = opentelemetry::exporter::zipkin; | ||
|
||
namespace | ||
{ | ||
opentelemetry::exporter::zipkin::ZipkinExporterOptions opts; | ||
void InitTracer() | ||
{ | ||
// Create zipkin exporter instance | ||
opentelemetry::sdk::resource::ResourceAttributes attributes = { | ||
{"service.name", "zipkin_demo_service"}}; | ||
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes); | ||
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(new zipkin::ZipkinExporter(opts)); | ||
auto processor = std::unique_ptr<sdktrace::SpanProcessor>( | ||
new sdktrace::SimpleSpanProcessor(std::move(exporter))); | ||
auto provider = nostd::shared_ptr<trace::TracerProvider>( | ||
new sdktrace::TracerProvider(std::move(processor), resource)); | ||
// Set the global trace provider | ||
trace::Provider::SetTracerProvider(provider); | ||
} | ||
} // namespace | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc == 2) | ||
{ | ||
opts.endpoint = argv[1]; | ||
} | ||
// Removing this line will leave the default noop TracerProvider in place. | ||
InitTracer(); | ||
|
||
foo_library(); | ||
} |