Skip to content

Commit cbab740

Browse files
authored
rename SegmentContext -> TracingContext (#58)
1 parent 00e1b5a commit cbab740

File tree

15 files changed

+162
-174
lines changed

15 files changed

+162
-174
lines changed

README.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cc_binary(
3535

3636
#### Config
3737

38-
cpp2sky provides simple configuration for tracer and segment. We can set `service name`, `instance name` to `SegmentConfig` and `token`. `address` to `TracerConfig`.
38+
cpp2sky provides simple configuration for tracer.
3939
The detail information is described in [official protobuf definition](https://github.com/apache/skywalking-data-collect-protocol/blob/master/language-agent/Tracing.proto#L57-L67).
4040

4141
```cpp
@@ -49,8 +49,12 @@ int main() {
4949
static const std::string oap_addr = "oap:12800";
5050
static const std::string token = "token";
5151

52-
SegmentConfig seg_config(service_name, instance_name);
53-
TracerConfig tracer_config(oap_addr, token);
52+
TracerConfig tracer_config;
53+
54+
config.set_instance_name(instance_name);
55+
config.set_service_name(service_name);
56+
config.set_address(oap_addr);
57+
config.set_token(token);
5458
}
5559
```
5660

@@ -74,37 +78,29 @@ Then, you can create propagated span object by decoding these items.
7478
SpanContextPtr parent_span = createSpanContext(parent);
7579
```
7680

77-
#### Create workload segment
78-
79-
Create segment for current workload.
80-
81-
```cpp
82-
SegmentConfig seg_config(service_name, instance_name);
83-
SegmentContextPtr current_segment = createSegmentContext(seg_config);
84-
```
85-
8681
#### Create span
8782

88-
First, you must create root span to trace current workload.
83+
First, you must create tracing context that holds all spans, then crete initial entry span.
8984

9085
```cpp
91-
CurrentSegmentSpanPtr current_span = current_segment->createEntrySpan();
86+
TracingContextPtr tracing_context = tracer->newContext();
87+
TracingSpanPtr tracing_span = tracing_context->createEntrySpan();
9288
```
9389

9490
After that, you can create another span to trace another workload, such as RPC to other services.
9591
Note that you must have parent span to create secondary span. It will construct parent-child relation when analysis.
9692

9793
```cpp
98-
CurrentSegmentSpanPtr current_span = current_segment->createExitSpan(current_span);
94+
TracingSpanPtr current_span = tracing_context->createExitSpan(current_span);
9995
```
10096
10197
Alternative approach is RAII based one. It is used like below,
10298
10399
```cpp
104100
{
105-
StartEntrySpan entry_span(current_segment, "sample_op1");
101+
StartEntrySpan entry_span(tracing_context, "sample_op1");
106102
{
107-
StartExitSpan exit_span(current_segment, entry_span.get(), "sample_op2");
103+
StartExitSpan exit_span(tracing_context, entry_span.get(), "sample_op2");
108104
109105
// something...
110106
}
@@ -113,17 +109,17 @@ Alternative approach is RAII based one. It is used like below,
113109

114110
#### Send segment to OAP
115111

116-
Note that SegmentContextPtr is unique pointer. So when you'd like to send data, you must move it and don't refer after sending,
112+
Note that TracingContext is unique pointer. So when you'd like to send data, you must move it and don't refer after sending,
117113
to avoid undefined behavior.
118114

119115
```cpp
120-
SegmentContextPtr current_segment = createSegmentContext(config);
121-
CurrentSegmentSpanPtr current_span = current_segment->createEntrySpan();
116+
TracingContextPtr tracing_context = tracer->newContext();
117+
TracingSpanPtr tracing_span = tracing_context->createEntrySpan();
122118

123-
current_span->startSpan("sample_workload");
124-
current_span->endSpan();
119+
tracing_span->startSpan("sample_workload");
120+
tracing_span->endSpan();
125121

126-
tracer->sendSegment(std::move(current_segment));
122+
tracer->report(std::move(tracing_context));
127123
```
128124
129125
## Security

cpp2sky/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cc_library(
1818
name = "cpp2sky_interface",
1919
hdrs = [
2020
"tracer.h",
21-
"segment_context.h",
21+
"tracing_context.h",
2222
"propagation.h",
2323
"well_known_names.h",
2424
"exception.h",

cpp2sky/tracer.h

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

1919
#include "cpp2sky/config.pb.h"
2020
#include "cpp2sky/propagation.h"
21-
#include "cpp2sky/segment_context.h"
21+
#include "cpp2sky/tracing_context.h"
2222

2323
namespace cpp2sky {
2424

@@ -29,13 +29,13 @@ class Tracer {
2929
/**
3030
* Start new segment. It will be called per request, for example.
3131
*/
32-
virtual SegmentContextPtr newSegment() = 0;
33-
virtual SegmentContextPtr newSegment(SpanContextPtr span) = 0;
32+
virtual TracingContextPtr newContext() = 0;
33+
virtual TracingContextPtr newContext(SpanContextPtr span) = 0;
3434

3535
/**
3636
* Send SegmentContext to the collector.
3737
*/
38-
virtual void sendSegment(SegmentContextPtr obj) = 0;
38+
virtual void report(TracingContextPtr obj) = 0;
3939
};
4040

4141
using TracerPtr = std::unique_ptr<Tracer>;

cpp2sky/segment_context.h renamed to cpp2sky/tracing_context.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
namespace cpp2sky {
2828

29-
class CurrentSegmentSpan {
29+
class TracingSpan {
3030
public:
31-
virtual ~CurrentSegmentSpan() = default;
31+
virtual ~TracingSpan() = default;
3232

3333
/**
3434
* Generate Apache SkyWalking native span object from current segment span.
@@ -180,11 +180,11 @@ class CurrentSegmentSpan {
180180
virtual bool finished() const = 0;
181181
};
182182

183-
using CurrentSegmentSpanPtr = std::shared_ptr<CurrentSegmentSpan>;
183+
using TracingSpanPtr = std::shared_ptr<TracingSpan>;
184184

185-
class SegmentContext {
185+
class TracingContext {
186186
public:
187-
virtual ~SegmentContext() = default;
187+
virtual ~TracingContext() = default;
188188

189189
/**
190190
* Get trace ID. This value must be unique globally.
@@ -209,7 +209,7 @@ class SegmentContext {
209209
/**
210210
* Get spans generated by this segment context.
211211
*/
212-
virtual const std::list<CurrentSegmentSpanPtr>& spans() const = 0;
212+
virtual const std::list<TracingSpanPtr>& spans() const = 0;
213213

214214
/**
215215
* Get span context which generated this segment context as parent.
@@ -225,13 +225,12 @@ class SegmentContext {
225225
* Generate a segment span related with this segment context.
226226
* @param parent_span Parent span which is extracted from caller.
227227
*/
228-
virtual CurrentSegmentSpanPtr createExitSpan(
229-
CurrentSegmentSpanPtr parent_span) = 0;
228+
virtual TracingSpanPtr createExitSpan(TracingSpanPtr parent_span) = 0;
230229

231230
/**
232231
* Generate root segment span, called once per workload.
233232
*/
234-
virtual CurrentSegmentSpanPtr createEntrySpan() = 0;
233+
virtual TracingSpanPtr createEntrySpan() = 0;
235234

236235
/**
237236
* Generate sw8 value to send SegmentRef.
@@ -240,7 +239,7 @@ class SegmentContext {
240239
* https://github.com/apache/skywalking-data-collect-protocol/blob/master/language-agent/Tracing.proto#L97-L101
241240
*/
242241
virtual std::optional<std::string> createSW8HeaderValue(
243-
CurrentSegmentSpanPtr parent, const std::string_view target_address) = 0;
242+
TracingSpanPtr parent, const std::string_view target_address) = 0;
244243
// If you don't specify parent span, stored to current segment, it will be
245244
// selected newest span as parent span.
246245
virtual std::optional<std::string> createSW8HeaderValue(
@@ -267,48 +266,47 @@ class SegmentContext {
267266
virtual bool readyToSend() = 0;
268267
};
269268

270-
using SegmentContextPtr = std::shared_ptr<SegmentContext>;
269+
using TracingContextPtr = std::shared_ptr<TracingContext>;
271270
/**
272271
* RAII based span creation. It acquired then create new span with required
273272
* properties. The span wiil be closed and set end time when called destructor.
274273
*/
275274
class StartEntrySpan {
276275
public:
277-
StartEntrySpan(SegmentContextPtr segment_context,
276+
StartEntrySpan(TracingContextPtr tracing_context,
278277
std::string_view operation_name)
279-
: span_(segment_context->createEntrySpan()) {
278+
: span_(tracing_context->createEntrySpan()) {
280279
span_->startSpan(operation_name.data());
281280
}
282281

283282
~StartEntrySpan() {
284-
// Span won't be released because the entity is holded by SegmentContext.
283+
// Span won't be released because the entity is holded by TracingContext.
285284
span_->endSpan();
286285
}
287286

288-
CurrentSegmentSpanPtr get() { return span_; }
287+
TracingSpanPtr get() { return span_; }
289288

290289
private:
291-
CurrentSegmentSpanPtr span_;
290+
TracingSpanPtr span_;
292291
};
293292

294293
class StartExitSpan {
295294
public:
296-
StartExitSpan(SegmentContextPtr segment_context,
297-
CurrentSegmentSpanPtr parent_span,
295+
StartExitSpan(TracingContextPtr tracing_context, TracingSpanPtr parent_span,
298296
std::string_view operation_name)
299-
: span_(segment_context->createExitSpan(parent_span)) {
297+
: span_(tracing_context->createExitSpan(parent_span)) {
300298
span_->startSpan(operation_name.data());
301299
}
302300

303301
~StartExitSpan() {
304-
// Span won't be released because the entity is holded by SegmentContext.
302+
// Span won't be released because the entity is holded by TracingContext.
305303
span_->endSpan();
306304
}
307305

308-
CurrentSegmentSpanPtr get() { return span_; }
306+
TracingSpanPtr get() { return span_; }
309307

310308
private:
311-
CurrentSegmentSpanPtr span_;
309+
TracingSpanPtr span_;
312310
};
313311

314312
} // namespace cpp2sky

example/sample.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include <string>
1717

1818
#include "cpp2sky/propagation.h"
19-
#include "cpp2sky/segment_context.h"
2019
#include "cpp2sky/tracer.h"
20+
#include "cpp2sky/tracing_context.h"
2121
#include "cpp2sky/well_known_names.h"
2222
#include "httplib.h"
2323

@@ -41,24 +41,24 @@ int main() {
4141
svr.Get("/ping", [&](const httplib::Request& req, httplib::Response& res) {
4242
std::string context = req.get_header_value(kPropagationHeader.data());
4343

44-
SegmentContextPtr segment_context;
44+
TracingContextPtr tracing_context;
4545
if (!context.empty()) {
46-
// 2. Create segment context with propagated information.
47-
segment_context = tracer->newSegment(createSpanContext(context));
46+
// 2. Create tracing context with propagated information.
47+
tracing_context = tracer->newContext(createSpanContext(context));
4848
}
4949

5050
{
5151
// 3. Create entry span.
52-
StartEntrySpan current_span(segment_context, "sample_op3");
52+
StartEntrySpan current_span(tracing_context, "sample_op3");
5353

5454
/**
5555
* something....
5656
*/
5757
}
5858

5959
// 4. Send span data
60-
if (segment_context != nullptr) {
61-
tracer->sendSegment(std::move(segment_context));
60+
if (tracing_context != nullptr) {
61+
tracer->report(std::move(tracing_context));
6262
}
6363
});
6464

example/sample_client.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include <string>
1717

1818
#include "cpp2sky/propagation.h"
19-
#include "cpp2sky/segment_context.h"
2019
#include "cpp2sky/tracer.h"
20+
#include "cpp2sky/tracing_context.h"
2121
#include "cpp2sky/well_known_names.h"
2222
#include "httplib.h"
2323

@@ -37,8 +37,8 @@ int main() {
3737
// 1. Create tracer object to send span data to OAP.
3838
auto tracer = createInsecureGrpcTracer(config);
3939

40-
// 2. Create segment context
41-
auto current_segment = tracer->newSegment();
40+
// 2. Create tracing context
41+
auto tracing_context = tracer->newContext();
4242

4343
/**
4444
* 3. Create entry span it traces RPC call.
@@ -47,16 +47,16 @@ int main() {
4747
*
4848
* example:
4949
*
50-
* auto current_span = current_segment->createEntrySpan();
50+
* auto current_span = tracing_context->createEntrySpan();
5151
* current_span->startSpan("sample_op1");
5252
*
53-
* auto current_span2 = current_segment->createExitSpan();
53+
* auto current_span2 = tracing_context->createExitSpan();
5454
* current_span2->startSpan("sample_op2");
5555
*
5656
* httplib::Client cli("remote", 8082);
5757
* httplib::Headers headers = {
5858
* {kPropagationHeader.data(),
59-
* current_segment->createSW8HeaderValue(current_span, "remote:8082")}};
59+
* tracing_context->createSW8HeaderValue(current_span, "remote:8082")}};
6060
*
6161
* auto res = cli.Get("/ping", headers);
6262
*
@@ -65,22 +65,22 @@ int main() {
6565
*
6666
*/
6767
{
68-
StartEntrySpan entry_span(current_segment, "sample_op1");
68+
StartEntrySpan entry_span(tracing_context, "sample_op1");
6969

7070
{
7171
std::string target_address = "remote:8082";
72-
StartExitSpan exit_span(current_segment, entry_span.get(), "sample_op2");
72+
StartExitSpan exit_span(tracing_context, entry_span.get(), "sample_op2");
7373
exit_span.get()->setPeer(target_address);
7474

7575
httplib::Client cli("remote", 8082);
7676
httplib::Headers headers = {
77-
{kPropagationHeader.data(), *current_segment->createSW8HeaderValue(
77+
{kPropagationHeader.data(), *tracing_context->createSW8HeaderValue(
7878
exit_span.get(), target_address)}};
7979

8080
auto res = cli.Get("/ping", headers);
8181
}
8282
}
8383

84-
tracer->sendSegment(std::move(current_segment));
84+
tracer->report(std::move(tracing_context));
8585
return 0;
8686
}

source/BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ cc_library(
44
name = "cpp2sky_lib",
55
hdrs = [
66
"propagation_impl.h",
7-
"segment_context_impl.h",
7+
"tracing_context_impl.h",
88
"grpc_async_client_impl.h",
99
"tracer_impl.h",
1010
],
1111
srcs = [
1212
"propagation_impl.cc",
13-
"segment_context_impl.cc",
13+
"tracing_context_impl.cc",
1414
"grpc_async_client_impl.cc",
1515
"tracer_impl.cc",
1616
],
@@ -29,11 +29,11 @@ cc_library(
2929
name = "cpp2sky_data_lib",
3030
hdrs =[
3131
"propagation_impl.h",
32-
"segment_context_impl.h",
32+
"tracing_context_impl.h",
3333
],
3434
srcs = [
3535
"propagation_impl.cc",
36-
"segment_context_impl.cc",
36+
"tracing_context_impl.cc",
3737
],
3838
deps = [
3939
"@skywalking_data_collect_protocol//language-agent:tracing_protocol_cc_proto",

0 commit comments

Comments
 (0)