1
1
/*
2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License").
5
5
* You may not use this file except in compliance with the License.
6
6
* A copy of the License is located at
7
- *
7
+ *
8
8
* http://aws.amazon.com/apache2.0
9
- *
9
+ *
10
10
* or in the "license" file accompanying this file. This file is distributed
11
11
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
12
* express or implied. See the License for the specific language governing
@@ -31,7 +31,7 @@ public class Metrics : IMetrics, IDisposable
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>
@@ -46,7 +46,7 @@ public class Metrics : IMetrics, IDisposable
46
46
/// If true, Powertools for AWS Lambda (.NET) will throw an exception on empty metrics when trying to flush
47
47
/// </summary>
48
48
private readonly bool _raiseOnEmptyMetrics ;
49
-
49
+
50
50
/// <summary>
51
51
/// The capture cold start enabled
52
52
/// </summary>
@@ -76,8 +76,9 @@ internal Metrics(IPowertoolsConfigurations powertoolsConfigurations, string name
76
76
_raiseOnEmptyMetrics = raiseOnEmptyMetrics ;
77
77
_captureColdStartEnabled = captureColdStartEnabled ;
78
78
_context = InitializeContext ( nameSpace , service , null ) ;
79
-
79
+
80
80
_powertoolsConfigurations . SetExecutionEnvironment ( this ) ;
81
+
81
82
}
82
83
83
84
/// <summary>
@@ -95,20 +96,18 @@ void IMetrics.AddMetric(string key, double value, MetricUnit unit, MetricResolut
95
96
{
96
97
if ( string . IsNullOrWhiteSpace ( key ) )
97
98
throw new ArgumentNullException (
98
- nameof ( key ) ,
99
- "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." ) ;
100
-
101
- if ( value < 0 )
102
- {
99
+ nameof ( key ) , "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." ) ;
100
+
101
+ if ( value < 0 ) {
103
102
throw new ArgumentException (
104
103
"'AddMetric' method requires a valid metrics value. Value must be >= 0." , nameof ( value ) ) ;
105
104
}
106
105
107
106
lock ( _lockObj )
108
107
{
109
108
var metrics = _context . GetMetrics ( ) ;
110
-
111
- if ( metrics . Count > 0 &&
109
+
110
+ if ( metrics . Count > 0 &&
112
111
( metrics . Count == PowertoolsConfigurations . MaxMetrics ||
113
112
metrics . FirstOrDefault ( x => x . Name == key )
114
113
? . Values . Count == PowertoolsConfigurations . MaxMetrics ) )
@@ -135,14 +134,7 @@ void IMetrics.SetNamespace(string nameSpace)
135
134
/// <returns>Namespace identifier</returns>
136
135
string IMetrics . GetNamespace ( )
137
136
{
138
- try
139
- {
140
- return _context . GetNamespace ( ) ;
141
- }
142
- catch
143
- {
144
- return null ;
145
- }
137
+ return _context . GetNamespace ( ) ;
146
138
}
147
139
148
140
/// <summary>
@@ -151,14 +143,7 @@ string IMetrics.GetNamespace()
151
143
/// <returns>System.String.</returns>
152
144
string IMetrics . GetService ( )
153
145
{
154
- try
155
- {
156
- return _context . GetService ( ) ;
157
- }
158
- catch
159
- {
160
- return null ;
161
- }
146
+ return _context . GetService ( ) ;
162
147
}
163
148
164
149
/// <summary>
@@ -241,7 +226,7 @@ void IMetrics.Flush(bool metricsOverflow)
241
226
{
242
227
if ( ! _captureColdStartEnabled )
243
228
Console . WriteLine (
244
- "##User- WARNING## No application metrics to publish. The cold-start metric may be published if enabled. If application metrics should never be empty, consider using 'RaiseOnEmptyMetrics = true' " ) ;
229
+ "##WARNING## Metrics and Metadata have not been specified. No data will be sent to Cloudwatch Metrics. " ) ;
245
230
}
246
231
}
247
232
@@ -298,7 +283,7 @@ public void Dispose()
298
283
Dispose ( true ) ;
299
284
GC . SuppressFinalize ( this ) ;
300
285
}
301
-
286
+
302
287
/// <summary>
303
288
///
304
289
/// </summary>
@@ -371,7 +356,7 @@ public static void SetDefaultDimensions(Dictionary<string, string> defaultDimens
371
356
{
372
357
_instance . SetDefaultDimensions ( defaultDimensions ) ;
373
358
}
374
-
359
+
375
360
/// <summary>
376
361
/// Clears both default dimensions and dimensions lists
377
362
/// </summary>
@@ -404,14 +389,14 @@ private void Flush(MetricsContext context)
404
389
/// <param name="defaultDimensions">Default dimensions list</param>
405
390
/// <param name="metricResolution">Metrics resolution</param>
406
391
public static void PushSingleMetric ( string metricName , double value , MetricUnit unit , string nameSpace = null ,
407
- string service = null , Dictionary < string , string > defaultDimensions = null ,
408
- MetricResolution metricResolution = MetricResolution . Default )
392
+ string service = null , Dictionary < string , string > defaultDimensions = null , MetricResolution metricResolution = MetricResolution . Default )
409
393
{
410
394
_instance . PushSingleMetric ( metricName , value , unit , nameSpace , service , defaultDimensions , metricResolution ) ;
411
395
}
412
396
413
397
/// <summary>
414
- /// Sets global namespace, service name and default dimensions list.
398
+ /// Sets global namespace, service name and default dimensions list. Service name is automatically added as a default
399
+ /// dimension
415
400
/// </summary>
416
401
/// <param name="nameSpace">Metrics namespace</param>
417
402
/// <param name="service">Service Name</param>
@@ -421,26 +406,19 @@ private MetricsContext InitializeContext(string nameSpace, string service,
421
406
Dictionary < string , string > defaultDimensions )
422
407
{
423
408
var context = new MetricsContext ( ) ;
424
- var defaultDimensionsList = DictionaryToList ( defaultDimensions ) ;
425
409
426
410
context . SetNamespace ( ! string . IsNullOrWhiteSpace ( nameSpace )
427
411
? nameSpace
428
- : _instance . GetNamespace ( ) ?? _powertoolsConfigurations . MetricsNamespace ) ;
412
+ : _powertoolsConfigurations . MetricsNamespace ) ;
429
413
430
- // this needs to check if service is set through code or env variables
431
- // the default value service_undefined has to be ignored and return null so it is not added as default
432
- // TODO: Check if there is a way to get the default dimensions and if it makes sense
433
- var parsedService = ! string . IsNullOrWhiteSpace ( service )
414
+ context . SetService ( ! string . IsNullOrWhiteSpace ( service )
434
415
? service
435
- : _powertoolsConfigurations . Service == "service_undefined"
436
- ? null
437
- : _powertoolsConfigurations . Service ;
416
+ : _powertoolsConfigurations . Service ) ;
438
417
439
- if ( parsedService != null )
440
- {
441
- context . SetService ( parsedService ) ;
442
- defaultDimensionsList . Add ( new DimensionSet ( "Service" , context . GetService ( ) ) ) ;
443
- }
418
+ var defaultDimensionsList = DictionaryToList ( defaultDimensions ) ;
419
+
420
+ // Add service as a default dimension
421
+ defaultDimensionsList . Add ( new DimensionSet ( "Service" , context . GetService ( ) ) ) ;
444
422
445
423
context . SetDefaultDimensions ( defaultDimensionsList ) ;
446
424
@@ -469,4 +447,4 @@ internal static void ResetForTest()
469
447
{
470
448
_instance = null ;
471
449
}
472
- }
450
+ }
0 commit comments