Qpid Proton C++ API 0.40.0
 
Loading...
Searching...
No Matches
tracing_client.cpp

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.

/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "options.hpp"
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
// Include opentelemetry header files
#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;
class client : public proton::messaging_handler {
private:
std::string url;
std::vector<std::string> requests;
proton::receiver receiver;
public:
client(const std::string &u, const std::vector<std::string>& r) : url(u), requests(r) {}
sender = c.open_sender(url);
// Create a receiver requesting a dynamically created queue
// for the message source.
receiver_options opts = receiver_options().source(source_options().dynamic(true));
receiver = sender.connection().open_receiver("", opts);
}
void send_request() {
req.body(requests.front());
req.reply_to(receiver.source().address());
req.id(id_counter);
opentelemetry::trace::StartSpanOptions options;
options.kind = opentelemetry::trace::SpanKind::kClient;
// Start a span here before send
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",
{{"reply_to", req.reply_to()}, {"message", to_string(req.body())}},
options);
opentelemetry::trace::Scope scope = tracer->WithActiveSpan(span);
// Storing the 'scope' in a map to keep it alive and erasing it when a response is received.
scope_map[req.id()] = std::make_shared<opentelemetry::trace::Scope>(std::move(scope));
id_counter++;
sender.send(req);
}
send_request();
}
void on_message(proton::delivery &d, proton::message &response) override {
if (requests.empty()) return; // Spurious extra message!
// Converting the tag in proton::binary to std::string to add it as a span attribute. Tag in binary won't be visible.
proton::binary tag = d.tag();
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;
// Get Tracer
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer = provider->GetTracer("qpid-tracer", OPENTELEMETRY_SDK_VERSION);
// Start span with or without attributes as required.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> s = tracer->StartSpan("on_message",
{{"delivery_tag", delivery_tag}, {"message-received", to_string(response.body())}},
options);
// Mark span as active.
opentelemetry::trace::Scope sc = tracer->WithActiveSpan(s);
// Response has been received, thus erasing the 'scope' of the trace.
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");
// 1. Initialize the exporter and the provider.
// 2. Set the global trace provider.
// 3. Call proton::initOpenTelemetryTracer().
// Initialize otlp http Exporter
opentelemetry::exporter::otlp::OtlpHttpExporterOptions options;
options.url = "localhost:4318";
auto exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(new opentelemetry::exporter::otlp::OtlpHttpExporter(options));
// Set service-name
auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes
{
{"service.name", "qpid-example-client"}
};
// Creation of the resource for associating it with telemetry
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));
// Set the global trace provider
opentelemetry::trace::Provider::SetTracerProvider(provider);
// Enable tracing in proton cpp
// Sending 2 messages to the server.
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.
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.
A received message.
An AMQP message.
An AMQP message ID.
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.