Skip to content

chore: Metrics v2 always set namespace and service #807

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
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
18 changes: 15 additions & 3 deletions libraries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ internal Metrics(IPowertoolsConfigurations powertoolsConfigurations, string name
Instance = this;
_powertoolsConfigurations.SetExecutionEnvironment(this);

if (!string.IsNullOrEmpty(nameSpace)) SetNamespace(nameSpace);
if (!string.IsNullOrEmpty(service)) SetService(service);
// set namespace and service always
SetNamespace(nameSpace);
SetService(service);
}

/// <inheritdoc />
Expand Down Expand Up @@ -364,7 +365,18 @@ void IMetrics.PushSingleMetric(string name, double value, MetricUnit unit, strin

var context = new MetricsContext();
context.SetNamespace(nameSpace ?? GetNamespace());
context.SetService(service ?? _context.GetService());

var parsedService = !string.IsNullOrWhiteSpace(service)
? service
: _powertoolsConfigurations.Service == "service_undefined"
? null
: _powertoolsConfigurations.Service;

if (!string.IsNullOrWhiteSpace(parsedService))
{
context.SetService(parsedService);
context.AddDimension("Service", parsedService);
}

if (dimensions != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public async Task When_WithMetrics_Should_Add_ColdStart()
var options = new MetricsOptions
{
CaptureColdStart = true,
Namespace = "TestNamespace",
Service = "TestService"
Namespace = "TestNamespace"
};

var conf = Substitute.For<IPowertoolsConfigurations>();
Expand Down Expand Up @@ -61,7 +60,6 @@ public async Task When_WithMetrics_Should_Add_ColdStart_Dimensions()
{
CaptureColdStart = true,
Namespace = "TestNamespace",
Service = "TestService"
};

var conf = Substitute.For<IPowertoolsConfigurations>();
Expand Down Expand Up @@ -111,7 +109,6 @@ public async Task When_WithMetrics_Should_Add_ColdStart_Default_Dimensions()
{
CaptureColdStart = true,
Namespace = "TestNamespace",
Service = "TestService",
DefaultDimensions = new Dictionary<string, string>
{
{ "Environment", "Prod" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public async Task When_UseMetrics_Should_Add_ColdStart()
{
CaptureColdStart = true,
Namespace = "TestNamespace",
Service = "TestService"
};

var conf = Substitute.For<IPowertoolsConfigurations>();
Expand Down Expand Up @@ -59,7 +58,6 @@ public async Task When_UseMetrics_Should_Add_ColdStart_With_LambdaContext()
{
CaptureColdStart = true,
Namespace = "TestNamespace",
Service = "TestService"
};

var conf = Substitute.For<IPowertoolsConfigurations>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ public void When_PushSingleMetric_With_Env_Namespace()

// Assert
Assert.Contains("\"CloudWatchMetrics\":[{\"Namespace\":\"EnvNamespace\",\"Metrics\":[{\"Name\":\"SingleMetric\",\"Unit\":\"Count\",\"StorageResolution\":1}],\"Dimensions\":[[\"Default\"]]}]},\"Default\":\"SingleMetric\",\"SingleMetric\":1}", metricsOutput);

// assert with different service name
Assert.Contains("\"CloudWatchMetrics\":[{\"Namespace\":\"EnvNamespace\",\"Metrics\":[{\"Name\":\"SingleMetric2\",\"Unit\":\"Count\",\"StorageResolution\":1}],\"Dimensions\":[[\"Service\",\"Default\"]]}]},\"Service\":\"service1\",\"Default\":\"SingleMetric\",\"SingleMetric2\":1}", metricsOutput);

// assert with different service name
Assert.Contains("\"CloudWatchMetrics\":[{\"Namespace\":\"EnvNamespace\",\"Metrics\":[{\"Name\":\"SingleMetric3\",\"Unit\":\"Count\",\"StorageResolution\":1}],\"Dimensions\":[[\"Service\",\"Default\"]]}]},\"Service\":\"service2\",\"Default\":\"SingleMetric\",\"SingleMetric3\":1}", metricsOutput);
}

[Trait("Category", "MetricsImplementation")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public void PushSingleMetricWithEnvNamespace()
dimensions: new Dictionary<string, string> {
{ "Default", "SingleMetric" }
});

Metrics.PushSingleMetric("SingleMetric2", 1, MetricUnit.Count, resolution: MetricResolution.High,
service: "service1",
dimensions: new Dictionary<string, string> {
{ "Default", "SingleMetric" }
});

Metrics.PushSingleMetric("SingleMetric3", 1, MetricUnit.Count, resolution: MetricResolution.High,
service: "service2",
dimensions: new Dictionary<string, string> {
{ "Default", "SingleMetric" }
});
}

[Metrics(Namespace = "dotnet-powertools-test", Service = "testService")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ public void When_ColdStart_Should_Use_DefaultDimensions_From_Options()
{
CaptureColdStart = true,
Namespace = "dotnet-powertools-test",
Service = "testService",
DefaultDimensions = new Dictionary<string, string>
{
{ "Environment", "Test" },
Expand Down Expand Up @@ -268,7 +267,6 @@ public void When_ColdStart_And_DefaultDimensions_Is_Null_Should_Only_Add_Service
{
CaptureColdStart = true,
Namespace = "dotnet-powertools-test",
Service = "testService",
DefaultDimensions = null
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,15 @@ private void AssertSingleMetric(string output)
Assert.Equal("Count", unitElement.GetString());

Assert.True(cloudWatchMetricsElement[0].TryGetProperty("Dimensions", out JsonElement dimensionsElement));
Assert.Equal("FunctionName", dimensionsElement[0][0].GetString());
Assert.Equal("Service", dimensionsElement[0][0].GetString());
Assert.Equal("FunctionName", dimensionsElement[0][1].GetString());

Assert.True(root.TryGetProperty("FunctionName", out JsonElement functionNameElement));
Assert.Equal(_functionName, functionNameElement.GetString());

Assert.True(root.TryGetProperty("Service", out JsonElement serviceElement));
Assert.Equal("Test", serviceElement.GetString());

Assert.True(root.TryGetProperty("SingleMetric", out JsonElement singleMetricElement));
Assert.Equal(1, singleMetricElement.GetInt32());
}
Expand Down
Loading