Skip to content

Commit 208eb16

Browse files
authored
Added support to set custom timestamp (#55)
* Added support to set custom timestamp * Bumped version from 2.1.0 to 2.2.0
1 parent 384c9df commit 208eb16

File tree

14 files changed

+186
-17
lines changed

14 files changed

+186
-17
lines changed

README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Example:
177177

178178
```c#
179179
// Standard Resolution Example
180-
metrics.PutMetric("ProcessingLatency", 101, Unit.MILLISECONDS);
180+
logger.PutMetric("ProcessingLatency", 101, Unit.MILLISECONDS);
181181

182182
// High Resolution Example
183183
logger.PutMetric("Memory.HeapUsed", "1600424.0", Unit.BYTES, StorageResolution.HIGH);
@@ -189,16 +189,16 @@ Adds or updates the value for a given property on this context. This value is no
189189

190190
Example:
191191
```c#
192-
metrics.PutProperty("AccountId", "123456789");
193-
metrics.PutProperty("RequestId", "422b1569-16f6-4a03-b8f0-fe3fd9b100f8");
192+
logger.PutProperty("AccountId", "123456789");
193+
logger.PutProperty("RequestId", "422b1569-16f6-4a03-b8f0-fe3fd9b100f8");
194194

195195
Dictionary<string, object> payLoad = new Dictionary<string, object>
196196
{
197197
{ "sampleTime", 123456789 },
198198
{ "temperature", 273.0 },
199199
{ "pressure", 101.3 }
200200
};
201-
metrics.PutProperty("Payload", payLoad);
201+
logger.PutProperty("Payload", payLoad);
202202
```
203203

204204
- MetricsLogger **PutDimensions**(DimensionSet dimensions)
@@ -217,7 +217,7 @@ Example:
217217
DimensionSet dimensionSet = new DimensionSet();
218218
dimensionSet.AddDimension("Service", "Aggregator");
219219
dimensionSet.AddDimension("Region", "us-west-2");
220-
metrics.PutDimensions(dimensionSet);
220+
logger.PutDimensions(dimensionSet);
221221
```
222222

223223
- MetricsLogger **SetDimensions**(params DimensionSet[] dimensionSets)
@@ -237,14 +237,14 @@ Examples:
237237
DimensionSet dimensionSet = new DimensionSet();
238238
dimensionSet.AddDimension("Service", "Aggregator");
239239
dimensionSet.AddDimension("Region", "us-west-2");
240-
metrics.SetDimensions(true, dimensionSet); // Will preserve default dimensions
240+
logger.SetDimensions(true, dimensionSet); // Will preserve default dimensions
241241
```
242242

243243
```c#
244244
DimensionSet dimensionSet = new DimensionSet();
245245
dimensionSet.AddDimension("Service", "Aggregator");
246246
dimensionSet.AddDimension("Region", "us-west-2");
247-
metrics.SetDimensions(dimensionSet); // Will remove default dimensions
247+
logger.SetDimensions(dimensionSet); // Will remove default dimensions
248248
```
249249

250250
- MetricsLogger **ResetDimensions**(bool useDefault)
@@ -259,12 +259,23 @@ Namespaces must meet CloudWatch Namespace requirements, otherwise a `InvalidName
259259
Example:
260260

261261
```c#
262-
SetNamespace("MyApplication")
262+
logger.SetNamespace("MyApplication")
263263
```
264264

265+
- MetricsLogger **SetTimestamp**(DateTime dateTime)
266+
267+
Sets the timestamp of the metrics. If not set, the current time of the client will be used.
268+
Timestamp must meet CloudWatch requirements, otherwise a `InvalidTimestampException` will be thrown. See [Timestamps](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#about_timestamp) for valid values.
269+
270+
Example:
271+
```c#
272+
logger.SetTimestamp(DateTime.Now);
273+
```
274+
275+
265276
- **Flush**()
266277

267-
Flushes the current MetricsContext to the configured sink and resets all properties and metric values. The namespace and default dimensions will be preserved across flushes. Custom dimensions are preserved by default, but this behavior can be changed by setting `flushPreserveDimensions = false` on the metrics logger.
278+
Flushes the current MetricsContext to the configured sink and resets all properties and metric values. The namespace and default dimensions will be preserved across flushes. Custom dimensions are preserved by default, but this behavior can be changed by setting `FlushPreserveDimensions = false` on the metrics logger.
268279

269280
Examples:
270281

@@ -273,12 +284,12 @@ flush(); // default dimensions and custom dimensions will be preserved after ea
273284
```
274285

275286
```c#
276-
logger.setFlushPreserveDimensions = false;
287+
logger.FlushPreserveDimensions = false;
277288
flush(); // only default dimensions will be preserved after each flush()
278289
```
279290

280291
```c#
281-
setFlushPreserveDimensions(false);
282-
resetDimensions(false); // default dimensions are disabled; no dimensions will be preserved after each flush()
283-
flush();
292+
logger.FlushPreserveDimensions = false;
293+
logger.ResetDimensions(false); // default dimensions are disabled; no dimensions will be preserved after each flush()
294+
logger.Flush();
284295
```

examples/Amazon.CloudWatch.EMF.Examples.ConsoleApp/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private static void EmitMetrics(ILogger logger, IMetricsLogger metrics)
4545
{
4646

4747
var dimensionSet = new DimensionSet();
48+
metrics.SetTimestamp(DateTime.Now);
4849
dimensionSet.AddDimension("Service", "Aggregator");
4950
dimensionSet.AddDimension("Region", "us-west-2");
5051
metrics.SetDimensions(dimensionSet);

examples/Amazon.CloudWatch.EMF.Examples.Lambda/Function.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Amazon.CloudWatch.EMF.Model;
66
using Amazon.Lambda.Core;
77
using System.Text;
8-
8+
using System;
99
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
1010
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
1111

@@ -27,6 +27,7 @@ public string FunctionHandler(string input, ILambdaContext context)
2727
var dimensionSet = new DimensionSet();
2828
dimensionSet.AddDimension("Service", "Aggregator");
2929
dimensionSet.AddDimension("Region", "us-west-2");
30+
logger.SetTimestamp(DateTime.Now);
3031
logger.PutDimensions(dimensionSet);
3132
logger.SetNamespace("EMFLambda");
3233
logger.PutMetric("ProcessingLatency", 101, Unit.MILLISECONDS);

examples/Amazon.CloudWatch.EMF.Examples.Web/Controllers/WeatherForecastController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public IEnumerable<WeatherForecast> Get()
3737
var rng = new Random();
3838
var temperature = rng.Next(-20, 55);
3939
var WindSpeed = rng.Next(10, 100);
40-
40+
_metrics.SetTimestamp(DateTime.Now);
4141
_metrics.PutMetric("Temperature", temperature, Unit.NONE);
4242
_metrics.PutMetric("WindSpeed", WindSpeed, Unit.NONE, StorageResolution.HIGH);
4343

src/Amazon.CloudWatch.EMF/Amazon.CloudWatch.EMF.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<PackageId>Amazon.CloudWatch.EMF</PackageId>
5-
<VersionPrefix>2.1.0</VersionPrefix>
5+
<VersionPrefix>2.2.0</VersionPrefix>
66
<Authors>Amazon Web Services</Authors>
77
<Description>Amazon CloudWatch Embedded Metric Format Client Library</Description>
88
<Language>en-US</Language>

src/Amazon.CloudWatch.EMF/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ public class Constants
1616
public const int MaxMetricsPerEvent = 100;
1717

1818
public const string DefaultNamespace = "aws-embedded-metrics";
19+
20+
public const long MaxTimestampPastAgeDays = 14;
21+
22+
public const long MaxTimestampFutureAgeHours = 2;
1923
}
2024
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Amazon.CloudWatch.EMF
4+
{
5+
public class InvalidTimestampException : Exception
6+
{
7+
public InvalidTimestampException(string message)
8+
: base(message)
9+
{
10+
}
11+
}
12+
}

src/Amazon.CloudWatch.EMF/Logger/IMetricsLogger.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using Amazon.CloudWatch.EMF.Model;
34

45
namespace Amazon.CloudWatch.EMF.Logger
@@ -22,5 +23,7 @@ public interface IMetricsLogger
2223
public MetricsLogger PutMetadata(string key, object value);
2324

2425
public MetricsLogger SetNamespace(string logNamespace);
26+
27+
public MetricsLogger SetTimestamp(DateTime dateTime);
2528
}
2629
}

src/Amazon.CloudWatch.EMF/Logger/MetricsLogger.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ public void Dispose()
222222
this.Flush();
223223
}
224224

225+
/// <summary>
226+
/// Sets the timestamp of the metrics. If not set, the current time of the client will be used.
227+
/// </summary>
228+
/// <param name="dateTime">Date and Time</param>
229+
/// <returns>the current logger.</returns>
230+
public MetricsLogger SetTimestamp(DateTime dateTime)
231+
{
232+
Validator.ValidateTimestamp(dateTime);
233+
_context.SetTimestamp(dateTime);
234+
return this;
235+
}
236+
225237
/// <summary>
226238
/// Shutdown the associated environment's sink.
227239
/// </summary>

src/Amazon.CloudWatch.EMF/Model/MetaData.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,15 @@ internal MetaData DeepCloneWithNewMetrics(List<MetricDefinition> metrics)
5757
clone.Timestamp = Timestamp;
5858
return clone;
5959
}
60+
61+
internal void SetTimestamp(DateTime dateTime)
62+
{
63+
Timestamp = dateTime;
64+
}
65+
66+
internal DateTime GetTimeStamp()
67+
{
68+
return Timestamp;
69+
}
6070
}
6171
}

0 commit comments

Comments
 (0)