Skip to content

Commit b5181ce

Browse files
authored
replace string_view in span context (#74)
1 parent 147eaab commit b5181ce

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

cpp2sky/tracing_context.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class TracingSpan {
8888
/**
8989
* Get tags.
9090
*/
91-
virtual const std::vector<std::pair<std::string, std::string>>& tags()
92-
const = 0;
91+
virtual const std::vector<std::pair<std::string_view, std::string_view>>&
92+
tags() const = 0;
9393

9494
/**
9595
* Get logs.
@@ -109,10 +109,10 @@ class TracingSpan {
109109
/**
110110
* Set start time to calculate execution time.
111111
*/
112-
virtual void startSpan(std::string operation_name) = 0;
113-
virtual void startSpan(std::string operation_name,
112+
virtual void startSpan(std::string_view operation_name) = 0;
113+
virtual void startSpan(std::string_view operation_name,
114114
TimePoint<SystemTime> current_time) = 0;
115-
virtual void startSpan(std::string operation_name,
115+
virtual void startSpan(std::string_view operation_name,
116116
TimePoint<SteadyTime> current_time) = 0;
117117

118118
/**
@@ -158,15 +158,15 @@ class TracingSpan {
158158
/**
159159
* Set tag to current span.
160160
*/
161-
virtual void addTag(std::string key, std::string value) = 0;
161+
virtual void addTag(std::string_view key, std::string_view value) = 0;
162162

163163
/**
164164
* Add log related with current span.
165165
*/
166-
virtual void addLog(std::string key, std::string value) = 0;
167-
virtual void addLog(std::string key, std::string value,
166+
virtual void addLog(std::string_view key, std::string_view value) = 0;
167+
virtual void addLog(std::string_view key, std::string_view value,
168168
TimePoint<SystemTime> current_time) = 0;
169-
virtual void addLog(std::string key, std::string value,
169+
virtual void addLog(std::string_view key, std::string_view value,
170170
TimePoint<SteadyTime> current_time) = 0;
171171

172172
/**

source/tracing_context_impl.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "source/tracing_context_impl.h"
1616

1717
#include <string>
18+
#include <string_view>
1819

1920
#include "cpp2sky/exception.h"
2021
#include "cpp2sky/time.h"
@@ -61,8 +62,8 @@ skywalking::v3::SpanObject TracingSpanImpl::createSpanObject() {
6162

6263
for (auto& tag : tags_) {
6364
auto* entry = obj.mutable_tags()->Add();
64-
entry->set_key(tag.first);
65-
entry->set_value(tag.second);
65+
entry->set_key(std::string(tag.first));
66+
entry->set_value(std::string(tag.second));
6667
}
6768

6869
for (auto& log : logs_) {
@@ -73,46 +74,46 @@ skywalking::v3::SpanObject TracingSpanImpl::createSpanObject() {
7374
return obj;
7475
}
7576

76-
void TracingSpanImpl::addLog(std::string key, std::string value) {
77+
void TracingSpanImpl::addLog(std::string_view key, std::string_view value) {
7778
assert(!finished_);
7879
auto now = TimePoint<SystemTime>();
7980
addLog(key, value, now);
8081
}
8182

82-
void TracingSpanImpl::addLog(std::string key, std::string value,
83+
void TracingSpanImpl::addLog(std::string_view key, std::string_view value,
8384
TimePoint<SystemTime> current_time) {
8485
assert(!finished_);
8586
skywalking::v3::Log l;
8687
l.set_time(current_time.fetch());
8788
auto* entry = l.add_data();
88-
entry->set_key(key);
89-
entry->set_value(value);
89+
entry->set_key(std::string(key));
90+
entry->set_value(std::string(value));
9091
logs_.emplace_back(l);
9192
}
9293

93-
void TracingSpanImpl::addLog(std::string key, std::string value,
94+
void TracingSpanImpl::addLog(std::string_view key, std::string_view value,
9495
TimePoint<SteadyTime> current_time) {
9596
assert(!finished_);
9697
skywalking::v3::Log l;
9798
l.set_time(current_time.fetch());
9899
auto* entry = l.add_data();
99-
entry->set_key(key);
100-
entry->set_value(value);
100+
entry->set_key(std::string(key));
101+
entry->set_value(std::string(value));
101102
logs_.emplace_back(l);
102103
}
103104

104-
void TracingSpanImpl::startSpan(std::string operation_name) {
105+
void TracingSpanImpl::startSpan(std::string_view operation_name) {
105106
auto now = TimePoint<SystemTime>();
106107
startSpan(operation_name, now);
107108
}
108109

109-
void TracingSpanImpl::startSpan(std::string operation_name,
110+
void TracingSpanImpl::startSpan(std::string_view operation_name,
110111
TimePoint<SystemTime> current_time) {
111112
operation_name_ = operation_name;
112113
start_time_ = current_time.fetch();
113114
}
114115

115-
void TracingSpanImpl::startSpan(std::string operation_name,
116+
void TracingSpanImpl::startSpan(std::string_view operation_name,
116117
TimePoint<SteadyTime> current_time) {
117118
operation_name_ = operation_name;
118119
start_time_ = current_time.fetch();

source/tracing_context_impl.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#include <string_view>
18+
1719
#include "cpp2sky/config.pb.h"
1820
#include "cpp2sky/propagation.h"
1921
#include "cpp2sky/tracing_context.h"
@@ -37,7 +39,7 @@ class TracingSpanImpl : public TracingSpan {
3739
bool errorStatus() const override { return is_error_; }
3840
bool skipAnalysis() const override { return skip_analysis_; }
3941
int32_t componentId() const override { return component_id_; }
40-
const std::vector<std::pair<std::string, std::string>>& tags()
42+
const std::vector<std::pair<std::string_view, std::string_view>>& tags()
4143
const override {
4244
return tags_;
4345
}
@@ -51,10 +53,10 @@ class TracingSpanImpl : public TracingSpan {
5153
assert(!finished_);
5254
parent_span_id_ = span_id;
5355
}
54-
void startSpan(std::string operation_name) override;
55-
void startSpan(std::string operation_name,
56+
void startSpan(std::string_view operation_name) override;
57+
void startSpan(std::string_view operation_name,
5658
TimePoint<SystemTime> current_time) override;
57-
void startSpan(std::string operation_name,
59+
void startSpan(std::string_view operation_name,
5860
TimePoint<SteadyTime> current_time) override;
5961
void endSpan() override;
6062
void endSpan(TimePoint<SystemTime> current_time) override;
@@ -73,14 +75,14 @@ class TracingSpanImpl : public TracingSpan {
7375
}
7476
void setErrorStatus() override { is_error_ = true; }
7577
void setSkipAnalysis() override { skip_analysis_ = true; }
76-
void addTag(std::string key, std::string value) override {
78+
void addTag(std::string_view key, std::string_view value) override {
7779
assert(!finished_);
7880
tags_.emplace_back(key, value);
7981
}
80-
void addLog(std::string key, std::string value) override;
81-
void addLog(std::string key, std::string value,
82+
void addLog(std::string_view key, std::string_view value) override;
83+
void addLog(std::string_view key, std::string_view value,
8284
TimePoint<SystemTime> current_time) override;
83-
void addLog(std::string key, std::string value,
85+
void addLog(std::string_view key, std::string_view value,
8486
TimePoint<SteadyTime> current_time) override;
8587
void setComponentId(int32_t component_id) override;
8688

@@ -100,7 +102,7 @@ class TracingSpanImpl : public TracingSpan {
100102
// https://github.com/apache/skywalking/blob/master/docs/en/guides/Component-library-settings.md
101103
int32_t component_id_ = 9000;
102104
bool is_error_ = false;
103-
std::vector<std::pair<std::string, std::string>> tags_;
105+
std::vector<std::pair<std::string_view, std::string_view>> tags_;
104106
std::vector<skywalking::v3::Log> logs_;
105107
bool skip_analysis_ = false;
106108
bool finished_ = false;

test/tracing_context_test.cc

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <memory>
2020
#include <string>
21+
#include <string_view>
2122

2223
#include "external/skywalking_data_collect_protocol/language-agent/Tracing.pb.h"
2324
#include "mocks.h"
@@ -189,13 +190,25 @@ TEST_F(TracingContextTest, ChildSegmentContext) {
189190
span_child->setPeer("localhost:9000");
190191
span_child->addTag("category", "database");
191192

193+
std::string_view key = "method";
194+
std::string_view value = "GETxxxx";
195+
value.remove_suffix(4);
196+
span_child->addTag(key, value);
197+
192198
std::string log_key = "service_0";
193199
std::string log_value = "error";
194-
195200
auto t3 = TimePoint<SystemTime>(
196201
SystemTime(std::chrono::duration<int, std::milli>(300)));
197202
span_child->addLog(log_key, log_value, t3);
198203

204+
std::string_view log_key2 = "service_1";
205+
std::string_view log_value2 = "succeeded\x01\x03";
206+
log_value2.remove_suffix(2);
207+
208+
auto t4 = TimePoint<SystemTime>(
209+
SystemTime(std::chrono::duration<int, std::milli>(300)));
210+
span_child->addLog(log_key2, log_value2, t4);
211+
199212
span_child->endSpan(t2);
200213

201214
std::string json2 = R"EOF(
@@ -219,17 +232,32 @@ TEST_F(TracingContextTest, ChildSegmentContext) {
219232
"spanLayer": "Http",
220233
"componentId": "9000",
221234
"skipAnalysis": "false",
222-
"tags": {
223-
"key": "category",
224-
"value": "database"
225-
},
226-
"logs": {
227-
"time": "300",
228-
"data": {
229-
"key": "service_0",
230-
"value": "error"
235+
"tags": [
236+
{
237+
"key": "category",
238+
"value": "database"
239+
},
240+
{
241+
"key": "method",
242+
"value": "GET"
231243
}
232-
},
244+
],
245+
"logs": [
246+
{
247+
"time": "300",
248+
"data": {
249+
"key": "service_0",
250+
"value": "error"
251+
}
252+
},
253+
{
254+
"time": "300",
255+
"data": {
256+
"key": "service_1",
257+
"value": "succeeded"
258+
}
259+
}
260+
],
233261
"operationName": "sample1",
234262
}
235263
)EOF";

0 commit comments

Comments
 (0)