This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
/
Span.cpp
160 lines (136 loc) Β· 4.61 KB
/
Span.cpp
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
/*
* Copyright (c) 2017 Uber Technologies, Inc.
*
* 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 "jaegertracing/Span.h"
#include "jaegertracing/Tracer.h"
#include "jaegertracing/baggage/BaggageSetter.h"
#include <cassert>
#include <cstdint>
#include <istream>
#include <memory>
#include <opentracing/value.h>
namespace jaegertracing {
namespace {
struct SamplingPriorityVisitor {
using result_type = bool;
bool operator()(bool boolValue) const { return boolValue; }
bool operator()(double doubleValue) const { return doubleValue > 0; }
bool operator()(int64_t intValue) const { return intValue > 0; }
bool operator()(uint64_t uintValue) const { return uintValue > 0; }
bool operator()(const std::string& str) const
{
std::istringstream iss(str);
auto intValue = 0;
if (!(iss >> intValue)) {
return false;
}
return intValue > 0;
}
bool operator()(std::nullptr_t) const { return false; }
bool operator()(const char* str) const
{
return operator()(std::string(str));
}
template <typename OtherType>
bool operator()(OtherType) const
{
return false;
}
};
} // anonymous namespace
void Span::SetBaggageItem(opentracing::string_view restrictedKey,
opentracing::string_view value) noexcept
{
std::lock_guard<std::mutex> lock(_mutex);
const auto& baggageSetter = _tracer->baggageSetter();
auto baggage = _context.baggage();
baggageSetter.setBaggage(*this,
baggage,
restrictedKey,
value,
[this](std::vector<Tag>::const_iterator first,
std::vector<Tag>::const_iterator last) {
logFieldsNoLocking(first, last);
});
_context = _context.withBaggage(baggage);
}
void Span::FinishWithOptions(
const opentracing::FinishSpanOptions& finishSpanOptions) noexcept
{
const auto finishTimeSteady =
(finishSpanOptions.finish_steady_timestamp == SteadyClock::time_point())
? SteadyClock::now()
: finishSpanOptions.finish_steady_timestamp;
std::shared_ptr<const Tracer> tracer;
{
std::lock_guard<std::mutex> lock(_mutex);
if (isFinished()) {
// Already finished, so return immediately.
return;
}
_duration = finishTimeSteady - _startTimeSteady;
tracer = _tracer;
std::copy(finishSpanOptions.log_records.begin(),
finishSpanOptions.log_records.end(),
std::back_inserter(_logs));
}
// Call `reportSpan` even for non-sampled traces.
if (tracer) {
tracer->reportSpan(*this);
}
}
const opentracing::Tracer& Span::tracer() const noexcept
{
std::lock_guard<std::mutex> lock(_mutex);
if (_tracer) {
return *_tracer;
}
auto tracer = opentracing::Tracer::Global();
assert(tracer);
return *tracer;
}
std::string Span::serviceName() const noexcept
{
std::lock_guard<std::mutex> lock(_mutex);
return serviceNameNoLock();
}
std::string Span::serviceNameNoLock() const noexcept
{
if (!_tracer) {
return std::string();
}
return _tracer->serviceName();
}
void Span::setSamplingPriority(const opentracing::Value& value)
{
SamplingPriorityVisitor visitor;
const auto priority = opentracing::Value::visit(value, visitor);
std::lock_guard<std::mutex> lock(_mutex);
auto newFlags = _context.flags();
if (priority) {
newFlags |= static_cast<unsigned char>(SpanContext::Flag::kSampled) |
static_cast<unsigned char>(SpanContext::Flag::kDebug);
}
else {
newFlags &= ~static_cast<unsigned char>(SpanContext::Flag::kSampled);
}
_context = SpanContext(_context.traceID(),
_context.spanID(),
_context.parentID(),
newFlags,
_context.baggage(),
_context.debugID());
}
} // namespace jaegertracing