C++ opentracing zipkin
Useful page : https://github.com/openzipkin/b3-propagation & other official websites
Steps to run attached scripts
- Download opentracing: https://github.com/opentracing/opentracing-cpp
- Download and install OpenTracing implementation for Zipkin : https://github.com/rnburn/zipkin-cpp-opentracing
- Download ZipKin backend server :curl -sSL https://zipkin.io/quickstart.sh | bash -s
- Run opentracingtest/build.sh
- Start zipkin backend: java -jar zipkin.jar
- Start ./server
- Start ./client
- Go to http://localhsot:9411
// text_map_carrier.h, copy from example folder
#ifndef LIGHTSTEP_TEXT_MAP_CARRIER
#define LIGHTSTEP_TEXT_MAP_CARRIER #include <opentracing/propagation.h>
#include <string>
#include <unordered_map> using opentracing::TextMapReader;
using opentracing::TextMapWriter;
using opentracing::expected;
using opentracing::string_view; // class textMapCarrier
class TextMapCarrier : public TextMapReader, public TextMapWriter {
public:
TextMapCarrier(std::unordered_map<std::string, std::string> &text_map)
: text_map_(text_map) {} expected<void> Set(string_view key, string_view value) const override {
text_map_[key] = value;
return {};
} expected<void> ForeachKey(
std::function<expected<void>(string_view key, string_view value)> f)
const override {
for (const auto &key_value : text_map_) {
auto result = f(key_value.first, key_value.second);
if (!result)
return result;
}
return {};
} std::unordered_map<std::string, std::string> &text_map_;
}; // span context reader/writer helper std::string read_span_context(std::unordered_map<std::string, std::string> m)
{
std::string output = ""; for (auto it = m.cbegin(); it != m.cend(); it++)
{
output += (it->first) + ":" + it->second + ",";
} return output.substr(, output.size() - );
} void write_span_context(std::unordered_map<std::string, std::string> &m, char *context)
{
char * keypair = strtok(context, ",");
while(keypair != NULL)
{
std::string s(keypair);
std::string::size_type found = s.find_first_of(':');
m.insert(std::pair<std::string, std::string>(s.substr(,found),s.substr(found+)));
keypair =strtok(NULL, ",");
}
} #endif // LIGHTSTEP_TEXT_MAP_CARRIER
// client.cpp, copy from example folder
#include "text_map_carrier.h"
#include <unistd.h>
#include <sys/types.h> /* basic system data types */
#include <sys/socket.h> /* basic socket definitions */
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h> /* inet(3) functions */
#include <netdb.h> /* gethostbyname function */
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h> #include <cassert>
#include <iostream>
#include <unordered_map>
#include <zipkin/opentracing.h>
using namespace zipkin;
using namespace opentracing; #define MAXLINE 1024 int main()
{
int socket_fd;
char buf[MAXLINE];
char buf2[MAXLINE];
struct sockaddr_in serv_addr ; socket_fd = socket(PF_INET, SOCK_STREAM, );
bzero( &serv_addr, sizeof(serv_addr) );
serv_addr.sin_family = AF_INET ;
serv_addr.sin_port = htons();
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr); ZipkinOtTracerOptions options;
options.service_name = "client_app";
auto tracer = makeZipkinOtTracer(options);
assert(tracer); auto parent_span = tracer->StartSpan("startMain"); {
auto child_span =
tracer->StartSpan("Step1", {ChildOf(&parent_span->context())});
assert(child_span); // Set a simple tag.
child_span->SetTag("simple tag", ); // Set a complex tag.
child_span->SetTag("complex tag",
Values{, Dictionary{{"abc", }, {"xyz", 4.0}}}); // Log simple values.
child_span->Log({{"event", "simple log"}, {"abc", }}); // Log complex values.
child_span->Log({{"event", "complex log"},
{"data", Dictionary{{"a", }, {"b", Values{, }}}}}); sleep();
child_span->Finish();
} // Create a follows from span.
{
auto child_span =
tracer->StartSpan("Step1_1", {FollowsFrom(&parent_span->context())});
sleep(); // child_span's destructor will finish the span if not done so explicitly.
} // Use custom timestamps.
{
auto t1 = SystemClock::now();
sleep();
auto t2 = SteadyClock::now();
auto span = tracer->StartSpan(
"Step3",
{ChildOf(&parent_span->context()), StartTimestamp(t1)});
assert(span);
span->Finish({FinishTimestamp(t2)});
} if(connect(socket_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == )
{
printf("client starts ...\n"); while()
{
static int n = ; if( n>=) break; char span_name[];
sprintf(span_name, "request_%d", n++); auto child_span = tracer->StartSpan(span_name, {ChildOf(&parent_span->context())});
std::unordered_map<std::string, std::string> text_map;
TextMapCarrier carrier(text_map);
auto err = tracer->Inject(child_span->context(), carrier);
std::string contxt = read_span_context(text_map); // for testing
if(n == ) sleep(); printf("send message : %d\n", n);
sprintf(buf, "%s\r\n%d",contxt.c_str(), n);
if(write(socket_fd, buf, MAXLINE) == -)
printf("write error\n"); sleep(); // wait for response
while(read(socket_fd, buf2, MAXLINE) == -) { sleep();}
printf("get message from server: %s\n", buf2);
child_span->Finish();
}
} parent_span->Finish();
tracer->Close();
return ;
}
// server.cpp
#include "text_map_carrier.h"
#include <unistd.h>
#include <sys/types.h> /* basic system data types */
#include <sys/socket.h> /* basic socket definitions */
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h> /* inet(3) functions */
#include <netdb.h> /* gethostbyname function */
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h> #include <cassert>
#include <iostream>
#include <unordered_map>
#include <zipkin/opentracing.h>
using namespace zipkin;
using namespace opentracing; #define MAXLINE 1024
#define LISTENQ 1024 int main(int argc, char **argv)
{
int listen_fd, connect_fd;
char buf[MAXLINE];
char buf2[MAXLINE];
socklen_t len;
struct sockaddr_in serv_addr, client_addr; ZipkinOtTracerOptions options;
options.service_name = "server_app";
auto tracer = makeZipkinOtTracer(options); listen_fd = socket(PF_INET, SOCK_STREAM, );
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons();
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) ;
bind(listen_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr_in)); printf("start listening ..\n");
listen(listen_fd, LISTENQ);
for( ; ; )
{
connect_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &len);
if(connect_fd < ) continue; printf("accept from %s : %d \n", inet_ntoa(client_addr.sin_addr), client_addr.sin_port);
while()
{
if (read(connect_fd, buf, MAXLINE) > -)
{
char contxt[MAXLINE];
int data;
sscanf(buf,"%s\r\n%d",contxt,&data);
std::unordered_map<std::string, std::string> text_map;
write_span_context(std::ref(text_map),&contxt[]);
TextMapCarrier carrier(text_map);
auto span_context_maybe = tracer->Extract(carrier); if(!span_context_maybe)
printf("error context............\n"); auto span = tracer->StartSpan("receive", {ChildOf(span_context_maybe->get())});
printf("receive message : %d\n", data); // for testing
if (data == ) sleep(); sprintf(buf2, "back %d", data);
sleep();
if(write(connect_fd, buf2, MAXLINE) == -)
printf("write error");
span->Finish();
}
sleep();
}
close(connect_fd);
sleep();
} tracer->Close();
}
# build.sh
g++ -g -O0 client.cpp -o client -lcurl -lopentracing -lzipkin -lzipkin_opentracing -pthread -std=c++
g++ -g -O0 server.cpp -o server -lcurl -lopentracing -lzipkin -lzipkin_opentracing -pthread -std=c++
C++ opentracing zipkin的更多相关文章
- 带入gRPC:分布式链路追踪 gRPC + Opentracing + Zipkin
在实际应用中,你做了那么多 Server 端,写了 N 个 RPC 方法.想看看方法的指标,却无处下手? 本文将通过 gRPC + Opentracing + Zipkin 搭建一个分布式链路追踪系统 ...
- 【Springboot】实例讲解Springboot整合OpenTracing分布式链路追踪系统(Jaeger和Zipkin)
1 分布式追踪系统 随着大量公司把单体应用重构为微服务,对于运维人员的责任就更加重大了.架构更复杂.应用更多,要从中快速诊断出问题.找到性能瓶颈,并不是一件容易的事.因此,也随着诞生了一系列面向Dev ...
- python 微服务方案
介绍 使用python做web开发面临的一个最大的问题就是性能,在解决C10K问题上显的有点吃力.有些异步框架Tornado.Twisted.Gevent 等就是为了解决性能问题.这些框架在性能上有些 ...
- Spring Cloud 微服务分布式链路跟踪 Sleuth 与 Zipkin
Zipkin 是一个开放源代码分布式的跟踪系统,由 Twitter 公司开源,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集.存储.查找和展现.它的理论模型来自于Google ...
- OpenTracing:开放式分布式追踪规范
前言 想实现一个简单的追踪系统似乎是容易的,需要必要的调用链id,时间戳等:想实现一款易用不侵入代码的追踪系统就很麻烦了,需要接触CLR和IL相关知识:即使你费劲心力做出了那些,如果性能不够好,也没有 ...
- Laravel + go-micro + grpc 实践基于 Zipkin 的分布式链路追踪系统 摘自https://mp.weixin.qq.com/s/JkLMNabnYbod-b4syMB3Hw?
分布式调用链跟踪系统,属于监控系统的一类.系统架构逐步演进时,后期形态往往是一个平台由很多不同的服务.组件构成,用户请求过来后,可能会经过其中多个服务,如图 不过,出问题时往往很难排查,如整个请求变慢 ...
- TarsGo新版本发布,支持protobuf,zipkin和自定义插件
本文作者:陈明杰(sandyskies) Tars是腾讯从2008年到今天一直在使用的后台逻辑层的统一应用框架,目前支持C++,Java,PHP,Nodejs,Golang语言.该框架为用户提供了涉及 ...
- Opentracing + Uber Jaeger 全链路灰度调用链,Nepxion Discovery
当网关和服务在实施全链路分布式灰度发布和路由时候,我们需要一款追踪系统来监控网关和服务走的是哪个灰度组,哪个灰度版本,哪个灰度区域,甚至监控从Http Header头部全程传递的灰度规则和路由策略.这 ...
- 基于Opentracing+Jaeger全链路灰度调用链
当网关和服务在实施全链路分布式灰度发布和路由时候,我们需要一款追踪系统来监控网关和服务走的是哪个灰度组,哪个灰度版本,哪个灰度区域,甚至监控从Http Header头部全程传递的灰度规则和路由策略.这 ...
随机推荐
- SpringMVC 实现文件上传与下载,并配置异常页面
目录 上传文件的表单要求 Spring MVC实现上传文件 需要导入的jar包 配置MultipartResolver解析器 编写接收上传文件的控制器 Spring MVC实现文件下载 下载文件时的h ...
- [安全转帖]浅析安全威胁情报共享框架OpenIOC
浅析安全威胁情报共享框架OpenIOC https://www.freebuf.com/sectool/86580.html Indicator of compromise Outline: 1. I ...
- java循环1
public class f_w { public static void main(String []args) { int a=0; System.out.print("_info__w ...
- 通过VuePress管理项目文档(一)
VuePress 相关链接 完整的Vue组件代码以及完整的文档,仅适用于个人参考学习: 文档预览地址:预览链接 使用VuePress编辑文档的代码访问:组件文档 完整代码:组件代码 Vue组件开发 这 ...
- python调用openstack的api,create_instance的程序解析
python调用openstack的api,create_instance的程序解析 2017年10月17日 15:27:24 CloudXli 阅读数:848 版权声明:本文为博主原创文章,未经 ...
- nginx配置反向代理CAS单点登录应用
新增如下配置即可: location /cas { proxy_pass http://172.16.20.155:8080/cas; proxy_redirect default; proxy_re ...
- (十四)QFile操作,QByteArray,文件流操作,QTextStream,QDataStream,QFileInfo, QIODevice
QFile f 1.readall #include "widget.h" #include "ui_widget.h" #include <QFileD ...
- Redisson实现分布式锁
转: Redisson实现分布式锁 Redisson文档参考:https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95 redis是实现 ...
- \t \r \n \f
\t 的意思是 :水平制表符.将当前位置移到下一个tab位置. \r 的意思是: 回车.将当前位置移到本行的开头. \n 的意思是:回车换行.将当前位置移到下一行的开头. \f的意思是:换页.将当前位 ...
- 从线性模型(linear model)衍生出的机器学习分类器(classifier)
1. 线性模型简介 0x1:线性模型的现实意义 在一个理想的连续世界中,任何非线性的东西都可以被线性的东西来拟合(参考Taylor Expansion公式),所以理论上线性模型可以模拟物理世界中的绝大 ...