This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathspan_impl.cc
More file actions
199 lines (179 loc) · 6.37 KB
/
span_impl.cc
File metadata and controls
199 lines (179 loc) · 6.37 KB
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
// Copyright 2017, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "opencensus/trace/internal/span_impl.h"
#include <deque>
#include <unordered_map>
#include <utility>
#include <vector>
#include "absl/time/clock.h"
#include "opencensus/trace/attribute_value_ref.h"
#include "opencensus/trace/exporter/attribute_value.h"
#include "opencensus/trace/exporter/message_event.h"
#include "opencensus/trace/internal/local_span_store_impl.h"
#include "opencensus/trace/internal/running_span_store_impl.h"
#include "opencensus/trace/internal/span_exporter_impl.h"
#include "opencensus/trace/span.h"
namespace opencensus {
namespace trace {
namespace {
template <typename T>
std::vector<T> CopyTraceEvents(const std::deque<T>& events) {
std::vector<T> trace_events;
trace_events.reserve(events.size());
for (const auto& event : events) {
trace_events.emplace_back(event);
}
return trace_events;
}
template <typename T>
std::vector<exporter::SpanData::TimeEvent<T>> CopyEventWithTime(
const std::deque<EventWithTime<T>>& events) {
std::vector<exporter::SpanData::TimeEvent<T>> time_events;
time_events.reserve(events.size());
for (const auto& event : events) {
auto tmp_event = event.event;
time_events.emplace_back(event.time, std::move(tmp_event));
}
return time_events;
}
// Deep-copies an initializer_list of absl::string_view keys and
// AttributeValueRefs (cheap, used in the API) to an unordered_map that owns all
// of the data in it. If the same key appears multiple times, the last value
// wins.
std::unordered_map<std::string, exporter::AttributeValue> CopyAttributes(
AttributesRef attributes) {
std::unordered_map<std::string, exporter::AttributeValue> out;
for (const auto& pair : attributes) {
auto iter_inserted = out.insert(
{std::string(pair.first), exporter::AttributeValue(pair.second)});
if (!iter_inserted.second) {
// Already exists, update.
iter_inserted.first->second = exporter::AttributeValue(pair.second);
}
}
return out;
}
} // namespace
SpanImpl::SpanImpl(const SpanContext& context, const TraceParams& trace_params,
absl::string_view name, const SpanId& parent_span_id,
bool remote_parent)
: start_time_(absl::Now()),
name_(name),
parent_span_id_(parent_span_id),
context_(context),
annotations_(trace_params.max_annotations),
message_events_(trace_params.max_message_events),
links_(trace_params.max_links),
attributes_(trace_params.max_attributes),
has_ended_(false),
remote_parent_(remote_parent),
active_child_count_(0),
end_requested_(false) {}
void SpanImpl::AddAttributes(AttributesRef attributes) {
absl::MutexLock l(&mu_);
if (!has_ended_) {
for (const auto& attr : attributes) {
attributes_.AddAttribute(attr.first,
exporter::AttributeValue(attr.second));
}
}
}
void SpanImpl::AddAnnotation(absl::string_view description,
AttributesRef attributes) {
absl::MutexLock l(&mu_);
if (!has_ended_) {
annotations_.AddEvent(EventWithTime<exporter::Annotation>(
absl::Now(),
exporter::Annotation(description, CopyAttributes(attributes))));
}
}
void SpanImpl::AddMessageEvent(exporter::MessageEvent::Type type,
uint32_t message_id,
uint32_t compressed_message_size,
uint32_t uncompressed_message_size) {
absl::MutexLock l(&mu_);
if (!has_ended_) {
message_events_.AddEvent(EventWithTime<exporter::MessageEvent>(
absl::Now(),
exporter::MessageEvent(type, message_id, compressed_message_size,
uncompressed_message_size)));
}
}
void SpanImpl::AddLink(const SpanContext& context, exporter::Link::Type type,
AttributesRef attributes) {
absl::MutexLock l(&mu_);
if (!has_ended_) {
links_.AddEvent(exporter::Link(context, type, CopyAttributes(attributes)));
}
}
void SpanImpl::SetStatus(exporter::Status&& status) {
absl::MutexLock l(&mu_);
if (!has_ended_) {
status_ = std::move(status);
}
}
bool SpanImpl::End() {
absl::MutexLock l(&mu_);
if (context_.trace_options().HasStrictSpans() && active_child_count_ > 0) {
end_requested_ = true;
return false;
}
if (has_ended_) {
assert(false && "Invalid attempt to End() the same Span more than once.");
// In non-debug builds, just ignore the second End().
return false;
}
has_ended_ = true;
end_time_ = absl::Now();
return true;
}
bool SpanImpl::HasEnded() const {
absl::MutexLock l(&mu_);
return has_ended_;
}
void SpanImpl::IncrementChildCount() const {
absl::MutexLock l(&mu_);
active_child_count_++;
}
void SpanImpl::DecrementChildCount() {
bool shouldEnd = false;
{
absl::MutexLock l(&mu_);
assert(active_child_count_ > 0);
active_child_count_--;
shouldEnd = (active_child_count_ == 0 && end_requested_);
}
if (shouldEnd) {
Span::EndSpanById(context_.span_id());
}
}
exporter::SpanData SpanImpl::ToSpanData() const {
absl::MutexLock l(&mu_);
// Make a deep copy of attributes.
std::unordered_map<std::string, exporter::AttributeValue> attributes =
attributes_.attributes();
return exporter::SpanData(
name_, context_, parent_span_id_,
exporter::SpanData::TimeEvents<exporter::Annotation>(
CopyEventWithTime(annotations_.events()),
annotations_.num_events_dropped()),
exporter::SpanData::TimeEvents<exporter::MessageEvent>(
CopyEventWithTime(message_events_.events()),
message_events_.num_events_dropped()),
CopyTraceEvents(links_.events()), links_.num_events_dropped(),
std::move(attributes), attributes_.num_attributes_dropped(), has_ended_,
start_time_, end_time_, status_, remote_parent_);
}
} // namespace trace
} // namespace opencensus