]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/utils/HTTPTransporter.h
5340d24219f1ebf145db0377b30fad0f6aba4300
2 * Copyright (c) 2019, The Jaeger Authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef JAEGERTRACING_UTILS_UDPCLIENT5_H
18 #define JAEGERTRACING_UTILS_UDPCLIENT5_H
23 #include <system_error>
25 #include "jaegertracing/Compilers.h"
27 #include "jaegertracing/utils/Transport.h"
29 #include "jaegertracing/net/IPAddress.h"
30 #include "jaegertracing/net/Socket.h"
31 #include "jaegertracing/net/http/Response.h"
33 #include <thrift/protocol/TBinaryProtocol.h>
34 #include <thrift/transport/TBufferTransports.h>
35 #include <thrift/transport/THttpClient.h>
37 namespace jaegertracing
{
40 class HTTPTransporter
: public Transport
{
42 HTTPTransporter(const net::URI
& endpoint
, int maxPacketSize
);
44 ~HTTPTransporter() { close(); }
46 void emitBatch(const thrift::Batch
& batch
) override
48 // Resets the buffer to write a new batch
49 _buffer
->resetBuffer();
51 // Does the serialisation to Thrift
52 auto oprot
= _protocol
.get();
54 oprot
->writeMessageEnd();
55 oprot
->getTransport()->writeEnd();
56 oprot
->getTransport()->flush();
58 uint8_t* data
= nullptr;
60 _buffer
->getBuffer(&data
, &size
);
62 // Sends the HTTP message
63 const auto numWritten
= ::send(_socket
.handle(), reinterpret_cast<char*>(data
), sizeof(uint8_t) * size
, 0);
65 if (static_cast<unsigned>(numWritten
) != size
) {
66 std::ostringstream oss
;
67 oss
<< "Failed to write message, numWritten=" << numWritten
<< ", size=" << size
;
68 throw std::system_error(errno
, std::system_category(), oss
.str());
71 // Waits for response. Check that the server acknowledged
72 // and returned a green status [200, 201, 202, 203, 204]
73 net::http::Response response
= net::http::read(_socket
);
74 if (response
.statusCode() < 200 && response
.statusCode() > 204) {
75 std::ostringstream oss
;
76 oss
<< "Failed to write message, HTTP error " << response
.statusCode() << response
.reason();
77 throw std::system_error(errno
, std::system_category(), oss
.str());
81 std::unique_ptr
<apache::thrift::protocol::TProtocolFactory
>
82 protocolFactory() const override
84 return std::unique_ptr
<apache::thrift::protocol::TProtocolFactory
>(
85 new apache::thrift::protocol::TBinaryProtocolFactory());
89 std::shared_ptr
<apache::thrift::transport::TMemoryBuffer
> _buffer
;
90 net::IPAddress _serverAddr
;
91 std::shared_ptr
<::apache::thrift::transport::THttpClient
> _httpClient
;
92 std::shared_ptr
<apache::thrift::protocol::TProtocol
> _protocol
;
94 static constexpr auto kHttpPacketMaxLength
= 1024 * 1024; // 1MB
98 } // namespace jaegertracing
100 #endif // JAEGERTRACING_UTILS_UDPCLIENT_H