Skip to content

Commit

Permalink
Merge branch 'master' into origin/propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianlin-Zhao authored Aug 12, 2020
2 parents fa90b35 + a55843f commit c1fb682
Show file tree
Hide file tree
Showing 74 changed files with 5,142 additions and 523 deletions.
163 changes: 0 additions & 163 deletions .circleci/config.yml

This file was deleted.

2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ http_archive(

http_archive(
name = "github_nlohmann_json",
build_file = "//third_party:nlohmann_json.BUILD",
build_file = "//third_party/json:nlohmann_json.BUILD",
sha256 = "69cc88207ce91347ea530b227ff0776db82dcb8de6704e1a3d74f4841bc651cf",
urls = [
"https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip",
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/context/context_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using ContextValue = nostd::variant<bool,
int64_t,
uint64_t,
double,
nostd::shared_ptr<trace::SpanContext>,
nostd::shared_ptr<trace::Span>>;
nostd::shared_ptr<trace::Span>,
nostd::shared_ptr<trace::SpanContext>>;
} // namespace context
OPENTELEMETRY_END_NAMESPACE
56 changes: 42 additions & 14 deletions api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,53 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{
// Provides a wrapper for propagating the context object globally. In order
// to use either the threadlocal_context.h file must be included or another
// implementation which must be derived from the RuntimeContext can be
// provided.
class RuntimeContext
// The Token object provides is returned when attaching objects to the
// RuntimeContext object and is associated with a context object, and
// can be provided to the RuntimeContext Detach method to remove the
// associated context from the RuntimeContext.
class Token
{
public:
class Token
bool operator==(const Context &other) noexcept { return context_ == other; }

private:
friend class RuntimeContext;

// The ContextDetacher object automatically attempts to detach
// the Token when all copies of the Token are out of scope.
class ContextDetacher
{
public:
bool operator==(const Context &other) noexcept { return context_ == other; }
ContextDetacher(Context context) : context_(context) {}

~Token() noexcept { Detach(*this); }
~ContextDetacher();

private:
friend class RuntimeContext;
Context context_;
};

// A constructor that sets the token's Context object to the
// one that was passed in.
Token(Context context) noexcept : context_(context){};
Token() noexcept = default;

Token() noexcept = default;
// A constructor that sets the token's Context object to the
// one that was passed in.
Token(Context context)
{
context_ = context;

Context context_;
detacher_ = nostd::shared_ptr<ContextDetacher>(new ContextDetacher(context_));
};

Context context_;
nostd::shared_ptr<ContextDetacher> detacher_;
};

// Provides a wrapper for propagating the context object globally. In order
// to use either the threadlocal_context.h file must be included or another
// implementation which must be derived from the RuntimeContext can be
// provided.
class RuntimeContext
{
public:
// Return the current context.
static Context GetCurrent() noexcept { return context_handler_->InternalGetCurrent(); }

Expand Down Expand Up @@ -97,5 +118,12 @@ class RuntimeContext

virtual bool InternalDetach(Token &token) noexcept = 0;
};

inline Token::ContextDetacher::~ContextDetacher()
{
context::Token token;
token.context_ = context_;
context::RuntimeContext::Detach(token);
}
} // namespace context
OPENTELEMETRY_END_NAMESPACE
8 changes: 8 additions & 0 deletions api/include/opentelemetry/context/runtime_def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

//#include "opentelemetry/context/runtime_context.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{}
OPENTELEMETRY_BEGIN_NAMESPACE
15 changes: 8 additions & 7 deletions api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace plugin
class Span final : public trace::Span
{
public:
Span(std::shared_ptr<trace::Tracer> &&tracer, std::unique_ptr<trace::Span> &&span) noexcept
: tracer_{std::move(tracer)}, span_{std::move(span)}
Span(std::shared_ptr<trace::Tracer> &&tracer, nostd::shared_ptr<trace::Span> span) noexcept
: tracer_{std::move(tracer)}, span_{span}
{}

// trace::Span
Expand Down Expand Up @@ -50,9 +50,11 @@ class Span final : public trace::Span

trace::SpanContext GetContext() const noexcept override { return span_->GetContext(); }

void SetToken(nostd::unique_ptr<context::Token> &&token) noexcept override {}

private:
std::shared_ptr<trace::Tracer> tracer_;
std::unique_ptr<trace::Span> span_;
nostd::shared_ptr<trace::Span> span_;
};

class Tracer final : public trace::Tracer, public std::enable_shared_from_this<Tracer>
Expand All @@ -64,18 +66,17 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
{}

// trace::Tracer
nostd::unique_ptr<trace::Span> StartSpan(
nostd::shared_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, attributes, options);
if (span == nullptr)
{
return nullptr;
return nostd::shared_ptr<trace::Span>(nullptr);
}
trace::Span *spn = new (std::nothrow) Span{this->shared_from_this(), std::move(span)};
return nostd::unique_ptr<trace::Span>{spn};
return nostd::shared_ptr<trace::Span>{new (std::nothrow) Span{this->shared_from_this(), span}};
}

void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override
Expand Down
7 changes: 5 additions & 2 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// This file is part of the internal implementation of OpenTelemetry. Nothing in this file should be
// used directly. Please refer to span.h and tracer.h for documentation on these interfaces.

#include "opentelemetry/context/runtime_context.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/trace/span.h"
Expand Down Expand Up @@ -50,6 +51,8 @@ class NoopSpan final : public Span
SpanContext GetContext() const noexcept override { return span_context_; }
// Tracer &tracer() const noexcept override { return *tracer_; }

void SetToken(nostd::unique_ptr<context::Token> && /* token */) noexcept override {}

private:
std::shared_ptr<Tracer> tracer_;
SpanContext span_context_;
Expand All @@ -62,11 +65,11 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
{
public:
// Tracer
nostd::unique_ptr<Span> StartSpan(nostd::string_view /*name*/,
nostd::shared_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()}};
return nostd::shared_ptr<Span>{new (std::nothrow) NoopSpan{this->shared_from_this()}};
}

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

0 comments on commit c1fb682

Please sign in to comment.