Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
using System;
using AWS.Lambda.PowerTools.Aspects;
using AWS.Lambda.PowerTools.Core;

namespace AWS.Lambda.PowerTools.Metrics.Internal
{
internal class MetricsAspectHandler : IMethodAspectHandler
{
private readonly IPowerToolsConfigurations _powerToolsConfigurations;
private readonly IMetrics _metrics;
private readonly bool _captureColdStartEnabled;
private static bool _isColdStart = true;

internal MetricsAspectHandler
(
IMetrics metricsInstance,
bool captureColdStartEnabled,
IPowerToolsConfigurations powerToolsConfigurations
bool captureColdStartEnabled
)
{
_metrics = metricsInstance;
_captureColdStartEnabled = captureColdStartEnabled;
_powerToolsConfigurations = powerToolsConfigurations;
}

public void OnExit(AspectEventArgs eventArgs)
Expand Down
67 changes: 30 additions & 37 deletions libraries/src/AWS.Lambda.PowerTools.Metrics/Metrics.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
using System;
using System.Collections.Generic;
using AWS.Lambda.PowerTools.Core;

namespace AWS.Lambda.PowerTools.Metrics
{
public class Metrics : IMetrics
{
private static IMetrics Instance { get; set; }

private string MetricsNamespace { get; set; }
private string ServiceName { get; set; }
private bool CaptureEmptyMetricsEnabled { get; set; }
private MetricsContext _context;

private static IMetrics _instance;
private readonly MetricsContext _context;
private readonly bool _captureEmptyMetricsEnabled;
private readonly IPowerToolsConfigurations _powerToolsConfigurations;


/// <summary>
/// Creates Metrics with no namespace or service name defined - requires that they are defined after initialization
/// </summary>
public Metrics(string metricsNamespace = null, string serviceName = null, bool captureMetricsEvenIfEmpty = false)
internal Metrics(IPowerToolsConfigurations powerToolsConfigurations, string metricsNamespace = null, string serviceName = null, bool captureMetricsEvenIfEmpty = false)
{
if (Instance == null)
{
MetricsNamespace = metricsNamespace;
ServiceName = serviceName;
CaptureEmptyMetricsEnabled = captureMetricsEvenIfEmpty;

Instance = this;
_context = InitializeContext(MetricsNamespace, ServiceName, null);
}
if (_instance != null) return;
_instance = this;
_powerToolsConfigurations = powerToolsConfigurations;
_captureEmptyMetricsEnabled = captureMetricsEvenIfEmpty;
_context = InitializeContext(metricsNamespace, serviceName, null);
}

/// <summary>
Expand All @@ -39,15 +35,15 @@ void IMetrics.AddMetric(string key, double value, MetricUnit unit)
{
if (_context.GetMetrics().Count == 100)
{
Instance.Flush(true);
_instance.Flush(true);
}

_context.AddMetric(key, value, unit);
}

public static void AddMetric(string key, double value, MetricUnit unit = MetricUnit.NONE)
{
Instance.AddMetric(key, value, unit);
_instance.AddMetric(key, value, unit);
}

void IMetrics.SetNamespace(string metricsNamespace)
Expand All @@ -56,7 +52,7 @@ void IMetrics.SetNamespace(string metricsNamespace)
}

public static void SetNamespace(string metricsNamespace){
Instance.SetNamespace(metricsNamespace);
_instance.SetNamespace(metricsNamespace);
}

string IMetrics.GetNamespace()
Expand All @@ -65,7 +61,7 @@ string IMetrics.GetNamespace()
}

public static string GetNamespace(){
return Instance.GetNamespace();
return _instance.GetNamespace();
}

void IMetrics.AddDimension(string key, string value)
Expand All @@ -74,7 +70,7 @@ void IMetrics.AddDimension(string key, string value)
}

public static void AddDimension(string key, string value){
Instance.AddDimension(key, value);
_instance.AddDimension(key, value);
}

void IMetrics.AddMetadata(string key, dynamic value)
Expand All @@ -83,25 +79,21 @@ void IMetrics.AddMetadata(string key, dynamic value)
}

public static void AddMetadata(string key, dynamic value){
Instance.AddMetadata(key, value);
_instance.AddMetadata(key, value);
}

void IMetrics.SetDefaultDimensions(Dictionary<string, string> defaultDimensions){
_context.SetDefaultDimensions(DictionaryToList(defaultDimensions));
}

public static void SetDefaultDimensions(Dictionary<string, string> defaultDimensions){
Instance.SetDefaultDimensions(defaultDimensions);
}

public static void Flush(){
Instance.Flush(false);
_instance.SetDefaultDimensions(defaultDimensions);
}

void IMetrics.Flush(bool metricsOverflow)
{
if (_context.IsSerializable
|| CaptureEmptyMetricsEnabled)
|| _captureEmptyMetricsEnabled)
{
var emfPayload = _context.Serialize();

Expand Down Expand Up @@ -138,7 +130,7 @@ void IMetrics.PushSingleMetric(string metricName, double value, MetricUnit unit,
}

public static void PushSingleMetric(string metricName, double value, MetricUnit unit, string metricsNamespace = null, string serviceName = null, Dictionary<string, string> defaultDimensions = null){
Instance.PushSingleMetric(metricName, value, unit, metricsNamespace, serviceName, defaultDimensions);
_instance.PushSingleMetric(metricName, value, unit, metricsNamespace, serviceName, defaultDimensions);
}

private MetricsContext InitializeContext(string metricsNamespace, string serviceName, Dictionary<string, string> defaultDimensions)
Expand All @@ -149,17 +141,18 @@ private MetricsContext InitializeContext(string metricsNamespace, string service
{
context.SetNamespace(metricsNamespace);
}
else if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("POWERTOOLS_METRICS_NAMESPACE")))
else if (!string.IsNullOrEmpty(_powerToolsConfigurations.MetricsNamespace))
{
context.SetNamespace(Environment.GetEnvironmentVariable("POWERTOOLS_METRICS_NAMESPACE"));
context.SetNamespace(_powerToolsConfigurations.MetricsNamespace);
}

PowertoolsConfig.Service = serviceName;

if (string.IsNullOrWhiteSpace(serviceName))
serviceName = _powerToolsConfigurations.ServiceName;

var defaultDimensionsList = DictionaryToList(defaultDimensions);

// Add service as a default dimension
defaultDimensionsList.Add(new DimensionSet("Service", PowertoolsConfig.Service));
defaultDimensionsList.Add(new DimensionSet("Service", serviceName));

context.SetDefaultDimensions(defaultDimensionsList);

Expand All @@ -182,11 +175,11 @@ private List<DimensionSet> DictionaryToList(Dictionary<string, string> defaultDi

public void Dispose()
{
Flush();
_instance.Flush();
}

internal static void ResetForTesting(){
Instance = null;
_instance = null;
}
}
}
14 changes: 7 additions & 7 deletions libraries/src/AWS.Lambda.PowerTools.Metrics/MetricsAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System;
using AWS.Lambda.PowerTools.Aspects;
using AWS.Lambda.PowerTools.Metrics.Internal;
using AWS.Lambda.PowerTools.Core;
using AWS.Lambda.PowerTools.Metrics.Internal;

namespace AWS.Lambda.PowerTools.Metrics
{
[AttributeUsage(AttributeTargets.Method)]
public class MetricsAttribute : MethodAspectAttribute
{
private string MetricsNamespace { get; set; }
private string ServiceName { get; set; }
private bool CaptureColdStart { get; set; }
private bool CaptureEmptyMetrics { get; set; }
public string MetricsNamespace { get; set; }
public string ServiceName { get; set; }
public bool CaptureColdStart { get; set; }
public bool CaptureEmptyMetrics { get; set; }


private IMetrics _metricsInstance;
private IMetrics MetricsInstance =>
_metricsInstance ??= new Metrics(
PowerToolsConfigurations.Instance,
metricsNamespace: MetricsNamespace,
serviceName: ServiceName,
captureMetricsEvenIfEmpty: CaptureEmptyMetrics
Expand All @@ -27,8 +28,7 @@ protected override IMethodAspectHandler CreateHandler()
return new MetricsAspectHandler
(
MetricsInstance,
CaptureColdStart,
PowerToolsConfigurations.Instance
CaptureColdStart
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AWS.Lambda.PowerTools.Core;
using Newtonsoft.Json;

namespace AWS.Lambda.PowerTools.Metrics
Expand Down Expand Up @@ -67,9 +68,9 @@ public List<List<string>> AllDimensionKeys

public void AddMetric(string name, double value, MetricUnit unit)
{
if (Metrics.Count < PowertoolsConfig.MaxMetrics)
if (Metrics.Count < PowerToolsConfigurations.MaxMetrics)
{
var metric = Metrics.FirstOrDefault(metric => metric.Name == name);
var metric = Metrics.FirstOrDefault(m => m.Name == name);
if (metric != null)
{
metric.AddValue(value);
Expand All @@ -92,7 +93,7 @@ internal void SetNamespace(string metricsNamespace)

internal void AddDimension(DimensionSet dimension)
{
if (Dimensions.Count < PowertoolsConfig.MaxDimensions)
if (Dimensions.Count < PowerToolsConfigurations.MaxDimensions)
{
var matchingKeys = AllDimensionKeys.Where(x => x.Contains(dimension.DimensionKeys[0]));
if(!matchingKeys.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ namespace AWS.Lambda.PowerTools.Metrics
public class MetricsContext : IDisposable
{
private RootNode _rootNode;
internal bool IsSerializable {
get {
return !(GetMetrics().Count == 0 && _rootNode.AWS.CustomMetadata.Count == 0);
}
}
internal bool IsSerializable => !(GetMetrics().Count == 0 && _rootNode.AWS.CustomMetadata.Count == 0);

public MetricsContext()
{
Expand All @@ -20,7 +16,7 @@ public MetricsContext()

public MetricsContext(List<DimensionSet> dimensions) : this()
{
foreach (DimensionSet dimension in dimensions)
foreach (var dimension in dimensions)
{
AddDimension(dimension);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ public class UnixMillisecondDateTimeConverter : JsonConverter<DateTime>

public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
{
long ms;
if(value is DateTime dateTime)
{
ms = (long)(dateTime.ToUniversalTime() - UnixEpoch).TotalMilliseconds;
}
else
{
throw new JsonException("Expected Date object value.");
}

var ms = (long)(value.ToUniversalTime() - UnixEpoch).TotalMilliseconds;

if(ms < 0)
{
throw new JsonException("Invalid date");
Expand Down
2 changes: 2 additions & 0 deletions libraries/src/AWS.Lambda.PowerTools/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ internal static class Constants
internal const string SAM_LOCAL_ENV = "AWS_SAM_LOCAL";
internal const string TRACER_CAPTURE_RESPONSE_ENV = "POWERTOOLS_TRACER_CAPTURE_RESPONSE";
internal const string TRACER_CAPTURE_ERROR_ENV = "POWERTOOLS_TRACER_CAPTURE_ERROR";
internal const string METRICS_NAMESPACE_ENV = "POWERTOOLS_METRICS_NAMESPACE";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ public interface IPowerToolsConfigurations
{
string ServiceName { get; }
bool IsServiceNameDefined { get; }
bool TracerCaptureResponse{ get; }
bool TracerCaptureError{ get; }
bool IsSamLocal{ get; }
bool TracerCaptureResponse { get; }
bool TracerCaptureError { get; }
bool IsSamLocal { get; }
string MetricsNamespace { get; }

string GetEnvironmentVariable(string variable);
string GetEnvironmentVariableOrDefault(string variable, string defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace AWS.Lambda.PowerTools.Core
{
public class PowerToolsConfigurations : IPowerToolsConfigurations
{
public const int MaxDimensions = 9;
public const int MaxMetrics = 100;

private static IPowerToolsConfigurations _instance;
public static IPowerToolsConfigurations Instance => _instance ??= new PowerToolsConfigurations(SystemWrapper.Instance);

Expand Down Expand Up @@ -44,5 +47,8 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)

public bool IsSamLocal =>
GetEnvironmentVariableOrDefault(Constants.SAM_LOCAL_ENV, false);

public string MetricsNamespace =>
GetEnvironmentVariable(Constants.METRICS_NAMESPACE_ENV);
}
}
33 changes: 0 additions & 33 deletions libraries/src/AWS.Lambda.PowerTools/PowertoolsConfig.cs

This file was deleted.

Loading