Skip to content

Commit 67f43a3

Browse files
authored
Merge pull request #666 from hjgraca/fix(metrics)-dimessions-with-missing-array
chore: FIx Metrics - Add missing array to wrap metrics array
2 parents b7d3774 + af14147 commit 67f43a3

File tree

11 files changed

+265
-98
lines changed

11 files changed

+265
-98
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ AWS.Lambda.Powertools.sln.DotSettings.user
1919
[Oo]bj/**
2020
[Bb]in/**
2121
.DS_Store
22+
.cache
2223

2324
dist/
2425
site/

libraries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs

+50-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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+
}

libraries/src/AWS.Lambda.Powertools.Metrics/MetricsAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace AWS.Lambda.Powertools.Metrics;
6666
/// </listheader>
6767
/// <item>
6868
/// <term>Service</term>
69-
/// <description>string, service name is used for metric dimension across all metrics, by default service_undefined</description>
69+
/// <description>string, service name is used for metric dimension</description>
7070
/// </item>
7171
/// <item>
7272
/// <term>Namespace</term>

libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricDirective.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private MetricDirective(string nameSpace, List<MetricDefinition> metrics, List<D
105105
/// </summary>
106106
/// <value>All dimension keys.</value>
107107
[JsonPropertyName("Dimensions")]
108-
public List<string> AllDimensionKeys
108+
public List<List<string>> AllDimensionKeys
109109
{
110110
get
111111
{
@@ -123,7 +123,8 @@ public List<string> AllDimensionKeys
123123

124124
if (defaultKeys.Count == 0) defaultKeys = new List<string>();
125125

126-
return defaultKeys;
126+
// Wrap the list of strings in another list
127+
return new List<List<string>> { defaultKeys };
127128
}
128129
}
129130

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/ClearDimensionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void WhenClearAllDimensions_NoDimensionsInOutput()
2222
var metricsOutput = consoleOut.ToString();
2323

2424
// Assert
25-
Assert.Contains("{\"Namespace\":\"dotnet-powertools-test\",\"Metrics\":[{\"Name\":\"Metric Name\",\"Unit\":\"Count\"}],\"Dimensions\":[]", metricsOutput);
25+
Assert.Contains("{\"Namespace\":\"dotnet-powertools-test\",\"Metrics\":[{\"Name\":\"Metric Name\",\"Unit\":\"Count\"}],\"Dimensions\":[[]]", metricsOutput);
2626

2727
// Reset
2828
MetricsAspect.ResetForTest();

0 commit comments

Comments
 (0)