Skip to content

Commit

Permalink
[Trace SDK] Implement builders (open-telemetry#1393)
Browse files Browse the repository at this point in the history
Implement code review comments:
- Factory methods should return a unique_ptr.
- Used TracerContextFactory in examples.
  • Loading branch information
marcalff committed Jul 9, 2022
1 parent 8a4eadc commit f8fe74d
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 46 deletions.
3 changes: 2 additions & 1 deletion examples/batch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void initTracer()

auto processor = trace_sdk::BatchSpanProcessorFactory::Create(std::move(exporter), options);

auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor), resource);
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor), resource);

// Set the global trace provider.
trace_api::Provider::SetTracerProvider(provider);
Expand Down
8 changes: 5 additions & 3 deletions examples/grpc/tracer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "opentelemetry/exporters/ostream/span_exporter_factory.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_context.h"
#include "opentelemetry/sdk/trace/tracer_context_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/propagation/http_trace_context.h"
#include "opentelemetry/trace/provider.h"
Expand Down Expand Up @@ -77,8 +77,10 @@ void initTracer()
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
processors.push_back(std::move(processor));
// Default is an always-on sampler.
auto context = std::make_shared<opentelemetry::sdk::trace::TracerContext>(std::move(processors));
auto provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(context);
std::shared_ptr<opentelemetry::sdk::trace::TracerContext> context =
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
opentelemetry::sdk::trace::TracerProviderFactory::Create(context);
// Set the global trace provider
opentelemetry::trace::Provider::SetTracerProvider(provider);

Expand Down
8 changes: 6 additions & 2 deletions examples/http/tracer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/exporters/ostream/span_exporter_factory.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_context_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"

Expand Down Expand Up @@ -65,8 +67,10 @@ void initTracer()
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
processors.push_back(std::move(processor));
// Default is an always-on sampler.
auto context = std::make_shared<opentelemetry::sdk::trace::TracerContext>(std::move(processors));
auto provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(context);
std::shared_ptr<opentelemetry::sdk::trace::TracerContext> context =
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
opentelemetry::sdk::trace::TracerProviderFactory::Create(context);
// Set the global trace provider
opentelemetry::trace::Provider::SetTracerProvider(provider);

Expand Down
3 changes: 2 additions & 1 deletion examples/jaeger/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ void InitTracer()
// Create Jaeger exporter instance
auto exporter = jaeger::JaegerExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/multi_processor/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ std::shared_ptr<InMemorySpanData> initTracer()
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
processors.push_back(std::move(processor1));
processors.push_back(std::move(processor2));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processors));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processors));

// Set the global trace provider
trace_api::Provider::SetTracerProvider(std::move(provider));
Expand Down
5 changes: 3 additions & 2 deletions examples/multithreaded/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ void initTracer()
{
auto exporter = opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create();
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(
std::move(processor), opentelemetry::sdk::resource::Resource::Create({}));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor),
opentelemetry::sdk::resource::Resource::Create({}));
// Set the global trace provider
trace_api::Provider::SetTracerProvider(provider);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void InitTracer()
// Create OTLP exporter instance
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/otlp/grpc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ void InitTracer()
// Create OTLP exporter instance
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/otlp/http_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void InitTracer()
// Create OTLP exporter instance
auto exporter = otlp::OtlpHttpExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/otlp/http_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void InitTracer()
// Create OTLP exporter instance
auto exporter = otlp::OtlpHttpExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/simple/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ void initTracer()
{
auto exporter = trace_exporter::OStreamSpanExporterFactory::Create();
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));

// Set the global trace provider
trace_api::Provider::SetTracerProvider(provider);
Expand Down
3 changes: 2 additions & 1 deletion examples/zipkin/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void InitTracer()
auto resource = resource::Resource::Create(attributes);
auto exporter = zipkin::ZipkinExporterFactory::Create(opts);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor), resource);
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor), resource);
// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/include/opentelemetry/sdk/trace/tracer_context_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ class TracerContextFactory
/**
* Create a TracerContext.
*/
static std::shared_ptr<sdk::trace::TracerContext> Create(
static std::unique_ptr<TracerContext> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor);

/**
* Create a TracerContext.
*/
static std::shared_ptr<sdk::trace::TracerContext> Create(
static std::unique_ptr<TracerContext> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor,
opentelemetry::sdk::resource::Resource resource);

/**
* Create a TracerContext.
*/
static std::shared_ptr<sdk::trace::TracerContext> Create(
static std::unique_ptr<TracerContext> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler);

/**
* Create a TracerContext.
*/
static std::shared_ptr<sdk::trace::TracerContext> Create(
static std::unique_ptr<TracerContext> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
Expand Down
18 changes: 9 additions & 9 deletions sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,47 @@ class TracerProviderFactory
public:
/* Serie of builders with a single processor. */

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::unique_ptr<SpanProcessor> processor);

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::unique_ptr<SpanProcessor> processor,
opentelemetry::sdk::resource::Resource resource);

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::unique_ptr<SpanProcessor> processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler);

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::unique_ptr<SpanProcessor> processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator);

/* Serie of builders with a vector of processor. */

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors);

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
opentelemetry::sdk::resource::Resource resource);

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler);

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator);

/* Create with a tracer context. */

static std::shared_ptr<opentelemetry::trace::TracerProvider> Create(
static std::unique_ptr<opentelemetry::trace::TracerProvider> Create(
std::shared_ptr<sdk::trace::TracerContext> context);
};

Expand Down
10 changes: 5 additions & 5 deletions sdk/src/trace/tracer_context_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ namespace sdk
namespace trace
{

std::shared_ptr<TracerContext> TracerContextFactory::Create(
std::unique_ptr<TracerContext> TracerContextFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor)
{
auto resource = opentelemetry::sdk::resource::Resource::Create({});
return Create(std::move(processor), resource);
}

std::shared_ptr<TracerContext> TracerContextFactory::Create(
std::unique_ptr<TracerContext> TracerContextFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor,
opentelemetry::sdk::resource::Resource resource)
{
auto sampler = AlwaysOnSamplerFactory::Create();
return Create(std::move(processor), resource, std::move(sampler));
}

std::shared_ptr<TracerContext> TracerContextFactory::Create(
std::unique_ptr<TracerContext> TracerContextFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler)
Expand All @@ -37,13 +37,13 @@ std::shared_ptr<TracerContext> TracerContextFactory::Create(
return Create(std::move(processor), resource, std::move(sampler), std::move(id_generator));
}

std::shared_ptr<TracerContext> TracerContextFactory::Create(
std::unique_ptr<TracerContext> TracerContextFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator)
{
std::shared_ptr<TracerContext> context(new TracerContext(
std::unique_ptr<TracerContext> context(new TracerContext(
std::move(processor), resource, std::move(sampler), std::move(id_generator)));
return context;
}
Expand Down
24 changes: 12 additions & 12 deletions sdk/src/trace/tracer_provider_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ namespace sdk
namespace trace
{

std::shared_ptr<trace_api::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<trace_api::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<SpanProcessor> processor)
{
auto resource = opentelemetry::sdk::resource::Resource::Create({});
return Create(std::move(processor), resource);
}

std::shared_ptr<trace_api::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<trace_api::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<SpanProcessor> processor,
opentelemetry::sdk::resource::Resource resource)
{
auto sampler = AlwaysOnSamplerFactory::Create();
return Create(std::move(processor), resource, std::move(sampler));
}

std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<SpanProcessor> processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler)
Expand All @@ -39,33 +39,33 @@ std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Cre
return Create(std::move(processor), resource, std::move(sampler), std::move(id_generator));
}

std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<SpanProcessor> processor,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator)
{
std::shared_ptr<trace_api::TracerProvider> provider(new trace_sdk::TracerProvider(
std::unique_ptr<trace_api::TracerProvider> provider(new trace_sdk::TracerProvider(
std::move(processor), resource, std::move(sampler), std::move(id_generator)));
return provider;
}

std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors)
{
auto resource = opentelemetry::sdk::resource::Resource::Create({});
return Create(std::move(processors), resource);
}

std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
opentelemetry::sdk::resource::Resource resource)
{
auto sampler = AlwaysOnSamplerFactory::Create();
return Create(std::move(processors), resource, std::move(sampler));
}

std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler)
Expand All @@ -74,21 +74,21 @@ std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Cre
return Create(std::move(processors), resource, std::move(sampler), std::move(id_generator));
}

std::shared_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<opentelemetry::trace::TracerProvider> TracerProviderFactory::Create(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator)
{
std::shared_ptr<trace_api::TracerProvider> provider(new trace_sdk::TracerProvider(
std::unique_ptr<trace_api::TracerProvider> provider(new trace_sdk::TracerProvider(
std::move(processors), resource, std::move(sampler), std::move(id_generator)));
return provider;
}

std::shared_ptr<trace_api::TracerProvider> TracerProviderFactory::Create(
std::unique_ptr<trace_api::TracerProvider> TracerProviderFactory::Create(
std::shared_ptr<sdk::trace::TracerContext> context)
{
std::shared_ptr<trace_api::TracerProvider> provider(new trace_sdk::TracerProvider(context));
std::unique_ptr<trace_api::TracerProvider> provider(new trace_sdk::TracerProvider(context));
return provider;
}

Expand Down

0 comments on commit f8fe74d

Please sign in to comment.