< Summary

Information
Class: Ice.Internal.DispatchObserverI
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/InstrumentationI.cs
Tag: 125_37167941578
Line coverage
100%
Covered lines: 7
Uncovered lines: 0
Coverable lines: 7
Total lines: 1098
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage
100%
Covered methods: 3
Fully covered methods: 3
Total methods: 3
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
userException()100%22100%
reply(...)100%22100%
userException(...)100%11100%

File(s)

/_/csharp/src/Ice/Internal/InstrumentationI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceMX;
 4using System.Diagnostics;
 5using System.Text;
 6
 7namespace Ice.Internal;
 8
 9public class ObserverWithDelegate<T, O> : Observer<T>
 10    where T : Metrics, new()
 11    where O : Ice.Instrumentation.Observer
 12{
 13    public override void
 14    attach()
 15    {
 16        base.attach();
 17        delegate_?.attach();
 18    }
 19
 20    public override void
 21    detach()
 22    {
 23        base.detach();
 24        delegate_?.detach();
 25    }
 26
 27    public override void
 28    failed(string exceptionName)
 29    {
 30        base.failed(exceptionName);
 31        delegate_?.failed(exceptionName);
 32    }
 33
 34    public O
 35    getDelegate() => delegate_;
 36
 37    public void
 38    setDelegate(O del) => delegate_ = del;
 39
 40    public Observer getObserver<S, ObserverImpl, Observer>(string mapName, MetricsHelper<S> helper, Observer del)
 41        where S : Metrics, new()
 42        where ObserverImpl : ObserverWithDelegate<S, Observer>, Observer, new()
 43        where Observer : Ice.Instrumentation.Observer
 44    {
 45        ObserverImpl obsv = getObserver<S, ObserverImpl>(mapName, helper);
 46        if (obsv != null)
 47        {
 48            obsv.setDelegate(del);
 49            return obsv;
 50        }
 51        return del;
 52    }
 53
 54    protected O delegate_;
 55}
 56
 57public class ObserverFactoryWithDelegate<T, OImpl, O> : ObserverFactory<T, OImpl>
 58    where T : Metrics, new()
 59    where OImpl : ObserverWithDelegate<T, O>, O, new()
 60    where O : Ice.Instrumentation.Observer
 61{
 62    public ObserverFactoryWithDelegate(MetricsAdminI metrics, string name)
 63        : base(metrics, name)
 64    {
 65    }
 66
 67    public O getObserver(MetricsHelper<T> helper, O del)
 68    {
 69        OImpl o = getObserver(helper);
 70        if (o != null)
 71        {
 72            o.setDelegate(del);
 73            return o;
 74        }
 75        return del;
 76    }
 77
 78    public O getObserver(MetricsHelper<T> helper, object observer, O del)
 79    {
 80        OImpl o = getObserver(helper, observer);
 81        if (o != null)
 82        {
 83            o.setDelegate(del);
 84            return o;
 85        }
 86        return del;
 87    }
 88}
 89
 90internal static class AttrsUtil
 91{
 92    public static void
 93    addEndpointAttributes<T>(MetricsHelper<T>.AttributeResolver r, Type cl) where T : IceMX.Metrics
 94    {
 95        r.add("endpoint", cl.GetMethod("getEndpoint"));
 96
 97        Type cli = typeof(Ice.EndpointInfo);
 98        r.add("endpointType", cl.GetMethod("getEndpointInfo"), cli.GetMethod("type"));
 99        r.add("endpointIsDatagram", cl.GetMethod("getEndpointInfo"), cli.GetMethod("datagram"));
 100        r.add("endpointIsSecure", cl.GetMethod("getEndpointInfo"), cli.GetMethod("secure"));
 101        r.add("endpointCompress", cl.GetMethod("getEndpointInfo"), cli.GetField("compress"));
 102
 103        cli = typeof(Ice.IPEndpointInfo);
 104        r.add("endpointHost", cl.GetMethod("getEndpointInfo"), cli.GetField("host"));
 105        r.add("endpointPort", cl.GetMethod("getEndpointInfo"), cli.GetField("port"));
 106    }
 107
 108    public static void
 109    addConnectionAttributes<T>(MetricsHelper<T>.AttributeResolver r, Type cl) where T : IceMX.Metrics
 110    {
 111        Type cli = typeof(Ice.ConnectionInfo);
 112        r.add("incoming", cl.GetMethod("getConnectionInfo"), cli.GetField("incoming"));
 113        r.add("adapterName", cl.GetMethod("getConnectionInfo"), cli.GetField("adapterName"));
 114        r.add("connectionId", cl.GetMethod("getConnectionInfo"), cli.GetField("connectionId"));
 115
 116        cli = typeof(Ice.IPConnectionInfo);
 117        r.add("localHost", cl.GetMethod("getConnectionInfo"), cli.GetField("localAddress"));
 118        r.add("localPort", cl.GetMethod("getConnectionInfo"), cli.GetField("localPort"));
 119        r.add("remoteHost", cl.GetMethod("getConnectionInfo"), cli.GetField("remoteAddress"));
 120        r.add("remotePort", cl.GetMethod("getConnectionInfo"), cli.GetField("remotePort"));
 121
 122        cli = typeof(Ice.UDPConnectionInfo);
 123        r.add("mcastHost", cl.GetMethod("getConnectionInfo"), cli.GetField("mcastAddress"));
 124        r.add("mcastPort", cl.GetMethod("getConnectionInfo"), cli.GetField("mcastPort"));
 125
 126        addEndpointAttributes<T>(r, cl);
 127    }
 128}
 129
 130internal class ConnectionHelper : MetricsHelper<ConnectionMetrics>
 131{
 132    private class AttributeResolverI : AttributeResolver
 133    {
 134        public AttributeResolverI()
 135        {
 136            try
 137            {
 138                Type cl = typeof(ConnectionHelper);
 139                add("parent", cl.GetMethod("getParent"));
 140                add("id", cl.GetMethod("getId"));
 141                add("state", cl.GetMethod("getState"));
 142                AttrsUtil.addConnectionAttributes(this, cl);
 143            }
 144            catch (System.Exception)
 145            {
 146                Debug.Assert(false);
 147            }
 148        }
 149    }
 150
 151    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 152
 153    public ConnectionHelper(Ice.ConnectionInfo con, Ice.Endpoint endpt, Ice.Instrumentation.ConnectionState state)
 154        : base(_attributes)
 155    {
 156        _connectionInfo = con;
 157        _endpoint = endpt;
 158        _state = state;
 159    }
 160
 161    public string getId()
 162    {
 163        if (_id == null)
 164        {
 165            var os = new StringBuilder();
 166            Ice.IPConnectionInfo info = getIPConnectionInfo();
 167            if (info != null)
 168            {
 169                os.Append(info.localAddress).Append(':').Append(info.localPort);
 170                os.Append(" -> ");
 171                os.Append(info.remoteAddress).Append(':').Append(info.remotePort);
 172            }
 173            else
 174            {
 175                os.Append("connection-").Append(_connectionInfo);
 176            }
 177            if (_connectionInfo.connectionId.Length > 0)
 178            {
 179                os.Append(" [").Append(_connectionInfo.connectionId).Append(']');
 180            }
 181            _id = os.ToString();
 182        }
 183        return _id;
 184    }
 185
 186    public string getState()
 187    {
 188        switch (_state)
 189        {
 190            case Ice.Instrumentation.ConnectionState.ConnectionStateValidating:
 191                return "validating";
 192            case Ice.Instrumentation.ConnectionState.ConnectionStateHolding:
 193                return "holding";
 194            case Ice.Instrumentation.ConnectionState.ConnectionStateActive:
 195                return "active";
 196            case Ice.Instrumentation.ConnectionState.ConnectionStateClosing:
 197                return "closing";
 198            case Ice.Instrumentation.ConnectionState.ConnectionStateClosed:
 199                return "closed";
 200            default:
 201                Debug.Assert(false);
 202                return "";
 203        }
 204    }
 205
 206    public string getParent()
 207    {
 208        if (_connectionInfo.adapterName != null && _connectionInfo.adapterName.Length > 0)
 209        {
 210            return _connectionInfo.adapterName;
 211        }
 212        else
 213        {
 214            return "Communicator";
 215        }
 216    }
 217
 218    public Ice.ConnectionInfo getConnectionInfo() => _connectionInfo;
 219
 220    public Ice.Endpoint getEndpoint() => _endpoint;
 221
 222    public Ice.EndpointInfo getEndpointInfo()
 223    {
 224        _endpointInfo ??= _endpoint.getInfo();
 225        return _endpointInfo;
 226    }
 227
 228    private Ice.IPConnectionInfo
 229    getIPConnectionInfo()
 230    {
 231        for (Ice.ConnectionInfo p = _connectionInfo; p != null; p = p.underlying)
 232        {
 233            if (p is Ice.IPConnectionInfo)
 234            {
 235                return (Ice.IPConnectionInfo)p;
 236            }
 237        }
 238        return null;
 239    }
 240
 241    private readonly Ice.ConnectionInfo _connectionInfo;
 242    private readonly Ice.Endpoint _endpoint;
 243    private readonly Ice.Instrumentation.ConnectionState _state;
 244    private string _id;
 245    private Ice.EndpointInfo _endpointInfo;
 246}
 247
 248internal class DispatchHelper : MetricsHelper<DispatchMetrics>
 249{
 250    private class AttributeResolverI : AttributeResolver
 251    {
 252        public AttributeResolverI()
 253        {
 254            try
 255            {
 256                Type cl = typeof(DispatchHelper);
 257                add("parent", cl.GetMethod("getParent"));
 258                add("id", cl.GetMethod("getId"));
 259
 260                AttrsUtil.addConnectionAttributes(this, cl);
 261
 262                Type clc = typeof(Ice.Current);
 263                add("operation", cl.GetMethod("getCurrent"), clc.GetProperty("operation"));
 264                add("identity", cl.GetMethod("getIdentity"));
 265                add("facet", cl.GetMethod("getCurrent"), clc.GetProperty("facet"));
 266                add("requestId", cl.GetMethod("getCurrent"), clc.GetProperty("requestId"));
 267                add("mode", cl.GetMethod("getMode"));
 268            }
 269            catch (System.Exception)
 270            {
 271                Debug.Assert(false);
 272            }
 273        }
 274    }
 275
 276    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 277
 278    public DispatchHelper(Ice.Current current, int size)
 279        : base(_attributes)
 280    {
 281        _current = current;
 282        _size = size;
 283    }
 284
 285    protected override string defaultResolve(string attribute)
 286    {
 287        if (attribute.IndexOf("context.", 0, StringComparison.Ordinal) == 0)
 288        {
 289            if (_current.ctx.TryGetValue(attribute[8..], out string v))
 290            {
 291                return v;
 292            }
 293        }
 294        throw new ArgumentOutOfRangeException(attribute);
 295    }
 296
 297    public override void initMetrics(DispatchMetrics v) => v.size += _size;
 298
 299    public string getMode() => _current.requestId == 0 ? "oneway" : "twoway";
 300
 301    public string getId()
 302    {
 303        if (_id == null)
 304        {
 305            var os = new StringBuilder();
 306            if (_current.id.category.Length > 0)
 307            {
 308                os.Append(_current.id.category).Append('/');
 309            }
 310            os.Append(_current.id.name).Append(" [").Append(_current.operation).Append(']');
 311            _id = os.ToString();
 312        }
 313        return _id;
 314    }
 315
 316    public string getParent() => _current.adapter.getName();
 317
 318    public Ice.ConnectionInfo getConnectionInfo()
 319    {
 320        if (_current.con != null)
 321        {
 322            return _current.con.getInfo();
 323        }
 324        return null;
 325    }
 326
 327    public Ice.Endpoint getEndpoint()
 328    {
 329        if (_current.con != null)
 330        {
 331            return _current.con.getEndpoint();
 332        }
 333        return null;
 334    }
 335
 336    public Ice.EndpointInfo getEndpointInfo()
 337    {
 338        if (_current.con != null && _endpointInfo == null)
 339        {
 340            _endpointInfo = _current.con.getEndpoint().getInfo();
 341        }
 342        return _endpointInfo;
 343    }
 344
 345    public Ice.Current getCurrent() => _current;
 346
 347    public string getIdentity() => _current.adapter.getCommunicator().identityToString(_current.id);
 348
 349    private readonly Ice.Current _current;
 350    private readonly int _size;
 351    private string _id;
 352    private Ice.EndpointInfo _endpointInfo;
 353}
 354
 355internal class InvocationHelper : MetricsHelper<InvocationMetrics>
 356{
 357    private class AttributeResolverI : AttributeResolver
 358    {
 359        public AttributeResolverI()
 360        {
 361            try
 362            {
 363                Type cl = typeof(InvocationHelper);
 364                add("parent", cl.GetMethod("getParent"));
 365                add("id", cl.GetMethod("getId"));
 366
 367                add("operation", cl.GetMethod("getOperation"));
 368                add("identity", cl.GetMethod("getIdentity"));
 369
 370                Type cli = typeof(Ice.ObjectPrx);
 371                add("facet", cl.GetMethod("getProxy"), cli.GetMethod("ice_getFacet"));
 372                add("encoding", cl.GetMethod("getEncodingVersion"));
 373                add("mode", cl.GetMethod("getMode"));
 374                add("proxy", cl.GetMethod("getProxy"));
 375            }
 376            catch (System.Exception)
 377            {
 378                Debug.Assert(false);
 379            }
 380        }
 381    }
 382
 383    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 384
 385    public InvocationHelper(Ice.ObjectPrx proxy, string op, Dictionary<string, string> ctx)
 386        : base(_attributes)
 387    {
 388        _proxy = proxy;
 389        _operation = op;
 390        _context = ctx;
 391    }
 392
 393    protected override string defaultResolve(string attribute)
 394    {
 395        if (attribute.IndexOf("context.", 0, StringComparison.Ordinal) == 0)
 396        {
 397            if (_context.TryGetValue(attribute[8..], out string v))
 398            {
 399                return v;
 400            }
 401        }
 402        throw new ArgumentOutOfRangeException(attribute);
 403    }
 404
 405    public string getMode()
 406    {
 407        if (_proxy == null)
 408        {
 409            throw new ArgumentOutOfRangeException("mode");
 410        }
 411
 412        if (_proxy.ice_isTwoway())
 413        {
 414            return "twoway";
 415        }
 416        else if (_proxy.ice_isOneway())
 417        {
 418            return "oneway";
 419        }
 420        else if (_proxy.ice_isBatchOneway())
 421        {
 422            return "batch-oneway";
 423        }
 424        else if (_proxy.ice_isDatagram())
 425        {
 426            return "datagram";
 427        }
 428        else if (_proxy.ice_isBatchDatagram())
 429        {
 430            return "batch-datagram";
 431        }
 432        else
 433        {
 434            throw new ArgumentOutOfRangeException("mode");
 435        }
 436    }
 437
 438    public string getId()
 439    {
 440        if (_id == null)
 441        {
 442            if (_proxy != null)
 443            {
 444                var os = new StringBuilder();
 445                try
 446                {
 447                    os.Append(_proxy.ice_endpoints(emptyEndpoints)).Append(" [").Append(_operation).Append(']');
 448                }
 449                catch (Ice.Exception)
 450                {
 451                    // Either a fixed proxy or the communicator is destroyed.
 452                    os.Append(_proxy.ice_getCommunicator().identityToString(_proxy.ice_getIdentity()));
 453                    os.Append(" [").Append(_operation).Append(']');
 454                }
 455                _id = os.ToString();
 456            }
 457            else
 458            {
 459                _id = _operation;
 460            }
 461        }
 462        return _id;
 463    }
 464
 465    public string getParent() => "Communicator";
 466
 467    public Ice.ObjectPrx getProxy() => _proxy;
 468
 469    public string getEncodingVersion() => Protocol.encodingVersionToString(_proxy.ice_getEncodingVersion());
 470
 471    public string getIdentity()
 472    {
 473        if (_proxy != null)
 474        {
 475            return _proxy.ice_getCommunicator().identityToString(_proxy.ice_getIdentity());
 476        }
 477        else
 478        {
 479            return "";
 480        }
 481    }
 482
 483    public string getOperation() => _operation;
 484
 485    private static readonly Ice.Endpoint[] emptyEndpoints = [];
 486    private readonly Ice.ObjectPrx _proxy;
 487    private readonly string _operation;
 488    private readonly Dictionary<string, string> _context;
 489    private string _id;
 490}
 491
 492internal class ThreadHelper : MetricsHelper<ThreadMetrics>
 493{
 494    private class AttributeResolverI : AttributeResolver
 495    {
 496        public AttributeResolverI()
 497        {
 498            try
 499            {
 500                Type cl = typeof(ThreadHelper);
 501                add("parent", cl.GetField("_parent"));
 502                add("id", cl.GetField("_id"));
 503            }
 504            catch (System.Exception)
 505            {
 506                Debug.Assert(false);
 507            }
 508        }
 509    }
 510
 511    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 512
 513    public ThreadHelper(string parent, string id, Ice.Instrumentation.ThreadState state)
 514        : base(_attributes)
 515    {
 516        _parent = parent;
 517        _id = id;
 518        _state = state;
 519    }
 520
 521    public override void initMetrics(ThreadMetrics v)
 522    {
 523        switch (_state)
 524        {
 525            case Ice.Instrumentation.ThreadState.ThreadStateInUseForIO:
 526                ++v.inUseForIO;
 527                break;
 528            case Ice.Instrumentation.ThreadState.ThreadStateInUseForUser:
 529                ++v.inUseForUser;
 530                break;
 531            case Ice.Instrumentation.ThreadState.ThreadStateInUseForOther:
 532                ++v.inUseForOther;
 533                break;
 534            default:
 535                break;
 536        }
 537    }
 538
 539    public readonly string _parent;
 540    public readonly string _id;
 541    private readonly Ice.Instrumentation.ThreadState _state;
 542}
 543
 544internal class EndpointHelper : MetricsHelper<Metrics>
 545{
 546    private class AttributeResolverI : AttributeResolver
 547    {
 548        public AttributeResolverI()
 549        {
 550            try
 551            {
 552                Type cl = typeof(EndpointHelper);
 553                add("parent", cl.GetMethod("getParent"));
 554                add("id", cl.GetMethod("getId"));
 555                AttrsUtil.addEndpointAttributes(this, cl);
 556            }
 557            catch (System.Exception)
 558            {
 559                Debug.Assert(false);
 560            }
 561        }
 562    }
 563
 564    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 565
 566    public EndpointHelper(Ice.Endpoint endpt, string id)
 567        : base(_attributes)
 568    {
 569        _endpoint = endpt;
 570        _id = id;
 571    }
 572
 573    public EndpointHelper(Ice.Endpoint endpt)
 574        : base(_attributes) => _endpoint = endpt;
 575
 576    public Ice.EndpointInfo getEndpointInfo()
 577    {
 578        _endpointInfo ??= _endpoint.getInfo();
 579        return _endpointInfo;
 580    }
 581
 582    public string getParent() => "Communicator";
 583
 584    public string getId()
 585    {
 586        _id ??= _endpoint.ToString();
 587        return _id;
 588    }
 589
 590    public string getEndpoint() => _endpoint.ToString();
 591
 592    private readonly Ice.Endpoint _endpoint;
 593    private string _id;
 594    private Ice.EndpointInfo _endpointInfo;
 595}
 596
 597public class RemoteInvocationHelper : MetricsHelper<RemoteMetrics>
 598{
 599    private class AttributeResolverI : AttributeResolver
 600    {
 601        public AttributeResolverI()
 602        {
 603            try
 604            {
 605                Type cl = typeof(RemoteInvocationHelper);
 606                add("parent", cl.GetMethod("getParent"));
 607                add("id", cl.GetMethod("getId"));
 608                add("requestId", cl.GetMethod("getRequestId"));
 609                AttrsUtil.addConnectionAttributes(this, cl);
 610            }
 611            catch (System.Exception)
 612            {
 613                Debug.Assert(false);
 614            }
 615        }
 616    }
 617
 618    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 619
 620    public RemoteInvocationHelper(Ice.ConnectionInfo con, Ice.Endpoint endpt, int requestId, int size)
 621        : base(_attributes)
 622    {
 623        _connectionInfo = con;
 624        _endpoint = endpt;
 625        _requestId = requestId;
 626        _size = size;
 627    }
 628
 629    public override void initMetrics(RemoteMetrics metrics) => metrics.size += _size;
 630
 631    public string getId()
 632    {
 633        if (_id == null)
 634        {
 635            _id = _endpoint.ToString();
 636            if (_connectionInfo.connectionId != null && _connectionInfo.connectionId.Length > 0)
 637            {
 638                _id += " [" + _connectionInfo.connectionId + "]";
 639            }
 640        }
 641        return _id;
 642    }
 643
 644    public int getRequestId() => _requestId;
 645
 646    public string getParent()
 647    {
 648        if (_connectionInfo.adapterName != null && _connectionInfo.adapterName.Length > 0)
 649        {
 650            return _connectionInfo.adapterName;
 651        }
 652        else
 653        {
 654            return "Communicator";
 655        }
 656    }
 657
 658    public Ice.ConnectionInfo getConnectionInfo() => _connectionInfo;
 659
 660    public Ice.Endpoint getEndpoint() => _endpoint;
 661
 662    public Ice.EndpointInfo getEndpointInfo()
 663    {
 664        _endpointInfo ??= _endpoint.getInfo();
 665        return _endpointInfo;
 666    }
 667
 668    private readonly Ice.ConnectionInfo _connectionInfo;
 669    private readonly Ice.Endpoint _endpoint;
 670    private readonly int _size;
 671    private readonly int _requestId;
 672    private string _id;
 673    private Ice.EndpointInfo _endpointInfo;
 674}
 675
 676public class CollocatedInvocationHelper : MetricsHelper<CollocatedMetrics>
 677{
 678    private class AttributeResolverI : AttributeResolver
 679    {
 680        public AttributeResolverI()
 681        {
 682            try
 683            {
 684                Type cl = typeof(CollocatedInvocationHelper);
 685                add("parent", cl.GetMethod("getParent"));
 686                add("id", cl.GetMethod("getId"));
 687                add("requestId", cl.GetMethod("getRequestId"));
 688            }
 689            catch (System.Exception)
 690            {
 691                Debug.Assert(false);
 692            }
 693        }
 694    }
 695
 696    private static readonly AttributeResolver _attributes = new AttributeResolverI();
 697
 698    public CollocatedInvocationHelper(Ice.ObjectAdapter adapter, int requestId, int size)
 699        : base(_attributes)
 700    {
 701        _id = adapter.getName();
 702        _requestId = requestId;
 703        _size = size;
 704    }
 705
 706    public override void initMetrics(CollocatedMetrics metrics) => metrics.size += _size;
 707
 708    public string getId() => _id;
 709
 710    public int getRequestId() => _requestId;
 711
 712    public string getParent() => "Communicator";
 713
 714    private readonly int _size;
 715    private readonly int _requestId;
 716    private readonly string _id;
 717}
 718
 719public class ObserverWithDelegateI : ObserverWithDelegate<Metrics, Ice.Instrumentation.Observer>
 720{
 721}
 722
 723public class ConnectionObserverI : ObserverWithDelegate<ConnectionMetrics, Ice.Instrumentation.ConnectionObserver>,
 724    Ice.Instrumentation.ConnectionObserver
 725{
 726    public void sentBytes(int num)
 727    {
 728        _sentBytes = num;
 729        forEach(sentBytesUpdate);
 730        delegate_?.sentBytes(num);
 731    }
 732
 733    public void receivedBytes(int num)
 734    {
 735        _receivedBytes = num;
 736        forEach(receivedBytesUpdate);
 737        delegate_?.receivedBytes(num);
 738    }
 739
 740    private void sentBytesUpdate(ConnectionMetrics v) => v.sentBytes += _sentBytes;
 741
 742    private void receivedBytesUpdate(ConnectionMetrics v) => v.receivedBytes += _receivedBytes;
 743
 744    private int _sentBytes;
 745    private int _receivedBytes;
 746}
 747
 748public class DispatchObserverI : ObserverWithDelegate<DispatchMetrics, Ice.Instrumentation.DispatchObserver>,
 749    Ice.Instrumentation.DispatchObserver
 750{
 751    public void
 752    userException()
 753    {
 1754        forEach(userException);
 1755        delegate_?.userException();
 1756    }
 757
 758    public void reply(int size)
 759    {
 1760        forEach((DispatchMetrics v) => v.replySize += size);
 1761        delegate_?.reply(size);
 1762    }
 763
 1764    private void userException(DispatchMetrics v) => ++v.userException;
 765}
 766
 767public class RemoteObserverI : ObserverWithDelegate<RemoteMetrics, Ice.Instrumentation.RemoteObserver>,
 768    Ice.Instrumentation.RemoteObserver
 769{
 770    public void reply(int size)
 771    {
 772        forEach((RemoteMetrics v) => v.replySize += size);
 773        delegate_?.reply(size);
 774    }
 775}
 776
 777public class CollocatedObserverI : ObserverWithDelegate<CollocatedMetrics, Ice.Instrumentation.CollocatedObserver>,
 778    Ice.Instrumentation.CollocatedObserver
 779{
 780    public void reply(int size)
 781    {
 782        forEach((CollocatedMetrics v) => v.replySize += size);
 783        delegate_?.reply(size);
 784    }
 785}
 786
 787public class InvocationObserverI : ObserverWithDelegate<InvocationMetrics, Ice.Instrumentation.InvocationObserver>,
 788    Ice.Instrumentation.InvocationObserver
 789{
 790    public void
 791    userException()
 792    {
 793        forEach(userException);
 794        delegate_?.userException();
 795    }
 796
 797    public void
 798    retried()
 799    {
 800        forEach(incrementRetry);
 801        delegate_?.retried();
 802    }
 803
 804    public Ice.Instrumentation.RemoteObserver getRemoteObserver(
 805        Ice.ConnectionInfo con,
 806        Ice.Endpoint endpt,
 807        int requestId,
 808        int size)
 809    {
 810        Ice.Instrumentation.RemoteObserver del = null;
 811        if (delegate_ != null)
 812        {
 813            del = delegate_.getRemoteObserver(con, endpt, requestId, size);
 814        }
 815        return getObserver<RemoteMetrics, RemoteObserverI,
 816            Ice.Instrumentation.RemoteObserver>(
 817                "Remote",
 818                new RemoteInvocationHelper(con, endpt, requestId, size),
 819                del);
 820    }
 821
 822    public Ice.Instrumentation.CollocatedObserver getCollocatedObserver(
 823        Ice.ObjectAdapter adapter,
 824        int requestId,
 825        int size)
 826    {
 827        Ice.Instrumentation.CollocatedObserver del = null;
 828        if (delegate_ != null)
 829        {
 830            del = delegate_.getCollocatedObserver(adapter, requestId, size);
 831        }
 832        return getObserver<CollocatedMetrics, CollocatedObserverI,
 833            Ice.Instrumentation.CollocatedObserver>(
 834                "Collocated",
 835                new CollocatedInvocationHelper(adapter, requestId, size),
 836                del);
 837    }
 838
 839    private void incrementRetry(InvocationMetrics v) => ++v.retry;
 840
 841    private void userException(InvocationMetrics v) => ++v.userException;
 842}
 843
 844public class ThreadObserverI : ObserverWithDelegate<ThreadMetrics, Ice.Instrumentation.ThreadObserver>,
 845    Ice.Instrumentation.ThreadObserver
 846{
 847    public void stateChanged(Ice.Instrumentation.ThreadState oldState, Ice.Instrumentation.ThreadState newState)
 848    {
 849        _oldState = oldState;
 850        _newState = newState;
 851        forEach(threadStateUpdate);
 852        delegate_?.stateChanged(oldState, newState);
 853    }
 854
 855    private void threadStateUpdate(ThreadMetrics v)
 856    {
 857        switch (_oldState)
 858        {
 859            case Ice.Instrumentation.ThreadState.ThreadStateInUseForIO:
 860                --v.inUseForIO;
 861                break;
 862            case Ice.Instrumentation.ThreadState.ThreadStateInUseForUser:
 863                --v.inUseForUser;
 864                break;
 865            case Ice.Instrumentation.ThreadState.ThreadStateInUseForOther:
 866                --v.inUseForOther;
 867                break;
 868            default:
 869                break;
 870        }
 871        switch (_newState)
 872        {
 873            case Ice.Instrumentation.ThreadState.ThreadStateInUseForIO:
 874                ++v.inUseForIO;
 875                break;
 876            case Ice.Instrumentation.ThreadState.ThreadStateInUseForUser:
 877                ++v.inUseForUser;
 878                break;
 879            case Ice.Instrumentation.ThreadState.ThreadStateInUseForOther:
 880                ++v.inUseForOther;
 881                break;
 882            default:
 883                break;
 884        }
 885    }
 886
 887    private Ice.Instrumentation.ThreadState _oldState;
 888    private Ice.Instrumentation.ThreadState _newState;
 889}
 890
 891public class CommunicatorObserverI : Ice.Instrumentation.CommunicatorObserver
 892{
 893    public CommunicatorObserverI(Ice.InitializationData initData)
 894    {
 895        _metrics = new MetricsAdminI(initData.properties, initData.logger);
 896        _delegate = initData.observer;
 897        _connections = new ObserverFactoryWithDelegate<ConnectionMetrics, ConnectionObserverI,
 898            Ice.Instrumentation.ConnectionObserver>(_metrics, "Connection");
 899        _dispatch = new ObserverFactoryWithDelegate<DispatchMetrics, DispatchObserverI,
 900            Ice.Instrumentation.DispatchObserver>(_metrics, "Dispatch");
 901        _invocations = new ObserverFactoryWithDelegate<InvocationMetrics, InvocationObserverI,
 902            Ice.Instrumentation.InvocationObserver>(_metrics, "Invocation");
 903        _threads = new ObserverFactoryWithDelegate<ThreadMetrics, ThreadObserverI,
 904            Ice.Instrumentation.ThreadObserver>(_metrics, "Thread");
 905        _connects = new ObserverFactoryWithDelegate<Metrics, ObserverWithDelegateI,
 906            Ice.Instrumentation.Observer>(_metrics, "ConnectionEstablishment");
 907        _endpointLookups = new ObserverFactoryWithDelegate<Metrics, ObserverWithDelegateI,
 908            Ice.Instrumentation.Observer>(_metrics, "EndpointLookup");
 909
 910        try
 911        {
 912            Type cl = typeof(InvocationMetrics);
 913            _invocations.registerSubMap<RemoteMetrics>("Remote", cl.GetField("remotes"));
 914            _invocations.registerSubMap<CollocatedMetrics>("Collocated", cl.GetField("collocated"));
 915        }
 916        catch (System.Exception)
 917        {
 918            Debug.Assert(false);
 919        }
 920    }
 921
 922    public Ice.Instrumentation.Observer getConnectionEstablishmentObserver(Ice.Endpoint endpt, string connector)
 923    {
 924        if (_connects.isEnabled())
 925        {
 926            try
 927            {
 928                Ice.Instrumentation.Observer del = null;
 929                if (_delegate != null)
 930                {
 931                    del = _delegate.getConnectionEstablishmentObserver(endpt, connector);
 932                }
 933                return _connects.getObserver(new EndpointHelper(endpt, connector), del);
 934            }
 935            catch (System.Exception ex)
 936            {
 937                _metrics.getLogger().error("unexpected exception trying to obtain observer:\n" + ex);
 938            }
 939        }
 940        return null;
 941    }
 942
 943    public Ice.Instrumentation.Observer getEndpointLookupObserver(Ice.Endpoint endpt)
 944    {
 945        if (_endpointLookups.isEnabled())
 946        {
 947            try
 948            {
 949                Ice.Instrumentation.Observer del = null;
 950                if (_delegate != null)
 951                {
 952                    del = _delegate.getEndpointLookupObserver(endpt);
 953                }
 954                return _endpointLookups.getObserver(new EndpointHelper(endpt), del);
 955            }
 956            catch (System.Exception ex)
 957            {
 958                _metrics.getLogger().error("unexpected exception trying to obtain observer:\n" + ex);
 959            }
 960        }
 961        return null;
 962    }
 963
 964    public Ice.Instrumentation.ConnectionObserver getConnectionObserver(
 965        Ice.ConnectionInfo c,
 966        Ice.Endpoint e,
 967        Ice.Instrumentation.ConnectionState s,
 968        Ice.Instrumentation.ConnectionObserver o)
 969    {
 970        if (_connections.isEnabled())
 971        {
 972            try
 973            {
 974                Ice.Instrumentation.ConnectionObserver del = null;
 975                ConnectionObserverI obsv = o is ConnectionObserverI ? (ConnectionObserverI)o : null;
 976                if (_delegate != null)
 977                {
 978                    del = _delegate.getConnectionObserver(c, e, s, obsv is not null ? obsv.getDelegate() : o);
 979                }
 980                return _connections.getObserver(new ConnectionHelper(c, e, s), o, del);
 981            }
 982            catch (System.Exception ex)
 983            {
 984                _metrics.getLogger().error("unexpected exception trying to obtain observer:\n" + ex);
 985            }
 986        }
 987        return null;
 988    }
 989
 990    public Ice.Instrumentation.ThreadObserver getThreadObserver(
 991        string parent,
 992        string id,
 993        Ice.Instrumentation.ThreadState s,
 994        Ice.Instrumentation.ThreadObserver o)
 995    {
 996        if (_threads.isEnabled())
 997        {
 998            try
 999            {
 1000                Ice.Instrumentation.ThreadObserver del = null;
 1001                ThreadObserverI obsv = o is ThreadObserverI ? (ThreadObserverI)o : null;
 1002                if (_delegate != null)
 1003                {
 1004                    del = _delegate.getThreadObserver(parent, id, s, obsv is not null ? obsv.getDelegate() : o);
 1005                }
 1006                return _threads.getObserver(new ThreadHelper(parent, id, s), o, del);
 1007            }
 1008            catch (System.Exception ex)
 1009            {
 1010                _metrics.getLogger().error("unexpected exception trying to obtain observer:\n" + ex);
 1011            }
 1012        }
 1013        return null;
 1014    }
 1015
 1016    public Ice.Instrumentation.InvocationObserver getInvocationObserver(
 1017        Ice.ObjectPrx prx,
 1018        string operation,
 1019        Dictionary<string, string> ctx)
 1020    {
 1021        if (_invocations.isEnabled())
 1022        {
 1023            try
 1024            {
 1025                Ice.Instrumentation.InvocationObserver del = null;
 1026                if (_delegate != null)
 1027                {
 1028                    del = _delegate.getInvocationObserver(prx, operation, ctx);
 1029                }
 1030                return _invocations.getObserver(new InvocationHelper(prx, operation, ctx), del);
 1031            }
 1032            catch (System.Exception ex)
 1033            {
 1034                _metrics.getLogger().error("unexpected exception trying to obtain observer:\n" + ex);
 1035            }
 1036        }
 1037        return null;
 1038    }
 1039
 1040    public Ice.Instrumentation.DispatchObserver getDispatchObserver(Ice.Current c, int size)
 1041    {
 1042        if (_dispatch.isEnabled())
 1043        {
 1044            try
 1045            {
 1046                Ice.Instrumentation.DispatchObserver del = null;
 1047                if (_delegate != null)
 1048                {
 1049                    del = _delegate.getDispatchObserver(c, size);
 1050                }
 1051                return _dispatch.getObserver(new DispatchHelper(c, size), del);
 1052            }
 1053            catch (System.Exception ex)
 1054            {
 1055                _metrics.getLogger().error("unexpected exception trying to obtain observer:\n" + ex);
 1056            }
 1057        }
 1058        return null;
 1059    }
 1060
 1061    public void setObserverUpdater(Ice.Instrumentation.ObserverUpdater updater)
 1062    {
 1063        if (updater == null)
 1064        {
 1065            _connections.setUpdater(null);
 1066            _threads.setUpdater(null);
 1067        }
 1068        else
 1069        {
 1070            _connections.setUpdater(updater.updateConnectionObservers);
 1071            _threads.setUpdater(updater.updateThreadObservers);
 1072        }
 1073        _delegate?.setObserverUpdater(updater);
 1074    }
 1075
 1076    public MetricsAdminI getFacet() => _metrics;
 1077
 1078    private readonly MetricsAdminI _metrics;
 1079    private readonly Ice.Instrumentation.CommunicatorObserver _delegate;
 1080
 1081    private readonly ObserverFactoryWithDelegate<ConnectionMetrics, ConnectionObserverI,
 1082        Ice.Instrumentation.ConnectionObserver> _connections;
 1083
 1084    private readonly ObserverFactoryWithDelegate<DispatchMetrics, DispatchObserverI,
 1085        Ice.Instrumentation.DispatchObserver> _dispatch;
 1086
 1087    private readonly ObserverFactoryWithDelegate<InvocationMetrics, InvocationObserverI,
 1088        Ice.Instrumentation.InvocationObserver> _invocations;
 1089
 1090    private readonly ObserverFactoryWithDelegate<ThreadMetrics, ThreadObserverI,
 1091        Ice.Instrumentation.ThreadObserver> _threads;
 1092
 1093    private readonly ObserverFactoryWithDelegate<Metrics, ObserverWithDelegateI,
 1094        Ice.Instrumentation.Observer> _connects;
 1095
 1096    private readonly ObserverFactoryWithDelegate<Metrics, ObserverWithDelegateI,
 1097        Ice.Instrumentation.Observer> _endpointLookups;
 1098}