Skip to content

Commit

Permalink
Merge branch 'master' into zpages-README
Browse files Browse the repository at this point in the history
  • Loading branch information
jajanet authored Jun 27, 2020
2 parents 2c33a66 + ed0a36e commit 9827ab0
Show file tree
Hide file tree
Showing 29 changed files with 388 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.3.0
28 changes: 28 additions & 0 deletions .github/.codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
codecov:
require_ci_to_pass: yes
max_report_age: off

coverage:
precision: 2
round: down
range: "80...100"

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "reach,diff,flags,tree"
behavior: default
require_changes: no

# Relative file path fixing.
# CI file paths must match Git file paths.
# This fix removes the "/home/runner/" prefix
# to coverage report file paths.
fixes:
- "/home/runner/::"
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,19 @@ jobs:
./ci/setup_windows_ci_environment.ps1
- name: run tests
run: ./ci/do_ci.ps1 cmake.test_example_plugin

code_coverage:
name: Code coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: setup
run: |
sudo ./ci/setup_cmake.sh
sudo ./ci/setup_ci_environment.sh
- name: run tests and generate report
run: ./ci/do_ci.sh code.coverage
- name: upload report
uses: codecov/codecov-action@v1
with:
file: /home/runner/build/coverage.info
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ include(CTest)
find_package(Threads)

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# Options for Visual C++ compiler:
# /Zc:__cplusplus - report an updated value for recent C++ language standards.
# Without this option MSVC returns the value of __cplusplus="199711L"
# Options for Visual C++ compiler: /Zc:__cplusplus - report an updated value
# for recent C++ language standards. Without this option MSVC returns the
# value of __cplusplus="199711L"
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
endif()

Expand Down
9 changes: 5 additions & 4 deletions api/include/opentelemetry/plugin/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class Factory final
* @param error_message on failure this will contain an error message.
* @return a Tracer on success or nullptr on failure.
*/
std::shared_ptr<Tracer> MakeTracer(nostd::string_view tracer_config,
std::string &error_message) const noexcept
std::shared_ptr<opentelemetry::trace::Tracer> MakeTracer(nostd::string_view tracer_config,
std::string &error_message) const
noexcept
{
nostd::unique_ptr<char[]> plugin_error_message;
auto tracer_handle = factory_impl_->MakeTracerHandle(tracer_config, plugin_error_message);
Expand All @@ -46,8 +47,8 @@ class Factory final
detail::CopyErrorMessage(plugin_error_message.get(), error_message);
return nullptr;
}
return std::shared_ptr<Tracer>{new (std::nothrow)
Tracer{library_handle_, std::move(tracer_handle)}};
return std::shared_ptr<opentelemetry::trace::Tracer>{
new (std::nothrow) Tracer{library_handle_, std::move(tracer_handle)}};
}

private:
Expand Down
8 changes: 7 additions & 1 deletion api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Span final : public trace::Span
{}

// trace::Span
void SetAttribute(nostd::string_view name, const common::AttributeValue &&value) noexcept override
{
span_->SetAttribute(name, std::move(value));
}

void AddEvent(nostd::string_view name) noexcept override { span_->AddEvent(name); }

void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override
Expand Down Expand Up @@ -61,9 +66,10 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
// trace::Tracer
nostd::unique_ptr<trace::Span> StartSpan(
nostd::string_view name,
const trace::KeyValueIterable &attributes,
const trace::StartSpanOptions &options = {}) noexcept override
{
auto span = tracer_handle_->tracer().StartSpan(name, options);
auto span = tracer_handle_->tracer().StartSpan(name, attributes, options);
if (span == nullptr)
{
return nullptr;
Expand Down
5 changes: 5 additions & 0 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class NoopSpan final : public Span
public:
explicit NoopSpan(const std::shared_ptr<Tracer> &tracer) noexcept : tracer_{tracer} {}

void SetAttribute(nostd::string_view /*key*/,
const common::AttributeValue && /*value*/) noexcept override
{}

void AddEvent(nostd::string_view /*name*/) noexcept override {}

void AddEvent(nostd::string_view /*name*/, core::SystemTimestamp /*timestamp*/) noexcept override
Expand Down Expand Up @@ -56,6 +60,7 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
public:
// Tracer
nostd::unique_ptr<Span> StartSpan(nostd::string_view /*name*/,
const KeyValueIterable & /*attributes*/,
const StartSpanOptions & /*options*/) noexcept override
{
return nostd::unique_ptr<Span>{new (std::nothrow) NoopSpan{this->shared_from_this()}};
Expand Down
9 changes: 3 additions & 6 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
Expand Down Expand Up @@ -40,7 +41,6 @@ struct StartSpanOptions
// Span(Context?) parent;
// SpanContext remote_parent;
// Links
// Attributes
SpanKind kind = SpanKind::kInternal;
};
/**
Expand Down Expand Up @@ -74,13 +74,10 @@ class Span
Span &operator=(const Span &) = delete;
Span &operator=(Span &&) = delete;

// TODO
// Sets an attribute on the Span. If the Span previously contained a mapping for
// the key, the old value is replaced.
//
// If an empty string is used as the value, the attribute will be silently
// dropped. Note: this behavior could change in the future.
// virtual void SetAttribute(nostd::string_view key, AttributeValue&& value) = 0;
virtual void SetAttribute(nostd::string_view key,
const common::AttributeValue &&value) noexcept = 0;

// Adds an event to the Span.
virtual void AddEvent(nostd::string_view name) noexcept = 0;
Expand Down
31 changes: 31 additions & 0 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,41 @@ class Tracer
virtual ~Tracer() = default;
/**
* Starts a span.
*
* Optionally sets attributes at Span creation from the given key/value pairs.
*
* Attributes will be processed in order, previous attributes with the same
* key will be overwritten.
*/
virtual nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const KeyValueIterable &attributes,
const StartSpanOptions &options = {}) noexcept = 0;

nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const StartSpanOptions &options = {}) noexcept
{
return this->StartSpan(name, {}, options);
}

template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const T &attributes,
const StartSpanOptions &options = {}) noexcept
{
return this->StartSpan(name, KeyValueIterableView<T>(attributes), options);
}

nostd::unique_ptr<Span> StartSpan(
nostd::string_view name,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
const StartSpanOptions &options = {}) noexcept
{
return this->StartSpan(name,
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()},
options);
}

/**
* Force any buffered spans to flush.
* @param timeout to complete the flush
Expand Down
1 change: 1 addition & 0 deletions ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CI tests can be run on docker by invoking the script `./ci/run_docker.sh ./ci/do
* `bazel.tsan`: build bazel targets and run tests with ThreadSanitizer.
* `benchmark`: run all benchmarks.
* `format`: use `tools/format.sh` to enforce text formatting.
* `code.coverage`: build cmake targets and run tests. Then upload coverage report to [codecov.io](https://codecov.io/).

Additionally, `./ci/run_docker.sh` can be invoked with no arguments to get a docker shell where tests
can be run manually.
10 changes: 10 additions & 0 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ elif [[ "$1" == "format" ]]; then
exit 1
fi
exit 0
elif [[ "$1" == "code.coverage" ]]; then
cd "${BUILD_DIR}"
rm -rf *
cmake -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-Werror --coverage" \
"${SRC_DIR}"
make
make test
lcov --directory $PWD --capture --output-file coverage.info
exit 0
fi

echo "Invalid do_ci.sh target, see ci/README.md for valid targets."
Expand Down
1 change: 1 addition & 0 deletions ci/setup_ci_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ apt-get install --no-install-recommends --no-install-suggests -y \
ca-certificates \
wget \
git
apt-get install -y lcov
20 changes: 14 additions & 6 deletions examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include <iostream>

namespace nostd = opentelemetry::nostd;
namespace core = opentelemetry::core;
namespace trace = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;
namespace common = opentelemetry::common;
namespace core = opentelemetry::core;
namespace trace = opentelemetry::trace;

namespace
{
Expand All @@ -13,6 +14,7 @@ class Span final : public trace::Span
public:
Span(std::shared_ptr<Tracer> &&tracer,
nostd::string_view name,
const opentelemetry::trace::KeyValueIterable & /*attributes*/,
const trace::StartSpanOptions & /*options*/) noexcept
: tracer_{std::move(tracer)}, name_{name}
{
Expand All @@ -22,6 +24,10 @@ class Span final : public trace::Span
~Span() { std::cout << "~Span\n"; }

// opentelemetry::trace::Span
void SetAttribute(nostd::string_view /*name*/,
const common::AttributeValue && /*value*/) noexcept override
{}

void AddEvent(nostd::string_view /*name*/) noexcept override {}

void AddEvent(nostd::string_view /*name*/, core::SystemTimestamp /*timestamp*/) noexcept override
Expand Down Expand Up @@ -52,9 +58,11 @@ class Span final : public trace::Span

Tracer::Tracer(nostd::string_view /*output*/) {}

nostd::unique_ptr<trace::Span> Tracer::StartSpan(nostd::string_view name,
const trace::StartSpanOptions &options) noexcept
nostd::unique_ptr<trace::Span> Tracer::StartSpan(
nostd::string_view name,
const opentelemetry::trace::KeyValueIterable &attributes,
const trace::StartSpanOptions &options) noexcept
{
return nostd::unique_ptr<opentelemetry::trace::Span>{
new (std::nothrow) Span{this->shared_from_this(), name, options}};
new (std::nothrow) Span{this->shared_from_this(), name, attributes, options}};
}
3 changes: 2 additions & 1 deletion examples/plugin/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
// opentelemetry::trace::Tracer
opentelemetry::nostd::unique_ptr<opentelemetry::trace::Span> StartSpan(
opentelemetry::nostd::string_view name,
const opentelemetry::trace::StartSpanOptions &options) noexcept override;
const opentelemetry::trace::KeyValueIterable & /*attributes*/,
const opentelemetry::trace::StartSpanOptions & /*options */) noexcept override;

void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}

Expand Down
10 changes: 10 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# Simple Trace Example

In this example, the application in `main.cc` initializes and registers 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 telemetry is directed to stdout through a custom exporter.

See [CONTRIBUTING.md](../../CONTRIBUTING.md) for instructions on building and running the example.
4 changes: 2 additions & 2 deletions examples/simple/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ void initTracer()
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 OT initialized with the default noop
// tracer, thus being effectively deactivated.
// Removing this line will leave the default noop TracerProvider in place.
initTracer();

foo_library();
Expand Down
9 changes: 9 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/recordable.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/trace/canonical_code.h"
Expand Down Expand Up @@ -33,6 +34,14 @@ class Recordable
opentelemetry::trace::SpanId span_id,
opentelemetry::trace::SpanId parent_span_id) noexcept = 0;

/**
* Set an attribute of a span.
* @param name the name of the attribute
* @param value the attribute value
*/
virtual void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &&value) noexcept = 0;

/**
* Add an event to a span.
* @param name the name of the event
Expand Down
Loading

0 comments on commit 9827ab0

Please sign in to comment.