@@ -63,32 +63,36 @@ class _MetricsFeatureId(str, Enum):
6363# Feature Value Constants
6464
6565
66- class _MetricsProtocolVersionValue ( str , Enum ):
67- """Protocol version values for metrics encoding .
66+ def _protocol_version_metrics_value ( protocol ):
67+ """Map protocol version to its single-character metrics value .
6868
69- Maps MQTT protocol versions to their single-character metric representations .
69+ Mapping: MQTT311->3, MQTT5->5 .
7070 """
71- MQTT311 = "3"
72- MQTT5 = "5"
71+ mapping = {
72+ "MQTT311" : "3" ,
73+ "MQTT5" : "5" ,
74+ }
75+ return mapping .get (protocol )
7376
7477
75- class _MetricsSocketImplementationValue ( str , Enum ):
76- """Socket implementation values for metrics encoding .
78+ def _socket_implementation_metrics_value ( ):
79+ """Detect the socket implementation and return its single-character metrics value .
7780
78- Maps the underlying platform socket layer to its metric representation.
79- POSIX covers macOS and Linux; WINSOCK covers Windows.
81+ Mapping: Windows (WINSOCK)->B, all other platforms (POSIX)->A.
8082 """
81- POSIX = "A"
82- WINSOCK = "B"
83+ if sys .platform == "win32" :
84+ return "B"
85+ return "A"
8386
8487
85- class _MetricsHttpProxyTypeValue ( str , Enum ):
86- """HTTP proxy type values for metrics encoding .
88+ def _http_proxy_type_metrics_value ( proxy_options ):
89+ """Map proxy options to the single-character metrics value for proxy type .
8790
88- Indicates whether the proxy connection uses plain HTTP or HTTPS (TLS) .
91+ Mapping: HTTPS (has tls_connection_options)->B, HTTP->A .
8992 """
90- HTTP = "A"
91- HTTPS = "B"
93+ if getattr (proxy_options , 'tls_connection_options' , None ) is not None :
94+ return "B"
95+ return "A"
9296
9397# Mappings from existing enums to metrics values
9498
@@ -200,16 +204,6 @@ def _tls_cipher_preference_metrics_value(pref):
200204 return mapping .get (pref )
201205
202206
203- def _detect_socket_implementation ():
204- """Detect the socket implementation based on the current platform.
205-
206- Returns _MetricsSocketImplementationValue.WINSOCK on Windows,
207- _MetricsSocketImplementationValue.POSIX on all other platforms
208- (macOS, Linux).
209- """
210- if sys .platform == "win32" :
211- return _MetricsSocketImplementationValue .WINSOCK
212- return _MetricsSocketImplementationValue .POSIX
213207
214208
215209# MQTT5 encoding list
@@ -277,18 +271,15 @@ def _get_encoded_feature_list(client_options):
277271 features .append (f"{ _MetricsFeatureId .INBOUND_TOPIC_ALIAS_BEHAVIOR .value } /{ val } " )
278272
279273 # F: protocol_version - MQTT5 always uses client options
280- features .append (f"{ _MetricsFeatureId .PROTOCOL_VERSION .value } /{ _MetricsProtocolVersionValue . MQTT5 . value } " )
274+ features .append (f"{ _MetricsFeatureId .PROTOCOL_VERSION .value } /{ _protocol_version_metrics_value ( ' MQTT5' ) } " )
281275
282276 # G: socket_implementation - Detect based on platform
283- features .append (f"{ _MetricsFeatureId .SOCKET_IMPLEMENTATION .value } /{ _detect_socket_implementation (). value } " )
277+ features .append (f"{ _MetricsFeatureId .SOCKET_IMPLEMENTATION .value } /{ _socket_implementation_metrics_value () } " )
284278
285279 # H: http_proxy_type - Determine based on whether proxy uses TLS
286280 if client_options .http_proxy_options is not None :
287- proxy_type = _MetricsHttpProxyTypeValue .HTTPS if getattr (
288- client_options .http_proxy_options ,
289- 'tls_connection_options' ,
290- None ) is not None else _MetricsHttpProxyTypeValue .HTTP
291- features .append (f"{ _MetricsFeatureId .HTTP_PROXY_TYPE .value } /{ proxy_type .value } " )
281+ val = _http_proxy_type_metrics_value (client_options .http_proxy_options )
282+ features .append (f"{ _MetricsFeatureId .HTTP_PROXY_TYPE .value } /{ val } " )
292283
293284 # I: certificate_source - Would need to be tracked from TLS context setup. This is set at a IoT SDK level
294285
@@ -329,14 +320,13 @@ def _get_encoded_feature_list_mqtt3(proxy_options, tls_ctx=None):
329320 str: The encoded feature list string.
330321 """
331322 features = [
332- f"{ _MetricsFeatureId .PROTOCOL_VERSION .value } /{ _MetricsProtocolVersionValue . MQTT311 . value } " ,
333- f"{ _MetricsFeatureId .SOCKET_IMPLEMENTATION .value } /{ _detect_socket_implementation (). value } "
323+ f"{ _MetricsFeatureId .PROTOCOL_VERSION .value } /{ _protocol_version_metrics_value ( ' MQTT311' ) } " ,
324+ f"{ _MetricsFeatureId .SOCKET_IMPLEMENTATION .value } /{ _socket_implementation_metrics_value () } "
334325 ]
335326 # H: http_proxy_type - Determine based on whether proxy uses TLS
336327 if proxy_options is not None :
337- proxy_type = _MetricsHttpProxyTypeValue .HTTPS if getattr (
338- proxy_options , 'tls_connection_options' , None ) is not None else _MetricsHttpProxyTypeValue .HTTP
339- features .append (f"{ _MetricsFeatureId .HTTP_PROXY_TYPE .value } /{ proxy_type .value } " )
328+ val = _http_proxy_type_metrics_value (proxy_options )
329+ features .append (f"{ _MetricsFeatureId .HTTP_PROXY_TYPE .value } /{ val } " )
340330
341331 # J: tls_cipher_preference - security policy
342332 if tls_ctx is not None :
0 commit comments