Skip to content

Commit 6f734b2

Browse files
authored
automatic timestamp injection to the trace data (#46)
* automatic timestamp injection to the trace data * add timepoint class and allow to accept arbitary time * delete component id limitation
1 parent b8a48a2 commit 6f734b2

File tree

12 files changed

+331
-68
lines changed

12 files changed

+331
-68
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ TracerPtr tracer = createInsecureGrpcTracer(tracer_config);
6666
6767
#### Fetch propagated span
6868
69-
cpp2sky supports only HTTP tracer now.
69+
cpp2sky supports only HTTP tracer now.
7070
Tracing span will be delivered from `sw8` and `sw8-x` HTTP headers. For more detail, please visit [here](https://github.com/apache/skywalking/blob/08781b41a8255bcceebb3287364c81745a04bec6/docs/en/protocols/Skywalking-Cross-Process-Propagation-Headers-Protocol-v3.md)
7171
Then, you can create propagated span object by decoding these items.
7272
@@ -100,16 +100,16 @@ CurrentSegmentSpanPtr current_span = current_segment->createCurrentSegmentSpan(c
100100
101101
#### Send segment to OAP
102102
103-
Note that SegmentContextPtr is unique pointer. So when you'd like to send data, you must move it and don't refer after sending,
103+
Note that SegmentContextPtr is unique pointer. So when you'd like to send data, you must move it and don't refer after sending,
104104
to avoid undefined behavior.
105105
106106
```cpp
107107
SegmentContextPtr current_segment = createSegmentContext(config);
108108
CurrentSegmentSpanPtr current_span = current_segment->createCurrentSegmentRootSpan();
109109
110+
current_span->startSpan();
110111
current_span->setOperationName("sample_workload");
111-
current_span->setStartTime(...);
112-
current_span->setEndTime(...);
112+
current_span->endSpan();
113113
114114
tracer->sendSegment(std::move(current_segment));
115115
```

cpp2sky/BUILD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ cc_library(
4848
visibility = ["//visibility:public"],
4949
)
5050

51+
cc_library(
52+
name = "time_interface",
53+
hdrs = ["time.h"],
54+
visibility = ["//visibility:public"],
55+
)
56+
5157
cc_library(
5258
name = "cpp2sky_interface",
5359
hdrs = [
@@ -61,4 +67,4 @@ cc_library(
6167
"@skywalking_data_collect_protocol//language-agent:tracing_protocol_cc_grpc",
6268
],
6369
visibility = ["//visibility:public"],
64-
)
70+
)

cpp2sky/segment_context.h

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "cpp2sky/config.h"
2222
#include "cpp2sky/propagation.h"
23+
#include "cpp2sky/time.h"
2324
#include "language-agent/Tracing.pb.h"
2425

2526
namespace cpp2sky {
@@ -33,11 +34,73 @@ class CurrentSegmentSpan {
3334
*/
3435
virtual SpanObject createSpanObject() = 0;
3536

37+
/**
38+
* Get sampling status. If true, spans belongs to this segment will be sent to
39+
* OAP.
40+
*/
41+
virtual bool samplingStatus() const = 0;
42+
3643
/**
3744
* Get span ID.
3845
*/
3946
virtual int32_t spanId() const = 0;
4047

48+
/**
49+
* Get parent span ID.
50+
*/
51+
virtual int32_t parentSpanId() const = 0;
52+
53+
/**
54+
* Get start time.
55+
*/
56+
virtual int64_t startTime() const = 0;
57+
58+
/**
59+
* Get end time.
60+
*/
61+
virtual int64_t endTime() const = 0;
62+
63+
/**
64+
* Get peer address.
65+
*/
66+
virtual const std::string& peer() const = 0;
67+
68+
/**
69+
* Get span type.
70+
*/
71+
virtual SpanType spanType() const = 0;
72+
73+
/**
74+
* Get span layer.
75+
*/
76+
virtual SpanLayer spanLayer() const = 0;
77+
78+
/**
79+
* Get error occurred or not.
80+
*/
81+
virtual bool errorStatus() const = 0;
82+
83+
/**
84+
* Enable to skip analysis or not.
85+
*/
86+
virtual bool skipAnalysis() const = 0;
87+
88+
/**
89+
* Get component ID.
90+
*/
91+
virtual int32_t componentId() const = 0;
92+
93+
/**
94+
* Get tags.
95+
*/
96+
virtual const std::vector<std::pair<std::string, std::string>>& tags()
97+
const = 0;
98+
99+
/**
100+
* Get logs.
101+
*/
102+
virtual const std::vector<Log>& logs() const = 0;
103+
41104
/**
42105
* Get operation name.
43106
*/
@@ -51,17 +114,21 @@ class CurrentSegmentSpan {
51114
/**
52115
* Set start time to calculate execution time.
53116
*/
54-
virtual void setStartTime(int64_t start_time) = 0;
117+
virtual void startSpan() = 0;
118+
virtual void startSpan(TimePoint<SystemTime> current_time) = 0;
119+
virtual void startSpan(TimePoint<SteadyTime> current_time) = 0;
55120

56121
/**
57122
* Set end time to calculate execution time.
58123
*/
59-
virtual void setEndTime(int64_t end_time) = 0;
124+
virtual void endSpan() = 0;
125+
virtual void endSpan(TimePoint<SystemTime> current_time) = 0;
126+
virtual void endSpan(TimePoint<SteadyTime> current_time) = 0;
60127

61128
/**
62129
* Set operation name for this span (lvalue)
63130
*/
64-
virtual void setOperationName(std::string& operation_name) = 0;
131+
virtual void setOperationName(const std::string& operation_name) = 0;
65132

66133
/**
67134
* Set operation name for this span (rvalue)
@@ -71,7 +138,7 @@ class CurrentSegmentSpan {
71138
/**
72139
* Set peer address for this span (lvalue)
73140
*/
74-
virtual void setPeer(std::string& remote_address) = 0;
141+
virtual void setPeer(const std::string& remote_address) = 0;
75142

76143
/**
77144
* Set peer address for this span (rvalue)
@@ -104,7 +171,7 @@ class CurrentSegmentSpan {
104171
/**
105172
* Set tag to current span. (lvalue)
106173
*/
107-
virtual void addTag(std::string& key, std::string& value) = 0;
174+
virtual void addTag(const std::string& key, const std::string& value) = 0;
108175

109176
/**
110177
* Set tag to current span. (rvalue)
@@ -113,8 +180,26 @@ class CurrentSegmentSpan {
113180

114181
/**
115182
* Add log related with current span.
183+
* @param set_time To determine whether to set actual time or not.
184+
* This value is introduced for unit-test.
185+
*/
186+
virtual void addLog(const std::string& key, const std::string& value,
187+
bool set_time = true) = 0;
188+
189+
/**
190+
* Set component ID.
191+
*/
192+
virtual void setComponentId(int32_t component_id) = 0;
193+
194+
/**
195+
* This span had finished or not.
196+
*/
197+
virtual bool finished() const = 0;
198+
199+
/**
200+
* Change sampling status. If true, it will be sampled.
116201
*/
117-
virtual void addLog(int64_t time, std::string& key, std::string& value) = 0;
202+
virtual void setSamplingStatus(bool do_sample) = 0;
118203
};
119204

120205
using CurrentSegmentSpanPtr = std::shared_ptr<CurrentSegmentSpan>;

cpp2sky/time.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2020 SkyAPM
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <chrono>
18+
19+
namespace cpp2sky {
20+
21+
using SystemTime = std::chrono::system_clock::time_point;
22+
using SteadyTime = std::chrono::steady_clock::time_point;
23+
24+
template <class T>
25+
static constexpr bool false_v = false;
26+
27+
template <class T>
28+
class TimePoint {
29+
static_assert(false_v<T>, "Invalid time type");
30+
};
31+
32+
template <>
33+
class TimePoint<SystemTime> {
34+
public:
35+
TimePoint(SystemTime point) : point_(point) {}
36+
TimePoint() { point_ = std::chrono::system_clock::now(); }
37+
38+
// Fetch timestamp with millisecond resolution
39+
int64_t fetch() {
40+
return std::chrono::duration_cast<std::chrono::milliseconds>(
41+
point_.time_since_epoch())
42+
.count();
43+
}
44+
45+
private:
46+
SystemTime point_;
47+
};
48+
49+
template <>
50+
class TimePoint<SteadyTime> {
51+
public:
52+
TimePoint(SteadyTime point) : point_(point) {}
53+
TimePoint() { point_ = std::chrono::steady_clock::now(); }
54+
55+
// Fetch timestamp with millisecond resolution
56+
int64_t fetch() {
57+
return std::chrono::duration_cast<std::chrono::milliseconds>(
58+
point_.time_since_epoch())
59+
.count();
60+
}
61+
62+
private:
63+
SteadyTime point_;
64+
};
65+
66+
} // namespace cpp2sky

example/sample.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ int main() {
4848
auto current_span = current_segment->createCurrentSegmentRootSpan();
4949

5050
// 4. Set info
51+
current_span->startSpan();
5152
current_span->setOperationName("/ping");
52-
current_span->setStartTime(now());
53-
current_span->setEndTime(now());
53+
current_span->endSpan();
5454

5555
// 5. Send span data
5656
tracer->sendSegment(std::move(current_segment));

example/sample_client.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ int main() {
4545
auto current_span = current_segment->createCurrentSegmentRootSpan();
4646

4747
// 4. Set info
48+
current_span->startSpan();
4849
current_span->setOperationName("/ping");
49-
current_span->setStartTime(now());
5050

5151
httplib::Client cli("remote", 8082);
5252
httplib::Headers headers = {{"sw8", current_segment->createSW8HeaderValue(
5353
current_span, "remote:8082")}};
5454
auto res = cli.Get("/ping", headers);
5555

56-
current_span->setEndTime(now());
56+
current_span->endSpan();
5757

5858
// 5. Send span data
5959
tracer->sendSegment(std::move(current_segment));

source/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cc_library(
2323
"//cpp2sky:segment_context_interface",
2424
"//cpp2sky:propagation_interface",
2525
"//cpp2sky:tracer_interface",
26+
"//cpp2sky:time_interface",
2627
"//source/utils:util_lib",
2728
],
2829
visibility = ["//visibility:public"],

0 commit comments

Comments
 (0)