generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Initial metrics implementation #7
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a9d027
implementation of custom metrics with EMF
t1agob 60befae
Update libraries/src/Amazon.LambdaPowertools.Metrics/MetricsManager.cs
t1agob 04003a6
Update libraries/src/Amazon.LambdaPowertools.Metrics/MetricsManager.cs
t1agob 43f1a40
Update libraries/src/Amazon.LambdaPowertools.Metrics/MetricsManager.cs
t1agob 8f3e9db
Update MetricsManager.cs
t1agob b40d4fe
Update libraries/src/Amazon.LambdaPowertools.Metrics/MetricsManager.cs
t1agob 6eefc51
Update libraries/src/Amazon.LambdaPowertools.Metrics/MetricsManager.cs
t1agob b6dccfe
updated method names to comply to suggestions
t1agob 7b457db
moved namespace and service to a shared project
t1agob 1ad3fe5
added support for multiple values for same metric
t1agob 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
libraries/src/Amazon.LambdaPowertools.Metrics/Amazon.LambdaPowertools.Metrics.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Amazon.LambdaPowertools\Amazon.LambdaPowertools.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| <PackageId>Amazon.LambdaPowertools.Metrics</PackageId> | ||
| <Version>0.0.15</Version> | ||
| <Authors>AWSLABS</Authors> | ||
| <Company>Amazon Web Services</Company> | ||
| <GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Amazon.LambdaPowertools.Metrics.Model; | ||
|
|
||
| namespace Amazon.LambdaPowertools.Metrics | ||
| { | ||
| public class Metrics : MetricsManager | ||
| { | ||
| public Metrics( | ||
| string metricsNamespace = null, | ||
| string serviceName = null, | ||
| Dictionary<string, string> dimensions = null, | ||
| Dictionary<string, List<Metric>> metrics = null, | ||
| Dictionary<string, dynamic> metadata = null) | ||
| : base(metricsNamespace, | ||
| serviceName, | ||
| dimensions, | ||
| metrics, | ||
| metadata) | ||
| { | ||
|
|
||
| } | ||
| } | ||
| } |
224 changes: 224 additions & 0 deletions
224
libraries/src/Amazon.LambdaPowertools.Metrics/MetricsManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel.DataAnnotations; | ||
| using System.Text.Json; | ||
| using Amazon.LambdaPowertools.Metrics.Model; | ||
|
|
||
| namespace Amazon.LambdaPowertools.Metrics | ||
| { | ||
|
|
||
| public class MetricsManager : IDisposable | ||
| { | ||
| [MaxLength(9)] | ||
| private Dictionary<string, string> _dimensions; | ||
| public Dictionary<string, string> Dimensions | ||
| { | ||
| get { return _dimensions; } | ||
| } | ||
|
|
||
| [MaxLength(100)] | ||
| private Dictionary<string, List<Metric>> _metrics; | ||
| public Dictionary<string, List<Metric>> Metrics | ||
| { | ||
| get { return _metrics; } | ||
| } | ||
|
|
||
| private Dictionary<string, dynamic> _metadata; | ||
|
|
||
| public Dictionary<string, dynamic> Metadata | ||
| { | ||
| get { return _metadata; } | ||
| } | ||
|
|
||
| private bool disposedValue; | ||
| private static int _metricCount = 0; | ||
|
|
||
| public MetricsManager( | ||
| string metricsNamespace = null, | ||
| string serviceName = null, | ||
| Dictionary<string, string> dimensions = null, | ||
| Dictionary<string, List<Metric>> metrics = null, | ||
| Dictionary<string, dynamic> metadata = null) | ||
| { | ||
| PowertoolsSettings.SetNamespace(metricsNamespace); | ||
| PowertoolsSettings.SetService(serviceName); | ||
|
|
||
| _dimensions = dimensions != null ? dimensions : new Dictionary<string, string>(); | ||
| _metrics = metrics != null ? metrics : new Dictionary<string, List<Metric>>(); | ||
| _metadata = metadata != null ? metadata : new Dictionary<string, dynamic>(); | ||
|
|
||
| if(!string.IsNullOrEmpty(PowertoolsSettings.Service)){ | ||
| AddDimension("service", PowertoolsSettings.Service); | ||
| } | ||
| } | ||
|
|
||
| public void AddDimension(string key, string value) | ||
| { | ||
| if (_dimensions.ContainsKey(key)) | ||
| { | ||
| Console.WriteLine($"Dimension '{key}' already exists. Skipping..."); | ||
| return; | ||
| } | ||
|
|
||
| if(_dimensions.Count == 9) | ||
| { | ||
| Console.WriteLine($"Maximum number of dimensions (9) reached. Cannot add '{key}' to dimensions."); | ||
| return; | ||
| } | ||
|
|
||
| _dimensions.Add(key, value); | ||
| } | ||
|
|
||
| public virtual void AddMetric(Metric metric) | ||
| { | ||
| if (_metrics.ContainsKey(metric.Name)) | ||
| { | ||
| _metrics[metric.Name].Add(metric); | ||
| _metricCount++; | ||
| } | ||
| else { | ||
| _metrics.Add(metric.Name, new List<Metric>{metric}); | ||
| _metricCount++; | ||
| } | ||
|
|
||
| if (_metricCount == 100) | ||
| { | ||
| Flush(); | ||
| } | ||
| } | ||
|
|
||
| public void AddMetadata(string key, dynamic value) | ||
| { | ||
|
|
||
| if (_metadata.ContainsKey(key)) | ||
| { | ||
| Console.WriteLine($"Metadata '{key}' already exists. Skipping..."); | ||
| return; | ||
| } | ||
|
|
||
| _metadata.Add(key, value); | ||
| } | ||
|
|
||
| //TODO: Turn this into a private method once decision has been made on the implementation | ||
| public void Flush() | ||
| { | ||
| var metricObj = new MetricsWrapper | ||
| { | ||
| Root = new MetricsDefinition | ||
| { | ||
| Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(), | ||
| CloudWatchMetrics = new List<CloudWatchMetrics>{ | ||
| new CloudWatchMetrics{ | ||
| Namespace = PowertoolsSettings.Namespace, | ||
| Dimensions = new List<List<string>>{ | ||
| ExtractDimensions(Dimensions) | ||
| }, | ||
| Metrics = ExtractMetricDefinitionSet(Metrics) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(metricObj, typeof(MetricsWrapper)); | ||
|
|
||
| string result = AddToJson(json, _dimensions, _metadata, _metrics); | ||
|
|
||
| Console.WriteLine(result); | ||
|
|
||
| _metrics.Clear(); | ||
| } | ||
|
|
||
| private List<string> ExtractDimensions(Dictionary<string, string> dimensions) | ||
| { | ||
| List<string> result = new List<string>(); | ||
|
|
||
| result.AddRange(dimensions.Keys); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private List<MetricsDefinitionSet> ExtractMetricDefinitionSet(Dictionary<string, List<Metric>> metrics) | ||
| { | ||
| List<MetricsDefinitionSet> metricDefinitionSet = new List<MetricsDefinitionSet>(); | ||
|
|
||
| foreach (var item in metrics) | ||
| { | ||
| metricDefinitionSet.Add(new MetricsDefinitionSet | ||
| { | ||
| Name = item.Value[0].Name, | ||
| Unit = item.Value[0].Unit | ||
| }); | ||
| } | ||
|
|
||
| return metricDefinitionSet; | ||
| } | ||
|
|
||
| private string AddToJson(string json, Dictionary<string, string> dimensions, Dictionary<string, dynamic> metadata, Dictionary<string, List<Metric>> metrics) | ||
| { | ||
| string result = ""; | ||
| foreach (var item in dimensions) | ||
| { | ||
| result += $",\"{item.Key}\":\"{item.Value}\""; | ||
| } | ||
|
|
||
| foreach (var item in metadata) | ||
| { | ||
| result += $",\"{item.Key}\":\"{item.Value}\""; | ||
| } | ||
|
|
||
| foreach (var item in metrics.Values) | ||
| { | ||
| if(item.Count > 1){ | ||
| string resultArray = $",\"{item[0].Name}\": ["; | ||
|
|
||
| for (int i = 0; i < item.Count; i++) | ||
| { | ||
| if(i == item.Count - 1){ | ||
| resultArray += $"{item[i].Value}]"; | ||
| } | ||
| else { | ||
| resultArray += $"{item[i].Value},"; | ||
| } | ||
| } | ||
|
|
||
| result += resultArray; | ||
| } | ||
| else { | ||
| result += $",\"{item[0].Name}\": {item[0].Value}"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return $"{json.Remove(json.Length - 1)}{result}}}"; | ||
| } | ||
|
|
||
| protected virtual void Dispose(bool disposing) | ||
| { | ||
| if (!disposedValue) | ||
| { | ||
| if (disposing) | ||
| { | ||
| Flush(); | ||
| } | ||
|
|
||
| _dimensions = null; | ||
| _metrics = null; | ||
| _metadata = null; | ||
| disposedValue = true; | ||
| } | ||
| } | ||
|
|
||
| ~MetricsManager() | ||
| { | ||
| // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
| Dispose(disposing: false); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
| Dispose(disposing: true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
libraries/src/Amazon.LambdaPowertools.Metrics/Model/Metric.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using Amazon.LambdaPowertools.Metrics.Model; | ||
|
|
||
| namespace Amazon.LambdaPowertools.Metrics | ||
| { | ||
| public class Metric | ||
| { | ||
| private string _name; | ||
| public string Name | ||
| { | ||
| get { return _name; } | ||
| set { _name = value; } | ||
| } | ||
|
|
||
| private string _unit; | ||
| public string Unit | ||
| { | ||
| get { return _unit; } | ||
| set { _unit = value; } | ||
| } | ||
|
|
||
| private double _value; | ||
| public double Value | ||
| { | ||
| get { return _value; } | ||
| set { _value = value; } | ||
| } | ||
|
|
||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
libraries/src/Amazon.LambdaPowertools.Metrics/Model/MetricUnit.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace Amazon.LambdaPowertools.Metrics.Model | ||
| { | ||
| public class MetricUnit | ||
| { | ||
| public static readonly string Seconds = "Seconds"; | ||
| public static readonly string Microseconds = "Microseconds"; | ||
| public static readonly string Milliseconds = "Milliseconds"; | ||
| public static readonly string Bytes = "Bytes"; | ||
| public static readonly string Kilobytes = "Kilobytes"; | ||
| public static readonly string Megabytes = "Megabytes"; | ||
| public static readonly string Gigabytes = "Gigabytes"; | ||
| public static readonly string Terabytes = "Terabytes"; | ||
| public static readonly string Bits = "Bits"; | ||
| public static readonly string Kilobits = "Kilobits"; | ||
| public static readonly string Megabits = "Megabits"; | ||
| public static readonly string Gigabits = "Gigabits"; | ||
| public static readonly string Terabits = "Terabits"; | ||
| public static readonly string Percent = "Percent"; | ||
| public static readonly string Count = "Count"; | ||
| public static readonly string BytesPerSecond = "Bytes/Second"; | ||
| public static readonly string KilobytesPerSecond = "Kilobytes/Second"; | ||
| public static readonly string MegabytesPerSecond = "Megabytes/Second"; | ||
| public static readonly string GigabytesPerSecond = "Gigabytes/Second"; | ||
| public static readonly string TerabytesPerSecond = "Terabytes/Second"; | ||
| public static readonly string BitsPerSecond = "Bits/Second"; | ||
| public static readonly string KilobitsPerSecond = "Kilobits/Second"; | ||
| public static readonly string MegabitsPerSecond = "Megabits/Second"; | ||
| public static readonly string GigabitsPerSecond = "Gigabits/Second"; | ||
| public static readonly string TerabitsPerSecond = "Terabits/Second"; | ||
| public static readonly string CountPerSecond = "Count/Second"; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
libraries/src/Amazon.LambdaPowertools.Metrics/Model/MetricsWrapper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System.Collections.Generic; | ||
| using System.Text.Json.Serialization; | ||
| using Amazon.LambdaPowertools.Metrics.Model; | ||
|
|
||
| namespace Amazon.LambdaPowertools.Metrics | ||
| { | ||
|
|
||
| public class MetricsWrapper | ||
| { | ||
| [JsonPropertyName("_aws")] | ||
| public MetricsDefinition Root { get; set; } | ||
| } | ||
|
|
||
| public class MetricsDefinition | ||
| { | ||
| public double Timestamp { get; set; } | ||
| public List<CloudWatchMetrics> CloudWatchMetrics { get; set; } | ||
| } | ||
|
|
||
| public class CloudWatchMetrics | ||
| { | ||
| public string Namespace { get; set; } | ||
| public List<List<string>> Dimensions { get; set; } | ||
| public List<MetricsDefinitionSet> Metrics { get; set; } | ||
| } | ||
|
|
||
| public class MetricsDefinitionSet | ||
| { | ||
| public string Name { get; set; } | ||
| public string Unit { get; set; } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.