|
| 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 | +} |
0 commit comments