1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/ObserverI.h>
#include <Ice/Connection.h>
#include <Ice/Endpoint.h>
#include <Ice/ObjectAdapter.h>
using namespace std;
using namespace Ice;
using namespace Ice::Instrumentation;
using namespace IceMX;
namespace
{
int ConnectionMetrics::*
getConnectionStateMetric(ConnectionState s)
{
switch(s)
{
case ConnectionStateValidating:
return &ConnectionMetrics::validating;
case ConnectionStateActive:
return &ConnectionMetrics::active;
case ConnectionStateHolding:
return &ConnectionMetrics::holding;
case ConnectionStateClosing:
return &ConnectionMetrics::closing;
case ConnectionStateClosed:
return &ConnectionMetrics::closed;
}
}
int ThreadMetrics::*
getThreadStateMetric(ThreadState s)
{
switch(s)
{
case ThreadStateIdle:
return 0;
case ThreadStateInUseForIO:
return &ThreadMetrics::inUseForIO;
case ThreadStateInUseForUser:
return &ThreadMetrics::inUseForUser;
case ThreadStateInUseForOther:
return &ThreadMetrics::inUseForOther;
}
}
template<typename Helper>
void addEndpointAttributes(typename Helper::Attributes& attrs)
{
attrs.add("endpointType", &Helper::getEndpointInfo, &EndpointInfo::type);
attrs.add("endpointIsDatagram", &Helper::getEndpointInfo, &EndpointInfo::datagram);
attrs.add("endpointIsSecure", &Helper::getEndpointInfo, &EndpointInfo::secure);
attrs.add("endpointProtocolVersion", &Helper::getEndpointInfo, &EndpointInfo::protocol);
attrs.add("endpointEncodingVersion", &Helper::getEndpointInfo, &EndpointInfo::encoding);
attrs.add("endpointTimeout", &Helper::getEndpointInfo, &EndpointInfo::timeout);
attrs.add("endpointCompress", &Helper::getEndpointInfo, &EndpointInfo::compress);
attrs.add("endpointHost", &Helper::getEndpointInfo, &IPEndpointInfo::host);
attrs.add("endpointPort", &Helper::getEndpointInfo, &IPEndpointInfo::port);
}
template<typename Helper>
void addConnectionAttributes(typename Helper::Attributes& attrs)
{
attrs.add("incoming", &Helper::getConnectionInfo, &ConnectionInfo::incoming);
attrs.add("adapterName", &Helper::getConnectionInfo, &ConnectionInfo::adapterName);
attrs.add("connectionId", &Helper::getConnectionInfo, &ConnectionInfo::connectionId);
attrs.add("localHost", &Helper::getConnectionInfo, &IPConnectionInfo::localAddress);
attrs.add("localPort", &Helper::getConnectionInfo, &IPConnectionInfo::localPort);
attrs.add("remoteHost", &Helper::getConnectionInfo, &IPConnectionInfo::remoteAddress);
attrs.add("remotePort", &Helper::getConnectionInfo, &IPConnectionInfo::remotePort);
attrs.add("mcastHost", &Helper::getConnectionInfo, &UDPConnectionInfo::mcastAddress);
attrs.add("mcastPort", &Helper::getConnectionInfo, &UDPConnectionInfo::mcastPort);
addEndpointAttributes<Helper>(attrs);
}
class ConnectionHelper : public MetricsHelperT<ConnectionMetrics>
{
public:
class Attributes : public AttributeResolverT<ConnectionHelper>
{
public:
Attributes()
{
add("parent", &ConnectionHelper::getParent);
add("id", &ConnectionHelper::getId);
addConnectionAttributes<ConnectionHelper>(*this);
}
};
static Attributes attributes;
ConnectionHelper(const ConnectionInfoPtr& con,
const EndpointInfoPtr& endpt,
ConnectionState state) :
_connection(con), _endpoint(endpt), _state(state)
{
}
virtual string operator()(const string& attribute) const
{
return attributes(this, attribute);
}
virtual void initMetrics(const ConnectionMetricsPtr& v) const
{
++(v.get()->*getConnectionStateMetric(_state));
}
string
getId() const
{
ostringstream os;
IPConnectionInfoPtr info = IPConnectionInfoPtr::dynamicCast(_connection);
if(info)
{
os << info->localAddress << ':' << info->localPort << '/' << info->remoteAddress << ':' << info->remotePort;
}
else
{
os << "connection-" << _connection.get();
}
return os.str();
}
string
getParent() const
{
if(!_connection->adapterName.empty())
{
return _connection->adapterName;
}
else
{
return "Communicator";
}
}
::Ice::ConnectionInfoPtr
getConnectionInfo() const
{
return _connection;
}
::Ice::EndpointInfoPtr
getEndpointInfo() const
{
return _endpoint;
}
private:
ConnectionInfoPtr _connection;
EndpointInfoPtr _endpoint;
ConnectionState _state;
};
ConnectionHelper::Attributes ConnectionHelper::attributes;
class DispatchRequestHelper : public MetricsHelperT<Metrics>
{
public:
class Attributes : public AttributeResolverT<DispatchRequestHelper>
{
public:
Attributes()
{
add("parent", &DispatchRequestHelper::getParent);
add("id", &DispatchRequestHelper::getId);
addConnectionAttributes<DispatchRequestHelper>(*this);
add("operation", &DispatchRequestHelper::getCurrent, &Current::operation);
add("identityCategory", &DispatchRequestHelper::getIdentity, &Identity::category);
add("identityName", &DispatchRequestHelper::getIdentity, &Identity::name);
add("facet", &DispatchRequestHelper::getCurrent, &Current::facet);
add("encoding", &DispatchRequestHelper::getCurrent, &Current::encoding);
add("mode", &DispatchRequestHelper::getMode);
}
};
static Attributes attributes;
DispatchRequestHelper(const Current& current) : _current(current)
{
}
virtual string operator()(const string& attribute) const
{
if(attribute.find("context.") == 0)
{
Ice::Context::const_iterator p = _current.ctx.find(attribute.substr(8));
if(p != _current.ctx.end())
{
return p->second;
}
return "unknown";
}
else
{
return attributes(this, attribute);
}
}
string
getMode() const
{
return _current.requestId == 0 ? "oneway" : "twoway";
}
string
getId() const
{
ostringstream os;
if(!_current.id.category.empty())
{
os << _current.id.category << '/';
}
os << _current.id.name << '-' << _current.operation;
return os.str();
}
string
getParent() const
{
return _current.adapter->getName();
}
::Ice::ConnectionInfoPtr
getConnectionInfo() const
{
return _current.con->getInfo();
}
::Ice::EndpointInfoPtr
getEndpointInfo() const
{
return _current.con->getEndpoint()->getInfo();
}
const Current&
getCurrent() const
{
return _current;
}
const Identity&
getIdentity() const
{
return _current.id;
}
private:
const Current& _current;
};
DispatchRequestHelper::Attributes DispatchRequestHelper::attributes;
class InvocationHelper : public MetricsHelperT<Metrics>
{
public:
class Attributes : public AttributeResolverT<InvocationHelper>
{
public:
Attributes()
{
add("parent", &InvocationHelper::getParent);
add("id", &InvocationHelper::getId);
add("operation", &InvocationHelper::_operation);
add("identityCategory", &InvocationHelper::getIdentity, &Identity::category);
add("identityName", &InvocationHelper::getIdentity, &Identity::name);
add("facet", &InvocationHelper::getProxy, &IceProxy::Ice::Object::ice_getFacet);
add("encoding", &InvocationHelper::getProxy, &IceProxy::Ice::Object::ice_getEncodingVersion);
add("mode", &InvocationHelper::getMode);
}
};
static Attributes attributes;
InvocationHelper(const Ice::ObjectPrx& proxy, const string& op, const Ice::Context& ctx = Ice::Context()) :
_proxy(proxy), _operation(op), _context(ctx)
{
}
virtual string operator()(const string& attribute) const
{
if(attribute.find("context.") == 0)
{
Ice::Context::const_iterator p = _context.find(attribute.substr(8));
if(p != _context.end())
{
return p->second;
}
return "unknown";
}
else
{
return attributes(this, attribute);
}
}
string
getMode() const
{
if(_proxy->ice_isTwoway())
{
return "twoway";
}
else if(_proxy->ice_isOneway())
{
return "oneway";
}
else if(_proxy->ice_isBatchOneway())
{
return "batch-oneway";
}
else if(_proxy->ice_isDatagram())
{
return "datagram";
}
else if(_proxy->ice_isBatchDatagram())
{
return "batch-datagram";
}
else
{
return "unknown";
}
}
string
getId() const
{
ostringstream os;
const Ice::Identity& id = _proxy->ice_getIdentity();
if(!id.category.empty())
{
os << id.category << '/';
}
os << id.name << '-' << _operation;
return os.str();
}
string
getParent() const
{
return "Communicator";
}
const ObjectPrx&
getProxy() const
{
return _proxy;
}
Identity
getIdentity() const
{
return _proxy->ice_getIdentity();
}
private:
const ObjectPrx& _proxy;
string _operation;
const Ice::Context& _context;
};
InvocationHelper::Attributes InvocationHelper::attributes;
class ThreadHelper : public MetricsHelperT<ThreadMetrics>
{
public:
class Attributes : public AttributeResolverT<ThreadHelper>
{
public:
Attributes()
{
add("parent", &ThreadHelper::_parent);
add("id", &ThreadHelper::_id);
}
};
static Attributes attributes;
ThreadHelper(const string& parent, const string& id, ThreadState state) :
_parent(parent), _id(id), _state(state)
{
}
virtual string operator()(const string& attribute) const
{
return attributes(this, attribute);
}
virtual void initMetrics(const ThreadMetricsPtr& v) const
{
if(_state != ThreadStateIdle)
{
++(v.get()->*getThreadStateMetric(_state));
}
}
private:
const std::string _parent;
const std::string _id;
const ThreadState _state;
};
ThreadHelper::Attributes ThreadHelper::attributes;
class ConnectHelper : public MetricsHelperT<Metrics>
{
public:
class Attributes : public AttributeResolverT<ConnectHelper>
{
public:
Attributes()
{
add("parent", &ConnectHelper::getParent);
add("id", &ConnectHelper::_id);
addEndpointAttributes<ConnectHelper>(*this);
}
};
static Attributes attributes;
ConnectHelper(const EndpointInfoPtr& endpt, const string& id) : _id(id), _endpoint(endpt)
{
}
virtual string operator()(const string& attribute) const
{
return attributes(this, attribute);
}
Ice::EndpointInfoPtr
getEndpointInfo() const
{
return _endpoint;
}
std::string
getParent() const
{
return "Communicator";
}
private:
const std::string _id;
const Ice::EndpointInfoPtr _endpoint;
};
ConnectHelper::Attributes ConnectHelper::attributes;
}
void
ConnectionObserverI::detach()
{
ObserverT<ConnectionMetrics>::detach();
forEach(dec(&ConnectionMetrics::closed));
}
void
ConnectionObserverI::stateChanged(ConnectionState oldState, ConnectionState newState)
{
struct StateChanged
{
StateChanged(ConnectionState oldState, ConnectionState newState) :
oldState(oldState), newState(newState)
{
}
void operator()(const ConnectionMetricsPtr& v)
{
--(v.get()->*getConnectionStateMetric(oldState));
++(v.get()->*getConnectionStateMetric(newState));
}
ConnectionState oldState;
ConnectionState newState;
};
forEach(StateChanged(oldState, newState));
}
void
ConnectionObserverI::sentBytes(Int num, Long duration)
{
forEach(chain(add(&ConnectionMetrics::sentBytes, num), add(&ConnectionMetrics::sentTime, duration)));
}
void
ConnectionObserverI::receivedBytes(Int num, Long duration)
{
forEach(chain(add(&ConnectionMetrics::receivedBytes, num), add(&ConnectionMetrics::receivedTime, duration)));
}
void
ThreadObserverI::stateChanged(ThreadState oldState, ThreadState newState)
{
struct StateChanged
{
StateChanged(ThreadState oldState, ThreadState newState) : oldState(oldState), newState(newState)
{
}
void operator()(const ThreadMetricsPtr& v)
{
if(oldState != ThreadStateIdle)
{
--(v.get()->*getThreadStateMetric(oldState));
}
if(newState != ThreadStateIdle)
{
++(v.get()->*getThreadStateMetric(newState));
}
}
ThreadState oldState;
ThreadState newState;
};
forEach(StateChanged(oldState, newState));
}
void
InvocationObserverI::retried()
{
forEach(inc(&InvocationMetrics::retry));
}
ObserverPtr
InvocationObserverI::getRemoteInvocationObserver(const Ice::ConnectionPtr&)
{
return 0;
}
ObserverResolverI::ObserverResolverI(const MetricsAdminIPtr& metrics) :
_metrics(metrics),
_connections("Connection", metrics),
_dispatch("Dispatch", metrics),
_invocations("Invocation", metrics),
_threads("Thread", metrics),
_connects("Connect", metrics),
_endpointResolves("EndpointResolve", metrics)
{
}
void
ObserverResolverI::setObserverUpdater(const ObserverUpdaterPtr& updater)
{
_metrics->addUpdater("Connection", newUpdater(updater, &ObserverUpdater::updateConnectionObservers));
_metrics->addUpdater("Thread", newUpdater(updater, &ObserverUpdater::updateThreadObservers));
}
Ice::Instrumentation::ObserverPtr
ObserverResolverI::getConnectObserver(const Ice::EndpointInfoPtr& endpt, const string& connector)
{
return _connects.getObserver(ConnectHelper(endpt, connector));
}
Ice::Instrumentation::ObserverPtr
ObserverResolverI::getEndpointResolveObserver(const Ice::EndpointInfoPtr& endpt, const string& endpoint)
{
return _endpointResolves.getObserver(ConnectHelper(endpt, endpoint));
}
ConnectionObserverPtr
ObserverResolverI::getConnectionObserver(const ConnectionInfoPtr& con,
const EndpointInfoPtr& endpt,
ConnectionState state,
const ConnectionObserverPtr& observer)
{
return _connections.getObserver(ConnectionHelper(con, endpt, state), observer);
}
ThreadObserverPtr
ObserverResolverI::getThreadObserver(const string& parent,
const string& id,
Ice::Instrumentation::ThreadState state,
const Ice::Instrumentation::ThreadObserverPtr& observer)
{
return _threads.getObserver(ThreadHelper(parent, id, state), observer);
}
InvocationObserverPtr
ObserverResolverI::getInvocationObserver(const ObjectPrx& proxy, const string& op)
{
return _invocations.getObserver(InvocationHelper(proxy, op));
}
InvocationObserverPtr
ObserverResolverI::getInvocationObserverWithContext(const ObjectPrx& proxy,
const string& op,
const Context& ctx)
{
return _invocations.getObserver(InvocationHelper(proxy, op, ctx));
}
ObserverPtr
ObserverResolverI::getDispatchObserver(const Current& current)
{
return _dispatch.getObserver(DispatchRequestHelper(current));
}
|