-
Notifications
You must be signed in to change notification settings - Fork 32
-
When I create metrics I keep getting the following warnings in my logs:
##WARNING##: Failed to Add dimension 'responseCode'. Dimension already exists.
I am adding a metric in a middleware component, the idea being to have a metric for every route that is requested.
My middleware looks like this:
public class MetricsMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext context)
{
var sw = new Stopwatch();
sw.Start();
await next(context);
sw.Stop();
var elapsedTime = sw.ElapsedMilliseconds;
var endpoint = context.GetEndpoint();
if (endpoint != null)
{
var routePattern = endpoint.Metadata.GetMetadata<IRouteDiagnosticsMetadata>();
var routePrefix = routePattern?.Route.ToLower() ?? "unknown";
var responseCode = context.Response.StatusCode.ToString();
var method = context.Request.Method.ToLower();
Metrics.AddDimension("method", method);
Metrics.AddDimension("responseCode", responseCode);
Metrics.AddMetric($"{routePrefix}-count", 1, MetricUnit.Count);
Metrics.AddMetric($"{routePrefix}-duration", elapsedTime, MetricUnit.Milliseconds);
}
}
}
This runs once per request and I only add the dimensions once for every request.
There is no other place in the code base where I add dimensions in this lambda function.
What am I doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 3 replies
-
Hi @RemotecUK, can you share the output you are getting in the console for the metrics json?
The obvious answer is that that key already exists, but since you are mentioning that is never set and runs only once it is strange.
I just want to see if the dimension is really there in the output json if it is there, is being set and somehow being set again.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi @hjgraca, thanks for your message.
After reading it, I took another look at my code to investigate and found that my middleware was being included twice.
I will fix that and check to see if the warnings go away.
Thanks again.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Hi @hjgraca, I am trying to look into this issue.
Can you let me know what controls if I log the metric data into a log group?
Sometimes I am getting a payload and sometimes I don't seem to see the entries.
A sample payload is shown below - I think the part you are interested in is _aws
?
{
"id": "AwAAAZYQSbmxE_wshgAAABhBWllRU2Q0TUFBRExYbksxWVFsdmxRQUoAAAAkMDE5NjEwNDktZDg1Yi00MzBhLWE1MDEtMmMxZmNiZTNiNjU1AAAAjw",
"content": {
"timestamp": "2025-04-07T12:46:29.297Z",
"tags": [
"environment:uat",
"forwardername:datadogintegration-forwarderstack-redacted",
"costcode:itcsi01",
"solution:my-application",
"repo:https://bitbucket.org/my-company/my-company-my-service-backend",
"account_id:xxxxxxxxxxxx",
"source:lambda",
"functionname:my-function-01",
"region:us-east-1",
"service:my-service",
"aws_account:xxxxxxxxxxxx",
"forwarder_version:3.100.0",
"sourcecategory:aws",
"env:uat",
"forwarder_memorysize:1024",
"datadog.submission_auth:api_key"
],
"host": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:my-function-01",
"service": "my-service",
"attributes": {
"site/{sitecode}-duration": 74,
"lambda": {
"arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:my-function-01"
},
"_aws": {
"CloudWatchMetrics": [
{
"Metrics": [
{
"Unit": "Count",
"Name": "site/{sitecode}-count"
},
{
"Unit": "Milliseconds",
"Name": "site/{sitecode}-duration"
}
],
"Dimensions": [
[
"Service",
"method",
"responseCode"
]
],
"Namespace": "my-function-01"
}
],
"Timestamp": 1744029989297
},
"method": "get",
"service": "my-service",
"host": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:my-function-01",
"Service": "api",
"id": "38893168409908486819767682150292536873521187062053339145",
"site/{sitecode}-count": 1,
"aws": {
"awslogs": {
"owner": "xxxxxxxxxxxx",
"logGroup": "/aws/lambda/my-function-01",
"logStream": "2025/04/07/[$LATEST]cee3a08499f7403cbf285300bd510ee5"
},
"invoked_function_arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:DatadogIntegration-ForwarderStack-redacted",
"function_version": "$LATEST"
},
"responseCode": "200",
"timestamp": 1744029989297
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi!
Are you using Metrics v2? If yes, use the Flush() method https://docs.powertools.aws.dev/lambda/dotnet/core/metrics-v2/#flushing-metrics
If you are using Metrics v1 it requires the [Metrics] decorator to flush the metrics, I believe there is no flush method on Metrics v1
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes I have v2.0.0 of the nuget package.
I'll try the flush command.
Am I using dimensions correctly? I have been setting my http status code as a dimension so that I can differentiate between successfully (==200) and unsuccessful (!= 200) requests.
Beta Was this translation helpful? Give feedback.