Skip to content

Commit 9e4af4e

Browse files
authored
replace config with protobuf (#35)
1 parent 6f734b2 commit 9e4af4e

27 files changed

+317
-215
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cpp2sky provides simple configuration for tracer and segment. We can set `servic
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
42-
#include <cpp2sky/config.h>
42+
#include <cpp2sky/config.pb.h>
4343

4444
int main() {
4545
using namespace cpp2sky;

cpp2sky/BUILD

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,47 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_library")
1+
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
23

3-
cc_library(
4-
name = "config_interface",
5-
hdrs = ["config.h"],
6-
visibility = ["//visibility:public"],
7-
)
8-
9-
cc_library(
10-
name = "random_interface",
11-
hdrs = ["internal/random_generator.h"],
12-
visibility = ["//visibility:public"],
13-
)
4+
licenses(["notice"]) # Apache 2
145

15-
cc_library(
16-
name = "propagation_interface",
17-
hdrs = ["propagation.h"],
18-
visibility = ["//visibility:public"],
6+
proto_library(
7+
name = "config_proto_lib",
8+
srcs = ["config.proto"],
199
)
2010

21-
cc_library(
22-
name = "segment_context_interface",
23-
hdrs = ["segment_context.h"],
24-
deps = [
25-
":config_interface",
26-
":propagation_interface",
27-
],
11+
cc_proto_library(
12+
name = "config_cc_proto",
13+
deps = [":config_proto_lib"],
2814
visibility = ["//visibility:public"],
2915
)
3016

3117
cc_library(
32-
name = "async_client_interface",
33-
hdrs = ["internal/async_client.h"],
34-
deps = [
35-
"@com_github_grpc_grpc//:grpc++",
36-
"@com_google_protobuf//:protobuf",
18+
name = "cpp2sky_interface",
19+
hdrs = [
20+
"tracer.h",
21+
"segment_context.h",
22+
"propagation.h",
23+
"well_known_names.h",
24+
"exception.h",
25+
"time.h",
3726
],
38-
visibility = ["//visibility:public"],
39-
)
40-
41-
cc_library(
42-
name = "tracer_interface",
43-
hdrs = ["tracer.h"],
4427
deps = [
4528
"@com_github_grpc_grpc//:grpc++",
4629
"@skywalking_data_collect_protocol//language-agent:tracing_protocol_cc_grpc",
30+
":config_cc_proto",
4731
],
4832
visibility = ["//visibility:public"],
4933
)
5034

5135
cc_library(
52-
name = "time_interface",
53-
hdrs = ["time.h"],
54-
visibility = ["//visibility:public"],
55-
)
56-
57-
cc_library(
58-
name = "cpp2sky_interface",
36+
name = "cpp2sky_internal_interface",
5937
hdrs = [
60-
"tracer.h",
61-
"segment_context.h",
62-
"propagation.h",
63-
"config.h",
38+
"internal/async_client.h",
39+
"internal/random_generator.h",
6440
],
6541
deps = [
6642
"@com_github_grpc_grpc//:grpc++",
6743
"@skywalking_data_collect_protocol//language-agent:tracing_protocol_cc_grpc",
44+
":config_cc_proto",
6845
],
6946
visibility = ["//visibility:public"],
7047
)

cpp2sky/config.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

cpp2sky/config.proto

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
syntax = "proto3";
2+
3+
package cpp2sky;
4+
5+
enum Protocol {
6+
GRPC = 0;
7+
REST = 1;
8+
}
9+
10+
message TracerConfig {
11+
// Tracer protocol.
12+
Protocol protocol = 1;
13+
14+
// Service name
15+
string service_name = 2;
16+
17+
// Instance name
18+
string instance_name = 3;
19+
20+
// OAP address.
21+
string address = 4;
22+
23+
// OAP token.
24+
string token = 5;
25+
26+
// The size of buffer it stores pending messages.
27+
uint32 delayed_buffer_size = 6;
28+
}
File renamed without changes.

cpp2sky/segment_context.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <memory>
1919
#include <string_view>
2020

21-
#include "cpp2sky/config.h"
21+
#include "cpp2sky/config.pb.h"
2222
#include "cpp2sky/propagation.h"
2323
#include "cpp2sky/time.h"
2424
#include "language-agent/Tracing.pb.h"
@@ -276,15 +276,32 @@ class SegmentContext {
276276
virtual SegmentObject createSegmentObject() = 0;
277277
};
278278

279-
using SegmentContextPtr = std::unique_ptr<SegmentContext>;
279+
using SegmentContextPtr = std::shared_ptr<SegmentContext>;
280280

281-
SegmentContextPtr createSegmentContext(SegmentConfig& config,
282-
SpanContextPtr span_ctx,
283-
SpanContextExtensionPtr span_ctx_ext);
281+
class SegmentContextFactory {
282+
public:
283+
virtual ~SegmentContextFactory() = default;
284+
285+
/**
286+
* Create segment context that doesn't have propagated info.
287+
*/
288+
virtual SegmentContextPtr create() = 0;
289+
290+
/**
291+
* Create segment context with propagated span context.
292+
*/
293+
virtual SegmentContextPtr create(SpanContextPtr span_context) = 0;
294+
295+
/**
296+
* Create segment context with propagated span context and extensions.
297+
*/
298+
virtual SegmentContextPtr create(
299+
SpanContextPtr span_context,
300+
SpanContextExtensionPtr ext_span_context) = 0;
301+
};
284302

285-
SegmentContextPtr createSegmentContext(SegmentConfig& config,
286-
SpanContextPtr span_ctx);
303+
using SegmentContextFactoryPtr = std::unique_ptr<SegmentContextFactory>;
287304

288-
SegmentContextPtr createSegmentContext(SegmentConfig& config);
305+
SegmentContextFactoryPtr createSegmentContextFactory(const TracerConfig& cfg);
289306

290307
} // namespace cpp2sky

cpp2sky/tracer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include <memory>
1818

19-
#include "cpp2sky/config.h"
19+
#include "cpp2sky/config.pb.h"
2020
#include "cpp2sky/segment_context.h"
2121

2222
namespace cpp2sky {

cpp2sky/well_known_names.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 <string_view>
18+
19+
namespace cpp2sky {
20+
21+
static constexpr std::string_view kPropagationHeader = "sw8";
22+
static constexpr std::string_view kPropagationExtensionHeader = "sw8-x";
23+
24+
} // namespace cpp2sky

example/sample.cc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,26 @@
2222

2323
using namespace cpp2sky;
2424

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";
25+
TracerConfig config;
2826

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();
27+
void init() {
28+
config.set_instance_name("node_0");
29+
config.set_service_name("");
30+
config.set_address("0.0.0.0:11800");
3631
}
3732

3833
int main() {
34+
init();
35+
3936
httplib::Server svr;
4037
// 1. Create tracer object to send span data to OAP.
41-
auto tracer = createInsecureGrpcTracer(tracer_config);
38+
auto tracer = createInsecureGrpcTracer(config);
39+
40+
SegmentContextFactoryPtr factory = createSegmentContextFactory(config);
4241

4342
svr.Get("/ping", [&](const httplib::Request& req, httplib::Response& res) {
4443
// 2. Create segment context
45-
auto current_segment = createSegmentContext(seg_config);
44+
auto current_segment = factory->create();
4645

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

example/sample_client.cc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@
1818
#include "cpp2sky/propagation.h"
1919
#include "cpp2sky/segment_context.h"
2020
#include "cpp2sky/tracer.h"
21+
#include "cpp2sky/well_known_names.h"
2122
#include "httplib.h"
2223

2324
using namespace cpp2sky;
2425

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";
26+
TracerConfig config;
2827

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();
28+
void init() {
29+
config.set_instance_name("client_0");
30+
config.set_service_name("");
31+
config.set_address("0.0.0.0:11800");
3632
}
3733

3834
int main() {
35+
init();
36+
37+
SegmentContextFactoryPtr factory = createSegmentContextFactory(config);
38+
3939
// 1. Create tracer object to send span data to OAP.
40-
auto tracer = createInsecureGrpcTracer(tracer_config);
40+
auto tracer = createInsecureGrpcTracer(config);
4141
// 2. Create segment context
42-
auto current_segment = createSegmentContext(seg_config);
42+
auto current_segment = factory->create();
4343

4444
// 3. Initialize span data to track root workload on current service.
4545
auto current_span = current_segment->createCurrentSegmentRootSpan();
@@ -49,8 +49,9 @@ int main() {
4949
current_span->setOperationName("/ping");
5050

5151
httplib::Client cli("remote", 8082);
52-
httplib::Headers headers = {{"sw8", current_segment->createSW8HeaderValue(
53-
current_span, "remote:8082")}};
52+
httplib::Headers headers = {
53+
{kPropagationHeader.data(),
54+
current_segment->createSW8HeaderValue(current_span, "remote:8082")}};
5455
auto res = cli.Get("/ping", headers);
5556

5657
current_span->endSpan();

0 commit comments

Comments
 (0)