< Summary

Information
Class: System.Net.Http.Metrics.ConnectionMetrics
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\SocketsHttpHandler\Metrics\ConnectionMetrics.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 55
Coverable lines: 55
Total lines: 91
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
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%
GetStateTag(...)0%220%
ConnectionEstablished()0%220%
ConnectionClosed(...)0%440%
IdleStateChanged(...)0%440%

File(s)

D:\runner\runtime\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 object _protocolVersionTag;
 14        private readonly object _schemeTag;
 15        private readonly object _hostTag;
 16        private readonly object _portTag;
 17        private readonly object? _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 = DiagnosticsHelper.GetBoxedInt32(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", _portTag);
 40
 041            if (_peerAddressTag is not null)
 042            {
 043                tags.Add("network.peer.address", _peerAddressTag);
 044            }
 45
 046            return tags;
 047        }
 48
 049        private static KeyValuePair<string, object?> GetStateTag(bool idle) => new KeyValuePair<string, object?>("http.c
 50
 51        public void ConnectionEstablished()
 052        {
 053            if (_openConnectionsEnabled)
 054            {
 055                _currentlyIdle = true;
 056                TagList tags = GetTags();
 057                tags.Add(GetStateTag(idle: true));
 058                _metrics.OpenConnections.Add(1, tags);
 059            }
 060        }
 61
 62        public void ConnectionClosed(long durationMs)
 063        {
 064            TagList tags = GetTags();
 65
 066            if (_metrics.ConnectionDuration.Enabled)
 067            {
 068                _metrics.ConnectionDuration.Record(durationMs / 1000d, tags);
 069            }
 70
 071            if (_openConnectionsEnabled)
 072            {
 073                tags.Add(GetStateTag(idle: _currentlyIdle));
 074                _metrics.OpenConnections.Add(-1, tags);
 075            }
 076        }
 77
 78        public void IdleStateChanged(bool idle)
 079        {
 080            if (_openConnectionsEnabled && _currentlyIdle != idle)
 081            {
 082                _currentlyIdle = idle;
 083                TagList tags = GetTags();
 084                tags.Add(GetStateTag(idle: !idle));
 085                _metrics.OpenConnections.Add(-1, tags);
 086                tags[tags.Count - 1] = GetStateTag(idle: idle);
 087                _metrics.OpenConnections.Add(1, tags);
 088            }
 089        }
 90    }
 91}