Skip to content
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

chore: Add Metrics support for Web projects using native dependency injection #21

Merged
merged 16 commits into from
Aug 21, 2021

Conversation

t1agob
Copy link
Contributor

@t1agob t1agob commented Jun 25, 2021

Description of changes:

One of our early discussions was if we should target a simple Lambda function or a typical .NET web project. Now that I have more experience working with AWS customers using .NET I see them using both patterns so I decided to implement both.

For a simple Lambda Function you have to initialize a static MetricsLogger and flush the data by the end of the execution.

private static MetricsLogger _metricsLogger = new MetricsLogger("dotnet-lambdapowertools", "lambda-example");

...

 _metricsLogger.AddDimension("Metric Type", "Aggregate");
 _metricsLogger.AddDimension("Method Execution Metrics", "getCallingIP");
 _metricsLogger.AddMetric("ElapsedExecutionTime", 1234, Unit.MILLISECONDS);

...

_metricsLogger.Flush();

As an alternative it can also be flushed automatically when the Metrics Logger object is disposed.

using (var logger = new MetricsLogger("dotnet-lambdapowertools-single", "lambda-example"))
{
      logger.AddDimension("Metric Type", "Single");
      logger.AddMetric("SingleExecution", 1, Unit.COUNT);
}

For web projects, I have created a ServiceCollectionExtension and ApplicationBuilderExtension to support native dependency injection. For this to work the developer just needs to add it to the startup file.

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers();
     services.AddMetrics();       
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }

     app.UseRouting();

     app.UseMetricsMiddleware();

     app.UseEndpoints(endpoints =>
     {
          endpoints.MapControllers();
     });
}

With this configuration, developers can now use the MetricsLogger on their code like so.

[Route("api/[controller]")]
public class LocationController : ControllerBase
{
     private readonly IMetricsLogger _metricsLogger;

     public LocationController(IMetricsLogger metricsLogger)
     {
         _metricsLogger = metricsLogger;
     }

     // GET api/location
     [HttpGet]
     public async Task<string> Get()
     {
             var watch = System.Diagnostics.Stopwatch.StartNew();
             var ip = await GetCallingIP();
             watch.Stop();

             _metricsLogger.AddMetric("GetIPExecutionTime", watch.ElapsedMilliseconds, Unit.MILLISECONDS);

            ...
      }
}

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@t1agob t1agob requested a review from sliedig June 25, 2021 11:38
@sliedig sliedig merged commit 85e5447 into aws-powertools:develop Aug 21, 2021
@t1agob t1agob added the internal Maintenance changes label Jan 18, 2022
@t1agob t1agob changed the title Add Metrics support for Web projects using native dependency injection refactor: Add Metrics support for Web projects using native dependency injection Jan 18, 2022
@t1agob t1agob removed the internal Maintenance changes label Jan 18, 2022
@t1agob t1agob changed the title refactor: Add Metrics support for Web projects using native dependency injection chore: Add Metrics support for Web projects using native dependency injection Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants