Skip to content

Commit e56be8b

Browse files
authored
Provide SegmentContextFactory from Tracer (#56)
1 parent 7dbdfbe commit e56be8b

File tree

14 files changed

+136
-130
lines changed

14 files changed

+136
-130
lines changed

cpp2sky/segment_context.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -269,30 +269,4 @@ class SegmentContext {
269269

270270
using SegmentContextPtr = std::shared_ptr<SegmentContext>;
271271

272-
class SegmentContextFactory {
273-
public:
274-
virtual ~SegmentContextFactory() = default;
275-
276-
/**
277-
* Create segment context that doesn't have propagated info.
278-
*/
279-
virtual SegmentContextPtr create() = 0;
280-
281-
/**
282-
* Create segment context with propagated span context.
283-
*/
284-
virtual SegmentContextPtr create(SpanContextPtr span_context) = 0;
285-
286-
/**
287-
* Create segment context with propagated span context and extensions.
288-
*/
289-
virtual SegmentContextPtr create(
290-
SpanContextPtr span_context,
291-
SpanContextExtensionPtr ext_span_context) = 0;
292-
};
293-
294-
using SegmentContextFactoryPtr = std::unique_ptr<SegmentContextFactory>;
295-
296-
SegmentContextFactoryPtr createSegmentContextFactory(const TracerConfig& cfg);
297-
298272
} // namespace cpp2sky

cpp2sky/tracer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <memory>
1818

1919
#include "cpp2sky/config.pb.h"
20+
#include "cpp2sky/propagation.h"
2021
#include "cpp2sky/segment_context.h"
2122

2223
namespace cpp2sky {
@@ -25,6 +26,12 @@ class Tracer {
2526
public:
2627
virtual ~Tracer() = default;
2728

29+
/**
30+
* Start new segment. It will be called per request, for example.
31+
*/
32+
virtual SegmentContextPtr newSegment() = 0;
33+
virtual SegmentContextPtr newSegment(SpanContextPtr span) = 0;
34+
2835
/**
2936
* Send SegmentContext to the collector.
3037
*/

example/sample.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ int main() {
3737
// 1. Create tracer object to send span data to OAP.
3838
auto tracer = createInsecureGrpcTracer(config);
3939

40-
SegmentContextFactoryPtr factory = createSegmentContextFactory(config);
41-
4240
svr.Get("/ping", [&](const httplib::Request& req, httplib::Response& res) {
4341
// 2. Create segment context
44-
auto current_segment = factory->create();
42+
auto current_segment = tracer->newSegment();
4543

4644
// 3. Initialize span data to track root workload on current service.
4745
auto current_span = current_segment->createCurrentSegmentRootSpan();

example/sample_client.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ void init() {
3434
int main() {
3535
init();
3636

37-
SegmentContextFactoryPtr factory = createSegmentContextFactory(config);
38-
3937
// 1. Create tracer object to send span data to OAP.
4038
auto tracer = createInsecureGrpcTracer(config);
39+
4140
// 2. Create segment context
42-
auto current_segment = factory->create();
41+
auto current_segment = tracer->newSegment();
4342

4443
// 3. Initialize span data to track root workload on current service.
4544
auto current_span = current_segment->createCurrentSegmentRootSpan();

source/segment_context_impl.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,20 @@ bool SegmentContextImpl::readyToSend() {
260260
return true;
261261
}
262262

263-
SegmentContextFactoryImpl::SegmentContextFactoryImpl(const TracerConfig& cfg)
263+
SegmentContextFactory::SegmentContextFactory(const TracerConfig& cfg)
264264
: service_name_(cfg.service_name()), instance_name_(cfg.instance_name()) {}
265265

266-
SegmentContextPtr SegmentContextFactoryImpl::create() {
266+
SegmentContextPtr SegmentContextFactory::create() {
267267
return std::make_unique<SegmentContextImpl>(service_name_, instance_name_,
268268
random_generator_);
269269
}
270270

271-
SegmentContextPtr SegmentContextFactoryImpl::create(
272-
SpanContextPtr span_context) {
271+
SegmentContextPtr SegmentContextFactory::create(SpanContextPtr span_context) {
273272
return std::make_unique<SegmentContextImpl>(service_name_, instance_name_,
274273
span_context, random_generator_);
275274
}
276275

277-
SegmentContextPtr SegmentContextFactoryImpl::create(
276+
SegmentContextPtr SegmentContextFactory::create(
278277
SpanContextPtr span_context, SpanContextExtensionPtr ext_span_context) {
279278
auto context = std::make_unique<SegmentContextImpl>(
280279
service_name_, instance_name_, span_context, ext_span_context,

source/segment_context_impl.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,19 @@ class SegmentContextImpl : public SegmentContext {
179179
bool should_skip_analysis_ = false;
180180
};
181181

182-
class SegmentContextFactoryImpl : public SegmentContextFactory {
182+
class SegmentContextFactory {
183183
public:
184-
SegmentContextFactoryImpl(const TracerConfig& cfg);
184+
SegmentContextFactory(const TracerConfig& cfg);
185185

186-
// SegmentContextFactory
187-
SegmentContextPtr create() override;
188-
SegmentContextPtr create(SpanContextPtr span_context) override;
186+
SegmentContextPtr create();
187+
SegmentContextPtr create(SpanContextPtr span_context);
189188
SegmentContextPtr create(SpanContextPtr span_context,
190-
SpanContextExtensionPtr ext_span_context) override;
189+
SpanContextExtensionPtr ext_span_context);
191190

192191
private:
193192
std::string service_name_;
194193
std::string instance_name_;
195194
RandomGeneratorImpl random_generator_;
196195
};
197196

198-
SegmentContextFactoryPtr createSegmentContextFactory(const TracerConfig& cfg) {
199-
return std::make_unique<SegmentContextFactoryImpl>(cfg);
200-
}
201-
202197
} // namespace cpp2sky

source/tracer_impl.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace cpp2sky {
2121
TracerImpl::TracerImpl(const TracerConfig& config,
2222
std::shared_ptr<grpc::ChannelCredentials> cred,
2323
GrpcAsyncSegmentReporterStreamFactory& factory)
24-
: th_([this] { this->run(); }) {
24+
: th_([this] { this->run(); }), segment_factory_(config) {
2525
if (config.protocol() == Protocol::GRPC) {
2626
client_ = std::make_unique<GrpcAsyncSegmentReporterClient>(
2727
config.address(), config.token(), &cq_, factory, cred);
@@ -36,6 +36,12 @@ TracerImpl::~TracerImpl() {
3636
th_.join();
3737
}
3838

39+
SegmentContextPtr TracerImpl::newSegment() { return segment_factory_.create(); }
40+
41+
SegmentContextPtr TracerImpl::newSegment(SpanContextPtr span) {
42+
return segment_factory_.create(span);
43+
}
44+
3945
void TracerImpl::sendSegment(SegmentContextPtr obj) {
4046
if (!obj || !obj->readyToSend()) {
4147
return;

source/tracer_impl.h

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

1919
#include "cpp2sky/tracer.h"
2020
#include "source/grpc_async_client_impl.h"
21+
#include "source/segment_context_impl.h"
2122

2223
namespace cpp2sky {
2324

@@ -31,6 +32,9 @@ class TracerImpl : public Tracer {
3132
GrpcAsyncSegmentReporterStreamFactory& factory);
3233
~TracerImpl();
3334

35+
SegmentContextPtr newSegment() override;
36+
SegmentContextPtr newSegment(SpanContextPtr span) override;
37+
3438
void sendSegment(SegmentContextPtr obj) override;
3539

3640
private:
@@ -39,6 +43,7 @@ class TracerImpl : public Tracer {
3943
AsyncClientPtr<TracerRequestType, TracerResponseType> client_;
4044
grpc::CompletionQueue cq_;
4145
std::thread th_;
46+
SegmentContextFactory segment_factory_;
4247
};
4348

4449
static GrpcAsyncSegmentReporterStreamFactory stream_factory;

source/utils/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ cc_library(
77
"random_generator.h",
88
"circular_buffer.h",
99
],
10+
srcs = [
11+
"random_generator.cc",
12+
],
1013
deps = [
1114
"//cpp2sky/internal:random_generator_interface",
1215
],

source/utils/random_generator.cc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
// From
16+
// https://github.com/envoyproxy/envoy/blob/master/source/common/common/random_generator.{h,cc}
17+
18+
#include "source/utils/random_generator.h"
19+
20+
namespace cpp2sky {
21+
22+
std::string RandomGeneratorImpl::uuid() {
23+
static thread_local char buffered[2048];
24+
static thread_local size_t buffered_idx = sizeof(buffered);
25+
26+
if (buffered_idx + 16 > sizeof(buffered)) {
27+
// TODO(shikugawa): Self-implemented random number generator is used right
28+
// now. RAND_bytes on OpenSSL is used on Envoy's RandomGenerator.
29+
randomBuffer(buffered, sizeof(buffered));
30+
buffered_idx = 0;
31+
}
32+
33+
// Consume 16 bytes from the buffer.
34+
assert(buffered_idx + 16 <= sizeof(buffered));
35+
char* rand = &buffered[buffered_idx];
36+
buffered_idx += 16;
37+
38+
// Create UUID from Truly Random or Pseudo-Random Numbers.
39+
// See: https://tools.ietf.org/html/rfc4122#section-4.4
40+
rand[6] = (rand[6] & 0x0f) | 0x40; // UUID version 4 (random)
41+
rand[8] = (rand[8] & 0x3f) | 0x80; // UUID variant 1 (RFC4122)
42+
43+
// Convert UUID to a string representation, e.g.
44+
// a121e9e1-feae-4136-9e0e-6fac343d56c9.
45+
static const char* const hex = "0123456789abcdef";
46+
char uuid[UUID_LENGTH];
47+
48+
for (uint8_t i = 0; i < 4; i++) {
49+
const uint8_t d = rand[i];
50+
uuid[2 * i] = hex[d >> 4];
51+
uuid[2 * i + 1] = hex[d & 0x0f];
52+
}
53+
54+
uuid[8] = '-';
55+
56+
for (uint8_t i = 4; i < 6; i++) {
57+
const uint8_t d = rand[i];
58+
uuid[2 * i + 1] = hex[d >> 4];
59+
uuid[2 * i + 2] = hex[d & 0x0f];
60+
}
61+
62+
uuid[13] = '-';
63+
64+
for (uint8_t i = 6; i < 8; i++) {
65+
const uint8_t d = rand[i];
66+
uuid[2 * i + 2] = hex[d >> 4];
67+
uuid[2 * i + 3] = hex[d & 0x0f];
68+
}
69+
70+
uuid[18] = '-';
71+
72+
for (uint8_t i = 8; i < 10; i++) {
73+
const uint8_t d = rand[i];
74+
uuid[2 * i + 3] = hex[d >> 4];
75+
uuid[2 * i + 4] = hex[d & 0x0f];
76+
}
77+
78+
uuid[23] = '-';
79+
80+
for (uint8_t i = 10; i < 16; i++) {
81+
const uint8_t d = rand[i];
82+
uuid[2 * i + 4] = hex[d >> 4];
83+
uuid[2 * i + 5] = hex[d & 0x0f];
84+
}
85+
86+
return std::string(uuid, UUID_LENGTH);
87+
}
88+
89+
void RandomGeneratorImpl::randomBuffer(char* ch, size_t len) {
90+
std::random_device engine;
91+
std::uniform_int_distribution<std::size_t> dist(0, CHARS.size() - 1);
92+
for (auto i = 0; i < len; ++i) {
93+
ch[i] = CHARS[dist(engine)];
94+
}
95+
}
96+
97+
} // namespace cpp2sky

0 commit comments

Comments
 (0)