Skip to content

Commit b09876b

Browse files
authored
Merge branch 'awslabs:main' into fix-awslabs#31
2 parents 516f25e + 40e03ef commit b09876b

27 files changed

+297
-144
lines changed

buildspecs/buildspec.canary.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

buildspecs/buildspec.release.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

buildspecs/buildspec.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Amazon.CloudWatch.EMF.Web/ApplicationBuilderExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public static class ApplicationBuilderExtensions
1313
{
1414
public static void UseEmfMiddleware(this IApplicationBuilder app)
1515
{
16-
app.UseEmfMiddleware((context, logger) => {
16+
app.UseEmfMiddleware((context, logger) =>
17+
{
1718
var dimensions = new Model.DimensionSet();
1819
var dimensionsWithStatusCode = new Model.DimensionSet();
1920

2021
var endpoint = context.GetEndpoint();
21-
if (endpoint != null) {
22+
if (endpoint != null)
23+
{
2224
var actionDescriptor = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
2325

2426
dimensions.AddDimension("Controller", actionDescriptor.ControllerName);
@@ -34,7 +36,8 @@ public static void UseEmfMiddleware(this IApplicationBuilder app)
3436
// Include the X-Ray trace id if it is set
3537
// https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
3638
var xRayTraceId = context.Request.Headers["X-Amzn-Trace-Id"];
37-
if (!String.IsNullOrEmpty(xRayTraceId) && xRayTraceId.Count > 0) {
39+
if (!String.IsNullOrEmpty(xRayTraceId) && xRayTraceId.Count > 0)
40+
{
3841
logger.PutProperty("XRayTraceId", xRayTraceId[0]);
3942
}
4043

@@ -44,7 +47,8 @@ public static void UseEmfMiddleware(this IApplicationBuilder app)
4447
// https://www.w3.org/TR/trace-context/#traceparent-header
4548
logger.PutProperty("TraceId", Activity.Current?.Id ?? context?.TraceIdentifier);
4649

47-
if (!String.IsNullOrEmpty(Activity.Current?.TraceStateString)) {
50+
if (!String.IsNullOrEmpty(Activity.Current?.TraceStateString))
51+
{
4852
logger.PutProperty("TraceState", Activity.Current.TraceStateString);
4953
}
5054

@@ -63,7 +67,8 @@ public static void UseEmfMiddleware(this IApplicationBuilder app, Func<HttpConte
6367
var config = context.RequestServices.GetRequiredService<EMF.Config.IConfiguration>();
6468
var logger = context.RequestServices.GetRequiredService<IMetricsLogger>();
6569

66-
if (!String.IsNullOrEmpty(config.ServiceName)) {
70+
if (!String.IsNullOrEmpty(config.ServiceName))
71+
{
6772
logger.SetNamespace(config.ServiceName);
6873
}
6974

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>1.0.0</VersionPrefix>
5+
<VersionPrefix>1.0.1</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/Config/Configuration.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using Amazon.CloudWatch.EMF.Environment;
1+
using Amazon.CloudWatch.EMF.Environment;
32

43
namespace Amazon.CloudWatch.EMF.Config
54
{

src/Amazon.CloudWatch.EMF/Constants.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ namespace Amazon.CloudWatch.EMF
22
{
33
public class Constants
44
{
5+
public const int MAX_DIMENSION_SET_SIZE = 30;
6+
public const int MAX_DIMENSION_NAME_LENGTH = 250;
7+
public const int MAX_DIMENSION_VALUE_LENGTH = 1024;
8+
public const int MAX_METRIC_NAME_LENGTH = 1024;
9+
public const int MAX_NAMESPACE_LENGTH = 256;
10+
public const string VALID_NAMESPACE_REGEX = "^[a-zA-Z0-9._#:/-]+$";
11+
512
public const int DEFAULT_AGENT_PORT = 25888;
613

714
public const string UNKNOWN = "Unknown";
815

9-
public const int MAX_DIMENSION_SET_SIZE = 30;
10-
1116
public const int MAX_METRICS_PER_EVENT = 100;
1217

1318
public const string DEFAULT_NAMESPACE = "aws-embedded-metrics";

src/Amazon.CloudWatch.EMF/Environment/IResourceFetcher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Net.Http;
43

54
namespace Amazon.CloudWatch.EMF.Environment
65
{

src/Amazon.CloudWatch.EMF/Exception/DimensionSetExceededException.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,5 @@ public DimensionSetExceededException()
99
". Account for default dimensions if not using SetDimensions.")
1010
{
1111
}
12-
13-
public DimensionSetExceededException(string message)
14-
: base(message)
15-
{
16-
}
17-
18-
public DimensionSetExceededException(string message, Exception inner)
19-
: base(message, inner)
20-
{
21-
}
2212
}
23-
}
13+
}

src/Amazon.CloudWatch.EMF/Exception/EMFClientException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public EMFClientException(string message, Exception inner)
1818
{
1919
}
2020
}
21-
}
21+
}
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 InvalidDimensionException : Exception
6+
{
7+
public InvalidDimensionException(string message)
8+
: base(message)
9+
{
10+
}
11+
}
12+
}
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 InvalidMetricException : Exception
6+
{
7+
public InvalidMetricException(string message)
8+
: base(message)
9+
{
10+
}
11+
}
12+
}
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 InvalidNamespaceException : Exception
6+
{
7+
public InvalidNamespaceException(string message)
8+
: base(message)
9+
{
10+
}
11+
}
12+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Amazon.CloudWatch.EMF.Config;
44
using Amazon.CloudWatch.EMF.Environment;
55
using Amazon.CloudWatch.EMF.Model;
6+
using Amazon.CloudWatch.EMF.Utils;
67
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Logging.Abstractions;
89

@@ -181,6 +182,7 @@ public MetricsLogger PutMetadata(string key, object value)
181182
/// <returns>the current logger.</returns>
182183
public MetricsLogger SetNamespace(string logNamespace)
183184
{
185+
Validator.ValidateNamespace(logNamespace);
184186
_context.Namespace = logNamespace;
185187
return this;
186188
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using Amazon.CloudWatch.EMF.Utils;
34

45
namespace Amazon.CloudWatch.EMF.Model
56
{
@@ -19,6 +20,7 @@ public DimensionSet()
1920
/// <param name="value">the value for the dimension</param>
2021
public DimensionSet(string key, string value)
2122
{
23+
Validator.ValidateDimensionSet(key, value);
2224
Dimensions[key] = value;
2325
}
2426

@@ -31,6 +33,7 @@ public DimensionSet(string key, string value)
3133
/// <param name="value">the dimension value</param>
3234
public void AddDimension(string key, string value)
3335
{
36+
Validator.ValidateDimensionSet(key, value);
3437
if (Dimensions.Count >= Constants.MAX_DIMENSION_SET_SIZE)
3538
throw new DimensionSetExceededException();
3639

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Amazon.CloudWatch.EMF.Utils;
45

56
namespace Amazon.CloudWatch.EMF.Model
67
{
@@ -102,6 +103,7 @@ public bool HasDefaultDimensions
102103
/// <param name="unit">the units of the metric</param>
103104
public void PutMetric(string key, double value, Unit unit)
104105
{
106+
Validator.ValidateMetric(key, value);
105107
_metricDirective.PutMetric(key, value, unit);
106108
}
107109

src/Amazon.CloudWatch.EMF/Sink/SocketClientFactory.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class SocketClientFactory : ISocketClientFactory
99
{
1010
public ISocketClient GetClient(Endpoint endpoint)
1111
{
12-
if (endpoint.CurrentProtocol == Protocol.UDP)
13-
{
14-
return new UDPClient(endpoint);
15-
}
12+
if (endpoint.CurrentProtocol == Protocol.UDP)
13+
{
14+
return new UDPClient(endpoint);
15+
}
1616

17-
return new TCPClient(endpoint);
17+
return new TCPClient(endpoint);
1818
}
1919
}
2020
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace Amazon.CloudWatch.EMF.Utils
5+
{
6+
public class Validator
7+
{
8+
internal static void ValidateDimensionSet(in string dimensionName, in string dimensionValue)
9+
{
10+
if (dimensionName == null || dimensionName.Trim().Length == 0)
11+
{
12+
throw new InvalidDimensionException("Dimension name must include at least one non-whitespace character");
13+
}
14+
15+
if (dimensionValue == null || dimensionValue.Trim().Length == 0)
16+
{
17+
throw new InvalidDimensionException("Dimension value must include at least one non-whitespace character");
18+
}
19+
20+
if (dimensionName.Length > Constants.MAX_DIMENSION_NAME_LENGTH)
21+
{
22+
throw new InvalidDimensionException($"Dimension name cannot be longer than {Constants.MAX_DIMENSION_NAME_LENGTH} characters: {dimensionName}");
23+
}
24+
25+
if (dimensionValue.Length > Constants.MAX_DIMENSION_VALUE_LENGTH)
26+
{
27+
throw new InvalidDimensionException($"Dimension value cannot be longer than {Constants.MAX_DIMENSION_VALUE_LENGTH} characters: {dimensionValue}");
28+
}
29+
30+
if (!IsAscii(dimensionName))
31+
{
32+
throw new InvalidDimensionException($"Dimension name contains invalid characters: {dimensionName}");
33+
}
34+
35+
if (!IsAscii(dimensionValue))
36+
{
37+
throw new InvalidDimensionException($"Dimension value contains invalid characters: {dimensionValue}");
38+
}
39+
40+
if (dimensionName.StartsWith(":"))
41+
{
42+
throw new InvalidDimensionException("Dimension name cannot start with ':'");
43+
}
44+
}
45+
46+
internal static void ValidateMetric(in string name, in double value)
47+
{
48+
if (name == null || name.Trim().Length == 0)
49+
{
50+
throw new InvalidMetricException($"Metric name {name} must include at least one non-whitespace character");
51+
}
52+
53+
if (name.Length > Constants.MAX_METRIC_NAME_LENGTH)
54+
{
55+
throw new InvalidMetricException($"Metric name {name} cannot be longer than {Constants.MAX_METRIC_NAME_LENGTH} characters");
56+
}
57+
58+
if (!Double.IsFinite(value))
59+
{
60+
throw new InvalidMetricException($"Metric value {value} must be a finite number");
61+
}
62+
}
63+
64+
internal static void ValidateNamespace(in string @namespace)
65+
{
66+
if (@namespace == null || @namespace.Trim().Length == 0)
67+
{
68+
throw new InvalidNamespaceException($"Namespace {@namespace} must include at least one non-whitespace character");
69+
}
70+
71+
if (@namespace.Length > Constants.MAX_NAMESPACE_LENGTH)
72+
{
73+
throw new InvalidNamespaceException($"Namespace {@namespace} cannot be longer than {Constants.MAX_NAMESPACE_LENGTH} characters");
74+
}
75+
76+
if (!System.Text.RegularExpressions.Regex.IsMatch(@namespace, Constants.VALID_NAMESPACE_REGEX))
77+
{
78+
throw new InvalidNamespaceException($"Namespace {@namespace} contains invalid characters");
79+
}
80+
}
81+
82+
private static bool IsAscii(in string str)
83+
{
84+
return Encoding.UTF8.GetByteCount(str) == str.Length;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)