11/*
22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3- *
3+ *
44 * Licensed under the Apache License, Version 2.0 (the "License").
55 * You may not use this file except in compliance with the License.
66 * A copy of the License is located at
7- *
7+ *
88 * http://aws.amazon.com/apache2.0
9- *
9+ *
1010 * or in the "license" file accompanying this file. This file is distributed
1111 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212 * express or implied. See the License for the specific language governing
@@ -31,7 +31,7 @@ public class Metrics : IMetrics, IDisposable
3131 /// The instance
3232 /// </summary>
3333 private static IMetrics _instance ;
34-
34+
3535 /// <summary>
3636 /// The context
3737 /// </summary>
@@ -46,7 +46,7 @@ public class Metrics : IMetrics, IDisposable
4646 /// If true, Powertools for AWS Lambda (.NET) will throw an exception on empty metrics when trying to flush
4747 /// </summary>
4848 private readonly bool _raiseOnEmptyMetrics ;
49-
49+
5050 /// <summary>
5151 /// The capture cold start enabled
5252 /// </summary>
@@ -76,9 +76,8 @@ internal Metrics(IPowertoolsConfigurations powertoolsConfigurations, string name
7676 _raiseOnEmptyMetrics = raiseOnEmptyMetrics ;
7777 _captureColdStartEnabled = captureColdStartEnabled ;
7878 _context = InitializeContext ( nameSpace , service , null ) ;
79-
79+
8080 _powertoolsConfigurations . SetExecutionEnvironment ( this ) ;
81-
8281 }
8382
8483 /// <summary>
@@ -96,18 +95,20 @@ void IMetrics.AddMetric(string key, double value, MetricUnit unit, MetricResolut
9695 {
9796 if ( string . IsNullOrWhiteSpace ( key ) )
9897 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+ {
102103 throw new ArgumentException (
103104 "'AddMetric' method requires a valid metrics value. Value must be >= 0." , nameof ( value ) ) ;
104105 }
105106
106107 lock ( _lockObj )
107108 {
108109 var metrics = _context . GetMetrics ( ) ;
109-
110- if ( metrics . Count > 0 &&
110+
111+ if ( metrics . Count > 0 &&
111112 ( metrics . Count == PowertoolsConfigurations . MaxMetrics ||
112113 metrics . FirstOrDefault ( x => x . Name == key )
113114 ? . Values . Count == PowertoolsConfigurations . MaxMetrics ) )
@@ -134,7 +135,14 @@ void IMetrics.SetNamespace(string nameSpace)
134135 /// <returns>Namespace identifier</returns>
135136 string IMetrics . GetNamespace ( )
136137 {
137- return _context . GetNamespace ( ) ;
138+ try
139+ {
140+ return _context . GetNamespace ( ) ;
141+ }
142+ catch
143+ {
144+ return null ;
145+ }
138146 }
139147
140148 /// <summary>
@@ -143,7 +151,14 @@ string IMetrics.GetNamespace()
143151 /// <returns>System.String.</returns>
144152 string IMetrics . GetService ( )
145153 {
146- return _context . GetService ( ) ;
154+ try
155+ {
156+ return _context . GetService ( ) ;
157+ }
158+ catch
159+ {
160+ return null ;
161+ }
147162 }
148163
149164 /// <summary>
@@ -226,7 +241,7 @@ void IMetrics.Flush(bool metricsOverflow)
226241 {
227242 if ( ! _captureColdStartEnabled )
228243 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' " ) ;
230245 }
231246 }
232247
@@ -283,7 +298,7 @@ public void Dispose()
283298 Dispose ( true ) ;
284299 GC . SuppressFinalize ( this ) ;
285300 }
286-
301+
287302 /// <summary>
288303 ///
289304 /// </summary>
@@ -356,7 +371,7 @@ public static void SetDefaultDimensions(Dictionary<string, string> defaultDimens
356371 {
357372 _instance . SetDefaultDimensions ( defaultDimensions ) ;
358373 }
359-
374+
360375 /// <summary>
361376 /// Clears both default dimensions and dimensions lists
362377 /// </summary>
@@ -389,14 +404,14 @@ private void Flush(MetricsContext context)
389404 /// <param name="defaultDimensions">Default dimensions list</param>
390405 /// <param name="metricResolution">Metrics resolution</param>
391406 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 )
393409 {
394410 _instance . PushSingleMetric ( metricName , value , unit , nameSpace , service , defaultDimensions , metricResolution ) ;
395411 }
396412
397413 /// <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.
400415 /// </summary>
401416 /// <param name="nameSpace">Metrics namespace</param>
402417 /// <param name="service">Service Name</param>
@@ -406,19 +421,26 @@ private MetricsContext InitializeContext(string nameSpace, string service,
406421 Dictionary < string , string > defaultDimensions )
407422 {
408423 var context = new MetricsContext ( ) ;
424+ var defaultDimensionsList = DictionaryToList ( defaultDimensions ) ;
409425
410426 context . SetNamespace ( ! string . IsNullOrWhiteSpace ( nameSpace )
411427 ? nameSpace
412- : _powertoolsConfigurations . MetricsNamespace ) ;
428+ : _instance . GetNamespace ( ) ?? _powertoolsConfigurations . MetricsNamespace ) ;
413429
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 )
415434 ? service
416- : _powertoolsConfigurations . Service ) ;
417-
418- var defaultDimensionsList = DictionaryToList ( defaultDimensions ) ;
435+ : _powertoolsConfigurations . Service == "service_undefined"
436+ ? null
437+ : _powertoolsConfigurations . Service ;
419438
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+ }
422444
423445 context . SetDefaultDimensions ( defaultDimensionsList ) ;
424446
@@ -447,4 +469,4 @@ internal static void ResetForTest()
447469 {
448470 _instance = null ;
449471 }
450- }
472+ }
0 commit comments