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,9 +76,8 @@ 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
-
82
81
}
83
82
84
83
/// <summary>
@@ -96,18 +95,20 @@ void IMetrics.AddMetric(string key, double value, MetricUnit unit, MetricResolut
96
95
{
97
96
if ( string . IsNullOrWhiteSpace ( key ) )
98
97
throw new ArgumentNullException (
99
- nameof ( key ) , "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." ) ;
100
-
101
- if ( value < 0 ) {
98
+ nameof ( key ) ,
99
+ "'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed." ) ;
100
+
101
+ if ( value < 0 )
102
+ {
102
103
throw new ArgumentException (
103
104
"'AddMetric' method requires a valid metrics value. Value must be >= 0." , nameof ( value ) ) ;
104
105
}
105
106
106
107
lock ( _lockObj )
107
108
{
108
109
var metrics = _context . GetMetrics ( ) ;
109
-
110
- if ( metrics . Count > 0 &&
110
+
111
+ if ( metrics . Count > 0 &&
111
112
( metrics . Count == PowertoolsConfigurations . MaxMetrics ||
112
113
metrics . FirstOrDefault ( x => x . Name == key )
113
114
? . Values . Count == PowertoolsConfigurations . MaxMetrics ) )
@@ -134,7 +135,14 @@ void IMetrics.SetNamespace(string nameSpace)
134
135
/// <returns>Namespace identifier</returns>
135
136
string IMetrics . GetNamespace ( )
136
137
{
137
- return _context . GetNamespace ( ) ;
138
+ try
139
+ {
140
+ return _context . GetNamespace ( ) ;
141
+ }
142
+ catch
143
+ {
144
+ return null ;
145
+ }
138
146
}
139
147
140
148
/// <summary>
@@ -143,7 +151,14 @@ string IMetrics.GetNamespace()
143
151
/// <returns>System.String.</returns>
144
152
string IMetrics . GetService ( )
145
153
{
146
- return _context . GetService ( ) ;
154
+ try
155
+ {
156
+ return _context . GetService ( ) ;
157
+ }
158
+ catch
159
+ {
160
+ return null ;
161
+ }
147
162
}
148
163
149
164
/// <summary>
@@ -226,7 +241,7 @@ void IMetrics.Flush(bool metricsOverflow)
226
241
{
227
242
if ( ! _captureColdStartEnabled )
228
243
Console . WriteLine (
229
- "##WARNING## Metrics and Metadata have not been specified. No data will be sent to Cloudwatch Metrics. " ) ;
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' " ) ;
230
245
}
231
246
}
232
247
@@ -283,7 +298,7 @@ public void Dispose()
283
298
Dispose ( true ) ;
284
299
GC . SuppressFinalize ( this ) ;
285
300
}
286
-
301
+
287
302
/// <summary>
288
303
///
289
304
/// </summary>
@@ -356,7 +371,7 @@ public static void SetDefaultDimensions(Dictionary<string, string> defaultDimens
356
371
{
357
372
_instance . SetDefaultDimensions ( defaultDimensions ) ;
358
373
}
359
-
374
+
360
375
/// <summary>
361
376
/// Clears both default dimensions and dimensions lists
362
377
/// </summary>
@@ -389,14 +404,14 @@ private void Flush(MetricsContext context)
389
404
/// <param name="defaultDimensions">Default dimensions list</param>
390
405
/// <param name="metricResolution">Metrics resolution</param>
391
406
public static void PushSingleMetric ( string metricName , double value , MetricUnit unit , string nameSpace = null ,
392
- string service = null , Dictionary < string , string > defaultDimensions = null , MetricResolution metricResolution = MetricResolution . Default )
407
+ string service = null , Dictionary < string , string > defaultDimensions = null ,
408
+ MetricResolution metricResolution = MetricResolution . Default )
393
409
{
394
410
_instance . PushSingleMetric ( metricName , value , unit , nameSpace , service , defaultDimensions , metricResolution ) ;
395
411
}
396
412
397
413
/// <summary>
398
- /// Sets global namespace, service name and default dimensions list. Service name is automatically added as a default
399
- /// dimension
414
+ /// Sets global namespace, service name and default dimensions list.
400
415
/// </summary>
401
416
/// <param name="nameSpace">Metrics namespace</param>
402
417
/// <param name="service">Service Name</param>
@@ -406,19 +421,26 @@ private MetricsContext InitializeContext(string nameSpace, string service,
406
421
Dictionary < string , string > defaultDimensions )
407
422
{
408
423
var context = new MetricsContext ( ) ;
424
+ var defaultDimensionsList = DictionaryToList ( defaultDimensions ) ;
409
425
410
426
context . SetNamespace ( ! string . IsNullOrWhiteSpace ( nameSpace )
411
427
? nameSpace
412
- : _powertoolsConfigurations . MetricsNamespace ) ;
428
+ : _instance . GetNamespace ( ) ?? _powertoolsConfigurations . MetricsNamespace ) ;
413
429
414
- context . SetService ( ! string . IsNullOrWhiteSpace ( service )
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 )
415
434
? service
416
- : _powertoolsConfigurations . Service ) ;
417
-
418
- var defaultDimensionsList = DictionaryToList ( defaultDimensions ) ;
435
+ : _powertoolsConfigurations . Service == "service_undefined"
436
+ ? null
437
+ : _powertoolsConfigurations . Service ;
419
438
420
- // Add service as a default dimension
421
- defaultDimensionsList . Add ( new DimensionSet ( "Service" , context . GetService ( ) ) ) ;
439
+ if ( parsedService != null )
440
+ {
441
+ context . SetService ( parsedService ) ;
442
+ defaultDimensionsList . Add ( new DimensionSet ( "Service" , context . GetService ( ) ) ) ;
443
+ }
422
444
423
445
context . SetDefaultDimensions ( defaultDimensionsList ) ;
424
446
@@ -447,4 +469,4 @@ internal static void ResetForTest()
447
469
{
448
470
_instance = null ;
449
471
}
450
- }
472
+ }
0 commit comments