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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <chrono>
#include <utility>
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/sdk/trace/processor.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/span_metadata.h"
#include "opentelemetry/trace/trace_flags.h"
#include "opentelemetry/version.h"
#include "src/trace/span.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
using opentelemetry::common::SteadyTimestamp;
using opentelemetry::common::SystemTimestamp;
namespace common = opentelemetry::common;
namespace
{
SystemTimestamp NowOr(const SystemTimestamp &system)
{
if (system == SystemTimestamp())
{
return SystemTimestamp(std::chrono::system_clock::now());
}
else
{
return system;
}
}
SteadyTimestamp NowOr(const SteadyTimestamp &steady)
{
if (steady == SteadyTimestamp())
{
return SteadyTimestamp(std::chrono::steady_clock::now());
}
else
{
return steady;
}
}
} // namespace
Span::Span(std::shared_ptr<Tracer> &&tracer,
nostd::string_view name,
const common::KeyValueIterable &attributes,
const opentelemetry::trace::SpanContextKeyValueIterable &links,
const opentelemetry::trace::StartSpanOptions &options,
const opentelemetry::trace::SpanContext &parent_span_context,
std::unique_ptr<opentelemetry::trace::SpanContext> span_context) noexcept
: tracer_{std::move(tracer)},
recordable_{tracer_->GetProcessor().MakeRecordable()},
start_steady_time{options.start_steady_time},
span_context_(std::move(span_context))
{
if (recordable_ == nullptr)
{
return;
}
recordable_->SetName(name);
recordable_->SetInstrumentationScope(tracer_->GetInstrumentationScope());
recordable_->SetIdentity(*span_context_, parent_span_context.IsValid()
? parent_span_context.span_id()
: opentelemetry::trace::SpanId());
recordable_->SetTraceFlags(span_context_->trace_flags());
attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
recordable_->SetAttribute(key, value);
return true;
});
links.ForEachKeyValue([&](const opentelemetry::trace::SpanContext &span_context,
const common::KeyValueIterable &attributes) {
recordable_->AddLink(span_context, attributes);
return true;
});
recordable_->SetSpanKind(options.kind);
recordable_->SetStartTime(NowOr(options.start_system_time));
start_steady_time = NowOr(options.start_steady_time);
recordable_->SetResource(tracer_->GetResource());
tracer_->GetProcessor().OnStart(*recordable_, parent_span_context);
}
Span::~Span()
{
End();
}
void Span::SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->SetAttribute(key, value);
}
void Span::AddEvent(nostd::string_view name) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->AddEvent(name);
}
void Span::AddEvent(nostd::string_view name, SystemTimestamp timestamp) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->AddEvent(name, timestamp);
}
void Span::AddEvent(nostd::string_view name, const common::KeyValueIterable &attributes) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->AddEvent(name, attributes);
}
void Span::AddEvent(nostd::string_view name,
SystemTimestamp timestamp,
const common::KeyValueIterable &attributes) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->AddEvent(name, timestamp, attributes);
}
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void Span::AddLink(const opentelemetry::trace::SpanContext &target,
const opentelemetry::common::KeyValueIterable &attrs) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->AddLink(target, attrs);
}
void Span::AddLinks(const opentelemetry::trace::SpanContextKeyValueIterable &links) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
links.ForEachKeyValue([&](const opentelemetry::trace::SpanContext &span_context,
const common::KeyValueIterable &attributes) {
recordable_->AddLink(span_context, attributes);
return true;
});
}
#endif
void Span::SetStatus(opentelemetry::trace::StatusCode code, nostd::string_view description) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->SetStatus(code, description);
}
void Span::UpdateName(nostd::string_view name) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}
recordable_->SetName(name);
}
void Span::End(const opentelemetry::trace::EndSpanOptions &options) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (has_ended_ == true)
{
return;
}
has_ended_ = true;
if (recordable_ == nullptr)
{
return;
}
auto end_steady_time = NowOr(options.end_steady_time);
recordable_->SetDuration(std::chrono::steady_clock::time_point(end_steady_time) -
std::chrono::steady_clock::time_point(start_steady_time));
tracer_->GetProcessor().OnEnd(std::move(recordable_));
recordable_.reset();
}
bool Span::IsRecording() const noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
return recordable_ != nullptr;
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
|