1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
6 #include "opentelemetry/sdk/common/atomic_unique_ptr.h"
7 #include "opentelemetry/sdk/resource/resource.h"
8 #include "opentelemetry/sdk/trace/processor.h"
9 #include "opentelemetry/sdk/trace/random_id_generator.h"
10 #include "opentelemetry/sdk/trace/samplers/always_on.h"
11 #include "opentelemetry/version.h"
13 OPENTELEMETRY_BEGIN_NAMESPACE
20 * A class which stores the TracerProvider context.
22 * This class meets the following design criteria:
23 * - A shared reference between TracerProvider and Tracers instantiated.
24 * - A thread-safe class that allows updating/altering processor/exporter pipelines
25 * and sampling config.
26 * - The owner/destroyer of Processors/Exporters. These will remain active until
27 * this class is destroyed. I.e. Sampling, Exporting, flushing, Custom Iterator etc. are all ok
28 * if this object is alive, and they will work together. If this object is destroyed, then no shared
29 * references to Processor, Exporter, Recordable, Custom Iterator etc. should exist, and all
30 * associated pipelines will have been flushed.
35 explicit TracerContext(
36 std::vector
<std::unique_ptr
<SpanProcessor
>> &&processor
,
37 opentelemetry::sdk::resource::Resource resource
=
38 opentelemetry::sdk::resource::Resource::Create({}),
39 std::unique_ptr
<Sampler
> sampler
= std::unique_ptr
<AlwaysOnSampler
>(new AlwaysOnSampler
),
40 std::unique_ptr
<IdGenerator
> id_generator
=
41 std::unique_ptr
<IdGenerator
>(new RandomIdGenerator())) noexcept
;
44 * Attaches a span processor to list of configured processors to this tracer context.
45 * Processor once attached can't be removed.
46 * @param processor The new span processor for this tracer. This must not be
47 * a nullptr. Ownership is given to the `TracerContext`.
49 * Note: This method is not thread safe.
51 void AddProcessor(std::unique_ptr
<SpanProcessor
> processor
) noexcept
;
54 * Obtain the sampler associated with this tracer.
55 * @return The sampler for this tracer.
57 Sampler
&GetSampler() const noexcept
;
60 * Obtain the configured (composite) processor.
62 * Note: When more than one processor is active, this will
63 * return an "aggregate" processor
65 SpanProcessor
&GetProcessor() const noexcept
;
68 * Obtain the resource associated with this tracer context.
69 * @return The resource for this tracer context.
71 const opentelemetry::sdk::resource::Resource
&GetResource() const noexcept
;
74 * Obtain the Id Generator associated with this tracer context.
75 * @return The ID Generator for this tracer context.
77 opentelemetry::sdk::trace::IdGenerator
&GetIdGenerator() const noexcept
;
80 * Force all active SpanProcessors to flush any buffered spans
81 * within the given timeout.
83 bool ForceFlush(std::chrono::microseconds timeout
= (std::chrono::microseconds::max
)()) noexcept
;
86 * Shutdown the span processor associated with this tracer provider.
88 bool Shutdown() noexcept
;
91 // order of declaration is important here - resource object should be destroyed after processor.
92 opentelemetry::sdk::resource::Resource resource_
;
93 std::unique_ptr
<Sampler
> sampler_
;
94 std::unique_ptr
<IdGenerator
> id_generator_
;
95 std::unique_ptr
<SpanProcessor
> processor_
;
100 OPENTELEMETRY_END_NAMESPACE