Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
89ce9f7
add sampler header file
Jun 17, 2020
e65cf5c
add param and comments
Jun 17, 2020
7b20dc1
change class definition
Jun 17, 2020
81cb054
add Decision enum
Jun 17, 2020
b206ced
add Decision enum
Jun 17, 2020
cdfcd3d
add ShouldSample params and placeholder
Jun 17, 2020
1f972e0
add ShouldSample params and placeholder
Jun 17, 2020
5097b2e
Merge branch 'sampling-api-header' of github.com:ziqizh/opentelemetry…
Jun 17, 2020
279ef0e
minor tweaks
Jun 17, 2020
bc14502
add sampler header file
Jun 17, 2020
f705b35
add param and comments
Jun 17, 2020
8111e93
change class definition
Jun 17, 2020
e4ac250
add Decision enum
Jun 17, 2020
48d4695
add Decision enum
Jun 17, 2020
7c4ac5a
minor tweaks
Jun 17, 2020
6fd2568
resolve conflict
Jun 17, 2020
5fcf4af
resolve conflict
Jun 17, 2020
ef00c7f
Merge branch 'sampling-api-header' of github.com:ziqizh/opentelemetry…
Jun 18, 2020
503edb6
change enum to enum class
Jun 18, 2020
7c204b8
change sampling result to struct
Jun 19, 2020
6d4632b
change param
Jun 19, 2020
71f8d9d
add AttributeKeyValue placeholder
Jun 19, 2020
590be00
add namespace
Jun 19, 2020
11b4420
define functions
Jun 18, 2020
a5fb523
remove sampling result class
Jun 19, 2020
fe817e5
add always on sampler
Jun 19, 2020
78b5be0
add function
Jun 19, 2020
8d43a4a
style fix
Jun 22, 2020
7586d1d
Merge branch 'master' into sampling-api-header
Jun 22, 2020
fab9d74
Merge branch 'sampling-api-header' into always-on-sampler
Jun 22, 2020
fff8bda
add test
Jun 22, 2020
82ccca0
minor syntax change
Jun 22, 2020
249e5be
Revert "minor syntax change"
Jun 22, 2020
d9ddf89
add CMake rule
Jun 22, 2020
620056c
add CMake rule
Jun 22, 2020
1be3e5e
Merge branch 'master' into always-on-sampler
ziqizh Jun 23, 2020
cddfc4e
update struct
Jun 23, 2020
9bd90ca
update imports'
Jun 23, 2020
db4b68b
update namespace
Jun 23, 2020
36a2c85
modify test file
Jun 23, 2020
f392e2f
modify test case
Jun 24, 2020
a555daa
modify test case
Jun 24, 2020
54db68e
remove extra header files
Jun 24, 2020
543ece9
change to inline function
Jun 24, 2020
03002b6
change cmake
Jun 24, 2020
93c0a5a
add sampler
Jun 24, 2020
408d988
add sampler
Jun 24, 2020
df32e4e
Merge remote-tracking branch 'upstream/master' into sampler-tracer-in…
Jun 24, 2020
f49f08a
fix bug
Jun 24, 2020
44fb580
add unit test
Jun 25, 2020
1a621e4
change bazel version
Jun 25, 2020
64a0b7f
resolve conflice
Jun 29, 2020
b08e544
move sampler to separate folder
Jun 29, 2020
a200940
format
Jun 29, 2020
7a3a5eb
fix conflict
Jun 29, 2020
881099b
add sampler and test for Tracer
Jun 29, 2020
9a8d572
remover sampler setter
Jun 30, 2020
ee76570
simplify constructor
Jun 30, 2020
9aa3373
resolve conflict
Jun 30, 2020
c704b9f
modify constructor and test cases
Jun 30, 2020
8e57117
fix test cases
Jul 1, 2020
3db6b5a
fix style
Jul 1, 2020
68c8e8d
Merge remote-tracking branch 'upstream/master' into sampler-tracer-in…
Jul 7, 2020
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
11 changes: 10 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

Expand All @@ -20,7 +21,8 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th
* @param processor The span processor for this tracer. This must not be a
* nullptr.
*/
explicit Tracer(std::shared_ptr<SpanProcessor> processor) noexcept : processor_{processor} {}
explicit Tracer(std::shared_ptr<SpanProcessor> processor,
std::shared_ptr<Sampler> sampler = std::make_shared<AlwaysOnSampler>()) noexcept;

/**
* Set the span processor associated with this tracer.
Expand All @@ -35,6 +37,12 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th
*/
std::shared_ptr<SpanProcessor> GetProcessor() const noexcept;

/**
* Obtain the sampler associated with this tracer.
* @return The sampler for this tracer.
*/
std::shared_ptr<Sampler> GetSampler() const noexcept;

nostd::unique_ptr<trace_api::Span> StartSpan(
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
Expand All @@ -46,6 +54,7 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th

private:
opentelemetry::sdk::AtomicSharedPtr<SpanProcessor> processor_;
const std::shared_ptr<Sampler> sampler_;
};
} // namespace trace
} // namespace sdk
Expand Down
16 changes: 14 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/samplers/always_on.h"
Comment thread
ziqizh marked this conversation as resolved.
#include "opentelemetry/sdk/trace/tracer.h"
#include "opentelemetry/trace/tracer_provider.h"

Expand All @@ -18,11 +19,15 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider
{
public:
/**
* Initialize a new tracer provider.
* Initialize a new tracer provider with a specified sampler
* @param processor The span processor for this tracer provider. This must
* not be a nullptr.
* @param sampler The sampler for this tracer provider. This must
* not be a nullptr.
*/
explicit TracerProvider(std::shared_ptr<SpanProcessor> processor) noexcept;
explicit TracerProvider(
std::shared_ptr<SpanProcessor> processor,
std::shared_ptr<Sampler> sampler = std::make_shared<AlwaysOnSampler>()) noexcept;

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
nostd::string_view library_name,
Expand All @@ -41,9 +46,16 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider
*/
std::shared_ptr<SpanProcessor> GetProcessor() const noexcept;

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

private:
opentelemetry::sdk::AtomicSharedPtr<SpanProcessor> processor_;
std::shared_ptr<opentelemetry::trace::Tracer> tracer_;
const std::shared_ptr<Sampler> sampler_;
};
} // namespace trace
} // namespace sdk
Expand Down
9 changes: 9 additions & 0 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace sdk
{
namespace trace
{
Tracer::Tracer(std::shared_ptr<SpanProcessor> processor, std::shared_ptr<Sampler> sampler) noexcept
: processor_{processor}, sampler_{sampler}
{}

void Tracer::SetProcessor(std::shared_ptr<SpanProcessor> processor) noexcept
{
processor_.store(processor);
Expand All @@ -19,6 +23,11 @@ std::shared_ptr<SpanProcessor> Tracer::GetProcessor() const noexcept
return processor_.load();
}

std::shared_ptr<Sampler> Tracer::GetSampler() const noexcept
{
return sampler_;
}

nostd::unique_ptr<trace_api::Span> Tracer::StartSpan(
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
Expand Down
10 changes: 8 additions & 2 deletions sdk/src/trace/tracer_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ namespace sdk
{
namespace trace
{
TracerProvider::TracerProvider(std::shared_ptr<SpanProcessor> processor) noexcept
: processor_{processor}, tracer_(new Tracer(std::move(processor)))
TracerProvider::TracerProvider(std::shared_ptr<SpanProcessor> processor,
std::shared_ptr<Sampler> sampler) noexcept
: processor_{processor}, tracer_(new Tracer(std::move(processor))), sampler_(sampler)
{}

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> TracerProvider::GetTracer(
Expand All @@ -28,6 +29,11 @@ std::shared_ptr<SpanProcessor> TracerProvider::GetProcessor() const noexcept
{
return processor_.load();
}

std::shared_ptr<Sampler> TracerProvider::GetSampler() const noexcept
{
return sampler_;
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
27 changes: 27 additions & 0 deletions sdk/test/trace/tracer_provider_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/sdk/trace/samplers/always_off.h"
#include "opentelemetry/sdk/trace/samplers/always_on.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/tracer.h"

Expand Down Expand Up @@ -27,3 +29,28 @@ TEST(TracerProvider, GetTracer)
ASSERT_NE(nullptr, sdkTracer);
ASSERT_EQ(processor, sdkTracer->GetProcessor());
}

TEST(TracerProvider, GetSampler)
{
std::shared_ptr<SpanProcessor> processor1(new SimpleSpanProcessor(nullptr));

// Create a TracerProvicer with a default AlwaysOnSampler.
TracerProvider tf1(processor1);
auto t1 = tf1.GetSampler();
auto t2 = tf1.GetSampler();
ASSERT_NE(nullptr, t1);
ASSERT_NE(nullptr, t2);

// Should return the same sampler each time.
ASSERT_EQ(t1, t2);

// Should be AlwaysOnSampler
ASSERT_EQ("AlwaysOnSampler", t2->GetDescription());

// Create a TracerProvicer with a custom AlwaysOffSampler.
std::shared_ptr<SpanProcessor> processor2(new SimpleSpanProcessor(nullptr));
TracerProvider tf2(processor2, std::make_shared<AlwaysOffSampler>());
auto t3 = tf2.GetSampler();

ASSERT_EQ("AlwaysOffSampler", t3->GetDescription());
}
20 changes: 20 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "opentelemetry/sdk/trace/tracer.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/samplers/always_on.h"
#include "opentelemetry/sdk/trace/samplers/always_off.h"
#include "opentelemetry/sdk/trace/span_data.h"

#include <gtest/gtest.h>
Expand Down Expand Up @@ -139,3 +141,21 @@ TEST(Tracer, StartSpanWithAttributes)
ASSERT_EQ(1, span_data2->GetAttributes().size());
ASSERT_EQ(3.0, nostd::get<double>(span_data2->GetAttributes().at("attr3")));
}


TEST(Tracer, GetSampler)
{
// Create a Tracer with a default AlwaysOnSampler
std::shared_ptr<SpanProcessor> processor_1(new SimpleSpanProcessor(nullptr));
std::shared_ptr<Tracer> tracer_on(new Tracer(std::move(processor_1)));

auto t1 = tracer_on->GetSampler();
ASSERT_EQ("AlwaysOnSampler", t1->GetDescription());

// Create a Tracer with a AlwaysOffSampler
std::shared_ptr<SpanProcessor> processor_2(new SimpleSpanProcessor(nullptr));
std::shared_ptr<Tracer> tracer_off(new Tracer(std::move(processor_2), std::make_shared<AlwaysOffSampler>()));

auto t2 = tracer_off->GetSampler();
ASSERT_EQ("AlwaysOffSampler", t2->GetDescription());
}