1616
1717#include < string>
1818
19+ #include " cpp2sky/exception.h"
1920#include " cpp2sky/time.h"
2021#include " language-agent/Tracing.pb.h"
2122#include " source/utils/base64.h"
@@ -40,6 +41,7 @@ SpanObject CurrentSegmentSpanImpl::createSpanObject() {
4041 obj.set_componentid (component_id_);
4142 obj.set_iserror (is_error_);
4243 obj.set_peer (peer_);
44+ obj.set_skipanalysis (skip_analysis_);
4345
4446 auto parent_span = parent_segment_context_.parentSpanContext ();
4547 // Inject request parent to the current segment.
@@ -68,39 +70,51 @@ SpanObject CurrentSegmentSpanImpl::createSpanObject() {
6870 *entry = log;
6971 }
7072
71- if (parent_segment_context_.parentSpanContextExtension () != nullptr ) {
72- if (parent_segment_context_.parentSpanContextExtension ()->tracingMode () ==
73- TracingMode::Skip) {
74- obj.set_skipanalysis (true );
75- }
76- }
7773 return obj;
7874}
7975
80- void CurrentSegmentSpanImpl::addLog (const std::string& key,
81- const std::string& value, bool set_time) {
76+ void CurrentSegmentSpanImpl::addLog (std::string key, std::string value) {
77+ assert (!finished_);
78+ auto now = TimePoint<SystemTime>();
79+ addLog (key, value, now);
80+ }
81+
82+ void CurrentSegmentSpanImpl::addLog (std::string key, std::string value,
83+ TimePoint<SystemTime> current_time) {
8284 assert (!finished_);
8385 Log l;
84- if (set_time) {
85- // SystemTimePoint now = SystemTime::now();
86- // l.set_time(millisecondsFromEpoch(now));
87- }
86+ l.set_time (current_time.fetch ());
8887 auto * entry = l.add_data ();
8988 entry->set_key (key);
9089 entry->set_value (value);
9190 logs_.emplace_back (l);
9291}
9392
94- void CurrentSegmentSpanImpl::startSpan () {
93+ void CurrentSegmentSpanImpl::addLog (std::string key, std::string value,
94+ TimePoint<SteadyTime> current_time) {
95+ assert (!finished_);
96+ Log l;
97+ l.set_time (current_time.fetch ());
98+ auto * entry = l.add_data ();
99+ entry->set_key (key);
100+ entry->set_value (value);
101+ logs_.emplace_back (l);
102+ }
103+
104+ void CurrentSegmentSpanImpl::startSpan (std::string operation_name) {
95105 auto now = TimePoint<SystemTime>();
96- startSpan (now);
106+ startSpan (operation_name, now);
97107}
98108
99- void CurrentSegmentSpanImpl::startSpan (TimePoint<SystemTime> current_time) {
109+ void CurrentSegmentSpanImpl::startSpan (std::string operation_name,
110+ TimePoint<SystemTime> current_time) {
111+ operation_name_ = operation_name;
100112 start_time_ = current_time.fetch ();
101113}
102114
103- void CurrentSegmentSpanImpl::startSpan (TimePoint<SteadyTime> current_time) {
115+ void CurrentSegmentSpanImpl::startSpan (std::string operation_name,
116+ TimePoint<SteadyTime> current_time) {
117+ operation_name_ = operation_name;
104118 start_time_ = current_time.fetch ();
105119}
106120
@@ -170,6 +184,10 @@ CurrentSegmentSpanPtr SegmentContextImpl::createCurrentSegmentSpan(
170184 }
171185 // It supports only HTTP request tracing.
172186 current_span->setSpanLayer (SpanLayer::Http);
187+ if (should_skip_analysis_) {
188+ current_span->setSkipAnalysis ();
189+ }
190+
173191 spans_.push_back (current_span);
174192 return current_span;
175193}
@@ -180,16 +198,33 @@ CurrentSegmentSpanPtr SegmentContextImpl::createCurrentSegmentRootSpan() {
180198}
181199
182200std::string SegmentContextImpl::createSW8HeaderValue (
183- CurrentSegmentSpanPtr parent_span, std::string& target_address,
184- bool sample) {
185- std::string header_value;
201+ CurrentSegmentSpanPtr parent_span, const std::string& target_address) {
186202 if (parent_span == nullptr ) {
187- return header_value;
203+ if (spans_.empty ()) {
204+ throw TracerException (
205+ " Can't create propagation header because current segment has no "
206+ " valid span." );
207+ }
208+ return encodeSpan (spans_.back (), target_address);
188209 }
210+ return encodeSpan (parent_span, target_address);
211+ }
212+
213+ std::string SegmentContextImpl::createSW8HeaderValue (
214+ CurrentSegmentSpanPtr parent_span, std::string&& target_address) {
215+ return createSW8HeaderValue (parent_span, target_address);
216+ }
217+
218+ std::string SegmentContextImpl::encodeSpan (CurrentSegmentSpanPtr parent_span,
219+ const std::string& target_address) {
220+ assert (parent_span);
221+ std::string header_value;
222+
189223 auto parent_spanid = std::to_string (parent_span->spanId ());
190224 auto endpoint = spans_.front ()->operationName ();
191225
192- header_value += sample ? " 1-" : " 0-" ;
226+ // always send to OAP
227+ header_value += " 1-" ;
193228 header_value += Base64::encode (trace_id_) + " -" ;
194229 header_value += Base64::encode (trace_segment_id_) + " -" ;
195230 header_value += parent_spanid + " -" ;
@@ -201,12 +236,6 @@ std::string SegmentContextImpl::createSW8HeaderValue(
201236 return header_value;
202237}
203238
204- std::string SegmentContextImpl::createSW8HeaderValue (
205- CurrentSegmentSpanPtr parent_span, std::string&& target_address,
206- bool sample) {
207- return createSW8HeaderValue (parent_span, target_address, sample);
208- }
209-
210239SegmentObject SegmentContextImpl::createSegmentObject () {
211240 SegmentObject obj;
212241 obj.set_traceid (trace_id_);
@@ -226,9 +255,8 @@ SegmentContextFactoryImpl::SegmentContextFactoryImpl(const TracerConfig& cfg)
226255 : service_name_(cfg.service_name()), instance_name_(cfg.instance_name()) {}
227256
228257SegmentContextPtr SegmentContextFactoryImpl::create () {
229- auto context = std::make_unique<SegmentContextImpl>(
230- service_name_, instance_name_, random_generator_);
231- return context;
258+ return std::make_unique<SegmentContextImpl>(service_name_, instance_name_,
259+ random_generator_);
232260}
233261
234262SegmentContextPtr SegmentContextFactoryImpl::create (
@@ -239,9 +267,13 @@ SegmentContextPtr SegmentContextFactoryImpl::create(
239267
240268SegmentContextPtr SegmentContextFactoryImpl::create (
241269 SpanContextPtr span_context, SpanContextExtensionPtr ext_span_context) {
242- return std::make_unique<SegmentContextImpl>(service_name_, instance_name_,
243- span_context, ext_span_context,
244- random_generator_);
270+ auto context = std::make_unique<SegmentContextImpl>(
271+ service_name_, instance_name_, span_context, ext_span_context,
272+ random_generator_);
273+ if (ext_span_context->tracingMode () == TracingMode::Skip) {
274+ context->setSkipAnalysis ();
275+ }
276+ return context;
245277}
246278
247279} // namespace cpp2sky
0 commit comments