< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 57
Coverable lines: 57
Total lines: 94
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
GetTags()0%220%
CreateTagKey(...)100%110%
ConnectionEstablished()0%220%
ConnectionClosed(...)0%440%
IdleStateChanged(...)0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Metrics/ConnectionMetrics.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6
 7namespace System.Net.Http.Metrics
 8{
 9    internal sealed class ConnectionMetrics
 10    {
 11        private readonly SocketsHttpHandlerMetrics _metrics;
 12        private readonly bool _openConnectionsEnabled;
 13        private readonly string _protocolVersionTag;
 14        private readonly string _schemeTag;
 15        private readonly string _hostTag;
 16        private readonly int _portTag;
 17        private readonly string? _peerAddressTag;
 18        private bool _currentlyIdle;
 19
 020        public ConnectionMetrics(SocketsHttpHandlerMetrics metrics, string protocolVersion, string scheme, string host, 
 021        {
 022            _metrics = metrics;
 023            _openConnectionsEnabled = _metrics.OpenConnections.Enabled;
 024            _protocolVersionTag = protocolVersion;
 025            _schemeTag = scheme;
 026            _hostTag = host;
 027            _portTag = port;
 028            _peerAddressTag = peerAddress;
 029        }
 30
 31        // TagList is a huge struct, so we avoid storing it in a field to reduce the amount we allocate on the heap.
 32        private TagList GetTags()
 033        {
 034            TagList tags = default;
 35
 036            tags.Add("network.protocol.version", _protocolVersionTag);
 037            tags.Add("url.scheme", _schemeTag);
 038            tags.Add("server.address", _hostTag);
 039            tags.Add("server.port", DiagnosticsHelper.GetBoxedInt32(_portTag));
 40
 041            if (_peerAddressTag is not null)
 042            {
 043                tags.Add("network.peer.address", _peerAddressTag);
 044            }
 45
 046            return tags;
 047        }
 48
 49        private OpenConnectionsTagKey CreateTagKey(bool idle) =>
 050            new OpenConnectionsTagKey(_protocolVersionTag, _schemeTag, _hostTag, _portTag, idle, _peerAddressTag);
 51
 52        public void ConnectionEstablished()
 053        {
 054            if (_openConnectionsEnabled)
 055            {
 056                lock (this)
 057                {
 058                    _currentlyIdle = true;
 059                    _metrics.OpenConnectionsTracker.Increment(CreateTagKey(idle: true));
 060                }
 061            }
 062        }
 63
 64        public void ConnectionClosed(long durationMs)
 065        {
 066            if (_metrics.ConnectionDuration.Enabled)
 067            {
 068                _metrics.ConnectionDuration.Record(durationMs / 1000d, GetTags());
 069            }
 70
 071            if (_openConnectionsEnabled)
 072            {
 073                lock (this)
 074                {
 075                    _metrics.OpenConnectionsTracker.Decrement(CreateTagKey(idle: _currentlyIdle));
 076                }
 077            }
 078        }
 79
 80        public void IdleStateChanged(bool idle)
 081        {
 082            if (_openConnectionsEnabled && _currentlyIdle != idle)
 083            {
 084                lock (this)
 085                {
 086                    _currentlyIdle = idle;
 087                    _metrics.OpenConnectionsTracker.Decrement(CreateTagKey(idle: !idle));
 088                    _metrics.OpenConnectionsTracker.Increment(CreateTagKey(idle: idle));
 089                }
 090            }
 091        }
 92    }
 93}
 94