]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | #include <memory> | |
6 | #include <string> | |
7 | ||
8 | #include "opentelemetry/plugin/detail/utility.h" | |
9 | #include "opentelemetry/plugin/tracer.h" | |
10 | #include "opentelemetry/version.h" | |
11 | ||
12 | OPENTELEMETRY_BEGIN_NAMESPACE | |
13 | namespace plugin | |
14 | { | |
15 | /** | |
16 | * Factory creates OpenTelemetry objects from configuration strings. | |
17 | */ | |
18 | class Factory final | |
19 | { | |
20 | public: | |
21 | class FactoryImpl | |
22 | { | |
23 | public: | |
24 | virtual ~FactoryImpl() {} | |
25 | ||
26 | virtual nostd::unique_ptr<TracerHandle> MakeTracerHandle( | |
27 | nostd::string_view tracer_config, | |
28 | nostd::unique_ptr<char[]> &error_message) const noexcept = 0; | |
29 | }; | |
30 | ||
31 | Factory(std::shared_ptr<DynamicLibraryHandle> library_handle, | |
32 | std::unique_ptr<FactoryImpl> &&factory_impl) noexcept | |
33 | : library_handle_{std::move(library_handle)}, factory_impl_{std::move(factory_impl)} | |
34 | {} | |
35 | ||
36 | /** | |
37 | * Construct a tracer from a configuration string. | |
38 | * @param tracer_config a representation of the tracer's config as a string. | |
39 | * @param error_message on failure this will contain an error message. | |
40 | * @return a Tracer on success or nullptr on failure. | |
41 | */ | |
42 | std::shared_ptr<opentelemetry::trace::Tracer> MakeTracer( | |
43 | nostd::string_view tracer_config, | |
44 | std::string &error_message) const noexcept | |
45 | { | |
46 | nostd::unique_ptr<char[]> plugin_error_message; | |
47 | auto tracer_handle = factory_impl_->MakeTracerHandle(tracer_config, plugin_error_message); | |
48 | if (tracer_handle == nullptr) | |
49 | { | |
50 | detail::CopyErrorMessage(plugin_error_message.get(), error_message); | |
51 | return nullptr; | |
52 | } | |
53 | return std::shared_ptr<opentelemetry::trace::Tracer>{ | |
54 | new (std::nothrow) Tracer{library_handle_, std::move(tracer_handle)}}; | |
55 | } | |
56 | ||
57 | private: | |
58 | // Note: The order is important here. | |
59 | // | |
60 | // It's undefined behavior to close the library while a loaded FactoryImpl is still active. | |
61 | std::shared_ptr<DynamicLibraryHandle> library_handle_; | |
62 | std::unique_ptr<FactoryImpl> factory_impl_; | |
63 | }; | |
64 | } // namespace plugin | |
65 | OPENTELEMETRY_END_NAMESPACE |