Skip to content
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

Add Zipkin example #917

Merged
merged 8 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ endif()
if(WITH_JAEGER)
add_subdirectory(jaeger)
endif()
if(WITH_ZIPKIN)
add_subdirectory(zipkin)
endif()
add_subdirectory(plugin)
add_subdirectory(simple)
add_subdirectory(batch)
Expand Down
10 changes: 10 additions & 0 deletions examples/zipkin/CMakeLists.txt
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)
31 changes: 31 additions & 0 deletions examples/zipkin/README.md
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`
37 changes: 37 additions & 0 deletions examples/zipkin/foo_library/foo_library.cc
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();
}
6 changes: 6 additions & 0 deletions examples/zipkin/foo_library/foo_library.h
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();
45 changes: 45 additions & 0 deletions examples/zipkin/main.cc
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();
}