Skip to content

Commit d628004

Browse files
authored
improve example (#29)
* improve example * client example * lvalue
1 parent 6c95538 commit d628004

File tree

8 files changed

+149
-2
lines changed

8 files changed

+149
-2
lines changed

cpp2sky/segment_context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ class SegmentContext {
181181
virtual std::string createSW8HeaderValue(CurrentSegmentSpanPtr parent,
182182
std::string& target_address,
183183
bool sample = true) = 0;
184+
virtual std::string createSW8HeaderValue(CurrentSegmentSpanPtr parent,
185+
std::string&& target_address,
186+
bool sample = true) = 0;
184187

185188
/**
186189
* Generate Apache SkyWalking native segment object.

example/BUILD

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ cc_binary(
55
srcs = ["sample.cc"],
66
deps = [
77
"//cpp2sky:cpp2sky_interface",
8-
"//source:cpp2sky_lib"
8+
"//source:cpp2sky_lib",
9+
"@com_github_httplib//:httplib",
10+
],
11+
)
12+
13+
cc_binary(
14+
name = "tracer_client_example",
15+
srcs = ["sample_client.cc"],
16+
deps = [
17+
"//cpp2sky:cpp2sky_interface",
18+
"//source:cpp2sky_lib",
19+
"@com_github_httplib//:httplib",
920
],
1021
)

example/sample.cc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,50 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
int main(void) { return 0; }
15+
#include <chrono>
16+
#include <string>
17+
18+
#include "cpp2sky/propagation.h"
19+
#include "cpp2sky/segment_context.h"
20+
#include "cpp2sky/tracer.h"
21+
#include "httplib.h"
22+
23+
using namespace cpp2sky;
24+
25+
static const std::string service_name = "";
26+
static const std::string instance_name = "node_0";
27+
static const std::string address = "0.0.0.0:11800";
28+
29+
SegmentConfig seg_config(service_name, instance_name);
30+
TracerConfig tracer_config(address);
31+
32+
uint64_t now() {
33+
using namespace std::chrono;
34+
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
35+
.count();
36+
}
37+
38+
int main() {
39+
httplib::Server svr;
40+
// 1. Create tracer object to send span data to OAP.
41+
auto tracer = createInsecureGrpcTracer(tracer_config);
42+
43+
svr.Get("/ping", [&](const httplib::Request& req, httplib::Response& res) {
44+
// 2. Create segment context
45+
auto current_segment = createSegmentContext(seg_config);
46+
47+
// 3. Initialize span data to track root workload on current service.
48+
auto current_span = current_segment->createCurrentSegmentRootSpan();
49+
50+
// 4. Set info
51+
current_span->setOperationName("/ping");
52+
current_span->setStartTime(now());
53+
current_span->setEndTime(now());
54+
55+
// 5. Send span data
56+
tracer->sendSegment(std::move(current_segment));
57+
});
58+
59+
svr.listen("0.0.0.0", 8081);
60+
return 0;
61+
}

example/sample_client.cc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
#include <chrono>
16+
#include <string>
17+
18+
#include "cpp2sky/propagation.h"
19+
#include "cpp2sky/segment_context.h"
20+
#include "cpp2sky/tracer.h"
21+
#include "httplib.h"
22+
23+
using namespace cpp2sky;
24+
25+
static const std::string service_name = "";
26+
static const std::string instance_name = "client_0";
27+
static const std::string address = "0.0.0.0:11800";
28+
29+
SegmentConfig seg_config(service_name, instance_name);
30+
TracerConfig tracer_config(address);
31+
32+
uint64_t now() {
33+
using namespace std::chrono;
34+
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
35+
.count();
36+
}
37+
38+
int main() {
39+
// 1. Create tracer object to send span data to OAP.
40+
auto tracer = createInsecureGrpcTracer(tracer_config);
41+
// 2. Create segment context
42+
auto current_segment = createSegmentContext(seg_config);
43+
44+
// 3. Initialize span data to track root workload on current service.
45+
auto current_span = current_segment->createCurrentSegmentRootSpan();
46+
47+
// 4. Set info
48+
current_span->setOperationName("/ping");
49+
current_span->setStartTime(now());
50+
51+
httplib::Client cli("remote", 8082);
52+
httplib::Headers headers = {{"sw8", current_segment->createSW8HeaderValue(
53+
current_span, "remote:8082")}};
54+
auto res = cli.Get("/ping", headers);
55+
56+
current_span->setEndTime(now());
57+
58+
// 5. Send span data
59+
tracer->sendSegment(std::move(current_segment));
60+
61+
return 0;
62+
}

source/grpc_async_client_impl.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ GrpcAsyncSegmentReporterStream::GrpcAsyncSegmentReporterStream(
6969
: client_(client) {}
7070

7171
GrpcAsyncSegmentReporterStream::~GrpcAsyncSegmentReporterStream() {
72+
{
73+
std::unique_lock<std::mutex> lck_(mux_);
74+
cond_.wait(lck_, [this] { return pending_messages_.empty(); });
75+
}
76+
7277
ctx_.TryCancel();
7378
request_writer_->Finish(&status_, toTag(&finish_));
7479
}
@@ -117,6 +122,12 @@ bool GrpcAsyncSegmentReporterStream::handleOperation(Operation incoming_op) {
117122
// Release pending messages which are inserted when stream is not ready
118123
// to write.
119124
clearPendingMessages();
125+
126+
// Release if lock with condition variable has been acquired.
127+
// It will blocked if stream has notified to be closed.
128+
if (pending_messages_.empty()) {
129+
cond_.notify_all();
130+
}
120131
return true;
121132
} else if (state_ == Operation::Finished) {
122133
gpr_log(GPR_INFO, "Stream closed with http status: %d",

source/grpc_async_client_impl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
#include <grpcpp/grpcpp.h>
1818

19+
#include <condition_variable>
1920
#include <memory>
21+
#include <mutex>
2022
#include <queue>
2123

2224
#include "cpp2sky/internal/async_client.h"
@@ -103,6 +105,9 @@ class GrpcAsyncSegmentReporterStream final
103105
TaggedStream connected_{Operation::Connected, this};
104106
TaggedStream write_done_{Operation::WriteDone, this};
105107
TaggedStream finish_{Operation::Finished, this};
108+
109+
std::mutex mux_;
110+
std::condition_variable cond_;
106111
};
107112

108113
class GrpcAsyncSegmentReporterStreamFactory final

source/segment_context_impl.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ std::string SegmentContextImpl::createSW8HeaderValue(
156156
return header_value;
157157
}
158158

159+
std::string SegmentContextImpl::createSW8HeaderValue(
160+
CurrentSegmentSpanPtr parent_span, std::string&& target_address,
161+
bool sample) {
162+
return createSW8HeaderValue(parent_span, target_address, sample);
163+
}
164+
159165
SegmentObject SegmentContextImpl::createSegmentObject() {
160166
SegmentObject obj;
161167
obj.set_traceid(trace_id_);

source/segment_context_impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ class SegmentContextImpl : public SegmentContext {
122122
std::string createSW8HeaderValue(CurrentSegmentSpanPtr parent_span,
123123
std::string& target_address,
124124
bool sample) override;
125+
std::string createSW8HeaderValue(CurrentSegmentSpanPtr parent,
126+
std::string&& target_address,
127+
bool sample = true) override;
125128
SegmentObject createSegmentObject() override;
126129

127130
private:

0 commit comments

Comments
 (0)