Skip to content

Commit

Permalink
Merge branch 'main' into sem-conv-trace
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Jul 30, 2021
2 parents 9db4611 + ef16d00 commit 3818109
Show file tree
Hide file tree
Showing 51 changed files with 791 additions and 182 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Increment the:

## [Unreleased]

* [BUILD] Allow to use local GSL
* [EXPORTER] Jaeger Exporter - Add Thrift HTTP exporter ([#926](https://github.com/open-telemetry/opentelemetry-cpp/pull/926))

## [1.0.0-rc3] 2021-07-12

* [DOCS] Add doxygen reference docs for SDK ([#902](https://github.com/open-telemetry/opentelemetry-cpp/pull/902))
Expand Down
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ if(WITH_STL)
# Guidelines Support Library path. Used if we are not on not get C++20.
#
# TODO: respect WITH_ABSEIL as alternate implementation of std::span
set(GSL_DIR third_party/ms-gsl)
include_directories(${GSL_DIR}/include)
find_package(Microsoft.GSL QUIET)
if(TARGET Microsoft.GSL::GSL)
list(APPEND CORE_RUNTIME_LIBS Microsoft.GSL::GSL)
else()
set(GSL_DIR third_party/ms-gsl)
include_directories(${GSL_DIR}/include)
endif()

# Optimize for speed to reduce the hops
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Expand Down
12 changes: 12 additions & 0 deletions docs/building-with-vcpkg.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ dependencies:
C++14 or C++17 compiler.
- `nlohmann-json` - required when building with zPages module.
- `prometheus-cpp` - required for Prometheus exporter.
- `gRPC` and `Protobuf` - required for OTLP exporter

It is possible to adjust the build system to use either vcpkg-installed
dependencies or OS-provided dependencies, e.g. `brew` or `deb` packages.
To install the dependencies through vcpkg,

- Set the VCPKG_ROOT env variable to the vcpkg install directory, or
- Set the CMake variable CMAKE_TOOLCHAIN_FILE to vcpkg toolchain file `vcpkg.cmake`.

With either of these settings, the appropriate vcpkg folders get added to the cmake
search path, and makes the required libraries to be found through `find_package()`.

The opentelemetry-cpp repo also brings the vcpkg package under `tools` directory.
This would be used during Windows builds to install the missing dependencies ONLY
if the external vcpkg toolchain is not configured.
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
6 changes: 5 additions & 1 deletion examples/batch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ void initTracer()
// We export `kNumSpans` after every `schedule_delay_millis` milliseconds.
options.max_export_batch_size = kNumSpans;

opentelemetry::sdk::resource::ResourceAttributes attributes = {{"service", "test_service"},
{"version", (uint32_t)1}};
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);

auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::BatchSpanProcessor(std::move(exporter), options));

auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));
new sdktrace::TracerProvider(std::move(processor), resource));
// Set the global trace provider.
opentelemetry::trace::Provider::SetTracerProvider(provider);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/jaeger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ target_link_libraries(jaeger_foo_library ${CMAKE_THREAD_LIBS_INIT}
add_executable(example_jaeger main.cc)
target_link_libraries(
example_jaeger ${CMAKE_THREAD_LIBS_INIT} jaeger_foo_library
opentelemetry_trace jaeger_trace_exporter)
opentelemetry_trace opentelemetry_exporter_jaeger_trace)
2 changes: 1 addition & 1 deletion examples/jaeger/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char *argv[])
{
if (argc == 2)
{
opts.server_addr = argv[1];
opts.endpoint = argv[1];
}
// Removing this line will leave the default noop TracerProvider in place.
InitTracer();
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();
}
3 changes: 2 additions & 1 deletion exporters/elasticsearch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ install(
DIRECTORY include/opentelemetry/exporters/elasticsearch
DESTINATION include/opentelemetry/exporters/
FILES_MATCHING
PATTERN "*.h")
PATTERN "*.h"
PATTERN "es_log_recordable.h" EXCLUDE)

if(BUILD_TESTING)
add_executable(es_log_exporter_test test/es_log_exporter_test.cc)
Expand Down
34 changes: 14 additions & 20 deletions exporters/elasticsearch/src/es_log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

#ifdef ENABLE_LOGS_PREVIEW

# include <sstream> // std::stringstream

# include "opentelemetry/exporters/elasticsearch/es_log_exporter.h"
# include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
# include "opentelemetry/sdk_config.h"

namespace nostd = opentelemetry::nostd;
namespace sdklogs = opentelemetry::sdk::logs;
Expand Down Expand Up @@ -74,23 +77,19 @@ class ResponseHandler : public http_client::EventHandler
switch (state)
{
case http_client::SessionState::ConnectFailed:
if (console_debug_)
std::cout << "Connection to elasticsearch failed\n";
OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Connection to elasticsearch failed");
cv_.notify_all();
break;
case http_client::SessionState::SendFailed:
if (console_debug_)
std::cout << "Request failed to be sent to elasticsearch\n";
OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request failed to be sent to elasticsearch");
cv_.notify_all();
break;
case http_client::SessionState::TimedOut:
if (console_debug_)
std::cout << "Request to elasticsearch timed out\n";
OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request to elasticsearch timed out");
cv_.notify_all();
break;
case http_client::SessionState::NetworkError:
if (console_debug_)
std::cout << "Network error to elasticsearch\n";
OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Network error to elasticsearch");
cv_.notify_all();
break;
}
Expand Down Expand Up @@ -131,11 +130,8 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export(
// Return failure if this exporter has been shutdown
if (is_shutdown_)
{
if (options_.console_debug_)
{
std::cout << "Export failed, exporter is shutdown" << std::endl;
}

OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Export failed, exporter is shutdown");
return sdk::common::ExportResult::kFailure;
}

Expand Down Expand Up @@ -172,8 +168,9 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export(
// Wait for the response to be received
if (options_.console_debug_)
{
std::cout << "waiting for response from Elasticsearch (timeout = " << options_.response_timeout_
<< " seconds)" << std::endl;
OTEL_INTERNAL_LOG_DEBUG(
"[ES Trace Exporter] waiting for response from Elasticsearch (timeout = "
<< options_.response_timeout_ << " seconds)");
}
bool write_successful = handler->waitForResponse();

Expand All @@ -191,12 +188,9 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export(
std::string responseBody = handler->GetResponseBody();
if (responseBody.find("\"failed\" : 0") == std::string::npos)
{
if (options_.console_debug_)
{
std::cout << "Logs were not written to Elasticsearch correctly, response body:" << std::endl;
std::cout << responseBody << std::endl;
}

OTEL_INTERNAL_LOG_ERROR(
"[ES Trace Exporter] Logs were not written to Elasticsearch correctly, response body: "
<< responseBody);
// TODO: Retry logic
return sdk::common::ExportResult::kFailure;
}
Expand Down
38 changes: 23 additions & 15 deletions exporters/jaeger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,41 @@ set(JAEGER_THRIFT_GENCPP_SOURCES
thrift-gen/zipkincore_types.cpp)

set(JAEGER_EXPORTER_SOURCES
src/jaeger_exporter.cc src/thrift_sender.cc src/udp_transport.cc
src/recordable.cc src/TUDPTransport.cc)
src/jaeger_exporter.cc
src/thrift_sender.cc
src/udp_transport.cc
src/recordable.cc
src/TUDPTransport.cc
src/http_transport.cc
src/THttpTransport.cc)

add_library(jaeger_trace_exporter ${JAEGER_EXPORTER_SOURCES}
${JAEGER_THRIFT_GENCPP_SOURCES})
add_library(opentelemetry_exporter_jaeger_trace ${JAEGER_EXPORTER_SOURCES}
${JAEGER_THRIFT_GENCPP_SOURCES})

set_target_properties(jaeger_trace_exporter PROPERTIES EXPORT_NAME
jaeger_trace_exporter)
set_target_properties(opentelemetry_exporter_jaeger_trace
PROPERTIES EXPORT_NAME jaeger_trace_exporter)

target_include_directories(
jaeger_trace_exporter
opentelemetry_exporter_jaeger_trace
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

target_link_libraries(
jaeger_trace_exporter
PUBLIC opentelemetry_resources
opentelemetry_exporter_jaeger_trace
PUBLIC opentelemetry_resources http_client_curl
PRIVATE thrift::thrift)

if(MSVC)
target_compile_definitions(jaeger_trace_exporter PUBLIC NOMINMAX)
target_compile_definitions(opentelemetry_exporter_jaeger_trace
PUBLIC NOMINMAX)
if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(jaeger_trace_exporter
target_compile_definitions(opentelemetry_exporter_jaeger_trace
PUBLIC THRIFT_STATIC_DEFINE)
endif()
endif()

install(
TARGETS jaeger_trace_exporter
TARGETS opentelemetry_exporter_jaeger_trace
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand All @@ -48,12 +54,14 @@ install(
DIRECTORY include/opentelemetry/exporters/jaeger
DESTINATION include/opentelemetry/exporters/
FILES_MATCHING
PATTERN "*.h")
PATTERN "*.h"
PATTERN "recordable.h" EXCLUDE)

if(BUILD_TESTING)
add_executable(jaeger_recordable_test test/jaeger_recordable_test.cc)
target_link_libraries(jaeger_recordable_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} jaeger_trace_exporter)
target_link_libraries(
jaeger_recordable_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_jaeger_trace)

gtest_add_tests(
TARGET jaeger_recordable_test
Expand Down
Loading

0 comments on commit 3818109

Please sign in to comment.