]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/contrib/fb303/TClientInfo.cpp
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 #include <thrift/server/TClientInfo.h>
22 namespace apache
{ namespace thrift
{ namespace server
{
24 using namespace apache::thrift
;
25 using namespace apache::thrift::transport
;
27 TClientInfoConnection::TClientInfoConnection() {
28 call_
[kNameLen
- 1] = '\0'; // insure NUL terminator is there
33 void TClientInfoConnection::recordAddr(const sockaddr
* addr
) {
38 if (addr
->sa_family
== AF_INET
) {
39 memcpy((void*)&addr_
.ipv4
, (const void *)addr
, sizeof(sockaddr_in
));
41 else if (addr
->sa_family
== AF_INET6
) {
42 memcpy((void*)&addr_
.ipv6
, (const void *)addr
, sizeof(sockaddr_in6
));
47 void TClientInfoConnection::eraseAddr() {
48 addr_
.ipv4
.sin_family
= AF_UNSPEC
;
51 const char* TClientInfoConnection::getAddr(char* buf
, int len
) const {
52 switch (addr_
.ipv4
.sin_family
) {
54 return inet_ntop(AF_INET
, &addr_
.ipv4
.sin_addr
, buf
, len
);
56 return inet_ntop(AF_INET6
, &addr_
.ipv6
.sin6_addr
, buf
, len
);
62 void TClientInfoConnection::recordCall(const char* name
) {
63 strncpy(call_
, name
, kNameLen
- 1); // NUL terminator set in constructor
67 void TClientInfoConnection::eraseCall() {
71 const char* TClientInfoConnection::getCall() const {
72 if (call_
[0] == '\0') {
78 void TClientInfoConnection::getTime(timespec
* time
) const {
82 uint64_t TClientInfoConnection::getNCalls() const {
86 void TClientInfoConnection::initTime() {
87 clock_gettime(CLOCK_REALTIME
, &time_
);
91 TClientInfoConnection
* TClientInfo::getConnection(int fd
, bool grow
) {
92 if (fd
< 0 || (!grow
&& fd
>= info_
.size())) {
98 size_t TClientInfo::size() const {
102 void* TClientInfoServerHandler::createContext(boost::shared_ptr
<TProtocol
> input
,
103 boost::shared_ptr
<TProtocol
> output
) {
106 return (void*) new Connect(&clientInfo_
);
109 void TClientInfoServerHandler::deleteContext(void* connectionContext
,
110 boost::shared_ptr
<TProtocol
> input
,
111 boost::shared_ptr
<TProtocol
> output
) {
112 Connect
* call
= static_cast<Connect
*>(connectionContext
);
113 if (call
->callInfo_
) {
114 call
->callInfo_
->eraseCall();
119 void TClientInfoServerHandler::processContext(void* connectionContext
,
120 shared_ptr
<TTransport
> transport
) {
121 Connect
* call
= static_cast<Connect
*>(connectionContext
);
122 if (call
->callInfo_
== NULL
) {
123 if (typeid(*(transport
.get())) == typeid(TSocket
)) {
124 TSocket
* tsocket
= static_cast<TSocket
*>(transport
.get());
125 int fd
= tsocket
->getSocketFD();
129 call
->callInfo_
= call
->clientInfo_
->getConnection(fd
, true);
130 assert(call
->callInfo_
!= NULL
);
132 call
->callInfo_
->recordAddr(tsocket
->getCachedAddress(&len
));
137 void TClientInfoServerHandler::getStatsStrings(vector
<string
>& result
) {
140 clock_gettime(CLOCK_REALTIME
, &now
);
142 for (int i
= 0; i
< clientInfo_
.size(); ++i
) {
143 TClientInfoConnection
* info
= clientInfo_
.getConnection(i
, false);
144 const char* callStr
= info
->getCall();
145 if (callStr
== NULL
) {
149 char addrBuf
[INET6_ADDRSTRLEN
];
150 const char* addrStr
= info
->getAddr(addrBuf
, sizeof addrBuf
);
151 if (addrStr
== NULL
) {
152 // cerr << "no addr!" << endl;
157 info
->getTime(&start
);
158 double secs
= (double)(now
.tv_sec
- start
.tv_sec
) + (now
.tv_nsec
- start
.tv_nsec
)*0.000000001;
161 snprintf(buf
, sizeof buf
, "%d %s %s %.3f %llu", i
, addrStr
, callStr
, secs
,
162 (uint64_t)info
->getNCalls());
164 result
.push_back(buf
);
168 void* TClientInfoCallHandler::getContext(const char* fn_name
, void* serverContext
) {
170 TClientInfoConnection
* callInfo
= static_cast<TClientInfoServerHandler::Connect
*>(serverContext
)->callInfo_
;
171 if (callInfo
!= NULL
) {
172 callInfo
->recordCall(fn_name
);
178 } } } // namespace apache::thrift::server