Skip to content

Commit

Permalink
Plug the SpanProcessor into the Tracer implementation (open-telemetry#65
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Johannes Tax authored May 5, 2020
1 parent 8683fbf commit 132aca1
Show file tree
Hide file tree
Showing 17 changed files with 337 additions and 150 deletions.
71 changes: 71 additions & 0 deletions sdk/include/opentelemetry/sdk/common/atomic_shared_ptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <atomic>
#include <memory>
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
/**
* A wrapper to provide atomic shared pointers.
*
* This wrapper relies on std::atomic for gcc 4.8 and C++20, while using
* specializations of std::atomic_store and std::atomic_load in all other
* instances.
*/
#if __cplusplus > 201703L
template <class T>
class AtomicSharedPtr
{
public:
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) noexcept : ptr_{std::move(ptr)} {}

void store(const std::shared_ptr<T> &other) noexcept
{
ptr_.store(other, std::memory_order_release);
}

std::shared_ptr<T> load() const noexcept { return ptr_.load(std::memory_order_acquire); }

private:
std::atomic<std::shared_ptr<T>> ptr_;
};
#elif (__GNUC__ == 4 && (__GNUC_MINOR__ >= 8))
template <class T>
class AtomicSharedPtr
{
public:
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) noexcept
: ptr_{new std::shared_ptr<T>(std::move(ptr))}
{}

~AtomicSharedPtr() noexcept { delete ptr_.load(std::memory_order_acquire); }

void store(const std::shared_ptr<T> &other) noexcept
{
ptr_.store(new std::shared_ptr<T>(other), std::memory_order_release);
}

std::shared_ptr<T> load() const noexcept { return *ptr_.load(std::memory_order_acquire); }

private:
std::atomic<std::shared_ptr<T> *> ptr_;
};
#else
template <class T>
class AtomicSharedPtr
{
public:
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) noexcept : ptr_{std::move(ptr)} {}

void store(const std::shared_ptr<T> &other) noexcept { std::atomic_store(&ptr_, other); }

std::shared_ptr<T> load() const noexcept { return std::atomic_load(&ptr_); }

private:
std::shared_ptr<T> ptr_;
};
#endif
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
30 changes: 0 additions & 30 deletions sdk/include/opentelemetry/sdk/trace/default_tracer_provider.h

This file was deleted.

51 changes: 51 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "opentelemetry/sdk/common/atomic_shared_ptr.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/trace/tracer.h"
#include "opentelemetry/version.h"

#include <memory>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
class Tracer final : public trace_api::Tracer, public std::enable_shared_from_this<Tracer>
{
public:
/**
* Initialize a new tracer.
* @param processor The span processor for this tracer. This must not be a
* nullptr.
*/
explicit Tracer(std::shared_ptr<SpanProcessor> processor) noexcept : processor_{processor} {}

/**
* Set the span processor associated with this tracer.
* @param processor The new span processor for this tracer. This must not be
* a nullptr.
*/
void SetProcessor(std::shared_ptr<SpanProcessor> processor) noexcept;

/**
* Obtain the span processor associated with this tracer.
* @return The span processor for this tracer.
*/
std::shared_ptr<SpanProcessor> GetProcessor() const noexcept;

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

void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override;

void CloseWithMicroseconds(uint64_t timeout) noexcept override;

private:
opentelemetry::sdk::AtomicSharedPtr<SpanProcessor> processor_;
};
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
50 changes: 50 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/tracer_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <map>
#include <memory>
#include <string>

#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/tracer.h"
#include "opentelemetry/trace/tracer_provider.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
class TracerProvider final : public opentelemetry::trace::TracerProvider
{
public:
/**
* Initialize a new tracer provider.
* @param processor The span processor for this tracer provider. This must
* not be a nullptr.
*/
explicit TracerProvider(std::shared_ptr<SpanProcessor> processor) noexcept;

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
nostd::string_view library_name,
nostd::string_view library_version = "") noexcept override;

/**
* Set the span processor associated with this tracer provider.
* @param processor The new span processor for this tracer provider. This
* must not be a nullptr.
*/
void SetProcessor(std::shared_ptr<SpanProcessor> processor) noexcept;

/**
* Obtain the span processor associated with this tracer provider.
* @return The span processor for this tracer provider.
*/
std::shared_ptr<SpanProcessor> GetProcessor() const noexcept;

private:
opentelemetry::sdk::AtomicSharedPtr<SpanProcessor> processor_;
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer_;
};
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
2 changes: 1 addition & 1 deletion sdk/src/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
add_library(opentelemetry_trace default_tracer_provider.cc tracer.cc span.cc)
add_library(opentelemetry_trace tracer_provider.cc tracer.cc span.cc)
21 changes: 0 additions & 21 deletions sdk/src/trace/default_tracer_provider.cc

This file was deleted.

31 changes: 0 additions & 31 deletions sdk/src/trace/recorder.h

This file was deleted.

6 changes: 4 additions & 2 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ namespace sdk
namespace trace
{
Span::Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const trace_api::StartSpanOptions &options) noexcept
: tracer_{std::move(tracer)}, recordable_{tracer_->recorder().MakeRecordable()}
: tracer_{std::move(tracer)}, processor_{processor}, recordable_{processor_->MakeRecordable()}
{
(void)options;
if (recordable_ == nullptr)
{
return;
}
processor_->OnStart(*recordable_);
recordable_->SetName(name);
}

Expand Down Expand Up @@ -63,7 +65,7 @@ void Span::End() noexcept
{
return;
}
tracer_->recorder().Record(std::move(recordable_));
processor_->OnEnd(std::move(recordable_));
recordable_.reset();
}

Expand Down
6 changes: 4 additions & 2 deletions sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <mutex>

#include "opentelemetry/sdk/trace/tracer.h"
#include "opentelemetry/version.h"
#include "src/trace/tracer.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
Expand All @@ -16,6 +16,7 @@ class Span final : public trace_api::Span
{
public:
explicit Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const trace_api::StartSpanOptions &options) noexcept;

Expand All @@ -37,7 +38,8 @@ class Span final : public trace_api::Span
trace_api::Tracer &tracer() const noexcept override { return *tracer_; }

private:
std::shared_ptr<Tracer> tracer_;
std::shared_ptr<trace_api::Tracer> tracer_;
std::shared_ptr<SpanProcessor> processor_;
mutable std::mutex mu_;
std::unique_ptr<Recordable> recordable_;
};
Expand Down
17 changes: 14 additions & 3 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "src/trace/tracer.h"
#include "opentelemetry/sdk/trace/tracer.h"

#include "opentelemetry/sdk/common/atomic_shared_ptr.h"
#include "opentelemetry/version.h"
#include "src/trace/span.h"

Expand All @@ -8,12 +9,22 @@ namespace sdk
{
namespace trace
{
void Tracer::SetProcessor(std::shared_ptr<SpanProcessor> processor) noexcept
{
processor_.store(processor);
}

std::shared_ptr<SpanProcessor> Tracer::GetProcessor() const noexcept
{
return processor_.load();
}

nostd::unique_ptr<trace_api::Span> Tracer::StartSpan(
nostd::string_view name,
const trace_api::StartSpanOptions &options) noexcept
{
return nostd::unique_ptr<trace_api::Span>{new (std::nothrow)
Span{this->shared_from_this(), name, options}};
return nostd::unique_ptr<trace_api::Span>{
new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, options}};
}

void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
Expand Down
36 changes: 0 additions & 36 deletions sdk/src/trace/tracer.h

This file was deleted.

Loading

0 comments on commit 132aca1

Please sign in to comment.