File: factory.h

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (65 lines) | stat: -rw-r--r-- 2,107 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <memory>
#include <string>

#include "opentelemetry/plugin/detail/utility.h"  // IWYU pragma: export
#include "opentelemetry/plugin/tracer.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace plugin
{
/**
 * Factory creates OpenTelemetry objects from configuration strings.
 */
class Factory final
{
public:
  class FactoryImpl
  {
  public:
    virtual ~FactoryImpl() {}

    virtual nostd::unique_ptr<TracerHandle> MakeTracerHandle(
        nostd::string_view tracer_config,
        nostd::unique_ptr<char[]> &error_message) const noexcept = 0;
  };

  Factory(std::shared_ptr<DynamicLibraryHandle> library_handle,
          std::unique_ptr<FactoryImpl> &&factory_impl) noexcept
      : library_handle_{std::move(library_handle)}, factory_impl_{std::move(factory_impl)}
  {}

  /**
   * Construct a tracer from a configuration string.
   * @param tracer_config a representation of the tracer's config as a string.
   * @param error_message on failure this will contain an error message.
   * @return a Tracer on success or nullptr on failure.
   */
  std::shared_ptr<trace::Tracer> MakeTracer(nostd::string_view tracer_config,
                                            std::string &error_message) const noexcept
  {
    nostd::unique_ptr<char[]> plugin_error_message;
    auto tracer_handle = factory_impl_->MakeTracerHandle(tracer_config, plugin_error_message);
    if (tracer_handle == nullptr)
    {
      detail::CopyErrorMessage(plugin_error_message.get(), error_message);
      return nullptr;
    }
    return std::shared_ptr<trace::Tracer>{new (std::nothrow)
                                              Tracer{library_handle_, std::move(tracer_handle)}};
  }

private:
  // Note: The order is important here.
  //
  // It's undefined behavior to close the library while a loaded FactoryImpl is still active.
  std::shared_ptr<DynamicLibraryHandle> library_handle_;
  std::unique_ptr<FactoryImpl> factory_impl_;
};
}  // namespace plugin
OPENTELEMETRY_END_NAMESPACE