-
Notifications
You must be signed in to change notification settings - Fork 50
/
span.h
114 lines (89 loc) · 3.49 KB
/
span.h
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
#pragma once
#include <atomic>
#include <exception>
#include <memory>
#include <mutex>
#include "common/serialization_chain.h"
#include "tracer/baggage_flat_map.h"
#include "tracer/lightstep_span_context.h"
#include "tracer/propagation.h"
#include "tracer/tracer_impl.h"
#include <google/protobuf/io/coded_stream.h>
#include <opentracing/span.h>
namespace lightstep {
/**
* LightStep's implementation of the OpenTracing Span class.
*/
class Span final : public opentracing::Span, public LightStepSpanContext {
public:
Span(std::shared_ptr<const TracerImpl>&& tracer,
opentracing::string_view operation_name,
const opentracing::StartSpanOptions& options);
~Span() noexcept override;
// opentracing::Span
void FinishWithOptions(
const opentracing::FinishSpanOptions& options) noexcept override;
void SetOperationName(opentracing::string_view name) noexcept override;
void SetTag(opentracing::string_view key,
const opentracing::Value& value) noexcept override;
void SetBaggageItem(opentracing::string_view restricted_key,
opentracing::string_view value) noexcept override;
std::string BaggageItem(opentracing::string_view restricted_key) const
noexcept override;
void Log(std::initializer_list<
std::pair<opentracing::string_view, opentracing::Value>>
fields) noexcept override;
const opentracing::SpanContext& context() const noexcept override {
return *this;
}
const opentracing::Tracer& tracer() const noexcept override {
return *tracer_;
}
// opentracing::SpanContext
void ForeachBaggageItem(
std::function<bool(const std::string& key, const std::string& value)> f)
const override;
opentracing::expected<void> Inject(
const PropagationOptions& propagation_options,
std::ostream& writer) const override {
return this->InjectImpl(propagation_options, writer);
}
opentracing::expected<void> Inject(
const PropagationOptions& propagation_options,
const opentracing::TextMapWriter& writer) const override {
return this->InjectImpl(propagation_options, writer);
}
opentracing::expected<void> Inject(
const PropagationOptions& propagation_options,
const opentracing::HTTPHeadersWriter& writer) const override {
return this->InjectImpl(propagation_options, writer);
}
// LightStepSpanContext
bool sampled() const noexcept override;
uint64_t trace_id() const noexcept override { return trace_id_; }
uint64_t span_id() const noexcept override { return span_id_; }
private:
mutable std::mutex mutex_;
std::unique_ptr<SerializationChain> serialization_chain_;
google::protobuf::io::CodedOutputStream stream_;
std::chrono::steady_clock::time_point start_steady_;
std::atomic<bool> is_finished_{false};
std::shared_ptr<const TracerImpl> tracer_;
uint64_t trace_id_;
uint64_t span_id_;
BaggageFlatMap baggage_;
bool sampled_;
template <class Carrier>
opentracing::expected<void> InjectImpl(
const PropagationOptions& propagation_options, Carrier& writer) const {
std::lock_guard<std::mutex> lock_guard{mutex_};
return InjectSpanContext(propagation_options, writer, trace_id_, span_id_,
sampled_, baggage_);
}
bool SetSpanReference(
const std::pair<opentracing::SpanReferenceType,
const opentracing::SpanContext*>& reference,
uint64_t& trace_id);
void FinishImpl(const opentracing::FinishSpanOptions& options) noexcept;
};
} // namespace lightstep