Skip to content

Commit b0d8807

Browse files
authored
Merge pull request #771 from hjgraca/feature/metrics-default-dimensions-coldstart
chore: Feature/metrics default dimensions coldstart
2 parents 4b879c9 + 518b874 commit b0d8807

File tree

15 files changed

+950
-282
lines changed

15 files changed

+950
-282
lines changed
Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,109 @@
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
1313
* permissions and limitations under the License.
1414
*/
1515

16-
using System;
1716
using System.Collections.Generic;
1817

1918
namespace AWS.Lambda.Powertools.Metrics;
2019

2120
/// <summary>
22-
/// Interface IMetrics
23-
/// Implements the <see cref="System.IDisposable" />
21+
/// Interface for metrics operations.
2422
/// </summary>
2523
/// <seealso cref="System.IDisposable" />
26-
public interface IMetrics
24+
public interface IMetrics
2725
{
2826
/// <summary>
29-
/// Adds metric
27+
/// Adds a metric to the collection.
3028
/// </summary>
31-
/// <param name="key">Metric key</param>
32-
/// <param name="value">Metric value</param>
33-
/// <param name="unit">Metric unit</param>
34-
/// <param name="metricResolution"></param>
35-
void AddMetric(string key, double value, MetricUnit unit, MetricResolution metricResolution);
29+
/// <param name="key">The metric key.</param>
30+
/// <param name="value">The metric value.</param>
31+
/// <param name="unit">The metric unit.</param>
32+
/// <param name="resolution">The metric resolution.</param>
33+
void AddMetric(string key, double value, MetricUnit unit = MetricUnit.None,
34+
MetricResolution resolution = MetricResolution.Default);
3635

3736
/// <summary>
38-
/// Adds a dimension
37+
/// Adds a dimension to the collection.
3938
/// </summary>
40-
/// <param name="key">Dimension key</param>
41-
/// <param name="value">Dimension value</param>
39+
/// <param name="key">The dimension key.</param>
40+
/// <param name="value">The dimension value.</param>
4241
void AddDimension(string key, string value);
4342

4443
/// <summary>
45-
/// Sets the default dimensions
44+
/// Adds metadata to the collection.
4645
/// </summary>
47-
/// <param name="defaultDimension">Default dimensions</param>
48-
void SetDefaultDimensions(Dictionary<string, string> defaultDimension);
46+
/// <param name="key">The metadata key.</param>
47+
/// <param name="value">The metadata value.</param>
48+
void AddMetadata(string key, object value);
4949

5050
/// <summary>
51-
/// Adds metadata
51+
/// Sets the default dimensions.
5252
/// </summary>
53-
/// <param name="key">Metadata key</param>
54-
/// <param name="value">Metadata value</param>
55-
void AddMetadata(string key, object value);
53+
/// <param name="defaultDimensions">The default dimensions.</param>
54+
void SetDefaultDimensions(Dictionary<string, string> defaultDimensions);
5655

5756
/// <summary>
58-
/// Pushes a single metric with custom namespace, service and dimensions.
57+
/// Sets the namespace for the metrics.
5958
/// </summary>
60-
/// <param name="metricName">Name of the metric</param>
61-
/// <param name="value">Metric value</param>
62-
/// <param name="unit">Metric unit</param>
63-
/// <param name="nameSpace">Metric namespace</param>
64-
/// <param name="service">Metric service</param>
65-
/// <param name="defaultDimensions">Metric default dimensions</param>
66-
/// <param name="metricResolution">Metrics resolution</param>
67-
void PushSingleMetric(string metricName, double value, MetricUnit unit, string nameSpace = null,
68-
string service = null, Dictionary<string, string> defaultDimensions = null, MetricResolution metricResolution = MetricResolution.Default);
59+
/// <param name="nameSpace">The namespace.</param>
60+
void SetNamespace(string nameSpace);
6961

7062
/// <summary>
71-
/// Sets the namespace
63+
/// Sets the service name for the metrics.
7264
/// </summary>
73-
/// <param name="nameSpace">Metrics namespace</param>
74-
void SetNamespace(string nameSpace);
65+
/// <param name="service">The service name.</param>
66+
void SetService(string service);
67+
68+
/// <summary>
69+
/// Sets whether to raise an event on empty metrics.
70+
/// </summary>
71+
/// <param name="raiseOnEmptyMetrics">If set to <c>true</c>, raises an event on empty metrics.</param>
72+
void SetRaiseOnEmptyMetrics(bool raiseOnEmptyMetrics);
7573

7674
/// <summary>
77-
/// Gets the namespace
75+
/// Sets whether to capture cold start metrics.
7876
/// </summary>
79-
/// <returns>System.String.</returns>
80-
string GetNamespace();
77+
/// <param name="captureColdStart">If set to <c>true</c>, captures cold start metrics.</param>
78+
void SetCaptureColdStart(bool captureColdStart);
8179

8280
/// <summary>
83-
/// Gets the service
81+
/// Pushes a single metric to the collection.
8482
/// </summary>
85-
/// <returns>System.String.</returns>
86-
string GetService();
83+
/// <param name="name">The metric name.</param>
84+
/// <param name="value">The metric value.</param>
85+
/// <param name="unit">The metric unit.</param>
86+
/// <param name="nameSpace">The namespace.</param>
87+
/// <param name="service">The service name.</param>
88+
/// <param name="defaultDimensions">The default dimensions.</param>
89+
/// <param name="resolution">The metric resolution.</param>
90+
void PushSingleMetric(string name, double value, MetricUnit unit, string nameSpace = null, string service = null,
91+
Dictionary<string, string> defaultDimensions = null, MetricResolution resolution = MetricResolution.Default);
8792

8893
/// <summary>
89-
/// Serializes metrics instance
94+
/// Clears the default dimensions.
9095
/// </summary>
91-
/// <returns>System.String.</returns>
92-
string Serialize();
96+
void ClearDefaultDimensions();
9397

9498
/// <summary>
95-
/// Flushes metrics to CloudWatch
99+
/// Flushes the metrics.
96100
/// </summary>
97-
/// <param name="metricsOverflow">if set to <c>true</c> [metrics overflow].</param>
101+
/// <param name="metricsOverflow">If set to <c>true</c>, indicates a metrics overflow.</param>
98102
void Flush(bool metricsOverflow = false);
99-
103+
100104
/// <summary>
101-
/// Clears both default dimensions and dimensions lists
105+
/// Gets the metrics options.
102106
/// </summary>
103-
void ClearDefaultDimensions();
104-
}
107+
/// <value>The metrics options.</value>
108+
public MetricsOptions Options { get; }
109+
}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ public void Before(
7070

7171
var trigger = triggers.OfType<MetricsAttribute>().First();
7272

73-
_metricsInstance ??= new Metrics(
74-
PowertoolsConfigurations.Instance,
75-
trigger.Namespace,
76-
trigger.Service,
77-
trigger.RaiseOnEmptyMetrics,
78-
trigger.CaptureColdStart
79-
);
73+
_metricsInstance ??= Metrics.Configure(options => {
74+
options.Namespace = trigger.Namespace;
75+
options.Service = trigger.Service;
76+
options.RaiseOnEmptyMetrics = trigger.IsRaiseOnEmptyMetricsSet ? trigger.RaiseOnEmptyMetrics : null;
77+
options.CaptureColdStart = trigger.IsCaptureColdStartSet ? trigger.CaptureColdStart : null;
78+
});
8079

8180
var eventArgs = new AspectEventArgs
8281
{
@@ -89,31 +88,27 @@ public void Before(
8988
Triggers = triggers
9089
};
9190

92-
if (trigger.CaptureColdStart && _isColdStart)
91+
if (_metricsInstance.Options.CaptureColdStart != null && _metricsInstance.Options.CaptureColdStart.Value && _isColdStart)
9392
{
93+
var defaultDimensions = _metricsInstance.Options?.DefaultDimensions;
9494
_isColdStart = false;
9595

96-
var nameSpace = _metricsInstance.GetNamespace();
97-
var service = _metricsInstance.GetService();
98-
Dictionary<string, string> dimensions = null;
99-
10096
var context = GetContext(eventArgs);
101-
97+
10298
if (context is not null)
10399
{
104-
dimensions = new Dictionary<string, string>
105-
{
106-
{ "FunctionName", context.FunctionName }
107-
};
100+
defaultDimensions ??= new Dictionary<string, string>();
101+
defaultDimensions.Add("FunctionName", context.FunctionName);
102+
_metricsInstance.SetDefaultDimensions(defaultDimensions);
108103
}
109104

110105
_metricsInstance.PushSingleMetric(
111106
"ColdStart",
112107
1.0,
113108
MetricUnit.Count,
114-
nameSpace,
115-
service,
116-
dimensions
109+
_metricsInstance.Options?.Namespace ?? "",
110+
_metricsInstance.Options?.Service ?? "",
111+
defaultDimensions
117112
);
118113
}
119114
}
@@ -137,7 +132,7 @@ internal static void ResetForTest()
137132
_isColdStart = true;
138133
Metrics.ResetForTest();
139134
}
140-
135+
141136
/// <summary>
142137
/// Gets the Lambda context
143138
/// </summary>

0 commit comments

Comments
 (0)