-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathtracer_provider.cc
149 lines (132 loc) · 4.9 KB
/
tracer_provider.cc
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <algorithm>
#include <chrono>
#include <mutex>
#include <utility>
#include <vector>
#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/instrumentationscope/scope_configurator.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/id_generator.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/sampler.h"
#include "opentelemetry/sdk/trace/tracer.h"
#include "opentelemetry/sdk/trace/tracer_config.h"
#include "opentelemetry/sdk/trace/tracer_context.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/tracer.h"
#include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
namespace resource = opentelemetry::sdk::resource;
namespace trace_api = opentelemetry::trace;
TracerProvider::TracerProvider(std::unique_ptr<TracerContext> context) noexcept
: context_(std::move(context))
{
OTEL_INTERNAL_LOG_DEBUG("[TracerProvider] TracerProvider created.");
}
TracerProvider::TracerProvider(
std::unique_ptr<SpanProcessor> processor,
const resource::Resource &resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator,
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>>
tracer_configurator) noexcept
{
std::vector<std::unique_ptr<SpanProcessor>> processors;
processors.push_back(std::move(processor));
context_ =
std::make_shared<TracerContext>(std::move(processors), resource, std::move(sampler),
std::move(id_generator), std::move(tracer_configurator));
}
TracerProvider::TracerProvider(
std::vector<std::unique_ptr<SpanProcessor>> &&processors,
const resource::Resource &resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator,
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>>
tracer_configurator) noexcept
: context_(std::make_shared<TracerContext>(std::move(processors),
resource,
std::move(sampler),
std::move(id_generator),
std::move(tracer_configurator)))
{}
TracerProvider::~TracerProvider()
{
// Tracer hold the shared pointer to the context. So we can not use destructor of TracerContext to
// Shutdown and flush all pending recordables when we have more than one tracer.These recordables
// may use the raw pointer of instrumentation_scope_ in Tracer
if (context_)
{
context_->Shutdown();
}
}
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url,
const opentelemetry::common::KeyValueIterable *attributes) noexcept
#else
nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url) noexcept
#endif
{
#if OPENTELEMETRY_ABI_VERSION_NO < 2
const opentelemetry::common::KeyValueIterable *attributes = nullptr;
#endif
if (name.data() == nullptr)
{
OTEL_INTERNAL_LOG_ERROR("[TracerProvider::GetTracer] Library name is null.");
name = "";
}
else if (name == "")
{
OTEL_INTERNAL_LOG_ERROR("[TracerProvider::GetTracer] Library name is empty.");
}
const std::lock_guard<std::mutex> guard(lock_);
for (auto &tracer : tracers_)
{
auto &tracer_scope = tracer->GetInstrumentationScope();
if (tracer_scope.equal(name, version, schema_url, attributes))
{
return nostd::shared_ptr<trace_api::Tracer>{tracer};
}
}
instrumentationscope::InstrumentationScopeAttributes attrs_map(attributes);
auto scope =
instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map);
auto tracer = std::shared_ptr<Tracer>(new Tracer(context_, std::move(scope)));
tracers_.push_back(tracer);
return nostd::shared_ptr<trace_api::Tracer>{tracer};
}
void TracerProvider::AddProcessor(std::unique_ptr<SpanProcessor> processor) noexcept
{
context_->AddProcessor(std::move(processor));
}
const resource::Resource &TracerProvider::GetResource() const noexcept
{
return context_->GetResource();
}
bool TracerProvider::Shutdown(std::chrono::microseconds timeout) noexcept
{
return context_->Shutdown(timeout);
}
bool TracerProvider::ForceFlush(std::chrono::microseconds timeout) noexcept
{
return context_->ForceFlush(timeout);
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE