@@ -25,13 +25,13 @@ namespace AWS.Lambda.Powertools.Metrics;
25
25
/// Implements the <see cref="IMetrics" />
26
26
/// </summary>
27
27
/// <seealso cref="IMetrics" />
28
- public class Metrics : IMetrics
28
+ public class Metrics : IMetrics , IDisposable
29
29
{
30
30
/// <summary>
31
31
/// The instance
32
32
/// </summary>
33
33
private static IMetrics _instance ;
34
-
34
+
35
35
/// <summary>
36
36
/// The context
37
37
/// </summary>
@@ -65,15 +65,15 @@ public class Metrics : IMetrics
65
65
internal Metrics ( IPowertoolsConfigurations powertoolsConfigurations , string nameSpace = null , string service = null ,
66
66
bool raiseOnEmptyMetrics = false , bool captureColdStartEnabled = false )
67
67
{
68
- if ( _instance != null ) return ;
68
+ _instance ??= this ;
69
69
70
- _instance = this ;
71
70
_powertoolsConfigurations = powertoolsConfigurations ;
72
71
_raiseOnEmptyMetrics = raiseOnEmptyMetrics ;
73
72
_captureColdStartEnabled = captureColdStartEnabled ;
74
73
_context = InitializeContext ( nameSpace , service , null ) ;
75
74
76
75
_powertoolsConfigurations . SetExecutionEnvironment ( this ) ;
76
+
77
77
}
78
78
79
79
/// <summary>
@@ -91,11 +91,11 @@ void IMetrics.AddMetric(string key, double value, MetricUnit unit, MetricResolut
91
91
{
92
92
if ( string . IsNullOrWhiteSpace ( key ) )
93
93
throw new ArgumentNullException (
94
- $ "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.") ;
94
+ nameof ( key ) , "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." ) ;
95
95
96
96
if ( value < 0 ) {
97
97
throw new ArgumentException (
98
- "'AddMetric' method requires a valid metrics value. Value must be >= 0." ) ;
98
+ "'AddMetric' method requires a valid metrics value. Value must be >= 0." , nameof ( value ) ) ;
99
99
}
100
100
101
101
var metrics = _context . GetMetrics ( ) ;
@@ -150,8 +150,8 @@ string IMetrics.GetService()
150
150
void IMetrics . AddDimension ( string key , string value )
151
151
{
152
152
if ( string . IsNullOrWhiteSpace ( key ) )
153
- throw new ArgumentNullException (
154
- $ "'AddDimension' method requires a valid dimension key. 'Null' or empty values are not allowed.") ;
153
+ throw new ArgumentNullException ( nameof ( key ) ,
154
+ "'AddDimension' method requires a valid dimension key. 'Null' or empty values are not allowed." ) ;
155
155
156
156
_context . AddDimension ( key , value ) ;
157
157
}
@@ -168,28 +168,28 @@ void IMetrics.AddDimension(string key, string value)
168
168
void IMetrics . AddMetadata ( string key , object value )
169
169
{
170
170
if ( string . IsNullOrWhiteSpace ( key ) )
171
- throw new ArgumentNullException (
172
- $ "'AddMetadata' method requires a valid metadata key. 'Null' or empty values are not allowed.") ;
171
+ throw new ArgumentNullException ( nameof ( key ) ,
172
+ "'AddMetadata' method requires a valid metadata key. 'Null' or empty values are not allowed." ) ;
173
173
174
174
_context . AddMetadata ( key , value ) ;
175
175
}
176
176
177
177
/// <summary>
178
178
/// Implements interface that sets default dimension list
179
179
/// </summary>
180
- /// <param name="defaultDimensions ">Default Dimension List</param>
180
+ /// <param name="defaultDimension ">Default Dimension List</param>
181
181
/// <exception cref="System.ArgumentNullException">
182
182
/// 'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty
183
183
/// values are not allowed.
184
184
/// </exception>
185
- void IMetrics . SetDefaultDimensions ( Dictionary < string , string > defaultDimensions )
185
+ void IMetrics . SetDefaultDimensions ( Dictionary < string , string > defaultDimension )
186
186
{
187
- foreach ( var item in defaultDimensions )
187
+ foreach ( var item in defaultDimension )
188
188
if ( string . IsNullOrWhiteSpace ( item . Key ) || string . IsNullOrWhiteSpace ( item . Value ) )
189
- throw new ArgumentNullException (
190
- $ "'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty values are not allowed.") ;
189
+ throw new ArgumentNullException ( nameof ( item . Key ) ,
190
+ "'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty values are not allowed." ) ;
191
191
192
- _context . SetDefaultDimensions ( DictionaryToList ( defaultDimensions ) ) ;
192
+ _context . SetDefaultDimensions ( DictionaryToList ( defaultDimension ) ) ;
193
193
}
194
194
195
195
/// <summary>
@@ -258,8 +258,8 @@ void IMetrics.PushSingleMetric(string metricName, double value, MetricUnit unit,
258
258
Dictionary < string , string > defaultDimensions , MetricResolution metricResolution )
259
259
{
260
260
if ( string . IsNullOrWhiteSpace ( metricName ) )
261
- throw new ArgumentNullException (
262
- $ "'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.") ;
261
+ throw new ArgumentNullException ( nameof ( metricName ) ,
262
+ "'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." ) ;
263
263
264
264
using var context = InitializeContext ( nameSpace , service , defaultDimensions ) ;
265
265
context . AddMetric ( metricName , value , unit , metricResolution ) ;
@@ -272,7 +272,21 @@ void IMetrics.PushSingleMetric(string metricName, double value, MetricUnit unit,
272
272
/// </summary>
273
273
public void Dispose ( )
274
274
{
275
- _instance . Flush ( ) ;
275
+ Dispose ( true ) ;
276
+ GC . SuppressFinalize ( this ) ;
277
+ }
278
+
279
+ /// <summary>
280
+ ///
281
+ /// </summary>
282
+ /// <param name="disposing"></param>
283
+ protected virtual void Dispose ( bool disposing )
284
+ {
285
+ // Cleanup
286
+ if ( disposing )
287
+ {
288
+ _instance . Flush ( ) ;
289
+ }
276
290
}
277
291
278
292
/// <summary>
0 commit comments