Skip to content

chore: Feature/metrics default dimensions coldstart #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 56 additions & 51 deletions libraries/src/AWS.Lambda.Powertools.Metrics/IMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,104 +1,109 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*
* http://aws.amazon.com/apache2.0
*
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;

namespace AWS.Lambda.Powertools.Metrics;

/// <summary>
/// Interface IMetrics
/// Implements the <see cref="System.IDisposable" />
/// Interface for metrics operations.
/// </summary>
/// <seealso cref="System.IDisposable" />
public interface IMetrics
public interface IMetrics
{
/// <summary>
/// Adds metric
/// Adds a metric to the collection.
/// </summary>
/// <param name="key">Metric key</param>
/// <param name="value">Metric value</param>
/// <param name="unit">Metric unit</param>
/// <param name="metricResolution"></param>
void AddMetric(string key, double value, MetricUnit unit, MetricResolution metricResolution);
/// <param name="key">The metric key.</param>
/// <param name="value">The metric value.</param>
/// <param name="unit">The metric unit.</param>
/// <param name="resolution">The metric resolution.</param>
void AddMetric(string key, double value, MetricUnit unit = MetricUnit.None,
MetricResolution resolution = MetricResolution.Default);

/// <summary>
/// Adds a dimension
/// Adds a dimension to the collection.
/// </summary>
/// <param name="key">Dimension key</param>
/// <param name="value">Dimension value</param>
/// <param name="key">The dimension key.</param>
/// <param name="value">The dimension value.</param>
void AddDimension(string key, string value);

/// <summary>
/// Sets the default dimensions
/// Adds metadata to the collection.
/// </summary>
/// <param name="defaultDimension">Default dimensions</param>
void SetDefaultDimensions(Dictionary<string, string> defaultDimension);
/// <param name="key">The metadata key.</param>
/// <param name="value">The metadata value.</param>
void AddMetadata(string key, object value);

/// <summary>
/// Adds metadata
/// Sets the default dimensions.
/// </summary>
/// <param name="key">Metadata key</param>
/// <param name="value">Metadata value</param>
void AddMetadata(string key, object value);
/// <param name="defaultDimensions">The default dimensions.</param>
void SetDefaultDimensions(Dictionary<string, string> defaultDimensions);

/// <summary>
/// Pushes a single metric with custom namespace, service and dimensions.
/// Sets the namespace for the metrics.
/// </summary>
/// <param name="metricName">Name of the metric</param>
/// <param name="value">Metric value</param>
/// <param name="unit">Metric unit</param>
/// <param name="nameSpace">Metric namespace</param>
/// <param name="service">Metric service</param>
/// <param name="defaultDimensions">Metric default dimensions</param>
/// <param name="metricResolution">Metrics resolution</param>
void PushSingleMetric(string metricName, double value, MetricUnit unit, string nameSpace = null,
string service = null, Dictionary<string, string> defaultDimensions = null, MetricResolution metricResolution = MetricResolution.Default);
/// <param name="nameSpace">The namespace.</param>
void SetNamespace(string nameSpace);

/// <summary>
/// Sets the namespace
/// Sets the service name for the metrics.
/// </summary>
/// <param name="nameSpace">Metrics namespace</param>
void SetNamespace(string nameSpace);
/// <param name="service">The service name.</param>
void SetService(string service);

/// <summary>
/// Sets whether to raise an event on empty metrics.
/// </summary>
/// <param name="raiseOnEmptyMetrics">If set to <c>true</c>, raises an event on empty metrics.</param>
void SetRaiseOnEmptyMetrics(bool raiseOnEmptyMetrics);

/// <summary>
/// Gets the namespace
/// Sets whether to capture cold start metrics.
/// </summary>
/// <returns>System.String.</returns>
string GetNamespace();
/// <param name="captureColdStart">If set to <c>true</c>, captures cold start metrics.</param>
void SetCaptureColdStart(bool captureColdStart);

/// <summary>
/// Gets the service
/// Pushes a single metric to the collection.
/// </summary>
/// <returns>System.String.</returns>
string GetService();
/// <param name="name">The metric name.</param>
/// <param name="value">The metric value.</param>
/// <param name="unit">The metric unit.</param>
/// <param name="nameSpace">The namespace.</param>
/// <param name="service">The service name.</param>
/// <param name="defaultDimensions">The default dimensions.</param>
/// <param name="resolution">The metric resolution.</param>
void PushSingleMetric(string name, double value, MetricUnit unit, string nameSpace = null, string service = null,
Dictionary<string, string> defaultDimensions = null, MetricResolution resolution = MetricResolution.Default);

/// <summary>
/// Serializes metrics instance
/// Clears the default dimensions.
/// </summary>
/// <returns>System.String.</returns>
string Serialize();
void ClearDefaultDimensions();

/// <summary>
/// Flushes metrics to CloudWatch
/// Flushes the metrics.
/// </summary>
/// <param name="metricsOverflow">if set to <c>true</c> [metrics overflow].</param>
/// <param name="metricsOverflow">If set to <c>true</c>, indicates a metrics overflow.</param>
void Flush(bool metricsOverflow = false);

/// <summary>
/// Clears both default dimensions and dimensions lists
/// Gets the metrics options.
/// </summary>
void ClearDefaultDimensions();
}
/// <value>The metrics options.</value>
public MetricsOptions Options { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ public void Before(

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

_metricsInstance ??= new Metrics(
PowertoolsConfigurations.Instance,
trigger.Namespace,
trigger.Service,
trigger.RaiseOnEmptyMetrics,
trigger.CaptureColdStart
);
_metricsInstance ??= Metrics.Configure(options => {
options.Namespace = trigger.Namespace;
options.Service = trigger.Service;
options.RaiseOnEmptyMetrics = trigger.IsRaiseOnEmptyMetricsSet ? trigger.RaiseOnEmptyMetrics : null;
options.CaptureColdStart = trigger.IsCaptureColdStartSet ? trigger.CaptureColdStart : null;
});

var eventArgs = new AspectEventArgs
{
Expand All @@ -89,31 +88,27 @@ public void Before(
Triggers = triggers
};

if (trigger.CaptureColdStart && _isColdStart)
if (_metricsInstance.Options.CaptureColdStart != null && _metricsInstance.Options.CaptureColdStart.Value && _isColdStart)
{
var defaultDimensions = _metricsInstance.Options?.DefaultDimensions;
_isColdStart = false;

var nameSpace = _metricsInstance.GetNamespace();
var service = _metricsInstance.GetService();
Dictionary<string, string> dimensions = null;

var context = GetContext(eventArgs);

if (context is not null)
{
dimensions = new Dictionary<string, string>
{
{ "FunctionName", context.FunctionName }
};
defaultDimensions ??= new Dictionary<string, string>();
defaultDimensions.Add("FunctionName", context.FunctionName);
_metricsInstance.SetDefaultDimensions(defaultDimensions);
}

_metricsInstance.PushSingleMetric(
"ColdStart",
1.0,
MetricUnit.Count,
nameSpace,
service,
dimensions
_metricsInstance.Options?.Namespace ?? "",
_metricsInstance.Options?.Service ?? "",
defaultDimensions
);
}
}
Expand All @@ -137,7 +132,7 @@ internal static void ResetForTest()
_isColdStart = true;
Metrics.ResetForTest();
}

/// <summary>
/// Gets the Lambda context
/// </summary>
Expand Down
Loading
Loading