A client part of a request-response example demonstrating the use of tracing.
A client part of a request-response example demonstrating the use of tracing. Send requests and receive responses from the tracing_server. Traces and spans are displayed on Jaeger UI. For detailed information, tracing documentation(tracing.dox) can be found in docs folder.
#include "options.hpp"
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <opentelemetry/sdk/trace/simple_processor.h>
#include <opentelemetry/sdk/trace/tracer_provider.h>
#include <opentelemetry/trace/provider.h>
#include <opentelemetry/nostd/unique_ptr.h>
#include <opentelemetry/exporters/otlp/otlp_http_exporter.h>
#include <opentelemetry/sdk/resource/resource.h>
#include <opentelemetry/trace/span.h>
#include <opentelemetry/trace/tracer.h>
#include <opentelemetry/trace/context.h>
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider;
std::map<proton::message_id, std::shared_ptr<opentelemetry::trace::Scope>> scope_map;
int id_counter = 0;
private:
std::string url;
std::vector<std::string> requests;
public:
client(const std::string &u, const std::vector<std::string>& r) : url(u), requests(r) {}
receiver_options opts = receiver_options().source(source_options().dynamic(true));
}
void send_request() {
req.
body(requests.front());
opentelemetry::trace::StartSpanOptions options;
options.kind = opentelemetry::trace::SpanKind::kClient;
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer = provider->GetTracer("qpid-tracer", OPENTELEMETRY_SDK_VERSION);
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span = tracer->StartSpan("send_request",
options);
opentelemetry::trace::Scope scope = tracer->WithActiveSpan(span);
scope_map[req.
id()] = std::make_shared<opentelemetry::trace::Scope>(std::move(scope));
id_counter++;
}
send_request();
}
if (requests.empty()) return;
std::string tag_in_string = std::string(tag);
std::stringstream ss;
for (int i = 0; i < (int)tag_in_string.length(); ++i)
ss << std::hex << (int)tag[i];
std::string delivery_tag = ss.str();
opentelemetry::trace::StartSpanOptions options;
options.kind = opentelemetry::trace::SpanKind::kClient;
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer = provider->GetTracer("qpid-tracer", OPENTELEMETRY_SDK_VERSION);
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> s = tracer->StartSpan("on_message",
{{
"delivery_tag", delivery_tag}, {
"message-received",
to_string(response.
body())}},
options);
opentelemetry::trace::Scope sc = tracer->WithActiveSpan(s);
scope_map.erase(response.
id());
std::cout << requests.front() <<
" => " << response.
body() << std::endl;
requests.erase(requests.begin());
if (!requests.empty()) {
send_request();
} else {
}
}
};
int main(int argc, char **argv) {
try {
std::string url("127.0.0.1:5672/examples");
opentelemetry::exporter::otlp::OtlpHttpExporterOptions options;
options.url = "localhost:4318";
auto exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(new opentelemetry::exporter::otlp::OtlpHttpExporter(options));
auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes
{
{"service.name", "qpid-example-client"}
};
auto resource = opentelemetry::sdk::resource::Resource::Create(resource_attributes);
auto processor = std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>(
new opentelemetry::sdk::trace::SimpleSpanProcessor(std::move(exporter)));
provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::sdk::trace::TracerProvider(std::move(processor), resource));
opentelemetry::trace::Provider::SetTracerProvider(provider);
std::vector<std::string> requests;
requests.push_back("Two roads diverged in a wood.");
requests.push_back("I took the one less traveled by.");
client c(url, requests);
return 0;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 1;
}
Arbitrary binary data.
Definition binary.hpp:40
void close()
Close the connection.
receiver open_receiver(const std::string &addr)
Open a receiver for addr on default_session().
A top-level container of connections, sessions, and links.
Definition container.hpp:50
void run()
Run the container in the current thread.
returned< sender > open_sender(const std::string &addr_url)
Open a connection and sender for addr_url.
A received message.
Definition delivery.hpp:40
binary tag() const
Return the tag for this delivery.
class connection connection() const
The connection that owns this link.
An AMQP message.
Definition message.hpp:48
void reply_to(const std::string &)
Set the address for replies.
void id(const message_id &)
Set the message ID.
void body(const value &x)
Set the body. Equivalent to body() = x.
Handler for Proton messaging events.
Definition messaging_handler.hpp:69
virtual void on_message(delivery &, message &)
A message is received.
virtual void on_receiver_open(receiver &)
The remote peer opened the link.
virtual void on_container_start(container &)
The container event loop is starting.
Options for creating a receiver.
Definition receiver_options.hpp:59
A channel for receiving messages.
Definition receiver.hpp:41
class source source() const
Get the source node.
A channel for sending messages.
Definition sender.hpp:40
tracker send(const message &m)
Send a message on the sender.
Options for creating a source node for a sender or receiver.
Definition source_options.hpp:46
std::string address() const
The address of the source.
class connection connection() const
Return the connection for this transfer.
A connection to a remote AMQP peer.
A top-level container of connections, sessions, and links.
Handler for Proton messaging events.
std::string to_string(const message &)
Human readable string representation.
void initOpenTelemetryTracer()
Tracer initializer.
Options for creating a receiver.
Options for creating a source node for a sender or receiver.
A tracker for a sent message.