-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial.cpp
More file actions
78 lines (62 loc) · 2.54 KB
/
tutorial.cpp
File metadata and controls
78 lines (62 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <thread>
#include "pinpoint/tracer.h"
#include "httplib.h"
#include "http_trace_context.h"
int main() {
setenv("PINPOINT_CPP_CONFIG_FILE", "/tmp/pinpoint-config.yaml", 0);
setenv("PINPOINT_CPP_APPLICATION_NAME", "cpp-tutorial", 0);
auto agent = pinpoint::CreateAgent();
std::this_thread::sleep_for(std::chrono::seconds(5));
httplib::Server svr;
svr.Get("/foo", [&](const httplib::Request& req, httplib::Response& resp) {
HttpHeaderReader trace_context_reader(req.headers);
auto span = agent->NewSpan("C++ Web Server", "/foo", trace_context_reader);
span->SetRemoteAddress(req.remote_addr);
auto end_point = req.get_header_value("Host");
if (end_point.empty()) {
end_point = req.local_addr + ":" + std::to_string(req.local_port);
}
span->SetEndPoint(end_point);
HttpHeaderReader http_reader(req.headers);
span->RecordHeader(pinpoint::HTTP_REQUEST, http_reader);
auto host = "localhost:8090";
auto se = span->NewSpanEvent("TestSpanEvent");
se->SetServiceType(pinpoint::SERVICE_TYPE_CPP_HTTP_CLIENT);
se->SetEndPoint(host);
se->SetDestination(host);
httplib::Headers headers;
HttpHeaderReaderWriter trace_context_writer(headers);
span->InjectContext(trace_context_writer);
auto anno = se->GetAnnotations();
anno->AppendString(pinpoint::ANNOTATION_HTTP_URL, "localhost:8090/foo");
httplib::Client cli(host);
auto res = cli.Get("/foo", headers);
if (res) {
if (res->status == httplib::OK_200) {
std::cout << res->body << std::endl;
}
anno->AppendInt(pinpoint::ANNOTATION_HTTP_STATUS_CODE, res->status);
} else {
auto err = res.error();
auto err_msg = httplib::to_string(err);
std::cout << "HTTP error: " << err_msg << std::endl;
se->SetError(err_msg);
span->SetError("http client error");
}
span->EndSpanEvent();
span->NewSpanEvent("TestSpanEvent2");
auto async_span = span->NewAsyncSpan("New Thread");
async_span->NewSpanEvent("ThreadSpanEvent");
async_span->EndSpanEvent();
async_span->EndSpan();
span->EndSpanEvent();
span->SetStatusCode(200);
span->EndSpan();
resp.set_content(req.body, "text/plain");
});
svr.listen("0.0.0.0", 8080);
std::this_thread::sleep_for(std::chrono::seconds(600));
agent->Shutdown();
return 0;
}