@@ -44,24 +44,36 @@ public Dictionary<string, object> MetricData
4444 var targetMembers = new Dictionary < string , object > ( ) ;
4545
4646 // Create snapshots to avoid concurrent modification issues
47- var dimensionsSnapshot = AWS . ExpandAllDimensionSets ( ) ;
48- foreach ( var dimension in dimensionsSnapshot )
49- targetMembers . Add ( dimension . Key , dimension . Value ) ;
47+ var dimensionsSnapshot = AWS ? . ExpandAllDimensionSets ( ) ;
48+ if ( dimensionsSnapshot != null )
49+ {
50+ foreach ( var dimension in dimensionsSnapshot )
51+ targetMembers . Add ( dimension . Key , dimension . Value ) ;
52+ }
5053
51- var metricsSnapshot = new List < MetricDefinition > ( AWS . GetMetrics ( ) ) ;
52- foreach ( var metricDefinition in metricsSnapshot )
54+ var metricsSnapshot = AWS ? . GetMetrics ( ) ;
55+ if ( metricsSnapshot != null )
5356 {
54- List < double > values ;
55- lock ( metricDefinition . Values )
57+ foreach ( var metricDefinition in metricsSnapshot )
5658 {
57- values = new List < double > ( metricDefinition . Values ) ;
59+ if ( metricDefinition ? . Values != null )
60+ {
61+ List < double > values ;
62+ lock ( metricDefinition . Values )
63+ {
64+ values = new List < double > ( metricDefinition . Values ) ;
65+ }
66+ targetMembers . Add ( metricDefinition . Name , values . Count == 1 ? values [ 0 ] : values ) ;
67+ }
5868 }
59- targetMembers . Add ( metricDefinition . Name , values . Count == 1 ? values [ 0 ] : values ) ;
6069 }
6170
62- var metadataSnapshot = new Dictionary < string , object > ( AWS . CustomMetadata ) ;
63- foreach ( var metadata in metadataSnapshot )
64- targetMembers . TryAdd ( metadata . Key , metadata . Value ) ;
71+ var metadataSnapshot = AWS ? . CustomMetadata ;
72+ if ( metadataSnapshot != null )
73+ {
74+ foreach ( var metadata in metadataSnapshot )
75+ targetMembers . TryAdd ( metadata . Key , metadata . Value ) ;
76+ }
6577
6678 return targetMembers ;
6779 }
@@ -90,34 +102,45 @@ private RootNode CreateSerializationSnapshot()
90102 var snapshot = new RootNode ( ) ;
91103
92104 // Copy namespace
93- snapshot . AWS . SetNamespace ( AWS . GetNamespace ( ) ) ;
105+ var namespaceValue = AWS ? . GetNamespace ( ) ;
106+ if ( ! string . IsNullOrWhiteSpace ( namespaceValue ) )
107+ {
108+ snapshot . AWS . SetNamespace ( namespaceValue ) ;
109+ }
94110
95111 // Copy service if set
96- if ( ! string . IsNullOrEmpty ( AWS . GetService ( ) ) )
112+ var serviceValue = AWS ? . GetService ( ) ;
113+ if ( ! string . IsNullOrEmpty ( serviceValue ) )
97114 {
98- snapshot . AWS . SetService ( AWS . GetService ( ) ) ;
115+ snapshot . AWS . SetService ( serviceValue ) ;
99116 }
100117
101118 // Copy metrics with their values
102- var metricsSnapshot = AWS . GetMetrics ( ) ;
103- foreach ( var metric in metricsSnapshot )
119+ var metricsSnapshot = AWS ? . GetMetrics ( ) ;
120+ if ( metricsSnapshot != null )
104121 {
105- List < double > valuesCopy ;
106- lock ( metric . Values )
122+ foreach ( var metric in metricsSnapshot )
107123 {
108- valuesCopy = new List < double > ( metric . Values ) ;
109- }
110-
111- // Add each value individually to ensure proper metric creation
112- foreach ( var value in valuesCopy )
113- {
114- snapshot . AWS . AddMetric ( metric . Name , value , metric . Unit , metric . StorageResolution ) ;
124+ if ( metric ? . Values != null )
125+ {
126+ List < double > valuesCopy ;
127+ lock ( metric . Values )
128+ {
129+ valuesCopy = new List < double > ( metric . Values ) ;
130+ }
131+
132+ // Add each value individually to ensure proper metric creation
133+ foreach ( var value in valuesCopy )
134+ {
135+ snapshot . AWS . AddMetric ( metric . Name , value , metric . Unit , metric . StorageResolution ) ;
136+ }
137+ }
115138 }
116139 }
117140
118141 // Copy dimensions
119- var dimensionsSnapshot = AWS . ExpandAllDimensionSets ( ) ;
120- if ( dimensionsSnapshot . Count > 0 )
142+ var dimensionsSnapshot = AWS ? . ExpandAllDimensionSets ( ) ;
143+ if ( dimensionsSnapshot != null && dimensionsSnapshot . Count > 0 )
121144 {
122145 // Create dimension set with first key-value pair, then add the rest
123146 var firstKvp = dimensionsSnapshot . First ( ) ;
@@ -132,10 +155,14 @@ private RootNode CreateSerializationSnapshot()
132155 }
133156
134157 // Copy custom metadata
135- var metadataSnapshot = new Dictionary < string , object > ( AWS . CustomMetadata ) ;
136- foreach ( var kvp in metadataSnapshot )
158+ var customMetadata = AWS ? . CustomMetadata ;
159+ if ( customMetadata != null )
137160 {
138- snapshot . AWS . AddMetadata ( kvp . Key , kvp . Value ) ;
161+ var metadataSnapshot = new Dictionary < string , object > ( customMetadata ) ;
162+ foreach ( var kvp in metadataSnapshot )
163+ {
164+ snapshot . AWS . AddMetadata ( kvp . Key , kvp . Value ) ;
165+ }
139166 }
140167
141168 return snapshot ;
0 commit comments