Line data Source code
1 : #include "source/extensions/tracers/xray/daemon_broker.h" 2 : 3 : #include "envoy/network/address.h" 4 : 5 : #include "source/common/buffer/buffer_impl.h" 6 : #include "source/common/network/socket_interface.h" 7 : #include "source/common/network/utility.h" 8 : #include "source/common/protobuf/utility.h" 9 : #include "source/extensions/tracers/xray/daemon.pb.h" 10 : 11 : namespace Envoy { 12 : namespace Extensions { 13 : namespace Tracers { 14 : namespace XRay { 15 : 16 : namespace { 17 : // creates a header JSON for X-Ray daemon. 18 : // For example: 19 : // { "format": "json", "version": 1} 20 0 : std::string createHeader(const std::string& format, uint32_t version) { 21 0 : source::extensions::tracers::xray::daemon::Header header; 22 0 : header.set_format(format); 23 0 : header.set_version(version); 24 0 : return MessageUtil::getJsonStringFromMessageOrError(header, false /* pretty_print */, 25 0 : false /* always_print_primitive_fields */); 26 0 : } 27 : 28 : } // namespace 29 : 30 : DaemonBrokerImpl::DaemonBrokerImpl(const std::string& daemon_endpoint) 31 : : address_(Network::Utility::parseInternetAddressAndPort(daemon_endpoint, false /*v6only*/)), 32 0 : io_handle_(Network::ioHandleForAddr(Network::Socket::Type::Datagram, address_, {})) {} 33 : 34 0 : void DaemonBrokerImpl::send(const std::string& data) const { 35 0 : auto& logger = Logger::Registry::getLog(Logger::Id::tracing); 36 0 : constexpr auto version = 1; 37 0 : constexpr auto format = "json"; 38 0 : const std::string payload = absl::StrCat(createHeader(format, version), "\n", data); 39 0 : Buffer::RawSlice buf; 40 0 : buf.mem_ = const_cast<char*>(payload.data()); 41 0 : buf.len_ = payload.length(); 42 0 : const auto rc = Network::Utility::writeToSocket(*io_handle_, &buf, 1 /*num_slices*/, 43 0 : nullptr /*local_ip*/, *address_); 44 : 45 0 : if (rc.return_value_ != payload.length()) { 46 : // TODO(suniltheta): report this in stats 47 0 : ENVOY_LOG_TO_LOGGER(logger, debug, "Failed to send trace payload to the X-Ray daemon."); 48 0 : } 49 0 : } 50 : 51 : } // namespace XRay 52 : } // namespace Tracers 53 : } // namespace Extensions 54 : } // namespace Envoy