1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
8 #include "opentelemetry/common/key_value_iterable.h"
9 #include "opentelemetry/plugin/detail/dynamic_library_handle.h"
10 #include "opentelemetry/plugin/detail/tracer_handle.h"
11 #include "opentelemetry/trace/tracer.h"
12 #include "opentelemetry/version.h"
14 OPENTELEMETRY_BEGIN_NAMESPACE
17 class Span final
: public trace::Span
20 Span(std::shared_ptr
<trace::Tracer
> &&tracer
, nostd::shared_ptr
<trace::Span
> span
) noexcept
21 : tracer_
{std::move(tracer
)}, span_
{span
}
25 void SetAttribute(nostd::string_view name
, const common::AttributeValue
&value
) noexcept override
27 span_
->SetAttribute(name
, value
);
30 void AddEvent(nostd::string_view name
) noexcept override
{ span_
->AddEvent(name
); }
32 void AddEvent(nostd::string_view name
, common::SystemTimestamp timestamp
) noexcept override
34 span_
->AddEvent(name
, timestamp
);
37 void AddEvent(nostd::string_view name
,
38 common::SystemTimestamp timestamp
,
39 const common::KeyValueIterable
&attributes
) noexcept override
41 span_
->AddEvent(name
, timestamp
, attributes
);
44 void SetStatus(trace::StatusCode code
, nostd::string_view description
) noexcept override
46 span_
->SetStatus(code
, description
);
49 void UpdateName(nostd::string_view name
) noexcept override
{ span_
->UpdateName(name
); }
51 void End(const trace::EndSpanOptions
&options
= {}) noexcept override
{ span_
->End(options
); }
53 bool IsRecording() const noexcept override
{ return span_
->IsRecording(); }
55 trace::SpanContext
GetContext() const noexcept override
{ return span_
->GetContext(); }
58 std::shared_ptr
<trace::Tracer
> tracer_
;
59 nostd::shared_ptr
<trace::Span
> span_
;
62 class Tracer final
: public trace::Tracer
, public std::enable_shared_from_this
<Tracer
>
65 Tracer(std::shared_ptr
<DynamicLibraryHandle
> library_handle
,
66 std::unique_ptr
<TracerHandle
> &&tracer_handle
) noexcept
67 : library_handle_
{std::move(library_handle
)}, tracer_handle_
{std::move(tracer_handle
)}
71 nostd::shared_ptr
<trace::Span
> StartSpan(
72 nostd::string_view name
,
73 const common::KeyValueIterable
&attributes
,
74 const trace::SpanContextKeyValueIterable
&links
,
75 const trace::StartSpanOptions
&options
= {}) noexcept override
77 auto span
= tracer_handle_
->tracer().StartSpan(name
, attributes
, links
, options
);
80 return nostd::shared_ptr
<trace::Span
>(nullptr);
82 return nostd::shared_ptr
<trace::Span
>{new (std::nothrow
) Span
{this->shared_from_this(), span
}};
85 void ForceFlushWithMicroseconds(uint64_t timeout
) noexcept override
87 tracer_handle_
->tracer().ForceFlushWithMicroseconds(timeout
);
90 void CloseWithMicroseconds(uint64_t timeout
) noexcept override
92 tracer_handle_
->tracer().CloseWithMicroseconds(timeout
);
96 // Note: The order is important here.
98 // It's undefined behavior to close the library while a loaded tracer is still active.
99 std::shared_ptr
<DynamicLibraryHandle
> library_handle_
;
100 std::unique_ptr
<TracerHandle
> tracer_handle_
;
102 } // namespace plugin
103 OPENTELEMETRY_END_NAMESPACE